Coding in Python
PyLander Game
This game simulates a rocket landing – hopefully without crashing.
Make a copy of the PyLander.py file and open it in IDLE
Run the program
Press Space to fire the rocket and arrow keys to rotate it
Try to land gently!
[pic]
The game runs once and then stops – the window stays open and won’t close. To play again, go back to the IDLE Editor and run it again. As with real life, it’s tricky to land safely – and very easy to crash!
Have a look through the code. Can you guess what any of it might do? Discuss your guesses with your partner. Don’t worry if part of it makes no sense, move on to a bit that does! There are some comments in the code (they begin with #) – Python will ignore these but they may help you understand the code.
You have two tasks – you can do whichever of these you like.
1 Task 1: Fix the game so it ends properly
The game would be better if the window closes when you click the Close icon. Most programs are an endless loop of Input ⋄ Process ⋄ Output and this game is no exception. Look for a main loop. The game responds to keys being pressed (going Down and Up again) and the Window Close button being clicked (a QUIT event). To make the game window close you need to add the following code:
|pygame.quit() |
You will need to add spaces to line this up with the code that’s already there. Have a go or if you’ve got an idea but not sure, ask for assistance. If you make a mess of things, make a new copy and start again!
2 Task 2: Change the rules to make the game easier or more difficult
Try changing the code and running the game to see if you can make it easier to play, harder to play, or even to make the rocket land safely all by itself.
Lots of games involve maths and physics to make them more realistic – so they behave like the real world – except that you can change the rules. This game is mostly maths and physics with a bit of responding to key presses and drawing shapes.
Programming often involves changing existing code to do new things. For each of these ideas, decide what you would like to change and why, predict what you think will happen when you make the change, then play the game and see if you were correct.
The start of the program sets up lots of variables including:
The mass of the rocket
The amount of fuel
How much gravity there is
The starting position and speed of the rocket
Whether the rocket is rotating or thrusters are firing
Run the game without pressing any keys and watch how the rocket falls.
Now, let’s try changing the rules…
Adjust the initial speed (how fast it is going and in what direction) of the rocket
so that it lands on the landing pad by itself
Or make the rocket start off heading upwards not downwards
Or make the rocket start from the top right
Make the game more difficult to play:
Adjust the mass of the rocket – does this make any difference?
Then adjust the gravity – does this make any difference?
Reduce the amount of fuel – how little do you actually need?
Personalise the game:
Change the messages it puts on screen to use your own words.
If you completed Task 1 you could try swapping the direction the rocket rotates when the arrow keys are pressed.
Or even change the keys used to control the rocket.
Or allow two sets of keys so a pilot or a co-pilot can control the rocket.
3 Resources
(download PyLander.py and lunarlander.png)
(download and install pygame using pip)
4 License
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
To view a copy of this license, visit
5 Acknowledgements
Ian Miller, National STEM Centre
Maths Games (Python 3.4)
Start IDLE – this will provide a Python Shell. Every time you type Python instructions into this window, Python will follow your instructions and show you the result.
[pic]
1 Input and Output
It’s traditional to start with Hello World! Type the following – take care to get the punctuation right:
|print("Hello World!") |
Try changing this to output your own choice of message.
Next, let’s make this a bit more personal – first by remembering a name in a variable:
|name = "Fred" |
|print("Hello", name) |
Then by asking your name first:
|name = input("What's your name: ") |
|print("Hello", name) |
Programs often consist of:
Input
Processing – do something with the input
Output
The input and output may be numbers as well as words.
Processing may involve maths or making decisions.
2 Maths
Python can do sums. For example:
|print(1+2+3+4+5) |
|print(8*6) |
|print(10-5+6) |
Try getting Python to do a sum you can’t do.
Python can also compare numbers. For example:
|print("Is 2 more than 1?", 2 > 1) |
|print("Is 8 less than 3?", 8 < 3) |
Try changing the values and predict what the outcome will be. Then see if you’re right.
Python can see if two numbers are the same – let’s say your lucky number is 8:
|lucky = 8 |
|print("Is 13 my lucky number?", 13 == lucky) |
|print("Is 8 my lucky number?", 8 == lucky) |
Take care to use one = sign when remembering the number and two == signs when checking it.
Python can also think of random numbers. This is a little bit trickier to type.
|import random |
|random.randint(1, 6) |
This is like rolling a die. Type the second line again to roll again. To save time you can click on the line you typed before and press Enter to get it typed in again for you.
How do you think you would change the number of sides on your dice? Have a try.
3 Loops
Programs often do the same thing repeatedly; in fact sometimes they keep going forever until you make them stop. Now try this:
|while True: |
|random.randint(1, 6) |
Press Ctrl+C to make it stop!
4 Decisions
We are now going to try making decisions. At this point you might want to, rather than have Python run each line as you type it, write a number of lines and have Python run them all at once.
To do this go to File menu > New File and start typing code into the Editor window.
When you want Python to run your code, press F5.
The first time you run your code you will be asked to save the file – save it on your Desktop and name it however you wish. Your output will appear in the Python Shell window. You can continue to click and type code into the Python Shell but it could start to get a bit repetitive!
Leaving the maths for a moment, let’s make a (very short) adventure game.
Take care to type four spaces before each indented line.
|print("You have two doors in front of you – which will you go through?") |
| |
| |
|choice = int(input("Type a number: ")) |
| |
| |
|if 1 == choice: |
|print("Door 1! A brave choice. Sadly you are devoured by a tiger.") |
|elif 2 == choice: |
|print("You choose the second door. Unfortunately a bear greets you.") |
|else: |
|print("A rule breaker eh! Best not to open doors at a zoo.") |
If you want you can try changing the words, adding extra doors, or changing which one is the safe choice. You could even offer a further choice once you go through the first door. Ask for assistance if you want to try this.
Going back to our eternal dice from the earlier, let’s combine a few of the things we’ve learnt to keep rolling only until you get a six.
|import random |
|while True: |
|roll = random.randint(1, 6) |
|print(roll) |
|if 6 == roll: |
|break |
The last line tells Python to stop looping if you roll a six. Try this a few times and see how long it takes to get a six. You could try changing this to look for a different number, or look for a number greater than 3 or less than 4 or something else. See what effect this has.
If you’re feeling particularly confident, think about how to make the program run until you get two sixes, or two in a row of any number.
5 Finally
Finally, you can put all you’ve done earlier together to make a “guess the number” game.
The brief for this game is it should:
Run forever unless stopped (or you win)
Think of a random number
Ask you to guess the number
If the guess is correct, output congratulations and stop looping
If the guess is wrong, go round again
Optionally, show if the guess is too high or too low
Tips:
Discuss in your group what code you might use for each step in this program
Try writing out a plan for your code on paper first
Type each bit in turn and see if it works before you type the next bit
6
7
8
9
10
11
12
13
14
15
16
License
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
To view a copy of this license, visit
17 Acknowledgements
Images from Microsoft Office
Maths Games (Python 2.7)
Start IDLE – this will provide a Python Shell. Every time you type Python instructions into this window, Python will follow your instructions and show you the result.
[pic]
1 Input and Output
It’s traditional to start with Hello World! Type the following – take care to get the punctuation right:
|print "Hello World!" |
Try changing this to output your own choice of message.
Next, let’s make this a bit more personal – first by remembering a name in a variable:
|name = "Fred" |
|print "Hello", name |
Then by asking your name first:
|name = raw_input("What's your name: ") |
|print "Hello", name |
Programs often consist of:
Input
Processing – do something with the input
Output
The input and output may be numbers as well as words.
Processing may involve maths or making decisions.
2 Maths
Python can do sums. For example:
|print 1+2+3+4+5 |
|print 8*6 |
|print 10-5+6 |
Try getting Python to do a sum you can’t do.
Python can also compare numbers. For example:
|print "Is 2 more than 1?", 2 > 1 |
|print "Is 8 less than 3?", 8 < 3 |
Try changing the values and predict what the outcome will be. Then see if you’re right.
Python can see if two numbers are the same – let’s say your lucky number is 8:
|lucky = 8 |
|print "Is 13 my lucky number?", 13 == lucky |
|print "Is 8 my lucky number?", 8 == lucky |
Take care to use one = sign when remembering the number and two == signs when checking it.
Python can also think of random numbers. This is a little bit trickier to type.
|import random |
|random.randint(1, 6) |
This is like rolling a die. Type the second line again to roll again. To save time you can click on the line you typed before and press Enter to get it typed in again for you.
How do you think you would change the number of sides on your dice? Have a try.
3 Loops
Programs often do the same thing repeatedly; in fact sometimes they keep going forever until you make them stop. Now try this:
|while True: |
|random.randint(1, 6) |
Press Ctrl+C to make it stop!
4 Decisions
We are now going to try making decisions. At this point you might want to, rather than have Python run each line as you type it, write a number of lines and have Python run them all at once.
To do this go to File menu > New File and start typing code into the Editor window.
When you want Python to run your code, press F5.
The first time you run your code you will be asked to save the file – save it on your Desktop and name it however you wish. Your output will appear in the Python Shell window. You can continue to click and type code into the Python Shell but it could start to get a bit repetitive!
Leaving the maths for a moment, let’s make a (very short) adventure game.
Take care to type four spaces before each indented line.
|print "You have two doors in front of you – which will you go through?" |
| |
| |
|choice = int(raw_input("Type a number: ")) |
| |
| |
|if 1 == choice: |
|print "Door 1! A brave choice. Sadly you are devoured by a tiger." |
|elif 2 == choice: |
|print "You choose the second door. Unfortunately a bear greets you." |
|else: |
|print "A rule breaker eh! Best not to open doors at a zoo." |
If you want you can try changing the words, adding extra doors, or changing which one is the safe choice. You could even offer a further choice once you go through the first door. Ask for assistance if you want to try this.
Going back to our eternal dice from the earlier, let’s combine a few of the things we’ve learnt to keep rolling only until you get a six.
|import random |
|while True: |
|roll = random.randint(1, 6) |
|print roll |
|if 6 == roll: |
|break |
The last line tells Python to stop looping if you roll a six. Try this a few times and see how long it takes to get a six. You could try changing this to look for a different number, or look for a number greater than 3 or less than 4 or something else. See what effect this has.
If you’re feeling particularly confident, think about how to make the program run until you get two sixes, or two in a row of any number.
5 Finally
Finally, you can put all you’ve done earlier together to make a “guess the number” game.
The brief for this game is it should:
Run forever unless stopped (or you win)
Think of a random number
Ask you to guess the number
If the guess is correct, output congratulations and stop looping
If the guess is wrong, go round again
Optionally, show if the guess is too high or too low
Tips:
Discuss in your group what code you might use for each step in this program
Try writing out a plan for your code on paper first
Type each bit in turn and see if it works before you type the next bit
6
7
8
9
10
11
12
13
14
15
16
License
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
To view a copy of this license, visit
17 Acknowledgements
Images from Microsoft Office
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.