python简单游戏消消乐
时间: 2023-08-24 19:08:03 浏览: 101
好的,这里是一个简单的消消乐游戏:
```python
import random
# 游戏区大小
ROWS = 8
COLS = 8
# 游戏区中不同颜色方块的数量
COLORS = 5
# 随机生成一个游戏区
def generate_board():
board = [[random.randint(1, COLORS) for _ in range(COLS)] for _ in range(ROWS)]
return board
# 打印游戏区
def print_board(board):
for row in board:
print(' '.join(map(str, row)))
# 消除相邻的同色方块
def eliminate(board, row, col):
if row < 0 or row >= ROWS or col < 0 or col >= COLS:
return
color = board[row][col]
if color == 0:
return
board[row][col] = 0
eliminate(board, row-1, col)
eliminate(board, row+1, col)
eliminate(board, row, col-1)
eliminate(board, row, col+1)
# 消除所有相邻的同色方块
def eliminate_all(board):
for row in range(ROWS):
for col in range(COLS):
if board[row][col] != 0:
eliminate(board, row, col)
# 移动方块
def move(board):
for col in range(COLS):
empty_row = ROWS - 1
for row in range(ROWS-1, -1, -1):
if board[row][col] == 0:
continue
if row != empty_row:
board[empty_row][col] = board[row][col]
board[row][col] = 0
empty_row -= 1
# 检测游戏是否结束
def check_game_over(board):
for row in range(ROWS):
for col in range(COLS):
if board[row][col] != 0:
return False
return True
# 游戏主循环
def main():
board = generate_board()
while True:
print_board(board)
row = int(input("请输入要消除方块的行:"))
col = int(input("请输入要消除方块的列:"))
eliminate(board, row, col)
eliminate_all(board)
move(board)
if check_game_over(board):
print("游戏结束!")
break
if __name__ == '__main__':
main()
```
该游戏为命令行游戏,玩家需要输入要消除的方块的行和列。如果有相邻的同色方块,则消除它们,并移动其他方块填补空缺。当所有方块都被消除时,游戏结束。
阅读全文