Snakes Pygame: Difference between revisions

imported>Jesstess
 
(8 intermediate revisions by 2 users not shown)
Line 1:
[[File:gridsnakes.png|right|300px]]
 
== Project ==
Line 7:
== Goals ==
 
* practicelearn about using graphics and sounds to create interactive games using the Pygame library
* practice usingreading anand eventunderstanding loopsomeone toelse's make action happencode
 
== Project setup ==
Line 40:
So far, the snake can only move up or down. Fix this by completing the "Handle the remaining movement keys" TODO in the <code>move</code> method of the <code>Snake</code> class. Test your changes by re-running the game and moving your snake left and right.
 
=== 3. SetGive athe keygame a repeatbetter speedbackground ===
 
The game has a pretty boring solid color background. Spice it up by adding a background image. We've provided a sample background image called <code>leaves.jpg</code> in the Snakes directory, but create and add your own if you want!
 
Use [http://www.pygame.org/docs/ref/image.html#pygame.image.load pygame.image.load] to load the background. You'll need to re-display the background on every tick of the game, just like we currently do with the solid color using <code>game_surface.fill(BACKGROUND_COLOR)</code>.
 
Test your changes by re-running the game and observing that your custom background is used.
 
=== 4. 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?
Line 48 ⟶ 56:
Test your changes by re-running the game and holding down the arrow key while moving your snake around.
 
=== 45. HandleMake quitthe andfood restartlook keymore pressesinteresting ===
 
Snake food is just a solid-colored square. Spice it up by using an image instead. We've provided a sample food image called <code>cherry.png</code> in the Snakes directory, but create and add your own if you want!
 
Currently, in the <code>Food</code> class <code>self.surface</code> is filled with a random color. Use [http://www.pygame.org/docs/ref/image.html#pygame.image.load pygame.image.load] to load and use the food image instead.
 
Test your changes by re-running the game and observing that your custom food image is used.
 
=== 6. Handle quit and restart key presses ===
 
I want to keep playing! So far, there's no way to quit (besides closing the window) or restart the game.
Line 55 ⟶ 71:
 
Test your changes by re-running the game, running into the bad snake to end the game, and making sure you can restart.
 
 
==Bonus exercises==
 
=== 1. Play a sound when you eat food ===
 
We've included <code>ding.ogg</code> as a sample sound, but find and add your own if you want!
 
Test your changes by re-running the game with the sound on and observing that a noise is played when the snake eats pieces of food.
 
=== 2. Give yourself multiple lives ===
 
Typically in games you have multiple lives available. Give your snake 3 lives to start with, lose a life each time you run into yourself or the bad snake, and display how many lives remain in the upper left corner.
 
You may find it convenient to create a new file with a new class called <code>Lives</code> that keeps track of this game state and the work of updating the display.
 
 
===Congratulations!===
 
You've read, modified, and improved a game using Pygame that uses images and sound and game concepts like an event loop and managing keypresses. Keep practicing!
 
[[File:Fireworks.png|150px]]
[[File:Balloons.png|150px]]
Anonymous user