我想创建一个类
Ball
,并创建用户为该类生成的对象列表。然后,我将在 run()
中引用此列表,以在 pygame
中绘制用户生成的对象。我的问题如下:
当我尝试引用
Ball(a, v, r...)
时,它说 Ball
在我的定义之前被引用。如有任何帮助,我们将不胜感激!
这里是完整的错误: 文件“/Users/.../Desktop/.../python/promot.py”,第 66 行,运行中 balls.append(Ball(a, v, r, c, x_pos, y_pos)) #这里我得到错误 ^^^^ UnboundLocalError:无法访问未与值关联的局部变量“Ball”
import pygame
import math
pygame.init()
#screen
framespd = 30 #what is frame speed for gravity = 9.81
gravity = 1
t = 0.01
clock = pygame.time.Clock()
width, height = 1000, 1000
pygame.display.set_caption("Orbit Take 1")
screen = pygame.display.set_mode([width, height])
class Ball:
def __init__(self, angle, velocity, radius, color, x_pos, y_pos):
self.angle = angle
self.velocity = velocity
self.radius = radius
self.color = color
self.x_pos = x_pos
self.y_pos = y_pos
def draw(self):
self.circle = pygame.draw.circle(screen, self.color, (self.x_pos, self.y_pos), self.radius)
def true_velocity_x(self):
true_velocity_x = self.velocity * math.cos(self.angle)
def true_velocity_y(self):
true_velocity_y = self.velocity * math.sin(self.angle)
new_velocity_y = true_velocity_y + (gravity * t)
#def update position(self):
#x pos = x velocity
#y pos += y velocity
balls = []
def run():
value = True
while value:
clock.tick(framespd)
# Balls in sim
number_balls = int(input("How many balls: "))
while len(balls) < number_balls:
# Variables for Ball
a = input("Angle: ")
v = input("Velocity: ")
r = input("Radius: ")
c = input("Color: ")
x_pos = '0'
y_pos = '500'
balls.append(Ball(a, v, r, c, x_pos, y_pos)) #here i get error "local variable "Ball" defined in enclosing scope on line 24 referenced before assignment"
screen.fill('black')
#draw ball
for Ball in balls:
Ball.draw(pygame.display.set_mode((width, height)))
#update position
for event in pygame.event.get():
if event.type == pygame.QUIT:
value = False
pygame.display.flip()
pygame.quit()
run()
我的第一次尝试以粗体引用为“我尝试过的其他东西”,但我在使用初始化之外的类时遇到了麻烦,没有错误。我的下一次尝试是“当前尝试”,当我运行时没有显示任何内容。
import pygame
import math
pygame.init()
# screen
framespd = 30 # what is frame speed for gravity = 9.81
gravity = 1
t = 0.01
clock = pygame.time.Clock()
width, height = 1000, 1000
pygame.display.set_caption("Orbit Take 1")
screen = pygame.display.set_mode([width, height])
class Ball:
def __init__(self, angle, velocity, radius, color, x_pos, y_pos):
self.angle = angle
self.velocity = velocity
self.radius = radius
self.color = color
self.x_pos = x_pos
self.y_pos = y_pos
def draw(self):
self.circle = pygame.draw.circle(screen, self.color, (self.x_pos, self.y_pos), self.radius)
def true_velocity_x(self):
true_velocity_x = self.velocity * math.cos(self.angle)
def true_velocity_y(self):
true_velocity_y = self.velocity * math.sin(self.angle)
new_velocity_y = true_velocity_y + (gravity * t)
# def update position(self):
# x pos = x velocity
# y pos += y velocity
balls = []
def run(cls = Ball):
value = True
while value:
clock.tick(framespd)
# Balls in sim
number_balls = int(input("How many balls: "))
while len(balls) < number_balls:
# Variables for Ball
a = input("Angle: ")
v = input("Velocity: ")
r = input("Radius: ")
c = input("Color: ")
x_pos = '0'
y_pos = '500'
balls.append(cls(a, v, r, c, x_pos,
y_pos)) # here i get error "local variable "Ball" defined in enclosing scope on line 24 referenced before assignment"
screen.fill('black')
# draw ball
for Ball in balls:
Ball.draw(pygame.display.set_mode((width, height)))
# update position
for event in pygame.event.get():
if event.type == pygame.QUIT:
value = False
pygame.display.flip()
pygame.quit()
run()