Nav (Full version)#
- class retro.examples.nav.Spaceship[source]#
A player-controlled agent which moves left and right, dodging asteroids. Spaceship is a pretty simple class. The ship’s character is
^
, and its position starts at the bottom center of the screen.
- class retro.examples.nav.Asteroid(position)[source]#
When Asteroids are spawned, they fall down the screen until they reach the bottom row and are removed. An Asteroid’s position is set when it is created. Whenever an asteroid moves, it checks whether it has it the ship.
- play_turn(game)[source]#
Nothing happens unless
game.turn_number
is divisible by 2. The result is that asteroids only move on even-numbered turns. If the asteroid is at the bottom of the screen, it has run its course and should be removed from the game. Otherwise, the asteroid’s new position is one space down from its old position. If the asteroid’s new position is the same as the ship’s position, the game ends.
- set_color()[source]#
To add to the game’s drama, asteroids gradually become visible as they fall down the screen. This method calculates the ratio of the asteroid’s position compared to the screen height–0 is the top of the screen and 1 is the bottom ot the screen. Then sets the asteroid’s color depending on the ratio. (Available colors)
- class retro.examples.nav.AsteroidSpawner[source]#
An agent which is not displayed on the board, but which constantly spawns asteroids.
- play_turn(game)[source]#
Adds 1 to the game score and then uses
should_spawn_asteroid()
to decide whether to spawn an asteroid. Whenshould_spawn_asteroid()
comes backTrue
, creates a new instance ofAsteroid
at a random position along the top of the screen and adds the asteroid to the game.
- should_spawn_asteroid(turn_number)[source]#
Decides whether to spawn an asteroid. Uses a simple but effective algorithm to make the game get progressively more difficult: choose a random number and return
True
if the number is less than the current turn number. At the beginning of the game, few asteroids will be spawned. As the turn number climbs toward 1000, asteroids are spawned almost every turn.- Parameters:
turn_number (int) – The current turn in the game.