EXTEND 3: Now that we are creating a random speed every time we create food, let's use that speed!
In GRAPHICS, from add Set y Speed indented inside the interval event. Change sprite to food . This assigns the .set_y_speed() command to the food we just created! In .set_y_speed() , change 5 to rand_drop . The food now drops at a randomly picked speed.
Run your game. Now the food drops at different speeds each time!
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background("underwater")
sprite = codesters.Sprite("fish")
sprite.go_to(0, -220)
stage.disable_floor()
score = 0
#my_display = codesters.Display(my_var, x, y)
score_board = codesters.Display(score, -200, 150)
def right_key():
sprite.move_right(30)
# add other actions...
stage.event_key("right", right_key)
def left_key():
sprite.move_left(30)
# add other actions...
stage.event_key("left", left_key)
def interval():
x = random.randint(-250, 250)
# sprite = codesters.Circle(x, y, diameter, "color")
food = codesters.Circle(x, 260, 10, "sienna")
rand_drop = random.randint(-15, -5)
# add any other actions...
stage.event_interval(interval, 1)
def collision(sprite, hit_sprite):
global score
stage.remove_sprite(hit_sprite)
score += 1
score_board.update(score)
# add any other actions...
sprite.event_collision(collision)
t = codesters.Teacher()
drops = t.get_parameters_for_function('set_y_speed')
drops_text = t.find_text('set_y_speed')
try:
tval1 = drops[0][0]
except:
tval1 = "DNE"
try:
tval2 = t.get_indent_at_line(drops_text[0][0])
except:
tval2 = "DNE"
t1 = TestObjective()
t1.add_success(tval1 == "rand_drop" and tval2 == 4, "Great job!")
t1.add_failure(tval1 == "DNE", "Did you add a Set y Speed command inside your interval event?")
t1.add_failure(tval1 != 'rand_drop', "Did you change the speed in .set_y_speed()?")
t1.add_failure(tval2 < 4, "Make sure you indent .set_y_speed() inside your interval event.")
t1.add_failure(tval2 > 4, "Make sure you indent .set_y_speed() only 4 spaces inside your interval event.")
tester = TestManager()
tester.add_test_list([t1])
tester.run_tests()
tester.display_first_feedback()
Run Code
Activity Submitted!
Submit Work
Stop Running Code
Show Chart
Show Console
Reset Code Editor
Codesters How To (opens in a new tab)