Compare commits
No commits in common. "a8009f744d6ba53213a4726321bcbd7de86b8e61" and "711ea155ac1e0b14457ab0b510c0ea8c0a5b1df4" have entirely different histories.
a8009f744d
...
711ea155ac
1 changed files with 11 additions and 59 deletions
70
game.py
70
game.py
|
@ -7,20 +7,9 @@ w, h = 640, 480
|
||||||
size = [w, h]
|
size = [w, h]
|
||||||
black = [0, 0, 0]
|
black = [0, 0, 0]
|
||||||
|
|
||||||
def new_oid():
|
class Player:
|
||||||
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):
|
def __init__(self, oid, address, pos, color):
|
||||||
Obj.__init__(self, oid)
|
self.oid = oid
|
||||||
self.address = address
|
self.address = address
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
self.color = color
|
self.color = color
|
||||||
|
@ -28,14 +17,6 @@ class Player(Obj):
|
||||||
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:
|
||||||
if oid == self.oid:
|
if oid == self.oid:
|
||||||
|
@ -46,8 +27,6 @@ class Player(Obj):
|
||||||
|
|
||||||
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]
|
||||||
|
@ -61,31 +40,23 @@ class Player(Obj):
|
||||||
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(Obj):
|
class EnergyBall:
|
||||||
def __init__(self, oid, pos, speed):
|
def __init__(self, oid, pos, speed):
|
||||||
Obj.__init__(self, oid)
|
self.oid = 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 objects:
|
for oid in players:
|
||||||
if oid == self.oid:
|
player = players[oid]
|
||||||
continue
|
if abs(self.pos[0]-player.pos[0])<8 or abs(self.pos[1]-player.pos[1])<8:
|
||||||
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
|
||||||
|
|
||||||
|
@ -95,24 +66,15 @@ 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(random.randint(0,2147483648), 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])
|
||||||
|
|
||||||
|
@ -125,27 +87,20 @@ 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
|
||||||
|
@ -181,12 +136,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":
|
||||||
removable.append(r["oid"])
|
players.pop(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)
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
|
@ -196,8 +150,6 @@ while True:
|
||||||
obj.draw(screen)
|
obj.draw(screen)
|
||||||
obj.print_debug(lines)
|
obj.print_debug(lines)
|
||||||
|
|
||||||
remove_objects()
|
|
||||||
|
|
||||||
xlog.display(screen, lines)
|
xlog.display(screen, lines)
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
Loading…
Reference in a new issue