generated from mwc/lab_retro
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
# player.py
|
|
# ------------
|
|
# By Pat Wick
|
|
# This module defines a player agent class. This is intended
|
|
# to be used in an implementation of an adventure game but could
|
|
# generally be adapted for other player character uses.
|
|
|
|
from retro.agent import ArrowKeyAgent
|
|
|
|
class Player:
|
|
character = "O"
|
|
name = "player"
|
|
level = 1
|
|
|
|
def __init__(self, position, race, class_):
|
|
self.position = position
|
|
if class_.capitalize() == "Warrior":
|
|
self.color = "red"
|
|
elif class_.capitalize() == "Rogue":
|
|
self.color = "green"
|
|
else:
|
|
self.color = "blue"
|
|
|
|
def handle_keystroke(self, keystroke, game):
|
|
x, y = self.position
|
|
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT"):
|
|
if keystroke.name == "KEY_LEFT":
|
|
new_position = (x - 1, y)
|
|
else:
|
|
new_position = (x + 1, y)
|
|
if game.on_board(new_position):
|
|
if game.is_empty(new_position):
|
|
self.position = new_position
|
|
|
|
if keystroke.name in ("KEY_DOWN", "KEY_UP"):
|
|
if keystroke.name == "KEY_DOWN":
|
|
new_position = (x, y + 1)
|
|
else:
|
|
new_position = (x, y - 1)
|
|
if game.on_board(new_position):
|
|
if game.is_empty(new_position):
|
|
self.position = new_position
|
|
|
|
def get_agent_in_position(self, position, game):
|
|
|
|
agents = game.get_agents_by_position()[position]
|
|
if agents:
|
|
return agents[0] |