python - 键盘输入和得分。蟒龟
问题描述
我找到了一个“flappy game”项目,我正在根据自己的喜好对其进行修改。
我想知道如何用键盘而不是鼠标来控制“点击”的定义(我已经尝试过在线代码“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()
我已经尝试了各种功能,但都没有奏效。
解决方案
我想知道如何使用键盘而不是鼠标来控制“点击”的定义
这应该像替换一样简单:
onscreenclick(tap)
和
onkey(tap, "Up")
listen()
并将x
andy
参数删除到tap()
. 我在下面重新编写了您的代码以进行此更改并消除:
from freegames import vector
并使其仅使用 turtle 中可用的内容运行。我还更改了保持向量的逻辑,并让默认的海龟在它们的位置绘制点,而不是让球和鸟都成为海龟,并根据需要移动它们。由于海龟通常不会被垃圾收集,因此我保留了一个已离开屏幕的球列表以再次重复使用:
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()
更新分数。
推荐阅读
- sql - 从 2 个表和 1 个条件中选择值
- visual-studio-code - 如何通过其 API 激活 vscode 主窗口?
- python - 分别打印每一行
- c - 为什么我们在调用 dup2 之后要关闭文件描述符?
- python - 使所有以“@”和“#”开头的单词都可点击
- netlogo - 如何为每个刻度设置死亡概率
- git - 连接到 AWS Linux 实例中的 github.com 端口 443
- c++ - 为什么显示密码而不是消息?
- c# - 如何在没有 Windows 调度程序的情况下自动化 Windows 窗体应用程序?
- javascript - 如何修复 nginx 服务器中的一些 javascript 错误