如何解决当我使用 collidepoint() 时,我的 pygame 窗口显示“无响应”
我正在尝试制作游戏。在我的游戏中有一个商店和一个战斗模式。当我使用 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()
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。