Advertisement
Josif_tepe

Untitled

Dec 22nd, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.68 KB | None | 0 0
  1. import random
  2.  
  3. import pygame as pg
  4. pg.init()
  5.  
  6. SCREEN_WIDTH = 288
  7. SCREEN_HEIGHT = 620
  8.  
  9. clock = pg.time.Clock()
  10. FPS = 80
  11. ground_scroll = 0
  12. scroll_speed = 4
  13. is_active = True
  14. game_running = False
  15. game_over = False
  16. pipe_gap = 300
  17. pipe_frequency = 600
  18. last_pipe = pg.time.get_ticks() - pipe_frequency
  19. screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  20. score = 0
  21. passed_pipe = False
  22. pg.display.set_caption('Flappy Bird', 'Assets/yellowbird-downflap.png')
  23.  
  24. bg = pg.image.load('Assets/background-day.png')
  25. ground_img = pg.image.load('Assets/base.png')
  26. font = pg.font.SysFont('Times New Roman', 60)
  27. white_color = (255, 255, 255)
  28. def draw_text(text, font, text_color, x, y):
  29.     img = font.render(text, True, text_color)
  30.     screen.blit(img, (x, y))
  31. class Bird(pg.sprite.Sprite):
  32.     def __init__(self, x, y):
  33.         pg.sprite.Sprite.__init__(self)
  34.         self.images = []
  35.         self.index = 0
  36.         self.counter = 0
  37.         for i in range(1, 4):
  38.             img = pg.image.load(f'Assets/red_bird_{i}.png')
  39.             self.images.append(img)
  40.  
  41.         self.image = self.images[self.index]
  42.         self.rect = self.image.get_rect()
  43.         self.rect.center = [x, y]
  44.         self.velocity = 0
  45.         self.clicked = False
  46.  
  47.     def update(self):
  48.         if game_running:
  49.             self.velocity += 0.3
  50.             if self.velocity > 8:
  51.                 self.velocity = 8
  52.             if self.rect.bottom < 512:
  53.                 self.rect.y += int(self.velocity)
  54.         if not game_over:
  55.             if pg.mouse.get_pressed()[0] == 1 and not self.clicked:
  56.                 self.velocity -= 10
  57.                 self.clicked = True
  58.  
  59.             if pg.mouse.get_pressed()[0] == 0:
  60.                 self.clicked = False
  61.  
  62.             self.counter += 1
  63.             flap_cooldown = 7
  64.  
  65.             if self.counter > flap_cooldown:
  66.                 self.counter = 0
  67.                 self.index += 1
  68.             if self.index >= len(self.images):
  69.                 self.index = 0
  70.             self.image = self.images[self.index]
  71.             self.image = pg.transform.rotate(self.images[self.index], self.velocity * -2)
  72.         else:
  73.             self.image = pg.transform.rotate(self.images[self.index], -90)
  74.  
  75. class Pipe(pg.sprite.Sprite):
  76.     def __init__(self, x, y, pos):
  77.         pg.sprite.Sprite.__init__(self)
  78.         self.image = pg.image.load('Assets/pipe-green.png')
  79.         self.rect = self.image.get_rect()
  80.         if pos == 1:
  81.             self.image = pg.transform.flip(self.image, False, True)
  82.             self.rect.bottomleft = [x, y - int(pipe_gap / 2)]
  83.         else:
  84.             self.rect.topleft = [x, y + int(pipe_gap / 2)]
  85.     def update(self):
  86.         self.rect.x -= scroll_speed
  87.         if self.rect.right < 0:
  88.             self.kill()
  89.  
  90. bird_group = pg.sprite.Group()
  91. pipe_group = pg.sprite.Group()
  92. player = Bird(50, SCREEN_HEIGHT / 2)
  93.  
  94. bird_group.add(player)
  95.  
  96.  
  97.  
  98.  
  99.  
  100. while is_active:
  101.     clock.tick(FPS)
  102.  
  103.     screen.blit(bg, (0, 0))
  104.     bird_group.draw(screen)
  105.     bird_group.update()
  106.     pipe_group.draw(screen)
  107.     screen.blit(ground_img, (ground_scroll, 512))
  108.  
  109.     if len(pipe_group) > 0:
  110.         if bird_group.sprites()[0].rect.left > pipe_group.sprites()[0].rect.left and bird_group.sprites()[0].rect.right < pipe_group.sprites()[0].rect.right and passed_pipe == False:
  111.             passed_pipe = True
  112.         if passed_pipe:
  113.             if bird_group.sprites()[0].rect.left > pipe_group.sprites()[0].rect.right:
  114.                 score += 1
  115.                 passed_pipe = False
  116.         print(score)
  117.     draw_text(str(score), font, white_color, int(SCREEN_WIDTH / 2), 40)
  118.  
  119.     if pg.sprite.groupcollide(bird_group, pipe_group, False, False) or player.rect.top < 0:
  120.         game_over = True
  121.  
  122.     if player.rect.bottom > 512:
  123.         game_over = True
  124.         game_running = False
  125.  
  126.     if game_over == False and game_running == True:
  127.         current_time = pg.time.get_ticks()
  128.         if current_time - last_pipe > pipe_frequency:
  129.             random_height = random.randint(-50, 50)
  130.             bottom_pipe = Pipe(SCREEN_WIDTH, int(SCREEN_HEIGHT / 2) + random_height, 1)
  131.             up_pipe = Pipe(SCREEN_WIDTH, int(SCREEN_HEIGHT / 2) + random_height, -1)
  132.             pipe_group.add(bottom_pipe)
  133.             pipe_group.add(up_pipe)
  134.             last_pipe = current_time
  135.         ground_scroll -= scroll_speed
  136.         if abs(ground_scroll) > 100:
  137.             ground_scroll = 0
  138.  
  139.     for event in pg.event.get():
  140.         if event.type == pg.QUIT:
  141.             is_active = False
  142.         if event.type == pg.MOUSEBUTTONDOWN and not game_running and not game_over:
  143.             game_running = True
  144.     pipe_group.update()
  145.  
  146.     pg.display.update()
  147. pg.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement