Compare commits
2 commits
711ea155ac
...
a8009f744d
Author | SHA1 | Date | |
---|---|---|---|
a8009f744d | |||
57a64d3292 |
1 changed files with 59 additions and 11 deletions
70
game.py
70
game.py
|
@ -7,15 +7,34 @@ w, h = 640, 480
|
||||||
size = [w, h]
|
size = [w, h]
|
||||||
black = [0, 0, 0]
|
black = [0, 0, 0]
|
||||||
|
|
||||||
class Player:
|
def new_oid():
|
||||||
def __init__(self, oid, address, pos, color):
|
return random.randint(0,2147483648)
|
||||||
|
|
||||||
|
class Obj:
|
||||||
|
def __init__(self, oid):
|
||||||
self.oid = 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.address = address
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
self.color = color
|
self.color = color
|
||||||
self.speed = [0, 0]
|
self.speed = [0, 0]
|
||||||
self.life = 100
|
self.life = 100
|
||||||
self.direction = [1, 0]
|
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):
|
def stab(self):
|
||||||
for oid in players:
|
for oid in players:
|
||||||
|
@ -27,6 +46,8 @@ class Player:
|
||||||
|
|
||||||
def damage(self, dmg):
|
def damage(self, dmg):
|
||||||
self.life -= dmg
|
self.life -= dmg
|
||||||
|
if self.life <= 0:
|
||||||
|
print("die!")
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.pos[0] += self.speed[0]
|
self.pos[0] += self.speed[0]
|
||||||
|
@ -40,23 +61,31 @@ class Player:
|
||||||
def print_debug(self, lines):
|
def print_debug(self, lines):
|
||||||
lines.append("Player {}: {}".format(self.oid, obj.address))
|
lines.append("Player {}: {}".format(self.oid, obj.address))
|
||||||
|
|
||||||
class EnergyBall:
|
class EnergyBall(Obj):
|
||||||
def __init__(self, oid, pos, speed):
|
def __init__(self, oid, pos, speed):
|
||||||
self.oid = oid
|
Obj.__init__(self, oid)
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
self.speed = speed
|
self.speed = speed
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.pos[0] += self.speed[0]
|
self.pos[0] += self.speed[0]
|
||||||
self.pos[1] += self.speed[1]
|
self.pos[1] += self.speed[1]
|
||||||
for oid in players:
|
for oid in objects:
|
||||||
player = players[oid]
|
if oid == self.oid:
|
||||||
if abs(self.pos[0]-player.pos[0])<8 or abs(self.pos[1]-player.pos[1])<8:
|
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):
|
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):
|
||||||
|
if dmg > 0:
|
||||||
|
removable.append(self.oid)
|
||||||
|
|
||||||
def print_debug(self, lines):
|
def print_debug(self, lines):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -66,15 +95,24 @@ clock = pygame.time.Clock()
|
||||||
sock = network.Sock()
|
sock = network.Sock()
|
||||||
sock.listen(address)
|
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}
|
objects = {me.oid: me}
|
||||||
players = {me.oid: me}
|
players = {me.oid: me}
|
||||||
|
removable = []
|
||||||
|
|
||||||
def send_all(msg):
|
def send_all(msg):
|
||||||
msg = json.dumps(msg)
|
msg = json.dumps(msg)
|
||||||
for oid in players:
|
for oid in players:
|
||||||
sock.send(msg, objects[oid].address)
|
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}), ["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(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})
|
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:
|
||||||
me.stab()
|
me.stab()
|
||||||
send_all({"type":"stab", "oid":me.oid})
|
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:
|
elif event.type == pygame.KEYUP:
|
||||||
if event.key == pygame.K_RIGHT:
|
if event.key == pygame.K_RIGHT:
|
||||||
me.speed[0] = 0
|
me.speed[0] = 0
|
||||||
|
@ -136,10 +181,11 @@ while True:
|
||||||
objects[player["oid"]] = Player(player["oid"], address, player["pos"], player["color"])
|
objects[player["oid"]] = Player(player["oid"], address, player["pos"], player["color"])
|
||||||
players[player["oid"]] = objects[player["oid"]]
|
players[player["oid"]] = objects[player["oid"]]
|
||||||
elif r["type"] == "quit":
|
elif r["type"] == "quit":
|
||||||
players.pop(r["oid"])
|
removable.append(r["oid"])
|
||||||
objects.pop(r["oid"])
|
|
||||||
elif r["type"] == "stab":
|
elif r["type"] == "stab":
|
||||||
objects[r["oid"]].stab()
|
objects[r["oid"]].stab()
|
||||||
|
|
||||||
|
remove_objects()
|
||||||
|
|
||||||
screen.fill(black)
|
screen.fill(black)
|
||||||
|
|
||||||
|
@ -149,6 +195,8 @@ while True:
|
||||||
obj.update()
|
obj.update()
|
||||||
obj.draw(screen)
|
obj.draw(screen)
|
||||||
obj.print_debug(lines)
|
obj.print_debug(lines)
|
||||||
|
|
||||||
|
remove_objects()
|
||||||
|
|
||||||
xlog.display(screen, lines)
|
xlog.display(screen, lines)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue