[英]Use a variable in another function
所以这个函数的一部分,我有两个变量, color
和color_rect
,以后是否可以使用这些变量另一个函数。 我尝试将 global 放在它之前,但是它导致语法错误或说在全局声明之前使用了variable
完整代码:
def colors():
clock = pygame.time.Clock()
button_1 = pygame.Rect(width/2, (height/10), width/4, 60)
button_1.centerx = width/2
colors = [("White", (255, 255, 255)), ("Red", (255, 0, 0)), ("Green", (0, 255, 0)), ("Blue", (0, 0, 255))]
current_color = 0
color_t, color = colors[current_color]
colors_rect = [("White", (255, 255, 255)), ("Red", (255, 0, 0)), ("Green", (0, 255, 0)), ("Blue", (0, 0, 255))]
current_color_rect = 0
color_t_rect, color_rect = colors[current_color]
text_particle = render_text_speech('pixel.ttf', 30, 'Particle:', (255,255,255), 100, 300, False)
text_colors = render_text_speech('pixel.ttf', 50, 'Colors', (255,255,255), button_1.centerx, button_1.centery, False)
text_color = render_text_speech('pixel.ttf', 30, color_t, color, 180, 300, False)
ball = pygame.Rect(350,200,50,50)
text_rect = render_text_speech('pixel.ttf', 30, 'Ball:', (255,255,255), 310, 300, False)
text_color_rect = render_text_speech('pixel.ttf', 30, color_t_rect, color_rect, 390, 300, False)
particles = []
running = True
while running:
screen.fill((0,0,0))
pygame.draw.rect(screen, (135, 206, 235), button_1)
pygame.draw.rect(screen, color_rect, ball, 3)
screen.blit(*text_rect)
screen.blit(*text_particle)
screen.blit(*text_colors)
screen.blit(*text_color)
screen.blit(*text_color_rect)
textRect = text_color[1]
textRect_rect = text_color_rect[1]
particles.append([[150,200],[random.randint(0,20) / 10 - 1, 1], random.randint(4,6)])
for particle in particles:
particle[0][0] += particle[1][0]
particle[0][1] += particle[1][1]
particle[2] -= 0.1
particle[1][1] += 0.03
pygame.draw.circle(screen, color, [int(particle[0][0]), int(particle[0][1])], int(particle[2]))
if particle[2] <= 0:
particles.remove(particle)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and textRect.collidepoint(event.pos):
current_color += 1
if current_color >= len(colors):
current_color = 0
color_t, color = colors[current_color]
text_color = render_text_speech('pixel.ttf', 30, color_t, color, 180, 300, False)
if event.button == 1 and textRect_rect.collidepoint(event.pos):
current_color_rect += 1
if current_color_rect >= len(colors_rect):
current_color_rect = 0
color_t_rect, color_rect = colors_rect[current_color_rect]
text_color_rect = render_text_speech('pixel.ttf', 30, color_t_rect, color_rect, 390, 300, False)
pygame.display.update()
clock.tick(60)
这两个变量是color
和color_rect
。 注意我说的是变量color
而不是colors
。
将变量def
之外/之上。 这会改变它们的作用域,因此它们可以在同一个.py
代码文件中的所有函数中使用。
color = ()
def method_that_uses_color():
do_something(color)
def colors():
clock = pygame.time.Clock()
button_1 = pygame.Rect(width/2, (height/10), width/4, 60)
button_1.centerx = width/2
colors = [("White", (255, 255, 255)), ("Red", (255, 0, 0)), ("Green", (0, 255, 0)), ("Blue", (0, 0, 255))]
current_color = 0
color_t, color = colors[current_color]
[.......]
如果你能帮忙,我不会这样做。 更好的做法是将依赖项作为方法参数传入,并将唯一计算的输出作为函数结果返回。
def __init__():
color = colors()
do_something(color)
def method_that_uses_color(color):
do_something(color)
def colors():
clock = pygame.time.Clock()
button_1 = pygame.Rect(width/2, (height/10), width/4, 60)
button_1.centerx = width/2
colors = [("White", (255, 255, 255)), ("Red", (255, 0, 0)), ("Green", (0, 255, 0)), ("Blue", (0, 0, 255))]
current_color = 0
color_t, color = colors[current_color]
[.......]
return color
最简单(我认为也是最干净的方法)是使这个函数成为一个类方法并将这些变量存储为成员变量,即self.color
和self.color_rect
。 然后,只要您使用该类的同一个实例,这些变量在该类的所有函数中都是相同的。
您可以在此处了解有关 Python 类的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.