【问题标题】:Variable assignment in Pygame Ball Bounce SimulationPygame Ball Bounce Simulation 中的变量赋值
【发布时间】:2019-04-20 13:51:43
【问题描述】:

我正在尝试使用 Python 3.7.3 中的 Pygame 模块进行球反弹模拟。我的课程展示了球,但不适用于运动。错误是“分配前引用的局部变量 x”。我认为这意味着它是本地的,但需要是全局的,但是要将球的数量作为变量(所以我可以说要生成多少)我不知道如何解决这个问题。

我尝试阅读其他问题,但没有一个能够解决我的问题。我可以让一个球在屏幕上弹跳,边界碰撞工作,但当我使它面向对象时却不行。我也尝试过使用自变量来引用每个单独的球,但这没有用。

class BallEntity():
    def __init__(self, radius):
        x = random.randint(radius, win_width - radius)
        y = random.randint(radius, win_height - radius)
        pos = x, y
        pygame.draw.circle(win, (248, 24, 148), pos, radius)
        dx = random.randint(-5, 5)
        dy = random.randint(-5, 5)
        BallEntity.movement(self)

    def movement(self):
        if x <= r1 or x >= win_width - r1:
            dx = -dx
        elif x > r1 and x < win_width -r1:
            x += dx

        if y <= r1 or y >= win_height - r1:
            dy = -dy
        elif self.y > r1 and self.y < win_height -r1:
            y += dy


numbBalls = 8
r1 = 8
for i in range(numbBalls):
    BallEntity.__init__(i, r1)

我希望球在碰撞工作的情况下打印和移动,但我收到错误“在赋值之前引用了局部变量 x。”

【问题讨论】:

  • 总是将完整的错误消息(Traceback)放在有问题的地方(作为文本,而不是屏幕截图)。还有其他有用的信息。
  • 你可以用BallEntity.movement(self)代替self.movement(),这是首选方法。
  • 您的变量是本地变量 - 您必须使用 self. 才能访问类中所有方法中的变量 - self.xself.yself.dxself.dy
  • 不是BallEntity.__init__(i, r1),而是BallEntity(r1)

标签: python pygame


【解决方案1】:
  • global win,x,y 让我们删除未定义的 x`
  • 另外,xy 用于内部class 而不是global variable 到各个圆点。

import random

import pygame, sys
import time
from pygame.locals import *

pygame.init()

win_width = 500
win_height = 400
radius = 90

win = pygame.display.set_mode((win_width, win_height), 0, 32)

class BallEntity():
    win = None

    x = 0
    y = 0

    dx = 1
    dy = 1

    radius = 0
    # preious position(used for delete previous circle after movement)
    prePos = 0, 0
    pos = 0, 0
    def __init__(self, i, radius):
        self.radius = radius

        # random position (x, y)
        self.x = random.randint(radius, win_width - radius)
        self.y = random.randint(radius, win_height - radius)

        self.dx = random.randint(-5, 5)
        self.dy = random.randint(-5, 5)

    def movement(self):
        global win

        if self.x <= r1 or self.x >= win_width - r1:
            self.dx = -self.dx
        elif self.x > r1 and self.x < win_width -r1:
            self.x += self.dx

        if self.y <= r1 or self.y >= win_height - r1:
            self.dy = -self.dy
        elif self.y > r1 and self.y < win_height -r1:
            self.y += self.dy

        self.pos = self.x, self.y
        # draw background color to delete previous position
        pygame.draw.circle(win, (0, 0, 0), self.prePos, self.radius)
        # draw circle
        pygame.draw.circle(win, (248, 24, 148), self.pos, self.radius)

        self.prePos = self.pos

numbBalls = 5
r1 = 8


balls = []



# Create balls and store list
for i in range(numbBalls):
    e = BallEntity(i, r1)
    balls.append(e)

while True:
    # update circle position via movement()
    for i in range(numbBalls):
        balls[i].movement()

    pygame.display.update()
    # delay for animation
    time.sleep(0.1)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

【讨论】:

  • 没有解释的代码不是首选答案。 -1
  • @furas 现在怎么样?
  • @WaiHaLee 的解释什么也没解释。代码中没有global x,y - 那么为什么会提到它呢?代码使用self.x, self.y,但对此没有任何解释。
  • @furas - 嗯:有效积分。我点击错误并遇到了这个答案。看到在您发表评论后应用了修改,并认为它可能足以证明删除您的反对票是合理的。无论哪种方式我都不在乎,但我认为你可能希望你的 1 代表回来。
猜你喜欢
  • 1970-01-01
  • 2016-09-21
  • 2020-02-21
  • 2017-04-21
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多