jsb-netgame/game.py

87 lines
2.7 KiB
Python
Raw Normal View History

2024-02-10 12:30:58 +00:00
import network, sys, json, pygame, random
2024-02-03 17:57:23 +00:00
pygame.init()
2024-02-10 12:30:58 +00:00
address = ["0.0.0.0", int(sys.argv[1])]
2024-02-03 17:57:23 +00:00
w, h = 640, 480
size = [w, h]
black = [0, 0, 0]
class Player:
2024-02-10 12:30:58 +00:00
def __init__(self, pid, address, pos, color):
self.pid = pid
2024-02-03 17:57:23 +00:00
self.address = address
self.pos = pos
self.color = color
2024-02-10 12:30:58 +00:00
self.speed = [0, 0]
2024-02-03 17:57:23 +00:00
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
sock = network.Sock()
sock.listen(address)
2024-02-10 12:30:58 +00:00
me = Player(random.randint(0,65535), address, [10, 10], [255, 0, 0])
players = {me.pid: me}
2024-02-03 17:57:23 +00:00
def send_all(msg):
msg = json.dumps(msg)
for pid in players:
sock.send(msg, players[pid].address)
2024-02-10 12:30:58 +00:00
sock.send(json.dumps({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"pid":me.pid}), ["192.168.0.255", 33033])
sock.send(json.dumps({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"pid":me.pid}), ["127.0.0.1", 33033])
2024-02-03 17:57:23 +00:00
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
2024-02-10 12:30:58 +00:00
if event.key == pygame.K_RIGHT:
me.speed[0] = 1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
elif event.key == pygame.K_LEFT:
me.speed[0] = -1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
elif event.key == pygame.K_DOWN:
me.speed[1] = 1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
elif event.key == pygame.K_UP:
me.speed[1] = -1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or event.key == pygame.K_DOWN or event.key == pygame.K_UP:
me.speed[0] = 0
me.speed[1] = 0
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
2024-02-03 17:57:23 +00:00
for (r, c) in sock.get():
r = json.loads(r.decode())
2024-02-10 12:30:58 +00:00
if r["pid"] == me.pid:
continue
2024-02-03 17:57:23 +00:00
if r["type"] == "join":
2024-02-10 12:30:58 +00:00
pl = []
for pid in players:
player = players[pid]
pl.append({"pid":pid, "pos":player.pos, "color":player.color, "address":player.address})
sock.send(json.dumps({"type":"recap", "players":pl, "pid":me.pid}), [c[0], r["port"]])
players[r["pid"]] = Player(r["pid"], [c[0], r["port"]], r["pos"], r["color"])
2024-02-03 17:57:23 +00:00
elif r["type"] == "move":
2024-02-10 12:30:58 +00:00
players[r["pid"]].speed = r["speed"]
elif r["type"] == "recap":
for player in r["players"]:
if not player["pid"] in players:
players[player["pid"]] = Player(player["pid"], player["address"], player["pos"], player["color"])
2024-02-03 17:57:23 +00:00
screen.fill(black)
pygame.draw.rect(screen, me.color, (*me.pos, 16, 16))
for pid in players:
player = players[pid]
2024-02-10 12:30:58 +00:00
player.pos[0] += player.speed[0]
player.pos[1] += player.speed[1]
2024-02-03 17:57:23 +00:00
pygame.draw.rect(screen, player.color, (*player.pos, 16, 16))
pygame.display.flip()
clock.tick(30)