Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import time
- pygame.init()
- WHITE = (255, 255, 255)
- fps = 60
- back = (200, 255, 255)
- RED = (255, 0, 0)
- GREEN = (0, 255, 51)
- YELLOW = (255, 255, 0)
- DARK_BLUE = (0, 0, 100)
- BLUE = (80, 80, 255)
- LIGHT_GREEN = (200, 255, 200)
- LIGHT_RED = (250, 128, 114)
- BLACK = (0, 0, 0)
- bird_images = [pygame.image.load("bird_down.png"),
- pygame.image.load("bird_mid.png"),
- pygame.image.load("bird_up.png")]
- skyline_image = pygame.image.load("background.png")
- ground_image = pygame.image.load("ground.png")
- top_pipe_image = pygame.image.load("pipe_top.png")
- bottom_pipe_image = pygame.image.load("pipe_bottom.png")
- game_over_image = pygame.image.load("game_over.png")
- start_image = pygame.image.load("start.png")
- rect_x = 50
- rect_y = 200
- rect_height = 60
- rect_width = 60
- window = pygame.display.set_mode((720, 551))
- clock = pygame.time.Clock()
- Pos = 200
- PosG = 0
- Speed = 0
- Acc = 0
- player = pygame.Rect(100, Pos, 40, 40)
- class FlapyBird():
- def __init__(self, x, y, color, width=500, height=500):
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.color = color
- self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
- def draw_rect(self, window):
- pygame.draw.rect(window, self.color, self.rect)
- class PipeUp():
- def __init__(self, x_up, y_up, color, height=500, width=50, hole=150):
- self.x_up = x_up
- self.y_up = y_up
- self.height = height
- self.width = width
- self.hole = random.randint(150, 200)
- self.color = color
- self.rect_up = pygame.Rect(self.x_up, self.y_up, self.width, self.height)
- def draw_rect(self, window):
- pygame.draw.rect(window, self.color, self.rect_up)
- class PipeDown():
- def __init__(self, x_down, y_down, color, height=500, width=50):
- self.x_down = x_down
- self.y_down = y_down
- self.height = height
- self.width = width
- self.color = color
- self.rect_down = pygame.Rect(self.x_down, self.y_down, self.width, self.height)
- def draw_rect(self, window):
- pygame.draw.rect(window, self.color, self.rect_down)
- start_time = time.time()
- cur_time = start_time
- font1 = pygame.font.Font(None, 40)
- PressInMenu = font1.render('Click to start', True, BLACK)
- record = 0
- Points = 0
- CX = 5
- flag = True
- game = 'lose'
- pipes = []
- while flag:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- flag = False
- pygame.quit()
- exit()
- window.blit(pygame.transform.scale(skyline_image, (window.get_width(), window.get_height())), (0, 0))
- if game != 'Pad':
- window.blit(bird_images[1], (player.x, player.y))
- greenFl = FlapyBird(0, 490, GREEN)
- greenFl.draw_rect(window)
- Butt = pygame.mouse.get_pressed()
- click = Butt[0]
- if game == 'lose':
- nameG = FlapyBird(100, 0, LIGHT_RED, 300, 100)
- nameG.draw_rect(window)
- window.blit(PressInMenu, (200, 250))
- if click:
- game = 'ready'
- elif game == 'ready':
- if CX:
- nameG = FlapyBird(100, PosG, LIGHT_RED, 300, 100)
- nameG.draw_rect(window)
- PosG -= 3
- if PosG == -150:
- CX = False
- # Відображення та рух труб
- for pipe_pair in pipes:
- pipe_up, pipe_down = pipe_pair
- pipe_up.draw_rect(window)
- pipe_down.draw_rect(window)
- pipe_up.rect_up.x -= 3
- pipe_down.rect_down.x -= 3
- # Генерація нових труб
- if pipes and pipes[-1][0].rect_up.x < 720 - 300:
- new_pipe_up = PipeUp(720, random.randint(-400, -300), RED, 500, 25)
- new_pipe_down = PipeDown(720, random.randint(300, 400), RED, 500, 25)
- pipes.append((new_pipe_up, new_pipe_down))
- elif not pipes:
- new_pipe_up = PipeUp(720, random.randint(-400, -300), RED, 500, 25)
- new_pipe_down = PipeDown(720, random.randint(300, 400), RED, 500, 25)
- pipes.append((new_pipe_up, new_pipe_down))
- # Видалення труб що виходять за межі екрану
- if pipes and pipes[0][0].rect_up.x < -50:
- pipes.pop(0)
- if click:
- Acc = -0.5
- else:
- Acc = 0
- Pos += Speed
- Speed = (Speed + Acc + 0.25)
- player.y = Pos
- if player.top < 0 or player.top > 450:
- game = 'Pad'
- elif game == 'Pad':
- window.blit(game_over_image, ((window.get_width() - game_over_image.get_width()) // 2, (window.get_height() - game_over_image.get_height()) // 2))
- if click:
- player.y = 200
- game = 'ready'
- Pos = 200
- Speed = 0
- Acc = 0
- CX = 5
- PosG = 0
- pipes.clear() # Очистка списку із трубами
- pygame.event.clear()
- pygame.display.update()
- clock.tick(fps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement