import pygame
from random import *
pygame.init()
WIDTH = 1400
HEIGHT = 850
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")
clock = pygame.time.Clock()
FPS = 60
pipe_gap = 200
pipe_frequency = 1500 # частота появления труб в миллисекундах
last_pipe = pygame.time.get_ticks() - pipe_frequency
ground_scroll = 0
ground_speed = 3
scroll_speed = 3
high_score = 0
score = 0
current_level = 1
levels = {
1: 'C://Users//User//Desktop//питон//проекты//flapy birds//background1.jpg',
2: 'C://Users//User//Desktop//питон//проекты//flapy birds//background2.jpg',
3: 'C://Users//User//Desktop//питон//проекты//flapy birds//background3.jpg'
}
platform_image = pygame.image.load('C://Users//User//Desktop//питон//проекты//flapy birds//glow.jpg')
def change_level():
global current_level, background
current_level += 1
if current_level in levels:
background = pygame.image.load(levels[current_level])
background = pygame.transform.scale(background, (WIDTH, HEIGHT))
def restart():
global score, current_level, last_pipe, game_over, flying
pipe_group.empty()
bird.rect.x = 100
bird.rect.y = HEIGHT // 2
bird.velocity = 0
score = 0
current_level = 1
last_pipe = pygame.time.get_ticks()
game_over = False
flying = False
class Button():
def init (self, x, y, image):
pygame.sprite.Sprite.init (self)
self.image = image
self.rect = self.image.get_rect(center=(x, y))
code Copy code
def draw(self):
action = False
mouse_position = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_position):
if pygame.mouse.get_pressed()[0] == 1:
action = True
screen.blit(self.image, self.rect)
return action
class Pipe(pygame.sprite.Sprite):
def init (self, x, y, position):
pygame.sprite.Sprite.init (self)
self.image = pygame.image.load('C://Users//User//Desktop//питон//проекты//flapy birds//pipe.png')
self.image = pygame.transform.scale(self.image, (100, 600))
self.rect = self.image.get_rect(topleft=(x, y))
code Copy code
if position == 'top':
self.image = pygame.transform.flip(self.image, False, True)
self.rect = self.image.get_rect(bottomleft=(x, y - pipe_gap // 2))
elif position == 'bottom':
self.rect = self.image.get_rect(topleft=(x, y + pipe_gap // 2))
def update(self):
global score
self.rect.x -= scroll_speed
if self.rect.right <= 0:
score += 1
self.kill()
class Bird(pygame.sprite.Sprite):
def init (self, x, y, width, height):
super().init ()
code Copy code
self.images = []
for num in range(1, 7):
img = pygame.image.load(f'C://Users//User//Desktop//питон//проекты//flapy birds//birds//{num}bird.png')
img = pygame.transform.scale(img, (width, height))
self.images.append(img)
self.index = 0
self.counter = 0
self.image = self.images[self.index]
self.rect = self.image.get_rect(center=(x, y))
self.velocity = 0
self.clicked = False
def update(self):
# Анимация птицы и падение происходят всегда, даже в меню
self.velocity += 0.2
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
if playing and pygame.mouse.get_pressed()[0] == 1 and not self.clicked:
self.clicked = True
self.velocity = -6
if self.rect.bottom < HEIGHT:
self.rect.y += self.velocity
self.counter += 1
flap_speed = 6
if self.counter > flap_speed:
self.counter = 0
self.index = (self.index + 1) % len(self.images)
self.image = pygame.transform.rotate(self.images[self.index], self.velocity * -6)
class Platform(pygame.sprite.Sprite):
def init (self, x, y, width, height, axis='horizontal', speed=2):
super().init ()
self.image = pygame.transform.scale(platform_image, (width, height))
self.rect = self.image.get_rect(topleft=(x, y))
self.axis = axis
self.speed = speed
self.direction = 1
self.range = 150
self.start_pos = self.rect.topleft
code Copy code
def update(self):
if self.axis == 'horizontal':
self.rect.x += self.speed * self.direction
if abs(self.rect.x - self.start_pos[0]) > self.range:
self.direction *= -1
elif self.axis == 'vertical':
self.rect.y += self.speed * self.direction
if abs(self.rect.y - self.start_pos[1]) > self.range:
self.direction *= -1
start_menu = pygame.image.load('C://Users//User//Desktop//питон//проекты//flapy birds//main_menu.jpg')
start_menu = pygame.transform.scale(start_menu, (WIDTH, HEIGHT))
start_button_img = pygame.image.load('C://Users//User//Desktop//питон//проекты//flapy birds//кнопка играть.png')
start_button_img = pygame.transform.scale(start_button_img, (200, 100))
start_button = Button(WIDTH // 2, HEIGHT // 2 + 50, start_button_img)
pipe_group = pygame.sprite.Group()
ground = pygame.image.load('C://Users//User//Desktop//питон//проекты//flapy birds//glow.jpg')
ground = pygame.transform.scale(ground, (WIDTH + 400, 100))
bird = Bird(100, HEIGHT // 2, 125, 75)
bird_group = pygame.sprite.Group()
bird_group.add(bird)
platforms = pygame.sprite.Group()
#platforms.add(Platform(300, 600, 200, 20, axis='horizontal', speed=3))
#platforms.add(Platform(800, 400, 200, 20, axis='vertical', speed=2))
#platforms.add(Platform(1100, 700, 150, 20, axis='horizontal', speed=4))
button_img = pygame.image.load('C://Users//User//Desktop//питон//проекты//flapy birds//button.png')
button_img = pygame.transform.scale(button_img, (200, 100))
button = Button(WIDTH // 2, HEIGHT // 2, button_img)
background = pygame.image.load(levels[1])
background = pygame.transform.scale(background, (WIDTH, HEIGHT))
game_over = False
playing = False
flying = False
running = True
while running:
clock.tick(FPS)
code Copy code
print(f"DEBUG: playing={playing}, game_over={game_over}, flying={flying}, score={score}")
if not playing:
screen.blit(start_menu, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and not flying:
flying = True
bird.update()
platforms.update()
if start_button.draw():
print("DEBUG: Start button clicked")
game_over = False
playing = True
flying = True
restart()
ground_scroll -= ground_speed
if abs(ground_scroll) > 400:
ground_scroll = 0
if score >= 4 and current_level == 1:
change_level()
if score >= 8 and current_level == 2:
change_level()
print(f"DEBUG: bird.rect.bottom={bird.rect.bottom}, bird.rect.top={bird.rect.top}")
if bird.rect.bottom > HEIGHT or bird.rect.top <= 0:
game_over = True
flying = False
with open('C://Users//User//Desktop//питон//проекты//flapy birds//maks_score.txt', 'r') as high_score_file:
high_score = int(high_score_file.read())
if score > high_score:
with open('C://Users//User//Desktop//питон//проекты//flapy birds//maks_score.txt', 'w') as high_score_file:
high_score_file.write(str(score))
if game_over:
if button.draw():
print("DEBUG: Restart button clicked")
game_over = False
restart()
if not game_over:
time_now = pygame.time.get_ticks()
if time_now - last_pipe > pipe_frequency:
pipe_x = WIDTH + randint(100, 600)
gap_y = randint(HEIGHT // 3, HEIGHT // 2)
pipe_top = Pipe(pipe_x, gap_y, 'top')
pipe_bottom = Pipe(pipe_x, gap_y, 'bottom')
pipe_group.add(pipe_top)
pipe_group.add(pipe_bottom)
last_pipe = time_now
if pygame.sprite.groupcollide(bird_group, pipe_group, False, False):
game_over = True
f1 = pygame.font.Font(None, 36)
text1 = f1.render(f'очки: {score}', True, (180, 0, 0))
text2 = f1.render(f'уровень: {current_level}', True, (180, 0, 0))
text3 = f1.render(f'лучший результат: {high_score}', True, (180, 0, 0))
screen.blit(background, (0, 0))
screen.blit(ground, (ground_scroll, HEIGHT - 100))
pipe_group.draw(screen)
pipe_group.update()
screen.blit(bird.image, bird.rect)
platforms.draw(screen)
screen.blit(text1, (10, 20))
screen.blit(text2, (10, 60))
screen.blit(text3, (10, 100))
pygame.display.flip()
pygame.quit()
pygame.quit()
исправь игру