Snakes Pygame: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
imported>Jesstess
Line 33: Line 33:
* What is in <code>objects/snake.py</code>? What actions can a snake take?
* What is in <code>objects/snake.py</code>? What actions can a snake take?
* What is in <code>objects/food.py</code>? What determines where food shows up on the screen?
* What is in <code>objects/food.py</code>? What determines where food shows up on the screen?

Run the game again. When the good snake runs into the bad snake, you die. Where is the code that checks for collisions between snakes? When a snake eats a piece of food, its tail grows. Where is the code that grows the snake?


=== 2. Give snakes the ability to move in all directions ===
=== 2. Give snakes the ability to move in all directions ===

Revision as of 01:55, 25 May 2013

Project

Implement parts of a Snakes clone using the Pygame graphical game development library.

Goals

  • practice using the Pygame library
  • practice using an event loop to make action happen

Project setup

1. Install the project dependencies

Install Pygame. On Linux, you can use your package manager to install the python-pygame package. On Windows and OSX, you can download the appropriate binary from http://www.pygame.org/download.shtml.

2. Download and un-archive the Snakes project skeleton code

Un-archiving will produce a Snakes folder containing several Python files.

3. Test your setup

Run the snakes.py script in your Snakes directory. A window with a Snakes game should pop up, including square bits of food, the snake you can control, and the enemy snake. Try using the Up and Down keys to move your snake.

Project steps

1. Familiarize yourself with codebase

  • What is in snakes.py? What parts of the game is pygame responsible for?
  • What is in objects/snake.py? What actions can a snake take?
  • What is in objects/food.py? What determines where food shows up on the screen?

Run the game again. When the good snake runs into the bad snake, you die. Where is the code that checks for collisions between snakes? When a snake eats a piece of food, its tail grows. Where is the code that grows the snake?

2. Give snakes the ability to move in all directions

So far, the snake can only move up or down. Fix this by completing the "Handle the remaining movement keys" TODO in the move method of the Snake class. Test your changes by re-running the game and moving your snake left and right.

3. Set a key repeat speed

So far, you have to press an arrow key for each movement of the snake. Wouldn't it be convenient to be able to just hold down an arrow key to keep moving our snake?

Fix this by completing the "Set the key repeat speed" TODO in the initialize_screen function in snake.py. See http://www.pygame.org/docs/ref/key.html#pygame.key.set_repeat for details on the Pygame function that controls this behavior.

Test your changes by re-running the game and holding down the arrow key while moving your snake around.

4. Handle quit and restart key presses

I want to keep playing! So far, there's no way to quit or restart the game (besides closing the window).

Fix this by completing the "3 key presses" TODO in the restart function in snakes.py.