Compare commits

..

2 commits

Author SHA1 Message Date
a8009f744d remove_objects 2024-02-17 13:37:55 +01:00
57a64d3292 Energy ball 2024-02-17 10:08:21 +01:00

70
game.py
View file

@ -7,9 +7,20 @@ w, h = 640, 480
size = [w, h]
black = [0, 0, 0]
class Player:
def __init__(self, oid, address, pos, color):
def new_oid():
return random.randint(0,2147483648)
class Obj:
def __init__(self, oid):
self.oid = oid
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)
self.address = address
self.pos = pos
self.color = color
@ -17,6 +28,14 @@ class Player:
self.life = 100
self.direction = [1, 0]
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)
def stab(self):
for oid in players:
if oid == self.oid:
@ -27,6 +46,8 @@ class Player:
def damage(self, dmg):
self.life -= dmg
if self.life <= 0:
print("die!")
def update(self):
self.pos[0] += self.speed[0]
@ -40,23 +61,31 @@ class Player:
def print_debug(self, lines):
lines.append("Player {}: {}".format(self.oid, obj.address))
class EnergyBall:
class EnergyBall(Obj):
def __init__(self, oid, pos, speed):
self.oid = oid
Obj.__init__(self, 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:
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
def draw(self, surface):
pygame.draw.rect(surface, (128,255,255), (self.pos[0]-2, self.pos[1]-2, 4, 4))
def damage(self, dmg):
if dmg > 0:
removable.append(self.oid)
def print_debug(self, lines):
pass
@ -66,15 +95,24 @@ clock = pygame.time.Clock()
sock = network.Sock()
sock.listen(address)
me = Player(random.randint(0,2147483648), address, [10, 10], [255, 0, 0])
me = Player(new_oid(), address, [10, 10], [255, 0, 0])
objects = {me.oid: me}
players = {me.oid: me}
removable = []
def send_all(msg):
msg = json.dumps(msg)
for oid in players:
sock.send(msg, objects[oid].address)
def remove_objects():
for oid in removable:
if oid in objects:
objects.pop(oid)
if oid in players:
players.pop(oid)
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(json.dumps({"type":"join","pos":me.pos,"color":me.color,"port":address[1],"oid":me.oid}), ["127.0.0.1", 33033])
@ -87,20 +125,27 @@ while True:
send_all({"type":"quit", "oid":me.oid})
sys.exit()
if event.key == pygame.K_RIGHT:
me.direction = [1, 0]
me.speed[0] = 2
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_LEFT:
me.direction = [-1, 0]
me.speed[0] = -2
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_DOWN:
me.direction = [0, 1]
me.speed[1] = 2
send_all({"type":"move", "oid":me.oid, "speed":me.speed})
elif event.key == pygame.K_UP:
me.direction = [0, -1]
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.key == pygame.K_c:
me.throw_energy_ball()
send_all({"type":"throw", "oid":me.oid})
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
me.speed[0] = 0
@ -136,11 +181,12 @@ while True:
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"])
removable.append(r["oid"])
elif r["type"] == "stab":
objects[r["oid"]].stab()
remove_objects()
screen.fill(black)
lines = []
@ -150,6 +196,8 @@ while True:
obj.draw(screen)
obj.print_debug(lines)
remove_objects()
xlog.display(screen, lines)
pygame.display.flip()