用python写消消乐小游戏
时间: 2023-07-18 16:32:17 浏览: 118
好的,我可以给你一个简单的消消乐小游戏的 Python 代码示例。
```python
import random
# 游戏界面大小
ROW, COL = 8, 8
# 方块的种类数
N_TYPES = 5
# 方块的颜色
COLORS = ['R', 'G', 'B', 'Y', 'P']
# 打印游戏界面
def print_board(board):
for row in board:
print(' '.join(str(col) for col in row))
# 生成初始游戏界面
def create_board():
board = [[random.randint(0, N_TYPES - 1) for _ in range(COL)] for _ in range(ROW)]
return board
# 检查是否有能消除的方块
def check_board(board):
for row in range(ROW):
for col in range(COL):
if col < COL - 2 and board[row][col] == board[row][col+1] == board[row][col+2]:
return True
if row < ROW - 2 and board[row][col] == board[row+1][col] == board[row+2][col]:
return True
return False
# 消除相同的方块
def eliminate(board):
for row in range(ROW):
for col in range(COL):
if col < COL - 2 and board[row][col] == board[row][col+1] == board[row][col+2]:
board[row][col] = board[row][col+1] = board[row][col+2] = -1
if row < ROW - 2 and board[row][col] == board[row+1][col] == board[row+2][col]:
board[row][col] = board[row+1][col] = board[row+2][col] = -1
for col in range(COL):
new_col = []
for row in range(ROW):
if board[row][col] != -1:
new_col.append(board[row][col])
new_col += [-1] * (ROW - len(new_col))
for row in range(ROW):
board[row][col] = new_col[row]
# 主函数
def main():
board = create_board()
print_board(board)
while check_board(board):
eliminate(board)
print_board(board)
print("Game Over!")
if __name__ == '__main__':
main()
```
这是一个比较简单的示例,你可以根据自己的需求对代码进行修改和扩展。
阅读全文