import pygame
pygame.init()
WIDTH, HEIGHT = 800,512
font1 = pygame.font.Font(None,35)
font2 = pygame.font.Font(None,35)
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bird")
clock = pygame.time.Clock()
BG_img = pygame.image.load("C:\\birdgame\\dirds\\background.png", "r")
Bird_img = pygame.image.load("C:\\birdgame\\dirds\\icon.png", "r")
PT_img = pygame.image.load("C:\\birdgame\\dirds\\pipe_bottom.png", "r")
PB_img = pygame.image.load("C:\\birdgame\\dirds\\pipe_top.png", "r")
b_y, b_v, b_a = HEIGHT//2,0,0
time = 10
sdvig = 0
lives = 0
scores = 0
state = "start"
bgs = []
bgs.append(pygame.Rect(0,0,288,512))
pipes = []
FPS = 240
player = pygame.Rect(WIDTH//3,b_y,50,50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
#управление объектом
press = pygame.mouse.get_pressed()
keys = pygame.key.get_pressed()
sdvig = (sdvig+0.4)%4
click = press[0] or keys[pygame.K_SPACE]
if time > 0:
time -=1
for i in range(len(pipes)-1,-1,-1):
pipe = pipes[i]
pipe.x-=3
if pipe.right<100:
pipes.remove(pipe)
for i in range(len(bgs)-1,-1,-1):
bg = bgs[i]
bg.x-=1
if bg.right<0:
bgs.remove(bg)
if bgs[len(bgs)-1].right<=WIDTH:
bgs.append(pygame.Rect(bgs[len(bgs)-1].right,0,288,512))
if state == "start":
if click and time == 0 and len(pipes) == 0:
state = "play"
#возврат на место
b_y+=(HEIGHT//2-b_y)*0.2
player.y = b_y
elif state =="play":
if click:
b_a -= 2
else:
b_a = 0
#механика падения
b_y += b_v
b_v = (b_v + b_a + 1) * 0.8
player.y = b_y
if len(pipes) == 0 or pipes[len(pipes)-1].x<WIDTH-300:
pipes.append(pygame.Rect(WIDTH,0,52,150))
pipes.append(pygame.Rect(WIDTH, 500, 52, 200))
if player.top <0 or player.bottom>HEIGHT:
state = "leave"
#проверка на столкновение с трубами
for pipe in pipes:
if player.colliderect(pipe):
state = "leave"
elif state == "leave":
b_v,b_a = 0,0
state = "start"
time = 60
else:
pass
win.fill(pygame.Color("black"))
for bg in bgs:
win.blit(BG_img,bg)
for pipe in pipes:
if pipe.y == 0:
rect = PT_img.get_rect(bottomleft = pipe.bottomleft)
win.blit(PT_img,rect)
else:
rect = PB_img.get_rect(bottomleft=pipe.bottomleft)
win.blit(PB_img, rect)
image = Bird_img.subsurface(34*int(sdvig), 0, 34, 24)
image = pygame.transform.rotate(image, b_v*-1.5)
win.blit(image, player)
text = font1.render("Очки: " + str(scores),1,pygame.Color("black"))
win.blit(text,(10,10))
text = font2.render("Жизни: " + str(lives), 1, pygame.Color("black"))
win.blit(text, (10, HEIGHT-30))
pygame.display.update()
clock.tick(FPS)