【问题标题】:Beginner Pygame, Scores not updating/Variables not being found初学者 Pygame,分数未更新/未找到变量
【发布时间】:2016-10-28 01:33:34
【问题描述】:

首先,我是一名教育专业的学生,​​辅修计算机科学。我的主要关注点不是编码,我可能在这里犯了一些我还没有看到的大错误。我目前的问题是我会收到错误

"Traceback (most recent call last):
  File "/home/user/Downloads/PongV1.py", line 158, in <module>
    main()
  File "/home/user/Downloads/PongV1.py", line 13, in <module>
    game.play()
  File "/home/user/Downloads/PongV1.py", line 42, in <module>
    self.update()
  File "/home/user/Downloads/PongV1.py", line 77, in <module>
    self.ball.move()
  File "/home/user/Downloads/PongV1.py", line 136, in <module>
    game.score2 = game.score2 + 1
builtins.NameError: name 'game' is not defined  

每当我尝试运行这个游戏时。我知道它目前正在下载中,但我是在匆忙组装的 VM 机器上运行它的。据我所知,我称我的分数 1/分数 2 变量非常好。

我正在尝试做的目标是在球撞到墙上时更新角落的分数。目前,它位于 def move 部分

This is what my screen looks like when I try to run this program

感谢大家观看!

  # pygame v2

import pygame
import uaio
import math
import time
from pygame.locals import *

# User-defined functions
def main():
    surface = create_window()
    game = Game(surface)
    game.play()
    pygame.quit()

# Create window 
def create_window():
    pygame.init()
    surface_size = (700,600)
    title = 'Pong'
    surface = pygame.display.set_mode(surface_size)
    pygame.display.set_caption(title)
    return surface
# define class games
class Game:
    def __init__ (self, surface):
        self.surface = surface   #locations and surface colors of games
        self.bg_color = pygame.Color('black')
        self.pause_time = 0.01
        self.close_clicked = False
        self.continue_game = True
        self.ball = Ball(pygame.Color('white'),[350,300],5,[6,2], surface)
        self.paddle= Paddle(pygame.Color('white'),(100,300),100,100, surface)
        self.score1 = 0
        self.score2 = 0

    def play(self):    #playing the game while the game is not closed
        self.draw()
        while not self.close_clicked:
            self.handle_event()
            if self.continue_game:
                self.update()
                self.decide_continue
            self.draw()
            time.sleep(self.pause_time)

def handle_event(self): #continuing the game
    event = pygame.event.poll()
    if event.type == QUIT:
        self.close_clicked = True

def draw(self):  #drawing the balls and paddles
    self.surface.fill(self.bg_color)
    self.ball.draw()
    self.paddle.draw()
    self.draw_score()
    pygame.display.update()

def draw_score(self):
    string = str(self.score1)
    location = 0,0
    size = 80
    #fg_color = pygame.Color('white')
    uaio.draw_string(string, self.surface,location,size)

    string = str(self.score2)
    location = 650,0
    size = 80
    #fg_color = pygame.Color('white')
    uaio.draw_string(string, self.surface,location,size)

def paddlecollide(self):
    self.paddle.collide_right(x, y)
    self.paddle.collidge_left(x, y)        

def update(self): #updating the movement of the ball
    self.ball.move()
    self.ball.collide(self.paddle)


def decide_continue(self): # deciding to continue teh game
    pass

class Paddle: #defining paddle

def __init__(self, color, left, width, height, surface):
   #location of paddle etc
    self.color = color
    self.left = left
    self.surface = surface
    self.width = width
    self.height = height
    self.paddle1 = pygame.Rect(140,270,20,80)
    self.paddle2 = pygame.Rect(540,270,20,80)
    #return self.paddle1, self.paddle2


def draw(self):
    #drawing paddle
    pygame.draw.rect(self.surface, self.color, self.paddle1)
    pygame.draw.rect(self.surface, self.color, self.paddle2)

def collide_left(self, x, y):
    return self.paddle1.collidepoint(x, y)

def collide_right(self, x, y):
    return self.paddle2.collidepoint(x, y)    

class Ball: #defining ball

def __init__(self, color, center, radius, velocity, surface):
    #charactersitics of said ball

    self.color = color
    self.center = center
    self.radius = radius
    self.velocity = velocity
    self.surface = surface


def draw(self):
    #drawing the ball
    pygame.draw.circle(self.surface, self.color, self.center, self.radius)

def move(self):

    # how the ball moves as well as ist velocity
    size = self.surface.get_size()
    for coord in range(0, 2):
        self.center[coord] = (self.center[coord] + self.velocity[coord]) 
        if self.center[coord] < self.radius:
            self.velocity[coord] = -self.velocity[coord]
            Game.score1 = Game.score1 + 1
        if self.center[coord] + self.radius > size[coord]:
            self.velocity[coord] = -self.velocity[coord]
            Game.score2 = Game.score2 + 1





def collide(self, paddles):
    xcoord =0 
    if paddles.collide_left(self.center[0], self.center[1]):
        self.velocity[xcoord] = -self.velocity[xcoord]

    if paddles.collide_right(self.center[0], self.center[1]):
        self.velocity[xcoord] = -self.velocity[xcoord]        

        #if x_velocity <= 0:
        #    collide = False
        #    
        #else: collide = True 

main()

【问题讨论】:

  • Game.score2 = Game.score2 + 1 坐在球中,它无法访问 Game,无论如何这不是变量。
  • 请参阅stackoverflow.com/questions/10791588/…,了解如何让球找到其父级,从而增加分数。

标签: python-3.x pygame pong


【解决方案1】:

有问题的行是这一行(在您给定的代码中):

Game.score2 = Game.score2 + 1

这行有两点错误:

  • 首先,您尝试使用不存在的变量。 Game 是您定义的类的名称,您必须创建一个新的Game 对象并将其分配给一个变量,然后才能使用它。我看到你有,这导致我进入问题 2...
  • 范围。您已经在函数main 中定义了变量game。但是,此变量将在本地中创建。除了定义它的函数(有一些例外)之外,它不能在任何地方访问。我建议阅读thisstackoverflow 的答案,以更好地理解范围。

score1score2 都在 Games 类中定义。在main 函数(第12 行)中,创建了一个新的Games 对象,并将其分配给变量games。该变量是局部变量,只能在函数main内访问。

现在,您有 2 个选项。第一种选择是从Games 类中完全删除变量score1score2,并将它们作为单独的变量定义在程序的主体中。这将允许在任何地方访问它们(显然您必须更改对 game.score1game.score2 的任何引用。

第二个,在我看来更可取的选择是使变量game 成为一个全局变量。在您的 main 函数中,代码如下所示:

def main():
    surface = create_window()
    global game
    game = Game(surface)
    game.play()
    pygame.quit()

然后,请记住在 main 函数之外对 Game 类的任何引用取消大写,以便您使用变量 game,撒谎:

Game.score1 = Game.score1 + 1

变成

game.score1 = game.score1 + 1

我希望我解释得足够清楚。在深入研究 pygame 之前,我真的建议阅读一下范围和类在 python 中的工作原理。

【讨论】:

  • 不用担心,我编辑了我的答案以提供更多帮助。希望它会有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 2023-01-21
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 2023-03-14
相关资源
最近更新 更多