用python语言编写连连看小游戏
时间: 2023-07-12 08:17:43 浏览: 78
好的,下面是用 Python 语言编写的连连看小游戏的代码:
```python
import random
# 游戏地图的大小
MAP_SIZE = 10
# 游戏地图
game_map = [[' ' for x in range(MAP_SIZE)] for y in range(MAP_SIZE)]
# 游戏方块
blocks = ['A', 'B', 'C', 'D', 'E']
# 随机在地图上生成方块
for block in blocks:
for i in range(2):
x = random.randint(0, MAP_SIZE - 1)
y = random.randint(0, MAP_SIZE - 1)
game_map[y][x] = block
# 打印游戏地图
def print_map():
for row in game_map:
print(' '.join(row))
# 判断两个方块是否可以消除
def can_remove(x1, y1, x2, y2):
if game_map[y1][x1] != game_map[y2][x2]:
return False
if x1 == x2 and y1 == y2:
return False
if x1 == x2:
for y in range(min(y1, y2) + 1, max(y1, y2)):
if game_map[y][x1] != ' ':
return False
return True
if y1 == y2:
for x in range(min(x1, x2) + 1, max(x1, x2)):
if game_map[y1][x] != ' ':
return False
return True
return False
# 消除两个方块
def remove(x1, y1, x2, y2):
game_map[y1][x1] = ' '
game_map[y2][x2] = ' '
# 判断游戏是否结束
def is_game_over():
for y in range(MAP_SIZE):
for x in range(MAP_SIZE):
if game_map[y][x] != ' ':
for y2 in range(y, MAP_SIZE):
x2_start = x + 1 if y2 == y else 0
for x2 in range(x2_start, MAP_SIZE):
if can_remove(x, y, x2, y2):
return False
return True
# 游戏循环
while True:
print_map()
if is_game_over():
print('Game Over!')
break
x1, y1, x2, y2 = map(int, input('Please enter two positions to remove: ').split())
if can_remove(x1, y1, x2, y2):
remove(x1, y1, x2, y2)
else:
print('Invalid positions!')
```
这个连连看小游戏使用二维数组来表示游戏地图,使用随机数来在地图上生成方块。玩家需要在输入两个方块的位置后,程序判断这两个方块是否可以消除,如果可以,则将它们从地图上删除。当地图上没有可以消除的方块时,游戏结束。
阅读全文