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,15 +7,34 @@ 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
|
||||
self.speed = [0, 0]
|
||||
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:
|
||||
|
@ -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,9 +95,10 @@ 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)
|
||||
|
@ -87,20 +117,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
|
||||
|
@ -149,6 +186,13 @@ while True:
|
|||
obj.update()
|
||||
obj.draw(screen)
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue