问题描述
我正在尝试制作游戏。在我的游戏中有一个商店和一个战斗模式。当我使用 collidepoint()
时,如果 collidepoint()
是 True
,那么它会调用一个函数。但是当我调用该函数时,窗口说,不回应。我想不通这个问题,希望你能解决它。
def lvl1():
screen.blit(stickmanlvlim,(0,0))
pygame.display.update()
def lvl2():
screen.blit(stickmanlvlim,0))
pygame.display.update()
def lvl3():
screen.blit(stickmanlvlim,0))
pygame.display.update()
def fight(events):
lvl1R = pygame.Rect((20,335),(50,50))
lvl2R = pygame.Rect((170,50))
lvl3R = pygame.Rect((320,50))
world1setup()
fight = True
while fight:
for event in events:
if event.type == pygame.QUIT:
fight = False
if event.type == pygame.MOUSEBUTTONDOWN:
spot2 = pygame.mouse.get_pos()
if lvl1R.collidepoint(spot2):
lvl1()
if lvl2R.collidepoint(spot2):
lvl2()
if lvl3R.collidepoint(spot2):
lvl3()
解决方法
尝试在不同的函数中调用事件,例如:
def fight(events):
lvl1R = pygame.Rect((20,335),(50,50))
lvl2R = pygame.Rect((170,50))
lvl3R = pygame.Rect((320,50))
world1setup()
fight = True
while fight:
spot2 = pygame.mouse.get_pos()
if lvl1R.collidepoint(spot2):
lvl1()
if lvl2R.collidepoint(spot2):
lvl2()
if lvl3R.collidepoint(spot2):
lvl3()
def main():
run = True
while run:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
fight(events)
main()
不要忘记在 while 循环的开头添加 pygame.event.get()
。