Getting Started With Games
The resources on this page were originally created by Dr. Aaron Bradley of Summit Middle School. I've done some reformatting to add clarity but that is about it. Enjoy!
|
During this lesson, you will learn several Python tools:
- How to create a program
- How to import a library
- Borrowing functions from the library
- Move and object based on user input.
- Adding comments using the # symbol
- Animate an object (i.e. move the object without user input)
Controlling a Sprite with the Keyboard
A "sprite" is an on-screen character, in our case, a red circle. The goal is to make keys control the red circle's location.
1. Create a new Python module called SimpleGame.py. Save it in your My PYTHON Programs directory. (Delete the default code.)
2. Import the "simple graphics" library:
from sgfx import *
3. Create a window and show it (this and subsequent steps modify the code; for example, here, just add the final two lines):
from sgfx import *
w = Window()
w.run()
Run the program. What happens? Click the "X" in the upper-right corner to end the program.
4. Create a circle in the window:
from sgfx import *
w = Window()
dot = w.circle(50, 100, 10)
w.run()
Run the program.
What happens?
What do you think the three numbers 50, 100, and 10 specify?
About how big is the window?
5. Create a function that moves the circle left, and attached that function to a key:
from sgfx import *
w = Window()
dot = w.circle(50, 100, 10)
def left():
w.move(dot, -10, 0)
w.on('j', left)
w.run()
Run the program.
Based on the code, which key on the keyboard should you press to see something happen?
What happens?
Explain w.move(dot, -10, 0).
What do you think w.on('j', left) does?
6. It's always good form to add comments inside your program letting other people know what you're doing. Using the # symbol, add short explanations for each piece of code.
For example:
#creates a dot with center at (50, 100) with a radius of 10 pixels
dot = w.circle(50, 100, 10)
7. Play with the numbers that appear in the program.
For example, change -10 to -20 and see what changes.
Change 'j' to another letter. What changes?
8. Write three additional functions like left: right, up, and down. Make them do what their names suggest.
Then attach each function to a separate letter (for example, l, i, and k, respectively).
Run the program.
Can you move the dot left, right, up, and down?
9. Write a function to move the dot diagonally up and to the left. Attach that function to the u key.
Write additional functions to move the dot diagonally in the three other directions, and attach them to appropriate keys.
A "sprite" is an on-screen character, in our case, a red circle. The goal is to make keys control the red circle's location.
1. Create a new Python module called SimpleGame.py. Save it in your My PYTHON Programs directory. (Delete the default code.)
2. Import the "simple graphics" library:
from sgfx import *
3. Create a window and show it (this and subsequent steps modify the code; for example, here, just add the final two lines):
from sgfx import *
w = Window()
w.run()
Run the program. What happens? Click the "X" in the upper-right corner to end the program.
4. Create a circle in the window:
from sgfx import *
w = Window()
dot = w.circle(50, 100, 10)
w.run()
Run the program.
What happens?
What do you think the three numbers 50, 100, and 10 specify?
About how big is the window?
5. Create a function that moves the circle left, and attached that function to a key:
from sgfx import *
w = Window()
dot = w.circle(50, 100, 10)
def left():
w.move(dot, -10, 0)
w.on('j', left)
w.run()
Run the program.
Based on the code, which key on the keyboard should you press to see something happen?
What happens?
Explain w.move(dot, -10, 0).
What do you think w.on('j', left) does?
6. It's always good form to add comments inside your program letting other people know what you're doing. Using the # symbol, add short explanations for each piece of code.
For example:
#creates a dot with center at (50, 100) with a radius of 10 pixels
dot = w.circle(50, 100, 10)
7. Play with the numbers that appear in the program.
For example, change -10 to -20 and see what changes.
Change 'j' to another letter. What changes?
8. Write three additional functions like left: right, up, and down. Make them do what their names suggest.
Then attach each function to a separate letter (for example, l, i, and k, respectively).
Run the program.
Can you move the dot left, right, up, and down?
9. Write a function to move the dot diagonally up and to the left. Attach that function to the u key.
Write additional functions to move the dot diagonally in the three other directions, and attach them to appropriate keys.
Animation
1. Create a blue rectangle, and make it rise:
from sgfx import *
w = Window()
block = w.rectangle(50, 100, 100, 50, color='blue')
def rise():
w.move(block, 0, 1)
w.animate(rise)
w.run()
Run the program.
Explain the program's behavior line-by-line.
What does w.animate(rise) do?
2. Modify the code so that the block starts at the top and centered and falls.
3. Modify the code so that the block moves left.
4. Modify the code so that the block moves both left and downward.
5. Modify the code so that the block only falls.
6. Write two functions, left and right, that move the block left and right. Attach each to a key. Run the program, and use the assigned keys to make the block move horizontally as it falls.
7. Add a yellow circle [remember we added circles at the top of the page].
(Notice how color='blue' set the color of the block.)
Animate it to move however you want.
8. Implement additional functions to move the yellow circle. Attach the functions to various keys. Run the program, and control the block and the circle simultaneously (at the same time) with the assigned keys.
1. Create a blue rectangle, and make it rise:
from sgfx import *
w = Window()
block = w.rectangle(50, 100, 100, 50, color='blue')
def rise():
w.move(block, 0, 1)
w.animate(rise)
w.run()
Run the program.
Explain the program's behavior line-by-line.
What does w.animate(rise) do?
2. Modify the code so that the block starts at the top and centered and falls.
3. Modify the code so that the block moves left.
4. Modify the code so that the block moves both left and downward.
5. Modify the code so that the block only falls.
6. Write two functions, left and right, that move the block left and right. Attach each to a key. Run the program, and use the assigned keys to make the block move horizontally as it falls.
7. Add a yellow circle [remember we added circles at the top of the page].
(Notice how color='blue' set the color of the block.)
Animate it to move however you want.
8. Implement additional functions to move the yellow circle. Attach the functions to various keys. Run the program, and control the block and the circle simultaneously (at the same time) with the assigned keys.