Skip to content

Repository files navigation

The Game Name: Snake

Game Goal:
To eat as many fruits as possible to elongate the snake and get a higher score. Hitting the wall, hitting any stone and eating itself will cause to snake to die.

Game Mods:
1.Speed mod: The snake will move faster each time it eats a fruit.
2.Stone mod: Two stones will be randomly generated each time the snake eats a fruit.

Game symbol:
'#': wall or stone
'@': the snake's head
'*': the snake's body
'o': fruit

Game control:

  1. In the main menu, press '1' or '2' to select either speed mod or stone mod.
  2. In the game, use "wasd" to control the snake to move upward, toward left, downward and toward right.
  3. Use 'q' to quit the current game and return to the main menu. Use 'r' to restart the current game You can do this either in the middle of a game or when the game is over.

Code description:

  1. Class Cord is used to represent the position(which row which column) of a single character on the screen
  2. Class Snake keeps track of the snake and provides useful functions.
  3. Class SnakeState inherited from sm.SM keeps track of the direction of the snake(up/down/left/right). It is used to filter out the invalid keyboard input and handle the cases in which the input direction is opposite to the snake's current direction
  4. Class ModStates inherited from sm.SM determines whether the program is under main menu, speed mod or stone mod. It also outputs functions to handle the keyboard input under different states to make the main function much simpler
  5. Main function will run the function outputted by mod_state. This function will return the user's choice. (return to main menu/ play speed mod/ restart the current game....) The return value will then be the input of mod_state and it will again generate another function. This process forms a loop so that the program will keep running until the user closes it.

Video Link

Code dependency: Unix-like operating system. (Windows do not have the module "curses" even though it is in Python Standard Lib)

How to run the game:
Open terminal under full screen(important!)
cd to the directory which snake.py lies in.
type "python snake.py" + Enter

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages