1

I have been trying to create a two player game in pygame. I did some research on sockets and have been trying to put them into the game. This is my setup sockets function in the server, I won't put all the game code in since its quite long:

def create_sockets(self): self.ip = "192.168.1.68" self.port = 8888 self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.bind((self.ip, self.port)) self.server_socket.listen(5) self.conn, self.addr = self.server_socket.accept() 

But through trial and error, when server_socket.accept() is run, I get a black screen and a color wheel (I'm on a mac). Why is this happening? the same code works fine in my server test from before. Since I am very new to sockets please correct me on any mistakes/bad practice

Thanks in advance

3
  • 2
    More context, please -- how and when do you call this function? You can generally expect that socket.accept() will block until a connection is accepted. If you call it from the main game loop thread, this means that the loop will temporarily stop executing and the game will "freeze". Does anything happen if you connect to the server while the game is frozen? Commented Nov 28, 2013 at 21:24
  • 1
    You probably need a subprocess or thread, to keep your main process/thread from blocking the GUI events. Commented Nov 28, 2013 at 21:42
  • Isn't it socket.Socket and not socket.socket? Also, though I may be wrong, it seems likely that the third part of the ip address should be 0, not 1, if you want to connect to someone nearby. Commented Dec 2, 2013 at 23:35

1 Answer 1

1

+1 to svk. Whenever I use Socket in pygame, I use threading to make it asynchronous. I'm pretty sure both .listen() and .accept() will freeze your program in a loop as those methods are waiting for something to happen.

Here is the full code for a pong clone in pygame. This is a "dumb" server, meaning it is not handling game logic, just sharing data with clients.

This may not be your preferred approach as a whole, but it does show how to handle asynchronous connection/listening. As well as using pickle to encode/decode whatever data type you want. That way you can kick lists around and stuff.

# Server example: from threading import Thread import socket, pickle, logging s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("0.0.0.0", 12354)) logging.basicConfig(format='%(asctime)s:%(levelname)s:%(lineno)s %(message)s', level=logging.DEBUG) client_list = [] max_clients = 2 global started started = 0 class Client(): def __init__(self, conn = ''): self.conn = conn # add to global clients list client_list.append(self) self.client_thread = Thread(target = self.process_messages) self.client_thread.start() def process_messages(self): while True: try: data = self.conn.recv(1024) # send to all in client_list except self data = pickle.loads(data) data.append(started) logging.info("Sending Data: {0}".format(data)) data = pickle.dumps(data) for client in client_list: if client != self: client.conn.sendall(data) data = "" except ConnectionResetError: logging.debug("Client Disconnected") break def connection_manager(): while len(client_list) < max_clients: logging.info('Listening for connections...') s.listen(1) conn, addr = s.accept() logging.info("Client connected: {0}".format(addr)) x = Client(conn) logging.debug(client_list) logging.warning("Max clients reached") logging.info("No longer listening..") started = 0 accept_connections_thread = Thread(target = connection_manager) accept_connections_thread.start() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.