Ejemplo n.º 1
0
screen.listen()
screen.onkey(rpaddle.userup, "Up")
screen.onkey(rpaddle.userdown, "Down")
screen.onkey(lpaddle.userup, "w")
screen.onkey(lpaddle.userdown, "s")
game_on = True
while game_on:
    screen.update()
    time.sleep(0.1)
    ball.hideturtle()
    if ball.ycor() > 340 or ball.ycor() < -340:
        ball.setheading(-ball.heading())

    if ball.distance(rpaddle) < 60 and ball.xcor() > 460:
        ball.setheading(180 - ball.heading())
        rscore.update()

    if ball.distance(lpaddle) < 60 and ball.xcor() < -460:
        ball.setheading(180 - ball.heading())
        lscore.update()

    if ball.xcor() > 480 or ball.ycor() < -480:
        game_on = False
    ball.move()
screen.exitonclick()




Ejemplo n.º 2
0
scorecard = Scorecard()
snake = Snake()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.right, "Right")
screen.onkey(snake.left, "Left")

while is_game_on:
    screen.update()
    time.sleep(0.1)
    snake.move()
    if food.distance(snake.head) < 15:
        food.generate_food()
        snake.add_segment()
        scorecard.value += 1
        scorecard.update()
    if snake.head.xcor() < -290 or snake.head.xcor() > 290 or snake.head.ycor() < -290 or snake.head.ycor() > 290:
        snake.reset()
        snake.create_snake()
        if scorecard.highestvalue < scorecard.value:
            scorecard.highestvalue = scorecard.value
            scorecard.update()
        scorecard.value = 0
    for segment in snake.segments:
        if segment == snake.head:
            pass
        elif snake.head.distance(segment) < 10:

screen.exitonclick()