asteroids.py
.
I've prepared a starter file for you,
available here on the course webpage.
Late penalties: 1 day late: -5%, 2 days: -10%, -3 days: -20%.
To turn your work in, go
to
mycourses.unh.edu
,
find CS414, open the syllabus, and click on Lab 8. Then click
Submit Assignment, click Choose file, and
find asteroids.py
.
Run Idle, and then open the file asteroids.py
.
Choose "Run -> Run Module" to see the program in action.
You may have to move your code windows to the right, to see
the turtle window.
Also, this lab now uses the keyboard. When you run the
program, remember to click INSIDE the turtle
window. Otherwise your program won't respond to
keyboard events.
Then, complete these steps:
random_motion()
: all the rocks have
size 20 (the last value in the returned list). Change that
value to be a random number between 20 and 60.
make_rocks()
: there is only one rock
being appended to the list rocks
. Replace
the last line with a for
-loop, which repeats 5
times, and appends a random motion each time.
move_bullets()
: it currently does
nothing. Make it call move_bullet()
for each
bullet (compare it to move_rocks()
, which does
something similar).
draw_bullets()
: it currently does
nothing. Make it call draw_bullet()
for each
bullet (compare it to draw_rocks()
, which does
something similar).
Look at main()
: the line that reads
turtle.onkey(fire_bullet, ' ')"registers a callback". It tells the
turtle
library to call fire_bullet()
each time the
user presses the space bar.
Run the program, click in the window, and press the space bar repeatedly. Bullets fly out from the center.
(There's a ship in the middle of the screen, but it is not being drawn).
There are too many bullets; the game will be too easy. Make two changes:
fire_bullet
, so that you don't add a
new bullet if there are already 10 or more entries in
the list bullets
.
remove_offscreen_bullets()
.
Create a for
-loop that visits every
bullet. Inside it, check if the bullet is off-screen
(see the function wrap_around()
, which
does a similar test). If bullet[X]
is
outside the left side, or the right side,
remove bullet
from the list, thus:
bullets.remove(bullet)Also check
bullet[Y]
.
Implement check_rock_bullet_hit()
: if some
bullet hits some rock, remove the rock. To do this, you
will need two for
-loops: a loop that visits
the rocks
list, and inside it a loop
that visits the bullets
list.
Call collided(rock, bullet)
, and, if it
is True
, remove the rock from the list.
Let the user turn the ship. Follow these steps:
turn_ship_left()
and
turn_ship_right()
. In the first one, add 5
to the ship's angle, and in the second one, subtract
5.
turtle
library: change the main()
function, by adding two lines:
turtle.onkey(turn_ship_left, 'Left') turtle.onkey(turn_ship_right, 'Right')These tell the library to call those functions if the user presses the Left or Right arrow keys.
If there are no more rocks, restart the game: add some
lines in update_scene()
so that, if the
rocks
list is empty (it has length
0), make_rocks()
is called.