The Curse of Python (continues…)

So finally I decided the game that I would be replicating in the text mode (In case you have no idea what this post is about, read my initial post on the same topic). I wanted to pick something that had “not-been-done-before” (Read : NOT Snake or Tetris). So I chose to work on the “T-Rex Runner” game that appears on Google Chrome when your system fails to connect.

The “T-Rex” game is more of a reminder that without the internet it is like living in the dinosaur age (True That!). The game has a very simple setup : “Make the T-Rex jump over the Cacti. If it hits the Cacti , GAME OVER!! “

The main components of the game are:

  1. The Scene that includes the ground, the “evil” cacti and the clouds
  2. The Actor , TRex, and
  3. The Game Controller

The game begins at the Start state and moves to the Init state when it sets default values for the class variables. Once the initialization is complete, the state machine enters the Run state where the scene and actors are drawn. This state accepts keyboard inputs to make jumps (SPACE-BAR key) or to exit the game (ESC key). The state machine in this state also monitors the collision conditions, where the TRex comes into contact with the Cactus. The state machine moves to the Score-and-End-game state as soon as the collision condition is met. At this stage, the player has the option of restarting the game or exiting the game. The game is restarted at the expiry of the 5 second timer. The user can exit the game using the ESC key or restart the game using the ENTER key.

trex_flow_chart
Figure 1: Flowchart

The Code

The python code is split into 3 files:

  • Scene.py : Contains 3 classes namely Cloud class, Ground class and Cactus Class. Each class contains an update method and a draw method. The update() method will decide the co-ordinates and the image to draw. The draw() method will simply draw the images onto the screen using the addstr() method.
  • Actor.py: Contains 1 class , Trex Class that will draw the “Trex” character onto the screen. This class also contains a draw() and an update() method that performs the same operation as the methods in the above mentioned classes. In future, a Pterodactyl Class will also be added to draw the “Pterodactyl” character on the screen
  • Trex-Game.py : This file is the main controller and binds the classes together. It contains a Main class that maintains the game state and executes the flow as per Figure 1. The Main class has the following methods:
    • The start() method draws the scene, the actors and animates them. It also checks for the collision condition, when met enables the isCollision flag using the check_collision() method.
    • The get_score(), set_score() and level_up() methods are used to update the score and levels of the game.
    • The reset_game() method re-initializes the game variables.
    • Helper methods like draw_score() and end_game() are used to display the score and display the end game graphics respectively.

I also implemented a logging interface that recorded the game states and values for certain variables that was useful for debugging while developing the game.However, I decided to remove it as it was affecting the readability of the code. I used the logging library to implement the logging as it was fast and easy to implement.

Open Issues & Final Comments

This game project was supposed to be a quick learning exercise for the ncurses library in Python. I have tried my best to code the initial versions of the actual TRex-Runner game. However, there are some open issues that need to be addressed. The list of the issues and the code are available on my github repo under the name TRexGame. I am open to any useful enhancements to the game play. Also if some Python-Guru can suggest optimization of the class structure, I will be happy to incorporate the changes.

Code: https://github.com/sukhoi/TRexGame

Trex-screenshot.png

 

-H-

Advertisements

3 thoughts on “The Curse of Python (continues…)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s