xlog, quit, stab, wip objects

This commit is contained in:
Pascal Engélibert 2024-02-10 18:06:30 +01:00
parent a0cebd89fb
commit 711ea155ac
3 changed files with 114 additions and 34 deletions

138
game.py
View file

@ -1,4 +1,4 @@
import network, sys, json, pygame, random
import network, sys, json, pygame, random, xlog
pygame.init()
@ -8,12 +8,57 @@ size = [w, h]
black = [0, 0, 0]
class Player:
def __init__(self, pid, address, pos, color):
self.pid = pid
def __init__(self, oid, address, pos, color):
self.oid = oid
self.address = address
self.pos = pos
self.color = color
self.speed = [0, 0]
self.life = 100
self.direction = [1, 0]
def stab(self):
for oid in players:
if oid == self.oid:
continue
player = players[oid]
if abs(player.pos[0]-self.pos[0]) < 32 and abs(player.pos[1]-self.pos[1]) < 32:
player.damage(10)
def damage(self, dmg):
self.life -= dmg
def update(self):
self.pos[0] += self.speed[0]
self.pos[1] += self.speed[1]
def draw(self, surface):
pygame.draw.rect(surface, self.color, (self.pos[0]-8, self.pos[1]-8, 16, 16))
lifebar = int(32*self.life/100)
pygame.draw.rect(surface, (64,255,64), (self.pos[0]-lifebar//2+8, self.pos[1]+6, lifebar, 4))
def print_debug(self, lines):
lines.append("Player {}: {}".format(self.oid, obj.address))
class EnergyBall:
def __init__(self, oid, pos, speed):
self.oid = oid
self.pos = pos
self.speed = speed
def update(self):
self.pos[0] += self.speed[0]
self.pos[1] += self.speed[1]
for oid in players:
player = players[oid]
if abs(self.pos[0]-player.pos[0])<8 or abs(self.pos[1]-player.pos[1])<8:
def draw(self, surface):
pygame.draw.rect(surface, (128,255,255), (self.pos[0]-2, self.pos[1]-2, 4, 4))
def print_debug(self, lines):
pass
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
@ -21,66 +66,91 @@ clock = pygame.time.Clock()
sock = network.Sock()
sock.listen(address)
me = Player(random.randint(0,65535), address, [10, 10], [255, 0, 0])
players = {me.pid: me}
me = Player(random.randint(0,2147483648), address, [10, 10], [255, 0, 0])
objects = {me.oid: me}
players = {me.oid: me}
def send_all(msg):
msg = json.dumps(msg)
for pid in players:
sock.send(msg, players[pid].address)
for oid in players:
sock.send(msg, objects[oid].address)
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])
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(json.dumps({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"oid":me.oid}), ["127.0.0.1", 33033])
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
send_all({"type":"quit", "oid":me.oid})
sys.exit()
if event.key == pygame.K_RIGHT:
me.speed[0] = 1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
me.speed[0] = 2
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_LEFT:
me.speed[0] = -1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
me.speed[0] = -2
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_DOWN:
me.speed[1] = 1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
me.speed[1] = 2
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_UP:
me.speed[1] = -1
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
me.speed[1] = -2
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_SPACE:
me.stab()
send_all({"type":"stab", "oid":me.oid})
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:
if event.key == pygame.K_RIGHT:
me.speed[0] = 0
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_LEFT:
me.speed[0] = 0
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_DOWN:
me.speed[1] = 0
send_all({"type":"move", "pid":me.pid, "speed":me.speed})
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_UP:
me.speed[1] = 0
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
for (r, c) in sock.get():
r = json.loads(r.decode())
if r["pid"] == me.pid:
if r["oid"] == me.oid:
continue
if r["type"] == "join":
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"])
for oid in players:
player = players[oid]
pl.append({"oid":oid, "pos":player.pos, "color":player.color, "address":player.address})
sock.send(json.dumps({"type":"recap", "players":pl, "oid":me.oid}), [c[0], r["port"]])
players[r["oid"]] = Player(r["oid"], [c[0], r["port"]], r["pos"], r["color"])
elif r["type"] == "move":
players[r["pid"]].speed = r["speed"]
players[r["oid"]].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"])
if not player["oid"] in players:
address = player["address"]
if address[0] == "0.0.0.0":
address = [c[0], c[1]]
objects[player["oid"]] = Player(player["oid"], address, player["pos"], player["color"])
players[player["oid"]] = objects[player["oid"]]
elif r["type"] == "quit":
players.pop(r["oid"])
objects.pop(r["oid"])
elif r["type"] == "stab":
objects[r["oid"]].stab()
screen.fill(black)
pygame.draw.rect(screen, me.color, (*me.pos, 16, 16))
lines = []
for oid in objects:
obj = objects[oid]
obj.update()
obj.draw(screen)
obj.print_debug(lines)
for pid in players:
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))
xlog.display(screen, lines)
pygame.display.flip()
clock.tick(30)

View file

@ -21,6 +21,7 @@ class Sock:
def listen(self, address, length=65535):
self.sock_thread = SockThread(self, tuple(address), length)
self.sock_thread.setDaemon(True)
self.sock_thread.start()
def get(self):

9
xlog.py Normal file
View file

@ -0,0 +1,9 @@
import pygame
pygame.font.init()
font = pygame.font.SysFont('FreeMono', 16)
def display(surface, lines):
for i in range(len(lines)):
surface.blit(font.render(lines[i], False, (255,255,255)), (0, i*20))