pygame中的窗口在等待时冻结 [英] window in pygame freeze while waiting

查看:157
本文介绍了pygame中的窗口在等待时冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的游戏及其在线使用pygame,但问题是,只要游戏的主循环等待服务器上的套接字,它就会冻结. 因此,除非您轮到自己,否则客户端将等待服务器上的套接字,而客户端在等待时不执行任何操作,然后冻结直到获得套接字并轮到它.

I'm using pygame for my game and its online but the problem is that whenever the main loop of the game waits for the socket from the server its freezes. so unless you are doing your turn, the client wait for a socket from the server and its doing nothing while waiting and freeze until its get the socket and do its turn.

因此,我在该站点以及其他一些站点上读了很少的答案和解决方案,而据我所知,在5秒钟不做任何操作后,操作系统认为窗口(已锁定)是什么?所以我创建了keep_run线程,但是它没有做任何更改,窗口仍然冻结,而轮到他了.

So I read few answers and solutions on this site and on some others and from what I understood after 5 seconds of doing nothing the OS thinks the window (locked itself)? so I created the thread keep_run but it does not make any change and the window still freeze while its not his turn.

如果服务器仍然有帮助,也要提及服务器与选择库一起使用,因为keep_run()是我使用的唯一线程.

Also to mention the server works with select library if its help in anyway because keep_run() is the only thread I used.

我的代码中没有包含很多行,因为有太多行了,但这是可能导致它的重要内容的基本结论吗?

I did not include many lines in my code because there are too much but its a basic conclusion of the important stuff that maybe cause it?

import sockets
import pygame
from threading import Thread
def keep_run():
    clock = pygame.time.Clock()
    fps = 60
    while True:
        pygame.event.pump()
        clock.tick(fps)

pygame.init()
keep_running = Thread(target=keep_run)
keep_running.setDaemon(True)
keep_running.start()


while Game_run:

   #the main loop
   server_command = client_socket.recv(1024)
   if server_command == "move":
       # make your turn
       do_turn()
   elif server_command == "over":
       # finish the game
       finish_game()

示例图片: https://i.stack.imgur.com/b4Qx8. png

推荐答案

您必须定期调用pygame.event.pump()(由pygame.event.get()pygame.event.clear()pygame.event.poll()pygame.event.wait()pygame.event.peek()隐式调用) 在初始化视频子系统的线程中 (Pygame是基于SDL构建的,因此链接到SDL文档).

You must call pygame.event.pump() (which is implicitly called by pygame.event.get(), pygame.event.clear(), pygame.event.poll(), pygame.event.wait() or pygame.event.peek()) regularly in the thread that initialized the video subsystem (Pygame is built on SDL, hence the link to SDL documentation).

我还建议您不要使用pygame.event.pump(),而是使用pygame.event.get()pygame.event.poll()正确处理所有事件. 此处.

I would also suggest that you don't use pygame.event.pump() and instead handle all events properly with pygame.event.get() or pygame.event.poll(). More about this here.

这篇关于pygame中的窗口在等待时冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