python错误'全球名称'播放器'没有定义'

时间:2015-12-09 16:12:12

标签: python pygame

我已经在过去的几个月里独自学习python而且我开始制作关于弱势游戏(使用pygame)的游戏,我绝对不知道我做错了什么

这是我的代码(btw im pong):

import pygame

pygame.init()

display_width = 800
display_height = 600

black = (0, 0, 0)
white = (255, 255, 255)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('pong')
clock = pygame.time.Clock()
FPS = 60

class Player():

    def __init__(self):

        self.padWid = 8
        self.padHei = 64
        self.x = display_width - 16
        self.y = (display_height/2) - (self.padHei/2)
        self.speed = 3
        self.score = 0
        self.scoreFont = pygame.font.Font('freesansbold.ttf', 40)

    def scoring(self):

        self.text = self.scoreFont.render(str(self.score),True,white)
        gameDisplay.blit(self.text, [(display_width * 0.75),30])

    def movement(self):

        key = pygame.key.get_pressed()

        if key[pygame.K_UP]:
            self.y -= self.speed
        elif key[pygame.K_DOWN]:
            self.y += self.speed

        if self.y <= 0:
            self.y = 0
        elif self.y >= display_height - self.padHei:
            self.y = display_height - self.padHei

    def draw(self):

        pygame.draw.rect(gameDisplay, white, [self.x, self.y, self.padWid, self.padHei])

class Enemy():

    def __init__(self):

        self.padWid = 8
        self.padHei = 64
        self.x = 16
        self.y = (display_height/2) - (self.padHei/2)
        self.speed = 3
        self.score = 0
        self.scoreFont = pygame.font.Font('freesansbold.ttf', 40)


    def scoring(self):

        self.text = self.scoreFont.render(str(self.score),True,white)
        gameDisplay.blit(self.text, [(display_width * 0.23),30])

    def movement(self):

        key = pygame.key.get_pressed()

        if key[pygame.K_w]:
            self.y -= self.speed
        elif key[pygame.K_s]:
            self.y += self.speed

        if self.y <= 0:
            self.y = 0
        elif self.y >= display_height - self.padHei:
            self.y = display_height - self.padHei

    def draw(self):

        pygame.draw.rect(gameDisplay, white, [self.x, self.y, self.padWid, self.padHei])

class Ball():

    def __init__(self):

        self.x = display_width/2
        self.y = display_height/2
        self.radius = 4
        self.x_speed = -3
        self.y_speed = 3

    def movement(self):

        self.x += self.x_speed
        self.y += self.y_speed

        if self.y <= self.radius or self.y >= display_height - self.radius:
            self.y *= -1

        if self.x + self.radius >= display_width:
            self.__init__()
            enemy.score += 1
        elif self.x - self.radius <= 0:
            self.__init__()
            player.score += 1

        for n in range(-self.radius, [player.padHei + self.radius]):
            if self.y == player.y + n:
                if self.x + self.radius >= player.x:
                    self.x_speed *= -1
                    break
            n += 1

        for n in range(-self.radius, [enemy.padHei + self.radius]):
            if self.y == player.y + n:
                if self.x - self.radius <= enemy.x + enemy.w:
                    self.x_speed *= -1
                    break
            n += 1

    def draw(self):

        pygame.draw.circle(gameDisplay, white, [self.x, self.y], self.radius)

def game_loop():

    running = True
    player = Player()
    enemy = Enemy()
    ball = Ball()

    while running:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(black)

        player.movement()
        enemy.movement()
        ball.movement()

        player.draw()
        enemy.draw()
        ball.draw()

        player.scoring()
        enemy.scoring()

        pygame.display.update()

game_loop()

我总是收到以下错误:

Traceback (most recent call last):
  File "C:\programs\python pong\pong.py", line 159, in <module>
    game_loop()
  File "C:\programs\python pong\pong.py", line 148, in game_loop
    ball.movement()
  File "C:\programs\python pong\pong.py", line 112, in movement
    for n in range(-self.radius, [player.padHei + self.radius]):
NameError: global name 'player' is not defined

1 个答案:

答案 0 :(得分:2)

您在函数player中创建game_loop(),但此变量是本地变量,因此您无法在该函数之外看到它。 将播放器作为ball.movement()函数中的参数传递。 这样:

def movement(self, player):
    self.x += self.x_speed
    self.y += self.y_speed
    ...
    ...


ball.movement(player)