Energy ball
This commit is contained in:
parent
711ea155ac
commit
57a64d3292
1 changed files with 53 additions and 9 deletions
62
game.py
62
game.py
|
@ -7,9 +7,20 @@ 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
|
||||||
|
@ -17,6 +28,14 @@ class Player:
|
||||||
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:
|
||||||
|
@ -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,9 +95,10 @@ 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)
|
||||||
|
@ -87,20 +117,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
|
||||||
|
@ -150,6 +187,13 @@ while True:
|
||||||
obj.draw(screen)
|
obj.draw(screen)
|
||||||
obj.print_debug(lines)
|
obj.print_debug(lines)
|
||||||
|
|
||||||
|
for oid in removable:
|
||||||
|
if oid in objects:
|
||||||
|
objects.pop(oid)
|
||||||
|
if oid in players:
|
||||||
|
players.pop(oid)
|
||||||
|
removable = []
|
||||||
|
|
||||||
xlog.display(screen, lines)
|
xlog.display(screen, lines)
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
Loading…
Reference in a new issue