我正在为我正在处理的游戏编制库存系统,至此我已为系统编写了这些代码块。我希望系统在不使用全局函数的情况下工作,因为我已经读过,应该避免使用全局函数。如何在不使用全局函数的情况下在我的代码中引用局部变量
while not done:
global inventory
pygame.mouse.set_visible(False)
#Event processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
inv_sys.inventory = True
inv_sys.inventory_u()
import pygame
import Constants
clock = pygame.time.Clock()
screen = pygame.display.set_mode(Constants.screen_size)
inv = pygame.image.load("inv.png").convert()
test = False
def uninventory():
global inventory
inventory = False
def inventory_u():
while inventory:
pygame.mouse.set_visible(True)
screen.blit(inv,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
uninventory()
pygame.display.update()
clock.tick(60)
提前谢谢你的帮助!