请用Python编写一个沙盒游戏代码
时间: 2023-11-16 08:12:45 浏览: 106
好的,下面是一个简单的沙盒游戏代码示例,使用Python 3编写:
```python
import random
# 游戏地图大小
MAP_SIZE = 10
# 初始化地图
game_map = [[0 for i in range(MAP_SIZE)] for j in range(MAP_SIZE)]
# 定义玩家初始位置
player_x = random.randint(0, MAP_SIZE-1)
player_y = random.randint(0, MAP_SIZE-1)
game_map[player_x][player_y] = 1
# 游戏主循环
while True:
# 打印地图
for row in game_map:
print(row)
# 玩家移动
move = input("请输入移动方向(w、a、s、d):")
if move == "w":
if player_x > 0:
game_map[player_x][player_y] = 0
player_x -= 1
game_map[player_x][player_y] = 1
elif move == "a":
if player_y > 0:
game_map[player_x][player_y] = 0
player_y -= 1
game_map[player_x][player_y] = 1
elif move == "s":
if player_x < MAP_SIZE-1:
game_map[player_x][player_y] = 0
player_x += 1
game_map[player_x][player_y] = 1
elif move == "d":
if player_y < MAP_SIZE-1:
game_map[player_x][player_y] = 0
player_y += 1
game_map[player_x][player_y] = 1
else:
print("无效的移动方向!")
# 判断游戏是否结束
if player_x == 0 or player_x == MAP_SIZE-1 or player_y == 0 or player_y == MAP_SIZE-1:
print("游戏结束!")
break
```
这个示例代码实现了一个简单的沙盒游戏,玩家可以在地图上移动,并且如果到达地图边界时游戏结束。当然,这只是一个简单的示例,你可以根据自己的需求和想象力来编写更加丰富和复杂的沙盒游戏。