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.

handle_keystroke(keystroke, game)[source]#

When the left or arrow key is pressed, it moves left or right. If the ship’s new position is empty, it moves to that position. If the new position is occupied (by an asteroid!) the game ends.

explode()[source]#

Sets the ship’s character to * and its color to red.

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. When should_spawn_asteroid() comes back True, creates a new instance of Asteroid 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.