Fixes, player speed

This commit is contained in:
Pascal Engélibert 2024-02-10 13:30:58 +01:00
parent 2863035228
commit a0cebd89fb
2 changed files with 51 additions and 25 deletions

62
game.py
View file

@ -1,17 +1,19 @@
import network, sys, json, pygame import network, sys, json, pygame, random
pygame.init() pygame.init()
address = ("0.0.0.0", 33033) address = ["0.0.0.0", int(sys.argv[1])]
w, h = 640, 480 w, h = 640, 480
size = [w, h] size = [w, h]
black = [0, 0, 0] black = [0, 0, 0]
class Player: class Player:
def __init__(self, address, pos, color): def __init__(self, pid, address, pos, color):
self.pid = pid
self.address = address self.address = address
self.pos = pos self.pos = pos
self.color = color self.color = color
self.speed = [0, 0]
screen = pygame.display.set_mode(size) screen = pygame.display.set_mode(size)
clock = pygame.time.Clock() clock = pygame.time.Clock()
@ -19,40 +21,56 @@ clock = pygame.time.Clock()
sock = network.Sock() sock = network.Sock()
sock.listen(address) sock.listen(address)
me = Player(address, [10, 10], [255, 0, 0]) me = Player(random.randint(0,65535), address, [10, 10], [255, 0, 0])
players = {} players = {me.pid: me}
def send_all(msg): def send_all(msg):
msg = json.dumps(msg) msg = json.dumps(msg)
for pid in players: for pid in players:
sock.send(msg, players[pid].address) sock.send(msg, players[pid].address)
sock.send(json.dumps({"type":"join","pos":me.pos,"color":me.color}), ("192.168.0.255", 33033)) 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])
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
sys.exit() sys.exit()
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_right: if event.key == pygame.K_RIGHT:
me.pos[0] += 1 me.speed[0] = 1
send_all({"type":"move", "x":me.pos[0], "y":me.pos[1]}) send_all({"type":"move", "pid":me.pid, "speed":me.speed})
elif event.key == pygame.K_left: elif event.key == pygame.K_LEFT:
me.pos[0] -= 1 me.speed[0] = -1
send_all({"type":"move", "x":me.pos[0], "y":me.pos[1]}) send_all({"type":"move", "pid":me.pid, "speed":me.speed})
elif event.key == pygame.K_bottom: elif event.key == pygame.K_DOWN:
me.pos[1] += 1 me.speed[1] = 1
send_all({"type":"move", "x":me.pos[0], "y":me.pos[1]}) send_all({"type":"move", "pid":me.pid, "speed":me.speed})
elif event.key == pygame.K_top: elif event.key == pygame.K_UP:
me.pos[1] -= 1 me.speed[1] = -1
send_all({"type":"move", "x":me.pos[0], "y":me.pos[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})
for (r, c) in sock.get(): for (r, c) in sock.get():
r = json.loads(r.decode()) r = json.loads(r.decode())
if r["pid"] == me.pid:
continue
if r["type"] == "join": if r["type"] == "join":
players[c] = Player(c, r["pos"], r["color"]) 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"])
elif r["type"] == "move": elif r["type"] == "move":
players[c].pos[0] += r["x"] players[r["pid"]].speed = r["speed"]
players[c].pos[1] += r["y"] 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"])
screen.fill(black) screen.fill(black)
@ -60,6 +78,8 @@ while True:
for pid in players: for pid in players:
player = players[pid] player = players[pid]
player.pos[0] += player.speed[0]
player.pos[1] += player.speed[1]
pygame.draw.rect(screen, player.color, (*player.pos, 16, 16)) pygame.draw.rect(screen, player.color, (*player.pos, 16, 16))
pygame.display.flip() pygame.display.flip()

View file

@ -1,6 +1,8 @@
import socket import socket, os, sys
from threading import Thread, Lock from threading import Thread, Lock
NETTRACE = str(os.environ.get("NETTRACE", "0")) == "1"
class Sock: class Sock:
def __init__(self): def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@ -13,10 +15,12 @@ class Sock:
def send(self, message, address): def send(self, message, address):
if type(message) == str: if type(message) == str:
message = message.encode() message = message.encode()
self.sock.sendto(message, address) if NETTRACE:
print("Send to", tuple(address), ":", message, file=sys.stderr)
self.sock.sendto(message, tuple(address))
def listen(self, address, length=65535): def listen(self, address, length=65535):
self.sock_thread = SockThread(self, address, length) self.sock_thread = SockThread(self, tuple(address), length)
self.sock_thread.start() self.sock_thread.start()
def get(self): def get(self):
@ -32,10 +36,12 @@ class SockThread(Thread):
Thread.__init__(self) Thread.__init__(self)
self.sock = sock self.sock = sock
self.length = length self.length = length
self.sock.sock.bind(address) self.sock.sock.bind(tuple(address))
def run(self): def run(self):
while True: while True:
r = self.sock.sock.recvfrom(self.length) r = self.sock.sock.recvfrom(self.length)
if NETTRACE:
print("Rec from", r[1], ":", r[0], file=sys.stderr)
with self.sock.queue_lock: with self.sock.queue_lock:
self.sock.queue.append(r) self.sock.queue.append(r)