python - Pygame窗口一直冻结
问题描述
我正在 pygame 中制作一个小程序,但它一直冻结并说“没有响应”,即使程序仍在运行并调用 pygame.draw 函数。
if timer==0:
mouseMoved=False
import ctypes
ctypes.windll.user32.SetProcessDPIAware()
true_res = (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
disp = pygame.display.set_mode(true_res,pygame.FULLSCREEN)
del ctypes
color=(0,0,0)
pos=[50,60]
pygame.Surface.fill(disp, (255,255,255))
#mouse.move(mPos[0], mPos[1])
firstMove=True
x, y = pygame.display.get_surface().get_size()
while not mouseMoved:
#mouse.move(mPos[0], mPos[1])
mouseMoved=False
pMouse=mPos
#print(pMouse)
mPos=mouse.get_position()
#print(pos)
if pos[0]+4>=x:
horiz=-1
color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
elif pos[0]-4<=0:
horiz=1
color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
if pos[1]-4<=0:
vert=1
color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
elif pos[1]+4>=y:
vert=-1
color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
pos[0]+=horiz
pos[1]+=vert
#pygame.Surface.fill(disp, (250,250,250))
pygame.draw.circle(disp, color, (pos[0], pos[1]), 9)
pygame.display.update()
#time.sleep(0.01)
if not pMouse==mPos:
if firstMove:
firstMove=False
else:
timer = 360
mouseMoved=True
#pygame.display.iconify()
#disp = pygame.display.set_mode((200,200))
pygame.display.quit()
#pygame.display.update()
print(f"moved from {mPos} to {pMouse}")
这个东西应该一直运行,直到鼠标移开显示器。
解决方案
在您的主 while 循环中,添加类似这样的内容,它应该允许调整屏幕大小和移动屏幕。这取决于您如何设置原始屏幕,我们无法从您的代码示例中看到或运行。
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
elif event.type == pg.VIDEORESIZE:
self.height = event.h
self.width = event.w
self.screen = pg.display.set_mode((self.width, self.height), pg.RESIZABLE)
推荐阅读
- r - 将来自多个参数的值添加到最终总和
- c - 如何在 Kotlin/Native 中从 C 导入生成的 Kotlin 类?
- python - Pandas 散点图上不显示 X 轴标签和次要刻度标签
- javascript - 以编程方式输入击键 javascript
- java - 如果超过 AWS Neptune Server 的容量,是否限制请求数量(读取、更新、删除)?
- html - 为什么 css 没有应用于具有相同类的元素?
- c# - Blazor:如何检查 OnFieldChanged 中的字段是否有效
- r - 每次在列中出现一个值时,有没有办法增加一个数字?
- google-analytics - Google Analytics - 自定义维度客户 ID 未显示
- api - 停止或禁用 API REST WSO2 ESB 的重试次数