当我使用collidepoint()时,我的pygame窗口显示“无响应”

2024-09-30 22:10:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个游戏。在我的游戏中有一个商店和战斗模式。当我使用collidepoint()时,如果collidepoint()True,那么它调用一个函数。但当我调用函数时,窗口会显示,不重新发送。我想不出这个问题,希望你能解决

def lvl1():
    screen.blit(stickmanlvlim,(0,0))
    pygame.display.update()
def lvl2():
    screen.blit(stickmanlvlim,(0,0))
    pygame.display.update()
def lvl3():
    screen.blit(stickmanlvlim,(0,0))
    pygame.display.update()
        


def fight(events):
    lvl1R = pygame.Rect((20,335),(50,50))
    lvl2R = pygame.Rect((170,335),(50,50))
    lvl3R = pygame.Rect((320,335),(50,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()
                    
            

问题出在fight()函数中。没有错误


Tags: recteventtrue游戏ifdefdisplayupdate
1条回答
网友
1楼 · 发布于 2024-09-30 22:10:31

尝试在不同的函数中调用事件,例如:

def fight(events):
  lvl1R = pygame.Rect((20,335),(50,50))
  lvl2R = pygame.Rect((170,335),(50,50))
  lvl3R = pygame.Rect((320,335),(50,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()

相关问题 更多 >