我正在爲我正在處理的遊戲編制庫存系統,至此我已爲系統編寫了這些代碼塊。我希望系統在不使用全局函數的情況下工作,因爲我已經讀過,應該避免使用全局函數。如何在不使用全局函數的情況下在我的代碼中引用局部變量
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)
提前謝謝你的幫助!