def reset(self): self.obs = np.array([self.minH, self.maxH, self.bird.center_y]) #self.obs = [self.pipe_sprites,self.bird] self.score = 0 self.done = False self.pipe_sprites = arcade.SpriteList() # A dict holding sprites of static stuff like background & base self.base = arcade.load_texture(BASE) self.bird._set_center_y(250) self.bird._set_angle(30) self.reward = 0 # Create a random pipe (Obstacle) to start with. start_pipe1 = Pipe.random_pipe_obstacle(self.sprites, self.height) self.pipe_sprites.append(start_pipe1[0]) self.pipe_sprites.append(start_pipe1[1]) return self.obs
def setup(self): self.score = 0 self.reward = 0 self.score_board = arcade.SpriteList() self.pipe_sprites = arcade.SpriteList() self.bird_list = arcade.SpriteList() # A dict holding sprites of static stuff like background & base self.background = arcade.load_texture(random.choice(BACKGROUNDS)) self.base = arcade.load_texture(BASE) # A dict holding a reference to the textures self.sprites = dict() self.sprites['background'] = self.background self.sprites['base'] = self.base # The bird object itself. # The AnimatedTimeSprite makes an animated sprite that animates over time. self.bird = Bird(50, self.height // 2, self.base.height) self.bird_list.append(self.bird) # Create a random pipe (Obstacle) to start with. start_pipe1 = Pipe.random_pipe_obstacle(self.sprites, self.height) self.pipe_sprites.append(start_pipe1[0]) self.pipe_sprites.append(start_pipe1[1])
def on_update(self, delta_time): """ This is the method called each frame to update objects (Like their position, angle, etc..) before drawing them. """ # Whatever the state, update the bird animation (as in advance the animation to the next frame) self.bird_list.update_animation() if self.state == State.PLAYING: self.build_score_board() new_pipe = None for pipe in self.pipe_sprites: if pipe.right <= 0: pipe.kill() elif len(self.pipe_sprites ) == 2 and pipe.right <= random.randrange( self.width // 2, self.width // 2 + 15): new_pipe = Pipe.random_pipe_obstacle( self.sprites, self.height) if new_pipe: self.pipe_sprites.append(new_pipe[0]) self.pipe_sprites.append(new_pipe[1]) # If the player pressed space, let the bird fly higher if self.flapped: self.bird.flap() self.flapped = False # Check if bird is too high if self.bird.top > self.height: self.bird.top = self.height self.reward += -1 # Check if bird is too low if self.bird.bottom <= self.base.height: if self.bird.change_y < 0: self.bird.change_y = 0 self.bird.bottom = self.base.height # Kill pipes that are no longer shown on the screen as they're useless and live in ram and create a new pipe # when needed. (If the center_x of the closest pipe to the bird passed the middle of the screen) # This calls "update()" Method on each object in the SpriteList self.pipe_sprites.update() self.bird.update(delta_time) self.bird_list.update() # If the bird passed the center of the pipe safely, count it as a point. # Hard coding.. :) if self.pipe_sprites and self.pipe_sprites[ 0] and self.bird.center_x >= self.pipe_sprites[ 0].center_x and not self.pipe_sprites[0].scored: self.score += 1 self.reward += (50 * self.score) # Well, since each "obstacle" is a two pipe system, we gotta count them both as scored. self.pipe_sprites[0].scored = True self.pipe_sprites[1].scored = True self.maxH = self.pipe_sprites[2].top self.minH = self.pipe_sprites[3].bottom self.draw_zone() # Check if the bird collided with any of the pipes hit = arcade.check_for_collision_with_list(self.bird, self.pipe_sprites) if any(hit): self.reward += -100 self.done = True time.sleep(1) self.state = State.PLAYING self.reset() #self.obs = [self.minH, self.maxH, self.bird.center_y] self.obs = np.array([ self.minH, self.maxH, self.bird.center_y, self.bird.center_x, self.pipe_sprites[0].center_x ]) elif self.state == State.GAME_OVER: # We need to keep updating the bird in the game over scene so it can still "die" self.bird.update()