命令onkey()出错

时间:2016-01-30 17:44:07

标签: python turtle-graphics

我正在尝试用Python龟创建类似射击游戏(它基本上是游戏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版本。

1 个答案:

答案 0 :(得分:1)

Python是一种命令式编程语言。这意味着订单很重要。似乎是你的游戏的主要逻辑是在onkey初始化部分之前宣布为无限循环:

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

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

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