消消乐代码python使用pygame并且能够人为操作消除
时间: 2023-10-24 16:35:54 浏览: 181
pygame实现消消乐小游戏.7z
好的,我来为你提供一份完整的消消乐游戏代码,可以使用Pygame库,并且可以通过人为操作消除:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 游戏窗口尺寸
WINDOW_SIZE = (400, 500)
# 设置游戏窗口
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('消消乐')
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 设置字体
font = pygame.font.Font(None, 36)
# 定义方块尺寸和间隔
BLOCK_SIZE = 40
BLOCK_MARGIN = 5
# 定义方块颜色
BLOCK_COLORS = [
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255)
]
# 定义方块类
class Block(pygame.sprite.Sprite):
def __init__(self, color, x, y):
super().__init__()
self.image = pygame.Surface([BLOCK_SIZE, BLOCK_SIZE])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 将方块移动到指定位置
def move(self, x, y):
self.rect.x = x
self.rect.y = y
# 创建方块组
block_group = pygame.sprite.Group()
# 创建方块二维数组
blocks = []
for row in range(10):
block_row = []
for column in range(10):
color = random.choice(BLOCK_COLORS)
x = (BLOCK_MARGIN + BLOCK_SIZE) * column + BLOCK_MARGIN
y = (BLOCK_MARGIN + BLOCK_SIZE) * row + BLOCK_MARGIN
block = Block(color, x, y)
block_group.add(block)
block_row.append(block)
blocks.append(block_row)
# 定义游戏循环标志
done = False
# 定义选中方块
selected_block = None
# 游戏循环
while not done:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标点击位置
pos = pygame.mouse.get_pos()
# 遍历方块,找到被点击的方块
for row in range(10):
for column in range(10):
block = blocks[row][column]
if block.rect.collidepoint(pos):
# 如果该方块被选中,则取消选中
if block == selected_block:
selected_block = None
# 如果该方块未被选中,则选中该方块
else:
selected_block = block
# 如果已选中两个方块,则交换它们的位置
if selected_block:
for row in range(10):
for column in range(10):
block = blocks[row][column]
if block == selected_block:
if row > 0:
up_block = blocks[row - 1][column]
if up_block == selected_block:
continue
if not up_block:
blocks[row][column] = None
blocks[row - 1][column] = selected_block
selected_block.move(up_block.rect.x, up_block.rect.y)
selected_block = None
break
if row < 9:
down_block = blocks[row + 1][column]
if down_block == selected_block:
continue
if not down_block:
blocks[row][column] = None
blocks[row + 1][column] = selected_block
selected_block.move(down_block.rect.x, down_block.rect.y)
selected_block = None
break
if column > 0:
left_block = blocks[row][column - 1]
if left_block == selected_block:
continue
if not left_block:
blocks[row][column] = None
blocks[row][column - 1] = selected_block
selected_block.move(left_block.rect.x, left_block.rect.y)
selected_block = None
break
if column < 9:
right_block = blocks[row][column + 1]
if right_block == selected_block:
continue
if not right_block:
blocks[row][column] = None
blocks[row][column + 1] = selected_block
selected_block.move(right_block.rect.x, right_block.rect.y)
selected_block = None
break
# 绘制背景
screen.fill(WHITE)
# 绘制方块
block_group.draw(screen)
# 更新屏幕
pygame.display.flip()
# 退出Pygame
pygame.quit()
```
这个代码在原有的基础上添加了人为操作消除功能,可以通过鼠标点击选中方块,并在选中两个方块后可以交换它们的位置。你可以根据自己的需求进行修改和完善。
阅读全文