Advertisement
Josif_tepe

Untitled

Dec 28th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.86 KB | None | 0 0
  1. import json
  2. import random
  3. import requests
  4. import pygame as pg
  5. pg.init()
  6.  
  7. SCREEN_WIDTH = 864
  8. SCREEN_HEIGHT = 768
  9.  
  10. clock = pg.time.Clock()
  11. FPS = 60
  12. ground_scroll = 0
  13. scroll_speed = 4
  14. is_active = True
  15. game_running = False
  16. game_over = False
  17. pipe_gap = 230
  18. pipe_frequency = 1500
  19. first_play = True
  20. last_pipe = pg.time.get_ticks() - pipe_frequency
  21. screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  22. score = 0
  23. passed_pipe = False
  24. pg.display.set_caption('Flappy Bird', 'Assets/yellowbird-downflap.png')
  25.  
  26. bg = pg.image.load('Assets/bg.png')
  27. ground_img = pg.image.load('Assets/ground.png')
  28. button_img = pg.image.load('Assets/PlayButton.png')
  29. start_screen_img = pg.image.load('Assets/message.png')
  30. font = pg.font.Font('Assets/Grand9K Pixel.ttf', 60)
  31. white_color = (255, 255, 255)
  32. def draw_text(text, font, text_color, x, y):
  33.     img = font.render(text, True, text_color)
  34.     screen.blit(img, (x, y))
  35.  
  36. def restart_game():
  37.     pipe_group.empty()
  38.     player.rect.x = 50
  39.     player.rect.y = SCREEN_WIDTH // 2
  40.     score = 0
  41.     return 0
  42. class Bird(pg.sprite.Sprite):
  43.     def __init__(self, x, y):
  44.         pg.sprite.Sprite.__init__(self)
  45.         self.images = []
  46.         self.index = 0
  47.         self.counter = 0
  48.         for i in range(1, 4):
  49.             img = pg.image.load(f'Assets/bird{i}.png')
  50.             self.images.append(img)
  51.  
  52.         self.image = self.images[self.index]
  53.         self.rect = self.image.get_rect()
  54.         self.rect.center = [x, y]
  55.         self.velocity = 0
  56.         self.clicked = False
  57.  
  58.     def update(self):
  59.         if game_running:
  60.             self.velocity += 0.5
  61.             if self.velocity > 8:
  62.                 self.velocity = 8
  63.             if self.rect.bottom < 600:
  64.                 self.rect.y += int(self.velocity)
  65.         if not game_over:
  66.             if pg.mouse.get_pressed()[0] == 1 and not self.clicked:
  67.                 self.velocity -= 10
  68.                 self.clicked = True
  69.  
  70.             if pg.mouse.get_pressed()[0] == 0:
  71.                 self.clicked = False
  72.  
  73.             self.counter += 1
  74.             flap_cooldown = 5
  75.  
  76.             if self.counter > flap_cooldown:
  77.                 self.counter = 0
  78.                 self.index += 1
  79.             if self.index >= len(self.images):
  80.                 self.index = 0
  81.             self.image = self.images[self.index]
  82.             self.image = pg.transform.rotate(self.images[self.index], self.velocity * -2)
  83.         else:
  84.             self.image = pg.transform.rotate(self.images[self.index], -90)
  85.  
  86. class Pipe(pg.sprite.Sprite):
  87.     def __init__(self, x, y, pos):
  88.         pg.sprite.Sprite.__init__(self)
  89.         self.image = pg.image.load('Assets/pipe.png')
  90.         self.rect = self.image.get_rect()
  91.         if pos == 1:
  92.             self.image = pg.transform.flip(self.image, False, True)
  93.             self.rect.bottomleft = [x, y - int(pipe_gap / 2)]
  94.         else:
  95.             self.rect.topleft = [x, y + int(pipe_gap / 2)]
  96.     def update(self):
  97.         self.rect.x -= scroll_speed
  98.         if self.rect.right < 0:
  99.             self.kill()
  100.  
  101. class Button():
  102.     def __init__(self, x, y, image):
  103.         self.image = image
  104.         self.rect = self.image.get_rect()
  105.         self.rect.topleft = (x, y)
  106.  
  107.     def draw(self):
  108.         mouse_position = pg.mouse.get_pos()
  109.         is_pressed = False
  110.         if self.rect.collidepoint(mouse_position):
  111.             if pg.mouse.get_pressed()[0] == 1:
  112.                 is_pressed = True
  113.  
  114.         screen.blit(self.image, (self.rect.x, self.rect.y))
  115.         return is_pressed
  116.  
  117. bird_group = pg.sprite.Group()
  118. pipe_group = pg.sprite.Group()
  119. player = Bird(100, SCREEN_HEIGHT / 2)
  120.  
  121. bird_group.add(player)
  122.  
  123. button = Button(SCREEN_WIDTH // 2 - 30, SCREEN_HEIGHT // 2 - 40, button_img)
  124.  
  125.  
  126.  
  127.  
  128. req = requests.get('https://skopje.pulse.eco/rest/current')
  129. to_json = json.loads(req.text)
  130. rand_idx = random.randint(0, len(to_json))
  131. value = to_json[rand_idx]['value']
  132. if int(value) < 40:
  133.     pipe_gap = 230
  134. else:
  135.     pipe_gap = 150
  136. while is_active:
  137.     clock.tick(FPS)
  138.  
  139.     screen.blit(bg, (0, 0))
  140.  
  141.     bird_group.draw(screen)
  142.     bird_group.update()
  143.     pipe_group.draw(screen)
  144.     screen.blit(ground_img, (ground_scroll, 600))
  145.  
  146.     if len(pipe_group) > 0:
  147.         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:
  148.             passed_pipe = True
  149.         if passed_pipe:
  150.             if bird_group.sprites()[0].rect.left > pipe_group.sprites()[0].rect.right:
  151.                 score += 1
  152.                 passed_pipe = False
  153.         print(score)
  154.     draw_text(str(score), font, white_color, int(SCREEN_WIDTH / 2), 40)
  155.  
  156.     if pg.sprite.groupcollide(bird_group, pipe_group, False, False) or player.rect.top < 0:
  157.         game_over = True
  158.  
  159.     if player.rect.bottom > 600:
  160.         game_over = True
  161.         game_running = False
  162.  
  163.     if game_over == False and game_running == True:
  164.         current_time = pg.time.get_ticks()
  165.         if current_time - last_pipe > pipe_frequency:
  166.             random_height = random.randint(-100, 100)
  167.             bottom_pipe = Pipe(SCREEN_WIDTH, int(SCREEN_HEIGHT / 2) + random_height, 1)
  168.             up_pipe = Pipe(SCREEN_WIDTH, int(SCREEN_HEIGHT / 2) + random_height, -1)
  169.             pipe_group.add(bottom_pipe)
  170.             pipe_group.add(up_pipe)
  171.             last_pipe = current_time
  172.         pipe_group.update()
  173.         ground_scroll -= scroll_speed
  174.         if abs(ground_scroll) > 35:
  175.             ground_scroll = 0
  176.  
  177.  
  178.     if game_over:
  179.         if button.draw():
  180.             game_over = False
  181.             score = restart_game()
  182.  
  183.  
  184.  
  185.     for event in pg.event.get():
  186.         if event.type == pg.QUIT:
  187.             is_active = False
  188.         if event.type == pg.MOUSEBUTTONDOWN and not game_running and not game_over:
  189.             game_running = True
  190.  
  191.     pg.display.update()
  192. pg.quit()
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement