2024-02-10 17:06:30 +00:00
|
|
|
import network, sys, json, pygame, random, xlog
|
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]
|
|
|
|
|
2024-02-17 09:08:21 +00:00
|
|
|
def new_oid():
|
|
|
|
return random.randint(0,2147483648)
|
|
|
|
|
|
|
|
class Obj:
|
|
|
|
def __init__(self, oid):
|
2024-02-10 17:06:30 +00:00
|
|
|
self.oid = oid
|
2024-02-17 09:08:21 +00:00
|
|
|
def damage(self, dmg): pass
|
|
|
|
def update(self): pass
|
|
|
|
def draw(self, surface): pass
|
|
|
|
def print_debug(self, lines): pass
|
|
|
|
|
|
|
|
class Player(Obj):
|
|
|
|
def __init__(self, oid, address, pos, color):
|
|
|
|
Obj.__init__(self, oid)
|
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-10 17:06:30 +00:00
|
|
|
self.life = 100
|
|
|
|
self.direction = [1, 0]
|
2024-02-17 09:08:21 +00:00
|
|
|
|
|
|
|
def throw_energy_ball(self):
|
|
|
|
oid = new_oid()
|
|
|
|
pos = [self.pos[0], self.pos[1]]
|
|
|
|
pos[0] += self.direction[0] * 16
|
|
|
|
pos[1] += self.direction[1] * 16
|
|
|
|
speed = [self.speed[0]+self.direction[0]*4, self.speed[1]+self.direction[1]*4]
|
|
|
|
objects[oid] = EnergyBall(oid, pos, speed)
|
2024-02-10 17:06:30 +00:00
|
|
|
|
|
|
|
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
|
2024-02-17 09:08:21 +00:00
|
|
|
if self.life <= 0:
|
|
|
|
print("die!")
|
2024-02-10 17:06:30 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2024-02-17 09:08:21 +00:00
|
|
|
class EnergyBall(Obj):
|
2024-02-10 17:06:30 +00:00
|
|
|
def __init__(self, oid, pos, speed):
|
2024-02-17 09:08:21 +00:00
|
|
|
Obj.__init__(self, oid)
|
2024-02-10 17:06:30 +00:00
|
|
|
self.pos = pos
|
|
|
|
self.speed = speed
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.pos[0] += self.speed[0]
|
|
|
|
self.pos[1] += self.speed[1]
|
2024-02-17 09:08:21 +00:00
|
|
|
for oid in objects:
|
|
|
|
if oid == self.oid:
|
|
|
|
continue
|
|
|
|
obj = objects[oid]
|
|
|
|
if abs(self.pos[0]-obj.pos[0])<8 and abs(self.pos[1]-obj.pos[1])<8:
|
|
|
|
obj.damage(10)
|
|
|
|
removable.append(self.oid)
|
|
|
|
return
|
2024-02-10 17:06:30 +00:00
|
|
|
|
|
|
|
def draw(self, surface):
|
|
|
|
pygame.draw.rect(surface, (128,255,255), (self.pos[0]-2, self.pos[1]-2, 4, 4))
|
|
|
|
|
2024-02-17 09:08:21 +00:00
|
|
|
def damage(self, dmg):
|
|
|
|
if dmg > 0:
|
|
|
|
removable.append(self.oid)
|
|
|
|
|
2024-02-10 17:06:30 +00:00
|
|
|
def print_debug(self, lines):
|
|
|
|
pass
|
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-17 09:08:21 +00:00
|
|
|
me = Player(new_oid(), address, [10, 10], [255, 0, 0])
|
2024-02-10 17:06:30 +00:00
|
|
|
objects = {me.oid: me}
|
|
|
|
players = {me.oid: me}
|
2024-02-17 09:08:21 +00:00
|
|
|
removable = []
|
2024-02-03 17:57:23 +00:00
|
|
|
|
|
|
|
def send_all(msg):
|
|
|
|
msg = json.dumps(msg)
|
2024-02-10 17:06:30 +00:00
|
|
|
for oid in players:
|
|
|
|
sock.send(msg, objects[oid].address)
|
2024-02-03 17:57:23 +00:00
|
|
|
|
2024-02-10 17:06:30 +00:00
|
|
|
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])
|
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 17:06:30 +00:00
|
|
|
if event.key == pygame.K_ESCAPE:
|
|
|
|
send_all({"type":"quit", "oid":me.oid})
|
|
|
|
sys.exit()
|
2024-02-10 12:30:58 +00:00
|
|
|
if event.key == pygame.K_RIGHT:
|
2024-02-17 09:08:21 +00:00
|
|
|
me.direction = [1, 0]
|
2024-02-10 17:06:30 +00:00
|
|
|
me.speed[0] = 2
|
|
|
|
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
|
2024-02-10 12:30:58 +00:00
|
|
|
elif event.key == pygame.K_LEFT:
|
2024-02-17 09:08:21 +00:00
|
|
|
me.direction = [-1, 0]
|
2024-02-10 17:06:30 +00:00
|
|
|
me.speed[0] = -2
|
|
|
|
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
|
2024-02-10 12:30:58 +00:00
|
|
|
elif event.key == pygame.K_DOWN:
|
2024-02-17 09:08:21 +00:00
|
|
|
me.direction = [0, 1]
|
2024-02-10 17:06:30 +00:00
|
|
|
me.speed[1] = 2
|
|
|
|
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
|
2024-02-10 12:30:58 +00:00
|
|
|
elif event.key == pygame.K_UP:
|
2024-02-17 09:08:21 +00:00
|
|
|
me.direction = [0, -1]
|
2024-02-10 17:06:30 +00:00
|
|
|
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})
|
2024-02-17 09:08:21 +00:00
|
|
|
elif event.key == pygame.K_c:
|
|
|
|
me.throw_energy_ball()
|
|
|
|
send_all({"type":"throw", "oid":me.oid})
|
2024-02-10 12:30:58 +00:00
|
|
|
elif event.type == pygame.KEYUP:
|
2024-02-10 17:06:30 +00:00
|
|
|
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:
|
2024-02-10 12:30:58 +00:00
|
|
|
me.speed[0] = 0
|
2024-02-10 17:06:30 +00:00
|
|
|
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
|
|
|
|
elif event.key == pygame.K_DOWN:
|
|
|
|
me.speed[1] = 0
|
|
|
|
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
|
|
|
|
elif event.key == pygame.K_UP:
|
2024-02-10 12:30:58 +00:00
|
|
|
me.speed[1] = 0
|
2024-02-10 17:06:30 +00:00
|
|
|
send_all({"type":"move", "oid":me.oid, "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 17:06:30 +00:00
|
|
|
if r["oid"] == me.oid:
|
2024-02-10 12:30:58 +00:00
|
|
|
continue
|
2024-02-03 17:57:23 +00:00
|
|
|
if r["type"] == "join":
|
2024-02-10 12:30:58 +00:00
|
|
|
pl = []
|
2024-02-10 17:06:30 +00:00
|
|
|
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"])
|
2024-02-03 17:57:23 +00:00
|
|
|
elif r["type"] == "move":
|
2024-02-10 17:06:30 +00:00
|
|
|
players[r["oid"]].speed = r["speed"]
|
2024-02-10 12:30:58 +00:00
|
|
|
elif r["type"] == "recap":
|
|
|
|
for player in r["players"]:
|
2024-02-10 17:06:30 +00:00
|
|
|
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()
|
2024-02-03 17:57:23 +00:00
|
|
|
|
|
|
|
screen.fill(black)
|
|
|
|
|
2024-02-10 17:06:30 +00:00
|
|
|
lines = []
|
|
|
|
for oid in objects:
|
|
|
|
obj = objects[oid]
|
|
|
|
obj.update()
|
|
|
|
obj.draw(screen)
|
|
|
|
obj.print_debug(lines)
|
2024-02-17 09:08:21 +00:00
|
|
|
|
|
|
|
for oid in removable:
|
|
|
|
if oid in objects:
|
|
|
|
objects.pop(oid)
|
|
|
|
if oid in players:
|
|
|
|
players.pop(oid)
|
|
|
|
removable = []
|
2024-02-03 17:57:23 +00:00
|
|
|
|
2024-02-10 17:06:30 +00:00
|
|
|
xlog.display(screen, lines)
|
2024-02-03 17:57:23 +00:00
|
|
|
|
|
|
|
pygame.display.flip()
|
|
|
|
clock.tick(30)
|