sandbox game
sandbox gameunknown
python
2 years ago
1.4 kB
7
Indexable
class Cell:
def __init__(self, content):
self.content = content
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, direction):
if direction == 'up':
self.y -= 1
elif direction == 'down':
self.y += 1
elif direction == 'left':
self.x -= 1
elif direction == 'right':
self.x += 1
# Creating a 5x5 grid
world = [[Cell(None) for _ in range(5)] for _ in range(5)]
# Placing the player in the middle of the grid
player = Player(2, 2)
while True:
# Display the world
for row in world:
for cell in row:
if cell.content:
print(cell.content, end=' ')
else:
print('-', end=' ')
print()
# Get player input
action = input('Enter your action (up/down/left/right/quit): ')
# Quit the game
if action == 'quit':
break
# Move the player
player.move(action)
# Check boundaries
if player.x < 0:
player.x = 0
elif player.x > 4:
player.x = 4
if player.y < 0:
player.y = 0
elif player.y > 4:
player.y = 4
# Perform action at the player's new location
current_cell = world[player.y][player.x]
if not current_cell.content:
current_cell.content = 'X'
Editor is loading...