rocks.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 7. Then click
Submit Assignment, click Choose file, and
find rocks.py
.
Run Idle, and then open the file rocks.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.
Then, complete these steps:
initialize_rocks()
, with a seven-number
list: [x,y, vx,vy, angle, rotation_rate, size]. Change
that line to modify the rock's vy:
rock_motion = [0,0, 30,20, 0, 0, 20]The rock should now move up, and to the right.
rock_motion = [0,0, 30,20, 0, 100, 20]
random_motion
function. It
generates some random numbers for the rock's motion:
x = random.randrange(window_left, window_right) y = random.randrange(window_bottom, window_top) vx = random.random() * 200 - 100 vy = random.random() * 200 - 100 rotation_rate = random.random() * 200 - 100use it by replacing the
rock_motion =
line
with this:
rock_motion = random_motion()Now, run the program again. If you don't see anything, the rock might have moved off-screen; run it again.
random_motion
, replace the return
line:
return [x, y, vx, vy, 0, rotation_rate, 100]
draw_rock
, change
the forward()
amounts. Replace 20
with size
.
move_rocks(t)
add this line at the end:
wrap_around(rock_motion)
wrap_around(motion)
. Enter this
code in that function:
x = motion[0] y = motion[1] if x > window_right: motion[0] = window_left if x < window_left: motion[0] = window_rightAdd two more if-statements, to handle the top and bottom. Note: if the rock seems to get stuck on one side, make sure you're doing the > and < comparisons correctly.
rock2_motion = []
initialize_rocks()
, you will need
to declare rock2_motion
global, and
add a line to give it a random motion.
move_rocks
, you'll have to add
two lines to move rock 2, and to wrap it around.
draw_rocks
, you'll have to add
another line to draw rock 2.
random_motion
,
call random.randrange(20,120)
, and put its
result as the last value in the returned list (it's
currently 20).
move_objects(T)
, add this code at the
bottom:
x1 = rock_motion[0] y1 = rock_motion[1] rad1 = rock_motion[6] x2 = rock2_motion[0] y2 = rock2_motion[1] rad2 = rock2_motion[6] if collided(x1,y1,rad1, x2,y2,rad2): resolve_collision(rock_motion, rock2_motion, T)
rocks.py
FIRST, in case this bonus effort doesn't work.
Instead of having 2 rock motion records, create a list with 5 of them. This will have several consequences in the code:
rock_motion
and rock2_motion
, there will be
a rock_motions
list.
initialize_rocks
.
move_rocks
.
draw_rocks
.
move_objects
, to check every rock
against every other rock, for collisions, something
like this:
for rock1 in rock_motions: for rock2 in rock_motions: if rock1 != rock2: x1 = rock1[0] x2 = ... ... rad2 = ... if collided(...): resolve_collision(...)