Python 命令onkey()出错
我正在尝试用Python turtle创建一个类似射击的游戏(它基本上是游戏Python 命令onkey()出错,python,turtle-graphics,Python,Turtle Graphics,我正在尝试用Python turtle创建一个类似射击的游戏(它基本上是游戏Boom dots的副本)。但我有很多问题,因为我对编程有点陌生。这次onkey()命令不起作用。我什么都试过了,但似乎没有任何帮助。 我没有任何回溯错误。只是当我按下分配给命令的按钮时,定义的命令不起作用 我怀疑问题所在的部分代码是: def cannon_left(): cannon_x = cannon_x - 10 cannon.goto(cannon_x, 0) def cannon_right():
Boom dots
的副本)。但我有很多问题,因为我对编程有点陌生。这次onkey()命令不起作用。我什么都试过了,但似乎没有任何帮助。
我没有任何回溯错误。只是当我按下分配给命令的按钮时,定义的命令不起作用
我怀疑问题所在的部分代码是:
def cannon_left():
cannon_x = cannon_x - 10
cannon.goto(cannon_x, 0)
def cannon_right():
cannon_x = cannon_x + 10
cannon.goto(cannon_x, 0)
def reset1():
live_score = 0
整个代码:
import random
import turtle
#images
image_coconut = "Coconut.png"
image_banana = "Banana.png"
image_pineapple = "Pineapple.png"
image_cannon = "Cannon.png"
#definitions
live_score = 0
screen = turtle.Screen()
wn = turtle.Screen()
cannon = turtle.Turtle()
enemy = turtle.Turtle()
score = turtle.Turtle()
background = turtle.Turtle()
reset = turtle.Turtle()
bullet = turtle.Turtle()
enemy_x = enemy.xcor()
enemy_y = enemy.ycor()
cannon_x = 0
move_speed = 2
enemy1 = 0
def cannon_shooting(x, y):
bullet.showturtle()
bullet.forward(280)
if bullet.ycor() == enemy_y - 10:
if not bullet.xcor() == enemy_x - 10:
if live_score == 0:
live_score = 0
else:
live_score = live_score + 1
if bullet.xcor() == enemy_x - 10:
live_score = live_score + 1
enemy1 = random.randint(1, 3)
bullet.hideturtle()
#image adding
screen.addshape(image_coconut)
screen.addshape(image_banana)
screen.addshape(image_pineapple)
screen.addshape(image_cannon)
def cannon_left():
cannon_x = cannon_x - 10
cannon.goto(cannon_x, 0)
def cannon_right():
cannon_x = cannon_x + 10
cannon.goto(cannon_x, 0)
def reset1():
live_score = 0
#setup
bullet.hideturtle()
bullet.speed(50)
bullet.penup()
bullet.shape('circle')
bullet.goto(0, -140)
bullet.left(90)
enemy.speed(0)
enemy.penup()
enemy.hideturtle()
enemy.goto(0, 140)
screen.addshape(image_coconut)
enemy.shape(image_coconut)
enemy.showturtle()
cannon.speed(0)
cannon.penup()
cannon.hideturtle()
cannon.goto(0, -140)
screen.addshape(image_cannon)
cannon.shape(image_cannon)
cannon.showturtle()
cannon.left(90)
score.speed(0)
score.penup()
score.hideturtle()
score.goto(90, -190)
score.color('white')
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
reset.speed(0)
reset.penup()
reset.hideturtle()
reset.goto(-185, -190)
reset.color('white')
reset.write("Reset (R)", font=(None, 11, "bold"))
#movement
while True:
enemy.forward(move_speed)
if enemy.xcor() == 140:
enemy.left(180)
enemy.forward(move_speed)
if enemy.xcor() == -140:
enemy.right(180)
enemy.forward(move_speed)
if enemy1 == 1:
screen.addshape(image_banana)
enemy.shape(image_banana)
if enemy1 == 2:
screen.addshape(image_pineapple)
enemy.shape(image_pineapple)
if enemy1 == 3:
enemy.shape(image_coconut)
#key presses
wn.onkey(cannon_right, "D")
wn.onkey(cannon_left, "A")
wn.onkey(cannon_right, "Right")
wn.onkey(cannon_left, "Left")
wn.onkey(cannon_shooting, "SPACE")
wn.onkey(reset1, "R")
#others
wn.listen()
wn.mainloop()
注意:我正在
welkit.io
中创建游戏。单击以转到weinket.io
版本。Python是一种命令式编程语言。这意味着秩序很重要。在onkey
初始化部分之前,游戏的主要逻辑被声明为无限循环:
#movement
while True:
enemy.forward(move_speed)
...
由于这个循环永远运行,这意味着它将开始执行,代码将永远不会到达设置键映射的部分
您需要循环中的代码将此代码放入一个函数中,并确定海龟何时需要调用它。您不应该将
while True
作为函数的一部分,因为已经存在一个由Turtle管理的主循环。谢谢。这很有帮助。现在我可以移动大炮了。但是敌人(又名椰子)停止移动。我怎样才能解决这个问题?