使用python编写一个枪战小游戏
时间: 2024-06-09 22:11:29 浏览: 23
好的,这是一个简单的枪战小游戏示例,使用 Python 语言编写。在游戏中,你将扮演一名士兵,需要在敌人的攻击下生存并消灭敌人。游戏界面使用 ASCII 字符绘制,操作使用键盘控制。
```python
import random
# 游戏地图
MAP = [
"+------------------------+",
"| |",
"| |",
"| E |",
"| |",
"| E |",
"| |",
"| |",
"| |",
"+------------------------+",
]
# 士兵位置
soldier_pos = [1, 1]
# 敌人位置
enemy_pos = [
[3, 11],
[5, 5],
]
# 子弹数
bullet_count = 6
# 游戏结束标志
game_over = False
def draw_map():
"""绘制游戏地图"""
for row in MAP:
print(row)
def move_soldier(direction):
"""移动士兵"""
global soldier_pos
row, col = soldier_pos
if direction == "up":
row -= 1
elif direction == "down":
row += 1
elif direction == "left":
col -= 1
elif direction == "right":
col += 1
if MAP[row][col] == " ":
MAP[soldier_pos[0]][soldier_pos[1]] = " "
soldier_pos = [row, col]
MAP[row][col] = "S"
return True
return False
def fire():
"""开火"""
global bullet_count, game_over
if bullet_count <= 0:
print("没有子弹了!")
return
bullet_count -= 1
print("剩余子弹数:", bullet_count)
for pos in enemy_pos:
if pos == soldier_pos:
print("游戏结束!")
game_over = True
return
elif pos[0] == soldier_pos[0] or pos[1] == soldier_pos[1]:
print("击中敌人!")
enemy_pos.remove(pos)
if not enemy_pos:
print("恭喜你获胜!")
game_over = True
return
def move_enemy():
"""移动敌人"""
for pos in enemy_pos:
if pos[0] == soldier_pos[0]:
if pos[1] < soldier_pos[1]:
pos[1] += 1
else:
pos[1] -= 1
elif pos[1] == soldier_pos[1]:
if pos[0] < soldier_pos[0]:
pos[0] += 1
else:
pos[0] -= 1
# 游戏主循环
while not game_over:
draw_map()
command = input("请输入命令(w:上 s:下 a:左 d:右 f:开火):")
if command == "w":
move_soldier("up")
elif command == "s":
move_soldier("down")
elif command == "a":
move_soldier("left")
elif command == "d":
move_soldier("right")
elif command == "f":
fire()
else:
print("无效命令!")
continue
if not enemy_pos:
print("恭喜你获胜!")
game_over = True
break
move_enemy()
for pos in enemy_pos:
if pos == soldier_pos:
print("游戏结束!")
game_over = True
break
if not game_over:
print("敌人移动了!")
print("剩余敌人数:", len(enemy_pos))
if random.randint(1, 10) == 1:
enemy_pos.append([random.randint(1, 8), random.randint(1, 18)])
```
在上面的代码中,我们使用了 Python 的基本语法和数据结构,包括列表、条件语句、循环语句等。玩家可以通过键盘输入命令来控制士兵的移动和开火,敌人会随机移动并且有一定几率生成新的敌人。当玩家击败所有敌人或者被敌人击败时,游戏结束。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)