I'm trying to create a simple multiplayer game in Python using pygame and socket modules. It just consists in two circles that are controlled with the W, A, S, D keys from two different computers.
At first I created a client with the recv() in the middle of the pygame loop. It worked well but the recv() blocked the loop, so the movement of the circles wasn't smooth and I had to increase the velocity to 6 (before it was set to 0.6) in order to have a normal speed. This is the code (I've summarised it a bit):
Client 1st version
#import modules
def main(sock):
pygame.init()
#Display screen and set initial me_x and me_y
vel = 0.6
while True:
keys_pressed = pygame.key.get_pressed()
#Change me_x and me_y (with A, D, W, S keys)
#Make sure me_x and me_y don't get off the screen
screen.fill(color)
if other_x and other_y:
pygame.draw.circle(screen, colorMe, (other_x, other_y), radi)
pygame.draw.circle(screen, colorOther, (int(me_x), int(me_y)), radi)
pygame.display.flip()
sock.send(int(me_x).to_bytes(3, byteorder = 'big') + int(me_y).to_bytes(3, byteorder = 'big'))
otherPos = sock.recv(BUFSIZ)
other_x = int.from_bytes(otherPos[:3], byteorder = 'big')
other_y = int.from_bytes(otherPos[3:], byteorder = 'big')
print(other_x, other_y)
#CONNECT TO TCP SOCKET
other_x = None
other_y = None
main(client_socket)
Then, I tried to put the recv() in a thread to stop blocking the loop:
Client 2nd version
#import modules
def main(sock):
pygame.init()
#Display screen and set initial me_x and me_y
vel = 0.6
while True:
for i in range(30):
keys_pressed = pygame.key.get_pressed()
#Change me_x and me_y (with A, D, W, S keys)
#Make sure me_x and me_y don't get off the screen
screen.fill(color)
if other_x and other_y:
pygame.draw.circle(screen, colorOther, (other_x, other_y), 15)
pygame.draw.circle(screen, colorMe, (int(me_x), int(me_y)), 15)
pygame.display.flip()
msg = int(me_x).to_bytes(3, byteorder = 'big') + int(me_y).to_bytes(3, byteorder = 'big')
sock.send(msg)
def recv_pos(sock):
while True:
other_pos = sock.recv(BUFSIZ)
other_x = int.from_bytes(other_pos[:3], byteorder = 'big')
other_y = int.from_bytes(other_pos[3:], byteorder = 'big')
print(other_x, other_y)
#CONNECT TO TCP SOCKET
other_x = None
other_y = None
receive_thread = threading.Thread(target = recv_pos, args = (client_socket,))
receive_thread.daemon = True
receive_thread.start()
main(client_socket)
However, when I start 2 instances of the client 2 in two different computers, it gives me an OverflowError:
OverflowError: Python int too large to convert to C long
I added the for i in range(30): because I thought that the server was getting collapsed because there were too much messages being sent at the same time. The output is the same: after 3 seconds more or less, the program crashes giving an OverflowError.
I added a print() statement in both versions just after the recv() to see which values of x and y I was receiving. In the 1st version, all of them were in the width and height range. However, in the 2nd version, 1/5 of the received messages was a big number such as 124381473265. If this number is even bigger, it gives the OverflowError. I don't understand why this is happening: I'm encoding and decoding the messages in the same way in both versions, but one works and the other doesn't.
I'm not including the server code because I don't think it's necessary. It's just just transmits the messages between both clients without modifying them. The server is not the error, as in the 1st client version the messages are sent and received properly.
Any help will be apprecieted. If you need more information about any point just tell me.
Thanks in advance!