Advertisement
Josif_tepe

Untitled

Dec 7th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import pygame as pg
  2. pg.init()
  3.  
  4. SCREEN_WIDTH = 270
  5. SCREEN_HEIGHT = 620
  6.  
  7. clock = pg.time.Clock()
  8. FPS = 60
  9. screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  10. pg.display.set_caption('Flappy Bird', 'Assets/yellowbird-downflap.png')
  11.  
  12. bg = pg.image.load('Assets/background-day.png')
  13. ground_img = pg.image.load('Assets/base.png')
  14.  
  15. class Bird(pg.sprite.Sprite):
  16.     def __init__(self, x, y):
  17.         pg.sprite.Sprite.__init__(self)
  18.         self.images = []
  19.         self.index = 0
  20.         self.counter = 0
  21.         for i in range(1, 4):
  22.             img = pg.image.load(f'Assets/red_bird_{i}.png')
  23.             self.images.append(img)
  24.  
  25.         self.image = self.images[self.index]
  26.         self.rect = self.image.get_rect()
  27.         self.rect.center = [x, y]
  28.         self.velocity = 0
  29.         self.clicked = False
  30.  
  31.     def update(self):
  32.         if game_running:
  33.             self.velocity += 0.3
  34.             if self.velocity > 8:
  35.                 self.velocity = 8
  36.             if self.rect.bottom < 512:
  37.                 self.rect.y += int(self.velocity)
  38.         if not game_over:
  39.             if pg.mouse.get_pressed()[0] == 1 and not self.clicked:
  40.                 self.velocity -= 10
  41.                 self.clicked = True
  42.  
  43.             if pg.mouse.get_pressed()[0] == 0:
  44.                 self.clicked = False
  45.  
  46.             self.counter += 1
  47.             flap_cooldown = 7
  48.  
  49.             if self.counter > flap_cooldown:
  50.                 self.counter = 0
  51.                 self.index += 1
  52.             if self.index >= len(self.images):
  53.                 self.index = 0
  54.             self.image = self.images[self.index]
  55.             self.image = pg.transform.rotate(self.images[self.index], self.velocity * -2)
  56.         else:
  57.             self.image = pg.transform.rotate(self.images[self.index], -90)
  58.  
  59.  
  60.  
  61. bird_group = pg.sprite.Group()
  62. player = Bird(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
  63. bird_group.add(player)
  64.  
  65.  
  66.  
  67.  
  68. ground_scroll = 0
  69. scroll_speed = 4
  70. is_active = True
  71. game_running = False
  72. game_over = False
  73. while is_active:
  74.     clock.tick(FPS)
  75.  
  76.     screen.blit(bg, (0, 0))
  77.     bird_group.draw(screen)
  78.     bird_group.update()
  79.  
  80.     screen.blit(ground_img, (ground_scroll, 512))
  81.  
  82.     if player.rect.bottom > 512:
  83.         game_over = True
  84.         game_running = False
  85.  
  86.     if game_over == False:
  87.         ground_scroll -= scroll_speed
  88.         if abs(ground_scroll) > 35:
  89.             ground_scroll = 0
  90.  
  91.     for event in pg.event.get():
  92.         if event.type == pg.QUIT:
  93.             is_active = False
  94.         if event.type == pg.MOUSEBUTTONDOWN and not game_running and not game_over:
  95.             game_running = True
  96.  
  97.     pg.display.update()
  98. pg.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement