为什么使用乌龟模块得分会给我一个错误

时间:2019-06-23 22:50:06

标签: python

我正在制作游戏,并且计分系统无法正常工作

我试图将分数乌龟更改为sprite类,但它似乎仍然无法正常工作,所以我只好回到乌龟

import turtle
import random

# create sprite
class Sprite(turtle.Turtle):
    def __init__(self, sprite_shape, color, startx, starty):
        super().__init__( shape=sprite_shape)

        self.speed(0)
        self.penup()
        self.color(color)
        self.goto(startx, starty)

        self.speed = 1

    def move(self):
        self.forward(self.speed)

        # Boundary detection
        if self.xcor() > 300:
            self.setx(300)
            self.rt(90)
        elif self.xcor() < -300:
            self.setx(-300)
            self.lt(120)

        if self.ycor() > 300:
            self.sety(300)
            self.lt(90)
        elif self.ycor() < -300:
            self.sety(-300)
            self.lt(120)


    def is_collision(self, other):
        return self.distance(other) < 20

# create enemy
class Enemy(Sprite):
    def __init__(self ,  spriteshape, color, startx, starty):
        Sprite.__init__(self  , spriteshape, color, startx, starty)
        self.speed = 4
        self.setheading(random.randint(0,360))  

#Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")

#Draw border
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()

#Draw the score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 280)
scorestring = "Score: %s" %score
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
score_pen.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)

         wn.ontimer(play, 0 )

play()

#Update the score
score += 10
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))



我希望分数显示并起作用,但是出现此错误 追溯(最近一次通话):   文件“ C:\ Users \ prana \ Desktop \ game.py \ players v敌人s.py”,第70行,在     scorestring =“分数:%s”%score NameError:未定义名称“得分”

  
    

>

  

0 个答案:

没有答案