template, fixes

This commit is contained in:
Pascal Engélibert 2024-02-17 18:05:21 +01:00
parent a8009f744d
commit ac045c3e0e
7 changed files with 238 additions and 37 deletions

14
chat.py Normal file
View file

@ -0,0 +1,14 @@
import network, sys, time
sock = network.Sock()
port = 33077
if sys.argv[1] == "s":
sock.listen(["0.0.0.0", port])
while True:
for d, a in sock.get():
print(a, d)
time.sleep(0.1)
elif sys.argv[1] == "c":
while True:
sock.send(input(), ["192.168.0.255", port])

View file

@ -12,5 +12,5 @@ if sys.argv[1] == "s":
time.sleep(0.1) time.sleep(0.1)
elif sys.argv[1] == "c": elif sys.argv[1] == "c":
while True: while True:
sock.send(str(time.time()), (sys.argv[3], port)) sock.send(time.time(), (sys.argv[3], port))
time.sleep(1) time.sleep(1)

8
demo1.py Normal file
View file

@ -0,0 +1,8 @@
import network, sys, time
sock = network.Sock()
destination = ["192.168.0.255", 33077]
while True:
message = input("envoyer: ")
sock.send(message, destination)

10
demo2.py Normal file
View file

@ -0,0 +1,10 @@
import network, sys, time
sock = network.Sock()
address = ("0.0.0.0", 33077)
sock.listen(address)
while True:
for d, a in sock.get():
print(a, d)
time.sleep(0.1)

106
game.py
View file

