Advertisement
Josif_tepe

Untitled

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