0

I am having an error with having 2 sprites show up on the screen. I want them to be able to connect seperately and have it so it is like a 2 player real movement engine. I am using sockets and pygame.

I tried to set it up so servers would send info too and fro. Currently I am having a broken pipe errno 33. How would I achieve my goal and how would I tweak my code?

#Server.py
import socket
soc = socket.socket()
hostname = socket.gethostname()
adress = socket.gethostbyname(hostname)
port = 1111
soc.bind((hostname, port))
soc.listen(2)
connection, adress = soc.accept()
print("Accepted and running")
while True:
    OtherX = connection.recv(1024)
    OtherY = connection.recv(1024)
    soc.send(OtherX)
    soc.send(OtherY)```
import socket, pygame, sys
soc = socket.socket()
hostname = socket.gethostname()
adress = socket.gethostbyname(hostname)
ServerHost = input("Enter requested server's ip: ")
port = 1111
soc.connect((ServerHost, port))
def main(): 
    x=100
    y=100
    DISPLAY = pygame.display.set_mode((600, 600))
    pygame.display.set_caption("TSMash!")
    BLUE = (0, 0, 255)
    WHITE = (255, 255, 255)
    RED = (255, 0, 0)
    while True:
        DISPLAY.fill(WHITE)
        pygame.draw.rect(DISPLAY,BLUE,(x,y,50,50))
        key = pygame.key.get_pressed()
        if key[pygame.K_w]:
            y -= 1.5
        if key[pygame.K_s]:
            y += 1.5
        if key[pygame.K_d]:
            x += 1.5
        if key[pygame.K_a]:
            x -= 1.5
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()
        pygame.time.delay(10)
main()
jsotola
  • 2,238
  • 1
  • 10
  • 22
  • You are only listening for one client. If you want to handle multiple clients, you will probably have to go to a threaded model. Every time `accept` returns a new socket, you have to go handle it, while still handling all of the others. And I don't see that your client ever sends anything. If you're serious about this, you need to look at the `twisted` package. It's the best socket comms package available, in my opinion. – Tim Roberts Mar 28 '22 at 05:23
  • Maybe practice socket programming with a chat system, and then once you understand sockets well, you can make a game – user253751 Mar 28 '22 at 08:36

0 Answers0