generated from mwc/project_game
Checkpoint 1: Create player
I created an agent the shape of a penguin. I used the retro lab to help guide its movement and how to run the program. The penguin moves left, right, up, and down. I noticed that when I move it over to the far right, the wall disappears. I think that's something I need to work on while figuring out collision detection. However, my penguin does not move beyond the board.
This commit is contained in:
BIN
__pycache__/player.cpython-312.pyc
Normal file
BIN
__pycache__/player.cpython-312.pyc
Normal file
Binary file not shown.
8
maze_game.py
Normal file
8
maze_game.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from retro.game import Game
|
||||
from player import Player
|
||||
|
||||
board_size = (100, 40)
|
||||
player = Player(board_size)
|
||||
|
||||
game = Game([player], {}, board_size=board_size)
|
||||
game.play()
|
||||
28
player.py
Normal file
28
player.py
Normal file
@@ -0,0 +1,28 @@
|
||||
class Player:
|
||||
name = "player"
|
||||
character = "🐧"
|
||||
color = "purple"
|
||||
|
||||
def __init__(self, board_size):
|
||||
# Start at bottom-left corner
|
||||
self.position = (0, board_size[1] - 1)
|
||||
|
||||
def handle_keystroke(self, keystroke, game):
|
||||
x, y = self.position
|
||||
|
||||
if keystroke.name == "KEY_LEFT":
|
||||
new_position = (x - 1, y)
|
||||
elif keystroke.name == "KEY_RIGHT":
|
||||
new_position = (x + 1, y)
|
||||
elif keystroke.name == "KEY_UP":
|
||||
new_position = (x, y - 1)
|
||||
elif keystroke.name == "KEY_DOWN":
|
||||
new_position = (x, y + 1)
|
||||
else:
|
||||
return
|
||||
|
||||
# Contain penguin inside board
|
||||
if game.on_board(new_position):
|
||||
self.position = new_position
|
||||
|
||||
|
||||
Reference in New Issue
Block a user