Advertisement
Okonar

F B_виправлений код

May 15th, 2024 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4.  
  5. pygame.init()
  6.  
  7. WHITE = (255, 255, 255)
  8. fps = 60
  9. back = (200, 255, 255)
  10. RED = (255, 0, 0)
  11. GREEN = (0, 255, 51)
  12. YELLOW = (255, 255, 0)
  13. DARK_BLUE = (0, 0, 100)
  14. BLUE = (80, 80, 255)
  15. LIGHT_GREEN = (200, 255, 200)
  16. LIGHT_RED = (250, 128, 114)
  17. BLACK = (0, 0, 0)
  18.  
  19. bird_images = [pygame.image.load("bird_down.png"),
  20.                pygame.image.load("bird_mid.png"),
  21.                pygame.image.load("bird_up.png")]
  22. skyline_image = pygame.image.load("background.png")
  23. ground_image = pygame.image.load("ground.png")
  24. top_pipe_image = pygame.image.load("pipe_top.png")
  25. bottom_pipe_image = pygame.image.load("pipe_bottom.png")
  26. game_over_image = pygame.image.load("game_over.png")
  27. start_image = pygame.image.load("start.png")
  28.  
  29. rect_x = 50
  30. rect_y = 200
  31. rect_height = 60
  32. rect_width = 60
  33.  
  34. window = pygame.display.set_mode((720, 551))
  35. clock = pygame.time.Clock()
  36. Pos = 200
  37. PosG = 0
  38. Speed = 0
  39. Acc = 0
  40. player = pygame.Rect(100, Pos, 40, 40)
  41.  
  42.  
  43. class FlapyBird():
  44.     def __init__(self, x, y, color, width=500, height=500):
  45.         self.x = x
  46.         self.y = y
  47.         self.width = width
  48.         self.height = height
  49.         self.color = color
  50.         self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
  51.  
  52.     def draw_rect(self, window):
  53.         pygame.draw.rect(window, self.color, self.rect)
  54.  
  55.  
  56. class PipeUp():
  57.     def __init__(self, x_up, y_up, color, height=500, width=50, hole=150):
  58.         self.x_up = x_up
  59.         self.y_up = y_up
  60.         self.height = height
  61.         self.width = width
  62.         self.hole = random.randint(150, 200)
  63.         self.color = color
  64.         self.rect_up = pygame.Rect(self.x_up, self.y_up, self.width, self.height)
  65.  
  66.     def draw_rect(self, window):
  67.         pygame.draw.rect(window, self.color, self.rect_up)
  68.  
  69.  
  70. class PipeDown():
  71.     def __init__(self, x_down, y_down, color, height=500, width=50):
  72.         self.x_down = x_down
  73.         self.y_down = y_down
  74.         self.height = height
  75.         self.width = width
  76.         self.color = color
  77.         self.rect_down = pygame.Rect(self.x_down, self.y_down, self.width, self.height)
  78.  
  79.     def draw_rect(self, window):
  80.         pygame.draw.rect(window, self.color, self.rect_down)
  81.  
  82.  
  83. start_time = time.time()
  84. cur_time = start_time
  85. font1 = pygame.font.Font(None, 40)
  86. PressInMenu = font1.render('Click to start', True, BLACK)
  87.  
  88. record = 0
  89. Points = 0
  90. CX = 5
  91. flag = True
  92. game = 'lose'
  93.  
  94. pipes = []
  95.  
  96. while flag:
  97.     for event in pygame.event.get():
  98.         if event.type == pygame.QUIT:
  99.             flag = False
  100.             pygame.quit()
  101.             exit()
  102.  
  103.     window.blit(pygame.transform.scale(skyline_image, (window.get_width(), window.get_height())), (0, 0))
  104.  
  105.     if game != 'Pad':
  106.         window.blit(bird_images[1], (player.x, player.y))
  107.         greenFl = FlapyBird(0, 490, GREEN)
  108.         greenFl.draw_rect(window)
  109.  
  110.     Butt = pygame.mouse.get_pressed()
  111.     click = Butt[0]
  112.  
  113.     if game == 'lose':
  114.         nameG = FlapyBird(100, 0, LIGHT_RED, 300, 100)
  115.         nameG.draw_rect(window)
  116.         window.blit(PressInMenu, (200, 250))
  117.  
  118.         if click:
  119.             game = 'ready'
  120.  
  121.     elif game == 'ready':
  122.         if CX:
  123.             nameG = FlapyBird(100, PosG, LIGHT_RED, 300, 100)
  124.             nameG.draw_rect(window)
  125.             PosG -= 3
  126.             if PosG == -150:
  127.                 CX = False
  128.  
  129.         # Відображення та рух труб
  130.         for pipe_pair in pipes:
  131.             pipe_up, pipe_down = pipe_pair
  132.             pipe_up.draw_rect(window)
  133.             pipe_down.draw_rect(window)
  134.             pipe_up.rect_up.x -= 3
  135.             pipe_down.rect_down.x -= 3
  136.  
  137.         # Генерація нових труб
  138.         if pipes and pipes[-1][0].rect_up.x < 720 - 300:
  139.             new_pipe_up = PipeUp(720, random.randint(-400, -300), RED, 500, 25)
  140.             new_pipe_down = PipeDown(720, random.randint(300, 400), RED, 500, 25)
  141.             pipes.append((new_pipe_up, new_pipe_down))
  142.         elif not pipes:
  143.             new_pipe_up = PipeUp(720, random.randint(-400, -300), RED, 500, 25)
  144.             new_pipe_down = PipeDown(720, random.randint(300, 400), RED, 500, 25)
  145.             pipes.append((new_pipe_up, new_pipe_down))
  146.  
  147.         # Видалення труб що виходять за межі екрану
  148.         if pipes and pipes[0][0].rect_up.x < -50:
  149.             pipes.pop(0)
  150.  
  151.         if click:
  152.             Acc = -0.5
  153.         else:
  154.             Acc = 0
  155.  
  156.         Pos += Speed
  157.         Speed = (Speed + Acc + 0.25)
  158.  
  159.         player.y = Pos
  160.  
  161.         if player.top < 0 or player.top > 450:
  162.             game = 'Pad'
  163.  
  164.     elif game == 'Pad':
  165.         window.blit(game_over_image, ((window.get_width() - game_over_image.get_width()) // 2, (window.get_height() - game_over_image.get_height()) // 2))
  166.         if click:
  167.             player.y = 200
  168.             game = 'ready'
  169.             Pos = 200
  170.             Speed = 0
  171.             Acc = 0
  172.             CX = 5
  173.             PosG = 0
  174.             pipes.clear()  # Очистка списку із трубами
  175.             pygame.event.clear()
  176.  
  177.     pygame.display.update()
  178.     clock.tick(fps)
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement