# Move all but first segments in reverse order body = snake.get_body() for index in range(len(body) - 1, 0, -1): x = body[index - 1].xcor() y = body[index - 1].ycor() body[index].goto(x, y) # Move segment 0 to where head is if len(body) > 0: x, y = snake.get_head_position() body[0].goto(x, y) snake.move() scoreboard.write_score(score, high_score) # Check for head collisions with body segments collision = False for i in range(len(body)): if snake.head_distance_from(body[i]) < 20: collision = True if collision: lose() break # Exit the loop, ending game game.update() time.sleep(game.delay) turtle.done()
This part is tough! Ask for help if you need it! What should x and y be? ''' # body is a list of the snake's segments except for the head body = snake.get_body() for i in range(len(body) - 1, 0, -1): x = 0 y = 0 #body[i].goto(x, y) continue # Update the snake's head position snake.move() ''' TODO Step 6: Write score on screen ''' ''' TODO Step 7: Check for the head colliding with body segments. Lose game if collision occurs. ''' collision = False # More code here if collision: lose() break # Exit the loop, ending the game game.update() # Go to next game tick time.sleep(game.delay) turtle.done()