Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

python - My pygame window says "not responding" when I use collidepoint()

I am trying to make a game. In my game there is a shop and a fight mode. When I use collidepoint(), and if collidepoint() is True, then it calls a function. But when I call the function the window says, not reponding. I can't figure out this problem, I hope you can fix it.

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()
                    
            

The problem is in the fight() function. There is no error.

question from:https://stackoverflow.com/questions/65848438/my-pygame-window-says-not-responding-when-i-use-collidepoint

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try calling the event in different function for example:

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()

don't forget to add the pygame.event.get() at the beginning of the while loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...