键盘输入和分数。 Python龟

时间:2018-05-26 10:56:46

标签: python turtle-graphics

我找到了一个" flappy游戏"项目,我正在修改它,我喜欢。

我想知道如何控制定义"点按"使用键盘而不是鼠标(我已经尝试过在线代码" onkey"它不起作用)我想知道我是否可以添加分数。

接下来我附上我现在拥有的代码,我希望得到一些反馈。

from random import *
from turtle import *
from freegames import vector

bird = vector(0, 0)
balls = []

def tap(x, y):
    "Move bird up in response to screen tap."
    up = vector(0, 30)
    bird.move(up)

def inside(point):
    "Return True if point on screen."
    return -200 < point.x < 200 and -200 < point.y < 200

def draw(alive):
    "Draw screen objects."
    clear()

    goto(bird.x, bird.y)

    if alive:
        dot(10, 'green')
    else:
        dot(10, 'red')

    for ball in balls:
        goto(ball.x, ball.y)
        dot(20, 'black')

    update()

def move():
    "Update object positions."
    bird.y -= 5

    for ball in balls:
        ball.x -= 3

    if randrange(10) == 0:
        y = randrange(-199, 199)
        ball = vector(199, y)
        balls.append(ball)

    while len(balls) > 0 and not inside(balls[0]):
        balls.pop(0)

    if not inside(bird):
        draw(False)
        return

    for ball in balls:
        if abs(ball - bird) < 15:
            draw(False)
            return

    draw(True)
    ontimer(move, 50)

setup(420, 420, 370, 0)
hideturtle()
up()
tracer(False)
onscreenclick(tap)
move()
done()

我尝试过各种各样的功能,但没有一种能够奏效。

1 个答案:

答案 0 :(得分:0)

  

我想知道如何用“控件”来控制“tap”的定义   键盘而不是鼠标

这应该像替换一样简单:

onscreenclick(tap)

onkey(tap, "Up")
listen()

并将xy参数放到tap()。我已经重新编写了下面的代码来进行此更改以及消除:

from freegames import vector

并使其与乌龟中的可用内容一起运行。我还改变了保持向量的逻辑,并在他们的位置使用默认的乌龟绘制点,而不是将球和鸟放在所有海龟中,并根据需要移动它们。由于海龟不是通常的垃圾收集,我会保留一个已经离开屏幕再次重复使用的球列表:

from random import randrange
from turtle import Turtle, Screen

BIRD_RADIUS = 10
BALL_RADIUS = 20
CURSOR_SIZE = 20

WINDOW_WIDTH, WINDOW_HEIGHT = 420, 420
GALLERY_WIDTH, GALLERY_HEIGHT = WINDOW_WIDTH - BALL_RADIUS, WINDOW_HEIGHT - BIRD_RADIUS

def click():
    "Move bird up in response to screen key click."

    bird.forward(min(BIRD_RADIUS * 3, WINDOW_HEIGHT/2 - bird.ycor()))  # flying higher

def is_ball_inside(turtle):
    "Return True if point on screen."

    x = turtle.xcor()

    return -GALLERY_WIDTH/2 < x

def is_bird_inside(turtle):
    "Return True if point on screen."

    y = turtle.ycor()

    return -GALLERY_HEIGHT/2 < y < GALLERY_HEIGHT/2

def draw(is_alive):
    "Draw screen objects."

    if not is_alive:
        bird.color('red')

    screen.update()

def move():
    "Update object positions."

    if not is_bird_inside(bird):
        draw(False)  # flew or fell out of gallery
        return

    bird.backward(BIRD_RADIUS/2)  # falling to earth

    for ball in balls:
        ball.forward(3)

    for ball in balls:
        if ball.distance(bird) < (BIRD_RADIUS + BALL_RADIUS) / 2:
            draw(False)
            return

    while balls and not is_ball_inside(balls[0]):
        ball = balls.pop(0)
        ball.hideturtle()

        spare_balls.append(ball)  # reduce, reuse, recycle

    if randrange(10) == 0:
        ball = spare_balls.pop() if spare_balls else Turtle("circle", visible=False)
        ball.shapesize(BALL_RADIUS / CURSOR_SIZE)
        ball.setheading(180)
        ball.penup()
        ball.goto(GALLERY_WIDTH/2, randrange(BALL_RADIUS - GALLERY_HEIGHT/2, GALLERY_HEIGHT/2 - BALL_RADIUS))
        ball.showturtle()

        balls.append(ball)

    draw(True)

    screen.ontimer(move, 50)

screen = Screen()
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT + 10)  # little bit added for title bar

bird = Turtle("circle", visible=False)
bird.shapesize(BIRD_RADIUS / CURSOR_SIZE)
bird.color("green")
bird.setheading(90)
bird.penup()
bird.showturtle()

balls = []
spare_balls = []

screen.tracer(False)

screen.onkey(click, "Up")
screen.listen()

move()

screen.mainloop()

要使乌龟窗口成为键盘监听器,您需要先用鼠标触摸它,使其处于活动状态,然后再按向上箭头键。

在将当前分数添加到窗口之前,您需要定义一个分数的分数。 (是否成功避开了球的数量?)在这里,我会使用一个固定位置的额外隐形乌龟,只需要undo()write()来更新scrore。