How to set up a server for a local wifi multiplayer game for python

大兔子大兔子 提交于 2021-02-11 15:34:24

问题


I'm making a Cards Against Humanity game (but nicer/family friendly-er) and I have it set up where all I need to do is run player functions and then a judge function until someone wins. I recently asked another question specifically about my game, but if you know a solid foolproof way to set up a multiplayer game over local wifi, I would love some help. Thanks!


回答1:


Have you tried network zero? It's an amazing networking library that I use all the time.

Install:

pip install networkzero

PyPI link: https://pypi.org/project/networkzero/

Docs: https://networkzero.readthedocs.io/en/latest/

Code sample (from their doc page):

Machine/process A:

import networkzero as nw0

address = nw0.advertise("hello")
while True:
    name = nw0.wait_for_message_from(address)
    nw0.send_reply_to(address, "Hello " + name)

Machine/process B:

import networkzero as nw0

hello = nw0.discover("hello")
reply = nw0.send_message_to(hello, "World!")
print(reply)
reply = nw0.send_message_to(hello, "Tim")
print(reply)

This library also supports more than just 2 connections on the local WiFi, read the docs for more info.



来源:https://stackoverflow.com/questions/62844934/how-to-set-up-a-server-for-a-local-wifi-multiplayer-game-for-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!