python猫和老鼠游戏制作大全集
时间: 2023-07-26 15:11:04 浏览: 66
猫和老鼠游戏是一款经典的策略游戏,通过编写Python程序可以实现这个游戏的制作。以下是一个简单的猫和老鼠游戏的Python代码示例:
```python
import random
# 定义地图大小
SIZE = 10
# 初始化地图
map = [['.' for x in range(SIZE)] for y in range(SIZE)]
# 定义猫和老鼠的初始位置
cat_x, cat_y = random.randint(0, SIZE - 1), random.randint(0, SIZE - 1)
mouse_x, mouse_y = random.randint(0, SIZE - 1), random.randint(0, SIZE - 1)
# 将猫和老鼠放置在地图中
map[cat_x][cat_y] = 'C'
map[mouse_x][mouse_y] = 'M'
# 游戏循环
while True:
# 打印地图
for row in map:
print(' '.join(row))
# 判断猫是否抓到了老鼠
if cat_x == mouse_x and cat_y == mouse_y:
print('Cat caught the mouse!')
break
# 等待用户输入移动方向
direction = input('Move (WASD): ')
# 根据用户输入移动老鼠
if direction == 'W' and mouse_x > 0:
map[mouse_x][mouse_y] = '.'
mouse_x -= 1
map[mouse_x][mouse_y] = 'M'
elif direction == 'A' and mouse_y > 0:
map[mouse_x][mouse_y] = '.'
mouse_y -= 1
map[mouse_x][mouse_y] = 'M'
elif direction == 'S' and mouse_x < SIZE - 1:
map[mouse_x][mouse_y] = '.'
mouse_x += 1
map[mouse_x][mouse_y] = 'M'
elif direction == 'D' and mouse_y < SIZE - 1:
map[mouse_x][mouse_y] = '.'
mouse_y += 1
map[mouse_x][mouse_y] = 'M'
# 猫移动
cat_direction = random.choice(['W', 'A', 'S', 'D'])
if cat_direction == 'W' and cat_x > 0:
map[cat_x][cat_y] = '.'
cat_x -= 1
map[cat_x][cat_y] = 'C'
elif cat_direction == 'A' and cat_y > 0:
map[cat_x][cat_y] = '.'
cat_y -= 1
map[cat_x][cat_y] = 'C'
elif cat_direction == 'S' and cat_x < SIZE - 1:
map[cat_x][cat_y] = '.'
cat_x += 1
map[cat_x][cat_y] = 'C'
elif cat_direction == 'D' and cat_y < SIZE - 1:
map[cat_x][cat_y] = '.'
cat_y += 1
map[cat_x][cat_y] = 'C'
```
这是一个最基础的猫和老鼠游戏,你可以根据自己的需要进行修改和扩展。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)