snake = Snake() food = Food() score = Scoreboard() scn.listen() scn.onkey(snake.up, "Up") scn.onkey(snake.down, "Down") scn.onkey(snake.left, "Left") scn.onkey(snake.right, "Right") game_on = True while game_on: scn.update() time.sleep(0.1) snake.move() # detect if food consumed if snake.head.distance(food) < 15: food.refresh() snake.extend_snake() score.increase_score() # detect collison with wall if snake.head.xcor() > 285 or snake.head.xcor() < -285 or snake.head.ycor( ) < -285 or snake.head.ycor() > 285: score.reset() snake.reset() scn.exitonclick()
screen.listen() screen.onkey(snake.up, "Up") screen.onkey(snake.down, "Down") screen.onkey(snake.right, "Right") screen.onkey(snake.left, "Left") game_is_on = True while game_is_on: screen.update() time.sleep(0.1) snake.move() # detecting collision with food if snake.head.distance(food) < 30: food.refresh() snake.extend() scoreboard.increase_score() # Detect collision with wall if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor( ) > 280 or snake.head.ycor() < -280: scoreboard.reset() snake.reset() # Detect collision with tail for segment in snake.segments[1:]: if snake.head.distance(segment) < 10: scoreboard.reset() snake.reset() screen.exitonclick()