连连看游戏代码python
时间: 2024-02-13 21:59:09 浏览: 194
连连看游戏是一种经典的消除类游戏,玩家需要在给定的方块中找到相同的图案并消除它们。以下是一个简单的连连看游戏的Python代码示例:
```python
import random
# 游戏方块的种类
BLOCK_TYPES = ['A', 'B', 'C', 'D', 'E']
# 游戏方块的尺寸
BLOCK_SIZE = 8
# 创建游戏方块矩阵
def create_board():
board = []
for _ in range(BLOCK_SIZE):
row = [random.choice(BLOCK_TYPES) for _ in range(BLOCK_SIZE)]
board.append(row)
return board
# 打印游戏方块矩阵
def print_board(board):
for row in board:
print(' '.join(row))
# 判断两个方块是否可以消除
def can_eliminate(block1, block2):
return block1 == block2
# 消除两个方块
def eliminate_blocks(board, row1, col1, row2, col2):
board[row1][col1] = ' '
board[row2][col2] = ' '
# 判断游戏是否结束
def is_game_over(board):
for row in board:
if any(block != ' ' for block in row):
return False
return True
# 游戏主循环
def game_loop():
board = create_board()
while not is_game_over(board):
print_board(board)
print('请输入两个方块的坐标,以空格分隔(例如:1 2):')
row1, col1 = map(int, input().split())
row2, col2 = map(int, input().split())
if can_eliminate(board[row1][col1], board[row2][col2]):
eliminate_blocks(board, row1, col1, row2, col2)
else:
print('无法消除这两个方块,请重新输入。')
print('游戏结束!')
# 启动游戏
game_loop()
```
这段代码实现了一个简单的连连看游戏,玩家需要通过输入两个方块的坐标来消除相同的方块,直到所有方块都被消除完毕。
阅读全文