generated from mwc/project_game
Compare commits
2 Commits
12f0b05e25
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
621a680676 | ||
|
|
b2dd17e4ad |
Binary file not shown.
Binary file not shown.
21
frog.py
21
frog.py
@@ -8,15 +8,18 @@ class Frog:
|
|||||||
|
|
||||||
def handle_keystroke(self, keystroke, game):
|
def handle_keystroke(self, keystroke, game):
|
||||||
x, y = self.position
|
x, y = self.position
|
||||||
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP"):
|
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT"):
|
||||||
if keystroke.name == "KEY_LEFT":
|
if keystroke.name == "KEY_LEFT":
|
||||||
new_position = (x - 1, y)
|
new_position = (x - 1, y)
|
||||||
if keystroke.name == "KEY_RIGHT":
|
elif keystroke.name == "KEY_RIGHT":
|
||||||
new_position = (x + 1, y)
|
new_position = (x + 1, y)
|
||||||
if keystroke.name == "KEY_UP":
|
else:
|
||||||
new_position = (x, y + 1)
|
return
|
||||||
if game.on_board(new_position):
|
if not game.on_board(new_position):
|
||||||
if game.is_empty(new_position):
|
return
|
||||||
self.position = new_position
|
if not game.is_empty(new_position):
|
||||||
else:
|
game.end()
|
||||||
game.end()
|
self.position = new_position
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
33
obstacle.py
33
obstacle.py
@@ -3,17 +3,34 @@ class Obstacle:
|
|||||||
|
|
||||||
def __init__(self, position):
|
def __init__(self, position):
|
||||||
self.position = position
|
self.position = position
|
||||||
|
|
||||||
|
|
||||||
|
def handle_keystroke(self, keystroke, game):
|
||||||
|
froggy = game.get_agent_by_name('froggy')
|
||||||
|
if keystroke.name != "KEY_UP":
|
||||||
|
return
|
||||||
|
_, height = game.board_size
|
||||||
|
x, y =self.position
|
||||||
|
if y == height -1:
|
||||||
|
game.remove_agent(self)
|
||||||
|
return
|
||||||
|
new_position = (x,y + 1)
|
||||||
|
if new_position == froggy.position:
|
||||||
|
game.end()
|
||||||
|
return
|
||||||
|
if game.on_board(new_position) and game.is_empty(new_position):
|
||||||
|
self.position = new_position
|
||||||
|
|
||||||
def play_turn(self, game):
|
def play_turn(self, game):
|
||||||
width, height = game.board_size
|
width, height = game.board_size
|
||||||
if game.turn_number % 2 == 0:
|
if game.turn_number % 2 == 0:
|
||||||
x, y = self.position
|
x, y = self.position
|
||||||
if y == height + 1:
|
if y >= height:
|
||||||
game.remove_agent(self)
|
game.remove_agent(self)
|
||||||
else:
|
|
||||||
froggy = game.get_agent_by_name('froggy')
|
|
||||||
new_position = (x, y)
|
|
||||||
if new_position == froggy.position:
|
|
||||||
game.end()
|
|
||||||
else:
|
|
||||||
self.position = new_position
|
|
||||||
|
|||||||
Reference in New Issue
Block a user