我遇到一个错误,它拒绝显示我的比赛得分。 另一个问题是,对于敌人来说,弹跳是非常可预测的。
对于敌人的弹跳,我尝试从右到左插入随机数,但似乎没有用。 对于计分问题,我尝试用Google搜索,但找不到任何东西
#set the score
score = 0
#draw the score
score = turtle.Turtle()
score.speed(0)
score.color("white")
score.penup()
score.setposition(-290, 280)
scorestring = "Score: %s" %score
score.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
score.hideturtle()
#Create the player turtle
player = turtle.Turtle()
player.color("yellow")
player.shape("square")
player.penup()
player.speed(0)
player.setposition(0, -250)
player.shapesize(2.3)
player_speed = 35
# player movement
def move_left():
x = player.xcor() - player_speed
if x < -280:
x = - 280
player.setx(x)
def move_right():
x = player.xcor() + player_speed
if x > 280:
x = 280
player.setx(x)
# keybindings
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
# create sprite
enemy = Enemy('circle', 'red', -200, 250)
# main game loop
def play():
enemy.move()
# check for collision
if enemy.is_collision(player):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
enemy.setposition(-250, 250)
enemy.setheading(250)
#update the score
score += 20
scorestring = "Score: %s" %score
scorepen.clear()
scorepen.write(scorestring, False, align="left", font=("Arial", 14,
"normal"))
wn.ontimer(play, 0 )
play()
我希望它能显示分数,但这是发生的错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\prana\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\prana\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 749, in callit
func(*args)
File "C:\Users\prana\Desktop\game.py\players v enemys.py", line 128, in play
score += 20
UnboundLocalError: local variable 'score' referenced before assignment