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.
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.
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()
image of the example: https://i.stack.imgur.com/b4Qx8.png
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…