Ejemplo n.º 1
0
class Arena_Game:

    ################################
    #ARENA / SCREEN
    ################################
    def __init__(self):
        pygame.init()
        self.game_settings = Settings()

        self.screen = pygame.display.set_mode([
            self.game_settings.screen_width, self.game_settings.screen_height
        ])
        self.title = pygame.display.set_caption(self.game_settings.title)
        self.running = True

        ############
        #OBJ IN GAME
        ############
        #SINGLE OBJ
        self.game_bird = Bird(self)
        self.game_platform = Platform(self)

        #GROUP OBJ
        self.game_pipes = pygame.sprite.Group()
        self.create_pipes()

    def update_bg_screen(self):
        self.screen.blit(self.game_settings.background, [0, 0])

    ###############################
    #BIRD
    ###############################
    def update_bird(self):
        self.game_bird.show_bird()

    ###############################
    #PLATFORM
    ###############################
    def update_platform(self):
        self.game_platform.move()
        self.game_platform.show_platform()

    ###############################
    #PIPE
    ###############################
    def update_pipes(self):
        for pipe in self.game_pipes.sprites():
            pipe.move()
            pipe.show_pipe()

    def create_pipes(self):
        screen_rect = self.screen.get_rect()
        screen_height = screen_rect.height

        pipe_top_height = randint(100, 454)
        pipe_bottom_height = 640 - pipe_top_height - 100

        pipe_top = Pipe(self)
        pipe_bottom = Pipe(self)

        pipe_top.pipe_image.height = pipe_top_height  #set ulang tinggi pipe_top
        pipe_bottom.pipe_image.height = pipe_bottom_height  #set ulang tinggi pipe_bottom

        pipe_bottom.pipe_image.midtop = pipe_top.pipe_image.midbottom  #set ulang posisi pipe_bottom
        pipe_bottom.pipe_image.y += 100  #memberikan jarak antara pipe_top dan pipe_bottom
        pipe_top.head.head_rect.midbottom = pipe_top.pipe_image.midbottom  #set posisi head pipe_top
        pipe_bottom.head.head_rect.midtop = pipe_bottom.pipe_image.midtop  #set posisi head pipe_bottom

        self.game_pipes.add(pipe_top)
        self.game_pipes.add(pipe_bottom)

    ################################
    #RUN GAME
    ################################
    def rg_check_events(self):
        events = pygame.event.get()
        #print(events)
        for event in events:
            if event.type == pygame.QUIT:
                self.running = False

    def rg_update_screen(self):
        self.update_bg_screen()  #Update BG
        self.update_bird()  # Update Bird Postision
        self.update_pipes()  #Update Pipes
        self.update_platform()
        pygame.display.flip()  #Update Frame Every Second

    def run_game(self):
        while self.running:
            self.rg_check_events()
            self.rg_update_screen()
Ejemplo n.º 2
0
class Game:

    win_width = 1450
    win_height = 800

    def __init__(self):
        self.win = Window((Game.win_width, Game.win_height))
        self.sky = Sky(Game.win_width, Game.win_height)
        self.platform = Platform(Game.win_width, 96, Game.win_height - 96 - 30)
        self.dino = None
        self.enemy_velocity = 250
        self.score = 0
        self.create_handlers()
        self.enemies = []

    def create_handlers(self):
        self.win.add_handler('w', lambda x: self.jump())
        self.win.add_handler('W', lambda x: self.jump())
        self.win.add_handler('<space>', lambda x: self.reset())

    def reset(self):
        if not self.dino.alive:
            self.dino.alive = True
            self.enemies = []
            self.score = 0
            self.enemy_velocity = 250

    def jump(self):
        if self.dino:
            self.dino.jump()

    @property
    def window_is_open(self):
        return self.win.open

    def check_collision_with_ground(self):
        if not (self.dino.y + self.dino.height >= self.platform.y):
            return
        self.dino.y = self.platform.y - self.dino.height
        self.dino.reset_force()
        self.dino.allow_jump()

    def update_dino(self, dt: float):
        if not self.dino:
            return
        self.dino.tick(dt)
        self.check_collision_with_ground()

    def remove_enemies(self):
        for enemy in self.enemies:
            if enemy.x + enemy.width < 0:
                self.enemies.remove(enemy)
            else:
                return

    def move_enemies(self, dt: float):
        delta = -dt * self.enemy_velocity
        for enemy in self.enemies:
            enemy.move(delta)

    def tick_enemies(self, dt: float):
        for enemy in self.enemies:
            enemy.tick(dt)

    def update_enemies(self, dt: float):
        self.move_enemies(dt)
        self.tick_enemies(dt)
        self.remove_enemies()

    def update_score(self, dt: float):
        self.score += dt * 10

    def update_platform(self, dt: float):
        delta = -dt * self.enemy_velocity
        self.platform.move(delta)

    def update(self, dt: float):
        self.update_dino(dt)
        self.update_enemies(dt)
        self.update_score(dt)
        self.update_platform(dt)
        self.sky.tick(dt)

    def tick(self, timedelta: float):
        if self.dino.alive:
            self.update(timedelta)
        self.draw()

    def display_score(self):
        c = self.win.canvas
        c.create_text(c.winfo_width() - 10,
                      20,
                      text=f'Score: {int(self.score)}',
                      anchor=tkinter.E,
                      font=("Purisa", 24))

    def display_game_over(self):
        c = self.win.canvas
        c.create_text(c.winfo_width() // 2,
                      c.winfo_height() // 2,
                      text='Game Over',
                      font=("Purisa", 80))
        c.create_text(c.winfo_width() // 2,
                      c.winfo_height() // 2 + 60,
                      text='Press space to play again',
                      font=("Purisa", 25))

    def draw(self):
        self.win.clear()
        self.win.draw(self.sky)
        self.win.draw(self.platform)
        for enemy in self.enemies:
            self.win.draw(enemy)
        if self.dino:
            self.win.draw(self.dino)
        if not self.dino.alive:
            self.display_game_over()
        self.display_score()
        self.win.update()

    def change_ground(self, path):
        self.platform.load_sprite(path)

    def add_player(self, dino: Dino):
        self.dino = dino

    def add_enemy(self, enemy):
        self.enemies.append(enemy)

    def increase_speed(self):
        self.enemy_velocity += 100