generated from mwc/project_game
add a alien target and a shooter to hit it
I am actually really proud that i was able to figure out how to make the alien move in every direction randomly.
This commit is contained in:
48
proposal.md/shooter.py
Normal file
48
proposal.md/shooter.py
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
|
||||
class Shooter:
|
||||
name = "shooter"
|
||||
character = 'I'
|
||||
display = False
|
||||
|
||||
def __init__(self):
|
||||
self.position = (0, 0)
|
||||
|
||||
def handle_keystroke(self, keystroke, game):
|
||||
|
||||
if keystroke == ' ' and not self.display:
|
||||
ship = game.get_agent_by_name('ship')
|
||||
if ship is None:
|
||||
return
|
||||
|
||||
x, y = ship.position
|
||||
new_position = (x, y - 1)
|
||||
|
||||
if game.on_board(new_position):
|
||||
self.position = new_position
|
||||
self.display = True
|
||||
|
||||
def play_turn(self, game):
|
||||
|
||||
if not self.display:
|
||||
|
||||
return
|
||||
|
||||
x, y = self.position
|
||||
new_position = (x, y - 1)
|
||||
|
||||
|
||||
if not game.on_board(new_position):
|
||||
self.display = False
|
||||
return
|
||||
|
||||
agents_by_position = game.get_agents_by_position()
|
||||
agents_here = agents_by_position.get(new_position, [])
|
||||
|
||||
for agent in agents_here:
|
||||
if getattr(agent, "character", None) == 'A':
|
||||
game.end()
|
||||
return
|
||||
|
||||
self.position = new_position
|
||||
|
||||
Reference in New Issue
Block a user