I'm making a multiplayer game using Pygame (connection via Socket) and am trying to understand how do I create and update a PyGame screen all the while receiving data from a server telling me to update it. should i create a thread? is there another library i should use? Apologies if this question is redundant or idiotic, I could not find a working solution. Thanks in advance.
this is the client-sided code:
import socket
import sys
import pygame as pg
...
def main():
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect((IP, PORT)) # connect to server
except Exception as error:
print(error) # could be an error connecting to server or establishing the socket
pg.init()
server.send(packed("add_me_lobby")) # sends request to add to lobby
screen = pg.display.set_mode((960, 720))
font = pg.font.Font(None, 32)
clock = pg.time.Clock()
in_lobby = True
finished = False # sets up the pygame window that will be used for the lobby and game
while True:
if in_lobby:
for event in pg.event.get():
if event.type == pg.QUIT:
finished = True
if finished:
break
screen.fill((30, 30, 30))
title_text = font.render("DiceWars", True, BLACK)
screen.blit(title_text, (350, 170)) # creates and renders caption
users_text = font.render("Users Online", True, BLACK)
screen.blit(users_text, (800, 170)) # creates and renders caption
server_cmd = server.recv(BIG_BUFFER).decode() # receives server command
...
pg.display.flip()
clock.tick(30)
pg.quit()
server.close()
if __name__ == '__main__':
main()
(code is not done or whole, only relevant things are included)