anasazhar

main.py

Nov 9th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3.  
  4.  
  5. from settings import Settings
  6. from bird import Bird
  7. from pipe import Pipe
  8. from platform import Platform
  9.  
  10. class Arena_Game:
  11.  
  12.     ################################
  13.     #ARENA / SCREEN
  14.     ################################
  15.     def __init__(self):
  16.         pygame.init()
  17.         self.game_settings = Settings()
  18.  
  19.         self.screen = pygame.display.set_mode([self.game_settings.screen_width, self.game_settings.screen_height])
  20.         self.title = pygame.display.set_caption(self.game_settings.title)
  21.         self.running = True
  22.  
  23.         ############
  24.         #OBJ IN GAME
  25.         ############
  26.         #SINGLE OBJ
  27.         self.game_bird = Bird(self)
  28.         self.game_platform = Platform(self)
  29.  
  30.         #GROUP OBJ
  31.         self.game_pipes = pygame.sprite.Group()
  32.         self.create_pipes()
  33.  
  34.     def update_bg_screen(self):
  35.         self.screen.blit(self.game_settings.background, [0,0])
  36.  
  37.     ###############################
  38.     #BIRD
  39.     ###############################
  40.     def update_bird(self):
  41.         self.game_bird.show_bird()
  42.  
  43.  
  44.     ###############################
  45.     #PLATFORM
  46.     ###############################
  47.     def update_platform(self):
  48.         self.game_platform.move()
  49.         self.game_platform.show_platform()
  50.  
  51.  
  52.     ###############################
  53.     #PIPE
  54.     ###############################
  55.     def update_pipes(self):
  56.         for pipe in self.game_pipes.sprites():
  57.             pipe.move()
  58.             pipe.show_pipe()
  59.  
  60.     def create_pipes(self):
  61.         screen_rect = self.screen.get_rect()
  62.         screen_height = screen_rect.height
  63.  
  64.         pipe_top_height = randint(100, 454)
  65.         pipe_bottom_height = 640 - pipe_top_height - 100
  66.  
  67.         pipe_top = Pipe(self)
  68.         pipe_bottom = Pipe(self)
  69.  
  70.         pipe_top.pipe_image.height = pipe_top_height #set ulang tinggi pipe_top
  71.         pipe_bottom.pipe_image.height = pipe_bottom_height #set ulang tinggi pipe_bottom
  72.  
  73.         pipe_bottom.pipe_image.midtop = pipe_top.pipe_image.midbottom #set ulang posisi pipe_bottom
  74.         pipe_bottom.pipe_image.y += 100 #memberikan jarak antara pipe_top dan pipe_bottom
  75.         pipe_top.head.head_rect.midbottom = pipe_top.pipe_image.midbottom #set posisi head pipe_top
  76.         pipe_bottom.head.head_rect.midtop = pipe_bottom.pipe_image.midtop #set posisi head pipe_bottom
  77.  
  78.         self.game_pipes.add(pipe_top)
  79.         self.game_pipes.add(pipe_bottom)
  80.  
  81.  
  82.     ################################
  83.     #RUN GAME
  84.     ################################
  85.     def rg_check_events(self):
  86.         events = pygame.event.get()
  87.         #print(events)
  88.         for event in events:
  89.             if event.type == pygame.QUIT:
  90.                 self.running = False
  91.  
  92.     def rg_update_screen(self):
  93.         self.update_bg_screen() #Update BG
  94.         self.update_bird() # Update Bird Postision
  95.         self.update_pipes() #Update Pipes
  96.         self.update_platform()
  97.         pygame.display.flip() #Update Frame Every Second
  98.  
  99.  
  100.     def run_game(self):
  101.         while self.running:
  102.             self.rg_check_events()
  103.             self.rg_update_screen()
  104.  
  105. flappy_bird_game = Arena_Game()
  106. flappy_bird_game.run_game()
Advertisement
Add Comment
Please, Sign In to add comment