screen.onkey(snake.up, "Up") screen.onkey(snake.down, "Down") screen.onkey(snake.left, "Left") screen.onkey(snake.right, "Right") screen.update() game_is_on = True while game_is_on: screen.update() time.sleep(0.1) snake.move() # Detect collision with food if snake.head.distance(food) < 15: score.add_point() food.refresh() snake.extend() screen.update() # Detect collision with wall if snake.head.xcor() > 290 or snake.head.xcor() < -290 or snake.head.ycor() > 290 or snake.head.ycor() < -290: score.reset() snake.reset() # Detect collision with tail for seg in snake.snake[1:]: if snake.head.distance(seg) < 10: score.reset() snake.reset() screen.exitonclick()
screen.listen() screen.onkey(r_paddle.go_up, "Up") screen.onkey(r_paddle.go_down, "Down") screen.onkey(l_paddle.go_up, "w") screen.onkey(l_paddle.go_down, "s") game_is_on = True while game_is_on: time.sleep(ball.move_speed) ball.move() if ball.ycor() >= 290 or ball.ycor() <= -290: ball.bounce_y() if ball.distance(r_paddle) < 53.9 and ball.xcor() > 320 or ball.distance( l_paddle) < 53.9 and ball.xcor() < -320: ball.bounce_x() if ball.xcor() >= 380: scoreboard_l.add_point() ball.reset_position() if ball.xcor() <= -380: scoreboard_r.add_point() ball.reset_position() screen.update() screen.exitonclick()