NameError:未定义(python)

时间:2017-05-28 08:54:33

标签: python-2.7 turtle-graphics

尝试使用乌龟图形和Python构建一个简单的游戏。 我创建了敌人并将它们放入while循环中,这样每当它们触及任何一侧的边界时,它们就会向下移动40个单位。我将y坐标的值放在变量u中。但是,当我运行代码时,它说:

  

nameError:'u'未定义

帮助!!

#!/usr/bin/python
import turtle
import os

#screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("spaceinvaders")

#boarder
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)

for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)

border_pen.hideturtle()

#player
player = turtle.Turtle()
player.color("blue")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0,-250)
player.setheading(90)
playerspeed = 15

#enemy
enemy = turtle.Turtle()
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
enemy.setposition(-200,250)
enemyspeed = 2

#move
def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -280:
        x = - 280
    player.setx(x)
def move_right():
    x = player.xcor()
    x += playerspeed
    if x > 280:
        x = +280
    player.setx(x)

#key bindings
turtle.listen()
turtle.onkey(move_left,"Left")
turtle.onkey(move_right,"Right")

#mainloop

while True:

#enemy moves
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)

if enemy.xcor() < -280:
    u = enemy.ycor()
    u -= 40
    enemyspeed *= -1
    enemy.sety(u)
if enemy.xcor() > 280:
    u = enemy.ycor()
    u -= 40
    enemyspeed *= -1
    enemy.sety(u)

delay = raw_input("press enter to finish")

1 个答案:

答案 0 :(得分:0)

即使@downshift指出错误的循环缩进,你也不应该在使用之前立即设置u所引用的错误。

我在您的代码设计中看到的主要问题是您使用while True:,这不应该出现在事件驱动的程序中。相反,应该通过计时器事件处理敌人的动作,并将程序控制权转交给mainloop(),以便其他事件可以正确触发。我已经按照以下方式对您的程序进行了重新设计,并进行了一些风格和优化调整:

import turtle

# player motion event handlers

def move_left():
    turtle.onkey(None, 'Left')  # avoid overlapping events
    player.setx(max(-280, player.xcor() - playerspeed))
    turtle.onkey(move_left, 'Left')

def move_right():
    turtle.onkey(None, 'Right')
    player.setx(min(280, player.xcor() + playerspeed))
    turtle.onkey(move_right, 'Right')

# enemy motion timer event handler

def move_enemy():
    global enemyspeed

    # enemy moves
    enemy.forward(enemyspeed)

    x = enemy.xcor()

    if x < -280 or x > 280:
        enemy.sety(enemy.ycor() - 40)
        enemyspeed *= -1

    wn.ontimer(move_enemy, 10)

# screen

wn = turtle.Screen()
wn.bgcolor('black')
wn.title('spaceinvaders')

# border

STAMP_SIZE = 20

border_pen = turtle.Turtle('square', visible=False)
border_pen.shapesize(600 / STAMP_SIZE, 600 / STAMP_SIZE, 3)
border_pen.pencolor('white')
border_pen.stamp()

# player

player = turtle.Turtle('triangle', visible=False)
player.color('blue')
player.speed('fastest')
player.penup()

player.setheading(90)
player.setposition(0, -250)
player.showturtle()

playerspeed = 15

# enemy

enemy = turtle.Turtle('circle', visible=False)
enemy.color('red')
enemy.speed('fastest')
enemy.penup()

enemy.setposition(-200, 250)
enemy.showturtle()

enemyspeed = 2

# key bindings
turtle.onkey(move_left, 'Left')
turtle.onkey(move_right, 'Right')
turtle.listen()

wn.ontimer(move_enemy, 100)

turtle.mainloop()  # for Python 3 use wn.mainloop()

这有望为您的游戏添加更多功能铺平道路。