python - 命令 onkey() 出错

标签 python turtle-graphics

我正在尝试用 Python turtle 创建一个类似射击游戏的游戏(它基本上是游戏 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()

注意:我正在 Trinket.io 中创建游戏。点击here转到 Trinket.io 版本。

最佳答案

Python 是一种命令式编程语言。这意味着顺序很重要。在 onkey 初始化部分之前将您游戏的主要逻辑声明为无限循环:

#movement
while True:
  enemy.forward(move_speed)
  ...

由于此循环永远运行,这意味着它将开始执行并且代码永远不会到达您设置键映射的部分。

你需要将循环中的代码放在一个函数中,并决定它何时需要被 Turtle 调用。您不应将 while True 作为函数的一部分,因为已经存在一个由 Turtle 管理的主循环。

关于python - 命令 onkey() 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35104950/

相关文章:

Python多进程调试

python - 当我们在终端python中输入时,如何在窗口中继续运行游戏

python - 使用递归绘制雪花

python - 适用于多只 turtle 的功能

php - 如何使用 Python 和 Flask 查找 HTML 单选按钮的值?

Python Fabric 不会传入变量

function - Python:设置配置参数

python - Python中运算符重载的综合指南

python - 使用 Python turtle 制作没有 circle 函数的圆

python - 如何在 Turtle 中填充这些方 block - Python