generated from mwc/lab_retro
66 lines
2.1 KiB
Python
66 lines
2.1 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
|
|
from retro.game import Game
|
|
from projectile import Projectile
|
|
|
|
class Player:
|
|
character = "O"
|
|
name = "player"
|
|
level = 1
|
|
direction = (1,0)
|
|
class_ = ""
|
|
|
|
def __init__(self, position, race, class_):
|
|
self.position = position
|
|
if class_.capitalize() == "Warrior":
|
|
self.color = "red"
|
|
self.class_ == class_
|
|
elif class_.capitalize() == "Rogue":
|
|
self.color = "green"
|
|
self.class_ == class_
|
|
else:
|
|
self.color = "blue"
|
|
self.class_ == class_
|
|
|
|
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)
|
|
self.direction = (-1,0)
|
|
else:
|
|
new_position = (x + 1, y)
|
|
self.direction = (1,0)
|
|
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)
|
|
self.direction = (0,1)
|
|
else:
|
|
new_position = (x, y - 1)
|
|
self.direction = (0,-1)
|
|
if game.on_board(new_position):
|
|
if game.is_empty(new_position):
|
|
self.position = new_position
|
|
|
|
if self.class_ != "Warrior" and keystroke == " ":
|
|
projectile = Projectile((self.position[0] + self.direction[0],self.position[1] + self.direction[1]), self.direction, 1)
|
|
game.add_agent(projectile)
|
|
print("pew pew pew")
|
|
|
|
|
|
|
|
def get_agent_in_position(self, position, game):
|
|
|
|
agents = game.get_agents_by_position()[position]
|
|
if agents:
|
|
return agents[0] |