STEP 19: Now we need to check if the turtle has collided with a car. That would mean we lose!

  • Go to the LOGIC tab and the toolkit. Drag if or into the collision event.
  • Make sure that the first line of the if statement is only indented once. The other lines should be indented twice!
  • Change both choice variables to image. Change "yes" to "car1" and change "y" to "car2".
  • This will check to see if we've collided with the red cars or the blue cars!

# 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) if my_var == -50 or my_var == 150: pass # delete after adding indented code # add your code here sprite = codesters.Sprite("car2", 400, my_var) sprite.turn_left(90) sprite.set_size(0.5) sprite.set_x_speed(-2) stage.event_interval(interval, 0.5) stage.disable_all_walls() def up_key(): sprite.move_up(20) # add other actions... stage.event_key("up", up_key) def down_key(): sprite.move_down(20) # add other actions... stage.event_key("down", down_key) def left_key(): sprite.move_left(20) # add other actions... stage.event_key("left", left_key) def right_key(): sprite.move_right(20) # add other actions... stage.event_key("right", right_key) # sprite = codesters.Text("text") result = codesters.Text("") def collision(sprite, hit_sprite): image = hit_sprite.get_image_name() color = hit_sprite.get_color() if color == "green": pass # delete after adding indented code # add your code here result.set_text("YOU WIN!") result.set_color("green") sprite.event_collision(collision)