STEP 11: Now let's set up another if statement for cars going the other direction.

  • From LOGIC and the toolkit, drag out if or so that it is indented one time in the interval event.
  • Change both choice variables to my_var. Change "yes" to -50 and change "y" to 150.
  • In GRAPHICS and , drag Car 2 inside the if statement. Make sure it is indented two times!
  • Now let's make it so that these cars drive to the right. Click Run, Submit, and Next to move on.

To navigate the page using the TAB key, first press ESC to exit the code editor.

# sprite = codesters.Rectangle(x, y, width, height, "color") start = codesters.Rectangle(0, -250, 500, 50, "red") # sprite = codesters.Rectangle(x, y, width, height, "color") finish = codesters.Rectangle(0, 250, 500, 50, "green") my_list = [-150, -50, 50, 150] for value in my_list: pass # delete after adding indented code # add code here # sprite = codesters.Rectangle(x, y, width, height, "color") sprite = codesters.Rectangle(0, value, 500, 50, "gray") # sprite = codesters.Sprite("image", x, y) sprite = codesters.Sprite("turtle1", 0, -240) sprite.set_size(.2) sprite.turn_left(90) def interval(): my_var = random.choice(my_list) if my_var == -150 or my_var == 50: pass # delete after adding indented code # add your code here sprite = codesters.Sprite("car1", -400, my_var) sprite.turn_right(90) sprite.set_size(0.5) sprite.set_x_speed(2) stage.event_interval(interval, 0.5)
  • Run Code
  • 提交作品
  • 下个活动
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)