@ -1,4 +1,4 @@
import network, sys, json, pygame, random, xlog import network, sys, pygame, random, xlog, time, colorsys
pygame.init() pygame.init()
@ -7,26 +7,37 @@ w, h = 640, 480
size = [w, h] size = [w, h]
black = [0, 0, 0] black = [0, 0, 0]
def random_color():
return [int(i*255) for i in colorsys.hsv_to_rgb(random.random(), 0.8, 1)]
def normalize(n):
if n > 0:
return 1
if n < 0:
return -1
return 0
def new_oid(): def new_oid():
return random.randint(0,2147483648) return random.randint(0,2147483648)
class Obj: class Obj:
def __init__(self, oid): def __init__(self, oid):
self.oid = oid self.oid = oid
def damage(self, dmg): pass def damage(self, dmg, author=None): pass
def update(self): pass def update(self): pass
def draw(self, surface): pass def draw(self, surface): pass
def print_debug(self, lines): pass def print_debug(self, lines): pass
class Player(Obj): class Player(Obj):
def __init__(self, oid, address, pos, color): def __init__(self, oid, address, pos, color, life=100, score=0):
Obj.__init__(self, oid) Obj.__init__(self, oid)
self.address = address self.address = address
self.pos = pos self.pos = pos
self.color = color self.color = color
self.score = score
self.speed = [0, 0] self.speed = [0, 0]
self.life = 100 self.life = life
self.direction = [1, 0] self.direction = [0, 1]
def throw_energy_ball(self): def throw_energy_ball(self):
oid = new_oid() oid = new_oid()
@ -34,7 +45,7 @@ class Player(Obj):
pos[0] += self.direction[0] * 16 pos[0] += self.direction[0] * 16
pos[1] += self.direction[1] * 16 pos[1] += self.direction[1] * 16
speed = [self.speed[0]+self.direction[0]*4, self.speed[1]+self.direction[1]*4] speed = [self.speed[0]+self.direction[0]*4, self.speed[1]+self.direction[1]*4]
objects[oid] = EnergyBall(oid, pos, speed) objects[oid] = EnergyBall(oid, pos, speed, self.oid)
def stab(self): def stab(self):
for oid in players: for oid in players:
@ -42,30 +53,37 @@ class Player(Obj):
continue continue
player = players[oid] player = players[oid]
if abs(player.pos[0]-self.pos[0]) < 32 and abs(player.pos[1]-self.pos[1]) < 32: if abs(player.pos[0]-self.pos[0]) < 32 and abs(player.pos[1]-self.pos[1]) < 32:
player.damage(10) player.damage(10, self.oid)
def damage(self, dmg): def damage(self, dmg, author=None):
self.life -= dmg self.life -= dmg
if self.life <= 0: if self.life <= 0:
print("die!") self.speed = [0, 0]
self.pos = [16, 16]
self.life = 100
if author in players:
players[author].score += 1
def update(self): def update(self):
if self.speed[0] != 0 or self.speed[1] != 0:
self.direction = [normalize(self.speed[0]), normalize(self.speed[1])]
self.pos[0] += self.speed[0] self.pos[0] += self.speed[0]
self.pos[1] += self.speed[1] self.pos[1] += self.speed[1]
def draw(self, surface): def draw(self, surface):
pygame.draw.rect(surface, self.color, (self.pos[0]-8, self.pos[1]-8, 16, 16)) pygame.draw.rect(surface, self.color, (self.pos[0]-8, self.pos[1]-8, 16, 16))
lifebar = int(32*self.life/100) lifebar = int(16*self.life/100)
pygame.draw.rect(surface, (64,255,64), (self.pos[0]-lifebar//2+8, self.pos[1]+6, lifebar, 4)) pygame.draw.rect(surface, (64,255,64), (self.pos[0]-8, self.pos[1]-16, lifebar, 4))
def print_debug(self, lines): def print_debug(self, lines):
lines.append("Player {}: {}".format(self.oid, obj.address)) lines.append("Player {}: {} / score {}".format(self.oid, obj.address, obj.score))
class EnergyBall(Obj): class EnergyBall(Obj):
def __init__(self, oid, pos, speed): def __init__(self, oid, pos, speed, thrower):
Obj.__init__(self, oid) Obj.__init__(self, oid)
self.pos = pos self.pos = pos
self.speed = speed self.speed = speed
self.thrower = thrower
def update(self): def update(self):
self.pos[0] += self.speed[0] self.pos[0] += self.speed[0]
@ -75,14 +93,14 @@ class EnergyBall(Obj):
continue continue
obj = objects[oid] obj = objects[oid]
if abs(self.pos[0]-obj.pos[0])<8 and abs(self.pos[1]-obj.pos[1])<8: if abs(self.pos[0]-obj.pos[0])<8 and abs(self.pos[1]-obj.pos[1])<8:
obj.damage(10) obj.damage(10, self.thrower)
removable.append(self.oid) removable.append(self.oid)
return return
def draw(self, surface): def draw(self, surface):
pygame.draw.rect(surface, (128,255,255), (self.pos[0]-2, self.pos[1]-2, 4, 4)) pygame.draw.rect(surface, (128,255,255), (self.pos[0]-2, self.pos[1]-2, 4, 4))
def damage(self, dmg): def damage(self, dmg, author=None):
if dmg > 0: if dmg > 0:
removable.append(self.oid) removable.append(self.oid)
@ -95,16 +113,30 @@ clock = pygame.time.Clock()
sock = network.Sock() sock = network.Sock()
sock.listen(address) sock.listen(address)
me = Player(new_oid(), address, [10, 10], [255, 0, 0]) me = Player(new_oid(), address, [random.randint(16, w-16), random.randint(16, h-16)], random_color())
objects = {me.oid: me} objects = {me.oid: me}
players = {me.oid: me} players = {me.oid: me}
removable = [] removable = []
def send_all(msg): def send_all(msg):
msg = json.dumps(msg)
for oid in players: for oid in players:
if oid != me.oid:
sock.send(msg, objects[oid].address) sock.send(msg, objects[oid].address)
def make_recap():
pl = []
for oid in players:
player = players[oid]
pl.append({
"oid": oid,
"pos": player.pos,
"color": player.color,
"address": player.address,
"life": player.life,
"score": player.score
})
return {"type":"recap", "players":pl, "oid":me.oid}
def remove_objects(): def remove_objects():
for oid in removable: for oid in removable:
if oid in objects: if oid in objects:
@ -113,8 +145,11 @@ def remove_objects():
players.pop(oid) players.pop(oid)
removable.clear() removable.clear()
sock.send(json.dumps({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"oid":me.oid}), ["192.168.0.255", 33033]) sock.send({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"oid":me.oid}, ["192.168.0.255", 33033])
sock.send(json.dumps({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"oid":me.oid}), ["127.0.0.1", 33033]) sock.send({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"oid":me.oid}, ["192.168.0.255", 33033])
sock.send({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"oid":me.oid}, ["127.0.0.1", 33033])
last_recap = 0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@ -125,19 +160,15 @@ while True:
send_all({"type":"quit", "oid":me.oid}) send_all({"type":"quit", "oid":me.oid})
sys.exit() sys.exit()
if event.key == pygame.K_RIGHT: if event.key == pygame.K_RIGHT:
me.direction = [1, 0]
me.speed[0] = 2 me.speed[0] = 2
send_all({"type":"move", "oid":me.oid, "speed":me.speed}) send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_LEFT: elif event.key == pygame.K_LEFT:
me.direction = [-1, 0]
me.speed[0] = -2 me.speed[0] = -2
send_all({"type":"move", "oid":me.oid, "speed":me.speed}) send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_DOWN: elif event.key == pygame.K_DOWN:
me.direction = [0, 1]
me.speed[1] = 2 me.speed[1] = 2
send_all({"type":"move", "oid":me.oid, "speed":me.speed}) send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_UP: elif event.key == pygame.K_UP:
me.direction = [0, -1]
me.speed[1] = -2 me.speed[1] = -2
send_all({"type":"move", "oid":me.oid, "speed":me.speed}) send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_SPACE: elif event.key == pygame.K_SPACE:
@ -160,16 +191,14 @@ while True:
me.speed[1] = 0 me.speed[1] = 0
send_all({"type":"move", "oid":me.oid, "speed":me.speed}) send_all({"type":"move", "oid":me.oid, "speed":me.speed})
for (r, c) in sock.get(): for (r, c) in sock.get():
r = json.loads(r.decode())
if r["oid"] == me.oid: if r["oid"] == me.oid:
continue continue
if r["type"] == "join": if r["type"] == "join":
pl = [] sock.send(make_recap(), [c[0], r["port"]])
for oid in players: sock.send(make_recap(), [c[0], r["port"]])
player = players[oid] new_player = Player(r["oid"], [c[0], r["port"]], r["pos"], r["color"])
pl.append({"oid":oid, "pos":player.pos, "color":player.color, "address":player.address}) players[r["oid"]] = new_player
sock.send(json.dumps({"type":"recap", "players":pl, "oid":me.oid}), [c[0], r["port"]]) objects[r["oid"]] = new_player
players[r["oid"]] = Player(r["oid"], [c[0], r["port"]], r["pos"], r["color"])
elif r["type"] == "move": elif r["type"] == "move":
players[r["oid"]].speed = r["speed"] players[r["oid"]].speed = r["speed"]
elif r["type"] == "recap": elif r["type"] == "recap":
@ -178,12 +207,21 @@ while True:
address = player["address"] address = player["address"]
if address[0] == "0.0.0.0": if address[0] == "0.0.0.0":
address = [c[0], c[1]] address = [c[0], c[1]]
objects[player["oid"]] = Player(player["oid"], address, player["pos"], player["color"]) new_player = Player(player["oid"], address, player["pos"], player["color"],
players[player["oid"]] = objects[player["oid"]] life=player["life"],
score=player["score"]
)
objects[player["oid"]] = new_player
players[player["oid"]] = new_player
elif c[0] == player["address"][0]:
players[player["oid"]].pos = player["pos"]
players[player["oid"]].speed = player["speed"]
elif r["type"] == "quit": elif r["type"] == "quit":
removable.append(r["oid"]) removable.append(r["oid"])
elif r["type"] == "stab": elif r["type"] == "stab":
objects[r["oid"]].stab() objects[r["oid"]].stab()
elif r["type"] == "throw":
objects[r["oid"]].throw_energy_ball()
remove_objects() remove_objects()
@ -200,5 +238,9 @@ while True:
xlog.display(screen, lines) xlog.display(screen, lines)
if time.time() > last_recap + 5:
send_all(make_recap())
last_recap = time.time() + 5
pygame.display.flip() pygame.display.flip()
clock.tick(30) clock.tick(30)

View file

@ -1,4 +1,4 @@
import socket, os, sys import socket, os, sys, json
from threading import Thread, Lock from threading import Thread, Lock
NETTRACE = str(os.environ.get("NETTRACE", "0")) == "1" NETTRACE = str(os.environ.get("NETTRACE", "0")) == "1"
@ -12,19 +12,25 @@ class Sock:
self.queue_lock = Lock() self.queue_lock = Lock()
self.sock_thread = None self.sock_thread = None
def send(self, message, address): def send_raw(self, message, address):
if type(message) == str: if type(message) == str:
message = message.encode() message = message.encode()
if NETTRACE: if NETTRACE:
print("Send to", tuple(address), ":", message, file=sys.stderr) print("Send to", tuple(address), ":", message, file=sys.stderr)
self.sock.sendto(message, tuple(address)) self.sock.sendto(message, tuple(address))
def send(self, message, address):
message = json.dumps(message)
if NETTRACE:
print("Send to", tuple(address), ":", message, file=sys.stderr)
self.sock.sendto(message.encode(), tuple(address))
def listen(self, address, length=65535): def listen(self, address, length=65535):
self.sock_thread = SockThread(self, tuple(address), length) self.sock_thread = SockThread(self, tuple(address), length)
self.sock_thread.setDaemon(True) self.sock_thread.setDaemon(True)
self.sock_thread.start() self.sock_thread.start()
def get(self): def get_raw(self):
if len(self.queue) == 0: if len(self.queue) == 0:
return [] return []
with self.queue_lock: with self.queue_lock:
@ -32,6 +38,19 @@ class Sock:
self.queue = [] self.queue = []
return queue return queue
def get(self):
if len(self.queue) == 0:
return []
queue = []
with self.queue_lock:
for r in self.queue:
try:
queue.append([json.loads(r[0].decode()), r[1]])
except:
pass
self.queue.clear()
return queue
class SockThread(Thread): class SockThread(Thread):
def __init__(self, sock, address, length): def __init__(self, sock, address, length):
Thread.__init__(self) Thread.__init__(self)

108
template.py Normal file
View file

@ -0,0 +1,108 @@
import network, sys, pygame, random, xlog, time, colorsys
pygame.init()
w, h = 640, 480 # Taille de la fenêtre
size = [w, h]
black = [0, 0, 0] # Couleur noire
# Créer une couleur aléatoire
def random_color():
return [int(i*255) for i in colorsys.hsv_to_rgb(random.random(), 0.8, 1)]
# Créer un identifiant d'objet aléatoire
def new_oid():
return random.randint(0,2147483648)
# Définit les méthodes par défaut d'un objet
class Obj:
def __init__(self, oid):
self.oid = oid
def update(self): pass
def draw(self, surface): pass
def print_debug(self, lines): pass
# Définit un joueur
class Player(Obj):
def __init__(self, oid, pos, color):
Obj.__init__(self, oid)
self.pos = pos
self.speed = [0, 0]
self.color = color
# Mise à jour de l'état du joueur
def update(self):
self.pos[0] += self.speed[0]
self.pos[1] += self.speed[1]
# Dessiner le joueur
def draw(self, surface):
pygame.draw.rect(surface, self.color, (self.pos[0]-8, self.pos[1]-8, 16, 16))
# Afficher le texte d'information
def print_debug(self, lines):
lines.append("Player {}".format(self.oid))
screen = pygame.display.set_mode(size) # définir la taille de la fenêtre
clock = pygame.time.Clock()
# Créer notre propre joueur
me = Player(new_oid(), [random.randint(16, w-16), random.randint(16, h-16)], random_color())
objects = {me.oid: me} # dictionnaire de tous les objets
players = {me.oid: me} # dictionnaire de tous les joueurs (qui sont aussi des objets)
removable = [] # identifiants des objets à supprimer
# Supprimer les objets à supprimer
def remove_objects():
for oid in removable:
if oid in objects:
objects.pop(oid)
if oid in players:
players.pop(oid)
removable.clear()
# Boucle principale
while True:
# Lecture des événements
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if event.key == pygame.K_RIGHT:
me.speed[0] = 2
elif event.key == pygame.K_LEFT:
me.speed[0] = -2
elif event.key == pygame.K_DOWN:
me.speed[1] = 2
elif event.key == pygame.K_UP:
me.speed[1] = -2
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
me.speed[0] = 0
elif event.key == pygame.K_LEFT:
me.speed[0] = 0
elif event.key == pygame.K_DOWN:
me.speed[1] = 0
elif event.key == pygame.K_UP:
me.speed[1] = 0
remove_objects()
screen.fill(black) # colorier le fond en noir
# Mettre à jour tous les objets
lines = []
for oid in objects:
obj = objects[oid]
obj.update()
obj.draw(screen)
obj.print_debug(lines)
remove_objects()
xlog.display(screen, lines) # afficher le texte d'information
pygame.display.flip() # mettre à jour l'écran
clock.tick(30) # attendre un peu