class Game: def __init__(self): pygame.mixer.music.stop() pygame.mixer.music.load("sounds/game.mp3") pygame.mixer.music.play(-1, 0.0) self.bg = pygame.image.load('imgs/game-bg.png') # variavel que determina a velocidade de tiro self.bullet_cd_timer = 0 self.bullet_cd_time = 60 self.bullets = [] # game objects self.player = Player() self.enemy = Enemies() self.hud = HUD() self.bullet_sound = pygame.mixer.Sound("sounds/shoot.wav") # funçao que determina o intervalo entre um tiro e outro def fire(self): if self.bullet_cd_timer <= 0: self.bullets.append(Bullet(self.player.p_X)) self.bullet_cd_timer = self.bullet_cd_time # funcao que faz a açao de atirar def update(self, keys,keypress): self.bullet_cd_timer -= 1 if keys[pygame.K_SPACE]: self.bullet_sound.play() self.bullet_sound.set_volume(0.1) self.fire() # atualiza na tela os objetos self.player.update(keys) # some o tiro caso n atinja ninguém for bullet in self.bullets: if not bullet.update(keys): self.bullets.remove(bullet) return # some o tiro caso atinja o inimigo if self.enemy.collision_bullet(bullet): self.bullets.remove(bullet) self.hud.add_score() # funçao que determina o fim de jogo if self.enemy.collision_pve(self.player): return 'game-over' self.hud.update(keys) if self.enemy: self.enemy.update(keys) # aparece o HUD na tela def draw(self, screen): screen.blit(self.bg, (0, 0)) self.hud.draw(screen) # desenha o inimigo e a bala na tela for bullet in self.bullets: bullet.draw(screen) self.player.draw(screen) if self.enemy: self.enemy.draw(screen)
class CustomEnv: def __init__(self): self.high = np.array([1.1, 1.1, 0.85, 1.03, 1.03]) self.low = np.array([-1.1, -1.1, -0.01, -0.04, -0.04]) self.WIDTH = world_settings["WIDTH"] self.HEIGHT = world_settings["HEIGHT"] self.display_render = False self.game_over = False self.player = Player( self, random.randint(0, self.WIDTH - player_setting["width"]), #Randon position x self.HEIGHT - player_setting["height"] - player_setting["padding"], #position bottom ) self.autoplayer = Player( self, random.randint(0, self.WIDTH - player_setting["width"]), #Randon position x player_setting["height"] - player_setting["padding"], #position top ) self.ball = Ball( self, random.randint(16, self.WIDTH - 16), self.HEIGHT / 2 - 8, [self.player, self.autoplayer] #Interact with ) self.clock = pg.time.Clock() def return_observation(self): return [ self.ball.vector["x"], self.ball.vector["y"], self.player.x / self.WIDTH, self.ball.x / self.WIDTH, self.ball.y / self.HEIGHT ] def reset(self): self.__init__() return self.return_observation() def step(self, action): self.ball.update() #Upgrade positon ball self.ball.speed += 0.001 #Acceleration self.player.move(action - 1) #action happens input (0, 1, 2) - 1 = (-1, 0, 1) self.autoplayer.auto(self.ball) #Auto move bot return self.return_observation(), self.player.score, self.game_over def render(self, FPS): if not self.display_render: self.display = pg.display.set_mode((self.WIDTH, self.HEIGHT)) self.display_render = True pg.event.get() pg.display.update() self.display.fill((0, 0, 0)) self.ball.draw() self.player.draw() self.autoplayer.draw() self.clock.tick(FPS)