Getting on with Graphics
We've done some reviewing of PyGame in the last few weeks (see https://inventwithpython.com/pygame/chapter2.html). Now we're going to write our own graphical game (the idea for this game was taken from here: https://inventwithpython.com/invent4thed/chapter19.html).
Purpose: Learn how to handle mouse and keyboard events to allow a user to interact with our games and to learn to handle collision events.
To start get the outline setup with initializing pygame, drawing a window with a title, and setting up our main game loop that handles the quit event so we can exit out of our game. It probably makes sense to have the window have a white background as well.
Our game will crate a larger rectangle that is controlled by the keyboard. When we start the game we will draw lots of food rectangles that are a different color from the player rectangle and will be much smaller. When the player controlled rectangle runs into a food rectangle it will "eat" it and the food rectangle will disapear from the screen. If the player clicks on the screen we will draw a new food rectangle there. We will also draw random food onto the screen every couple seconds.
Here is a list of possible events that PyGame will help you handle: http://www.pygame.org/docs/ref/event.html
Here is a list of keys that you can check for in those events: https://www.pygame.org/docs/ref/key.html
Look here and figure out how to test if two rectangles overlap: https://www.pygame.org/docs/ref/rect.html
Remember to break your program into small, distinct chunks and test each one. For example you might want to start with being able to draw a rectangle. Then try to get the rectangle to move. Then try to get it to change dirtections with the arrow keys.
You'll need some help as you need to keep a list of food rectangles to draw and we haven't gone over lists yet. You'll also want to randomly generate the initial food placement, we'll look at the random module for that (just remember you want a random point inside your canvas so an x and a y that are smaller than those dimensions).
Purpose: Learn how to handle mouse and keyboard events to allow a user to interact with our games and to learn to handle collision events.
To start get the outline setup with initializing pygame, drawing a window with a title, and setting up our main game loop that handles the quit event so we can exit out of our game. It probably makes sense to have the window have a white background as well.
Our game will crate a larger rectangle that is controlled by the keyboard. When we start the game we will draw lots of food rectangles that are a different color from the player rectangle and will be much smaller. When the player controlled rectangle runs into a food rectangle it will "eat" it and the food rectangle will disapear from the screen. If the player clicks on the screen we will draw a new food rectangle there. We will also draw random food onto the screen every couple seconds.
Here is a list of possible events that PyGame will help you handle: http://www.pygame.org/docs/ref/event.html
Here is a list of keys that you can check for in those events: https://www.pygame.org/docs/ref/key.html
Look here and figure out how to test if two rectangles overlap: https://www.pygame.org/docs/ref/rect.html
Remember to break your program into small, distinct chunks and test each one. For example you might want to start with being able to draw a rectangle. Then try to get the rectangle to move. Then try to get it to change dirtections with the arrow keys.
You'll need some help as you need to keep a list of food rectangles to draw and we haven't gone over lists yet. You'll also want to randomly generate the initial food placement, we'll look at the random module for that (just remember you want a random point inside your canvas so an x and a y that are smaller than those dimensions).
Comments
Post a Comment