Skip to content

Commit

Permalink
once you go black
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Sep 6, 2019
1 parent d8d5cce commit bde3d0a
Show file tree
Hide file tree
Showing 6 changed files with 508 additions and 310 deletions.
63 changes: 35 additions & 28 deletions characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@
DIR = "wasd"
DEFAULT_LIVES = 3


def vector2dir(vx, vy):
m = max(abs(vx), abs(vy))
if m == abs(vx):
if vx < 0:
d = 1 #a
d = 1 # a
else:
d = 3 #d
d = 3 # d
else:
if vy > 0:
d = 2 #s
d = 2 # s
else:
d = 0 #w
d = 0 # w
return d


class Character:
def __init__(self, x=1, y=1):
self._pos = x, y
self._spawn_pos = self._pos

@property
def pos(self):
return self._pos

@pos.setter
def pos(self, value):
self._pos = value
Expand All @@ -49,12 +51,11 @@ def __init__(self, pos, lives=DEFAULT_LIVES):
super().__init__(x=pos[0], y=pos[1])
self._lives = lives
self._powers = []

def to_dict(self):
return {"pos": self.pos,
"lives": self._lives}
return {"pos": self.pos, "lives": self._lives}

@property
@property
def powers(self):
return self._powers

Expand All @@ -66,7 +67,7 @@ def flames(self):
return len([p for p in self._powers if p == Powerups.Flames])

def kill(self):
self._lives-=1
self._lives -= 1

def powerup(self, _type):
self._powers.append(_type)
Expand All @@ -88,10 +89,10 @@ def __init__(self, pos, name, points, speed, smart, wallpass):
self.wander = 0

super().__init__(*pos)

def __str__(self):
return f"{self._name}"

def points(self):
return self._points

Expand All @@ -100,10 +101,12 @@ def move(self, mapa, bomberman, bombs):
return

if self._smart == Smart.LOW:
new_pos = mapa.calc_pos(self.pos, self.dir[self.lastdir]) #don't bump into stones/walls
new_pos = mapa.calc_pos(
self.pos, self.dir[self.lastdir]
) # don't bump into stones/walls
if new_pos == self.pos:
self.lastdir = (self.lastdir + 1) % len(self.dir)

elif self._smart == Smart.NORMAL:
b_x, b_y = bomberman.pos
o_x, o_y = self.pos
Expand All @@ -113,7 +116,7 @@ def move(self, mapa, bomberman, bombs):
else:
direction = vector2dir(b_x - o_x, b_y - o_y)

new_pos = mapa.calc_pos(self.pos, self.dir[direction]) #chase bomberman
new_pos = mapa.calc_pos(self.pos, self.dir[direction]) # chase bomberman
if new_pos == self.pos:
self.lastdir = (direction + random.choice([-1, 1])) % len(self.dir)
self.wander = 3
Expand All @@ -122,9 +125,9 @@ def move(self, mapa, bomberman, bombs):
self.wander -= 1
else:
self.lastdir = None

elif self._smart == Smart.HIGH:
#TODO
# TODO
pass

self.pos = new_pos
Expand All @@ -135,27 +138,31 @@ def ready(self):
self.step = 0
return True
return False



class Balloom(Enemy):
def __init__(self, pos):
super().__init__(pos, self.__class__.__name__,
100, Speed.SLOW, Smart.LOW, False)
super().__init__(
pos, self.__class__.__name__, 100, Speed.SLOW, Smart.LOW, False
)


class Oneal(Enemy):
def __init__(self, pos):
super().__init__(pos, self.__class__.__name__,
200, Speed.SLOWEST, Smart.NORMAL, False)
super().__init__(
pos, self.__class__.__name__, 200, Speed.SLOWEST, Smart.NORMAL, False
)


class Doll(Enemy):
def __init__(self, pos):
super().__init__(pos, self.__class__.__name__,
400, Speed.NORMAL, Smart.LOW, False)
super().__init__(
pos, self.__class__.__name__, 400, Speed.NORMAL, Smart.LOW, False
)


class Minvo(Enemy):
def __init__(self, pos):
super().__init__(pos, self.__class__.__name__,
800, Speed.FAST, Smart.NORMAL, False)

super().__init__(
pos, self.__class__.__name__, 800, Speed.FAST, Smart.NORMAL, False
)
48 changes: 27 additions & 21 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,71 @@

from mapa import Map

#Next 2 lines are not needed for AI agent
# Next 2 lines are not needed for AI agent
import pygame

pygame.init()

async def agent_loop(server_address = "localhost:8000", agent_name="student"):

async def agent_loop(server_address="localhost:8000", agent_name="student"):
async with websockets.connect(f"ws://{server_address}/player") as websocket:

# Receive information about static game properties
# Receive information about static game properties
await websocket.send(json.dumps({"cmd": "join", "name": agent_name}))
msg = await websocket.recv()
game_properties = json.loads(msg)
game_properties = json.loads(msg)

# You can create your own map representation or use the game representation:
mapa = Map(size = game_properties['size'], mapa = game_properties['map'])
mapa = Map(size=game_properties["size"], mapa=game_properties["map"])

#Next 3 lines are not needed for AI agent
# Next 3 lines are not needed for AI agent
SCREEN = pygame.display.set_mode((299, 123))
SPRITES = pygame.image.load("data/pad.png").convert_alpha()
SCREEN.blit(SPRITES, (0, 0))

while True:
try:
state = json.loads(await websocket.recv()) #receive game state, this must be called timely or your game will get out of sync with the server
state = json.loads(
await websocket.recv()
) # receive game state, this must be called timely or your game will get out of sync with the server

#Next lines are only for the Human Agent, the key values are nonetheless the correct ones!
# Next lines are only for the Human Agent, the key values are nonetheless the correct ones!
key = ""
for event in pygame.event.get():
if event.type == pygame.QUIT or not state['lives']:
if event.type == pygame.QUIT or not state["lives"]:
pygame.quit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
key = 'w'
key = "w"
elif event.key == pygame.K_LEFT:
key = 'a'
key = "a"
elif event.key == pygame.K_DOWN:
key = 's'
key = "s"
elif event.key == pygame.K_RIGHT:
key = 'd'
key = "d"
elif event.key == pygame.K_a:
key = 'A'
key = "A"
elif event.key == pygame.K_b:
key = 'B'
key = "B"

await websocket.send(json.dumps({"cmd": "key", "key": key})) #send key command to server - you must implement this send in the AI agent
await websocket.send(
json.dumps({"cmd": "key", "key": key})
) # send key command to server - you must implement this send in the AI agent
break
except websockets.exceptions.ConnectionClosedOK:
print("Server has cleanly disconnected us")
return

#Next line is not needed for AI agent
# Next line is not needed for AI agent
pygame.display.flip()


# DO NOT CHANGE THE LINES BELLOW
# You can change the default values using the command line, example:
# $ NAME='bombastico' python3 client.py
loop = asyncio.get_event_loop()
SERVER = os.environ.get('SERVER', 'localhost')
PORT = os.environ.get('PORT', '8000')
NAME = os.environ.get('NAME', getpass.getuser())
SERVER = os.environ.get("SERVER", "localhost")
PORT = os.environ.get("PORT", "8000")
NAME = os.environ.get("NAME", getpass.getuser())
loop.run_until_complete(agent_loop(f"{SERVER}:{PORT}", NAME))
Loading

0 comments on commit bde3d0a

Please sign in to comment.