连连看python小游戏
时间: 2023-08-31 08:05:19 浏览: 102
好的,我可以为您提供一个基于 Python 的连连看小游戏的思路和代码实现。
思路概述:
1. 游戏界面的绘制
2. 随机生成游戏区域的方块
3. 检测用户鼠标点击位置是否在方块上
4. 判断两个方块是否可以相互连接
5. 实现方块消除
6. 实现游戏结束条件
代码实现:
首先需要导入 pygame 库和 random 库。
```python
import pygame
import random
```
定义一些常量,包括屏幕大小、方块大小、颜色等。
```python
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BLOCK_SIZE = 40
BLOCK_COLORS = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(255, 0, 255), # 紫色
(0, 255, 255), # 青色
]
```
初始化 pygame 库和创建游戏窗口。
```python
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("连连看小游戏")
```
定义方块类,包括方块的位置、颜色和状态(是否被选中)。
```python
class Block:
def __init__(self, row, col, color):
self.row = row
self.col = col
self.color = color
self.selected = False
```
绘制方块。
```python
def draw_block(block):
x = block.col * BLOCK_SIZE + BLOCK_SIZE / 2
y = block.row * BLOCK_SIZE + BLOCK_SIZE / 2
pygame.draw.circle(screen, block.color, (x, y), BLOCK_SIZE // 2)
if block.selected:
pygame.draw.circle(screen, (255, 255, 255), (x, y), BLOCK_SIZE // 4)
```
生成随机方块。
```python
def generate_blocks():
blocks = []
for row in range(8):
for col in range(10):
color = random.choice(BLOCK_COLORS)
block = Block(row, col, color)
blocks.append(block)
return blocks
```
绘制游戏区域内的方块。
```python
def draw_blocks(blocks):
for block in blocks:
draw_block(block)
```
获取鼠标点击的方块。
```python
def get_clicked_block(blocks, pos):
for block in blocks:
x = block.col * BLOCK_SIZE + BLOCK_SIZE / 2
y = block.row * BLOCK_SIZE + BLOCK_SIZE / 2
distance = ((x - pos[0]) ** 2 + (y - pos[1]) ** 2) ** 0.5
if distance <= BLOCK_SIZE / 2:
return block
return None
```
判断两个方块是否可以相互连接。
```python
def can_connect(block1, block2):
if block1.color != block2.color:
return False
if block1.row == block2.row:
for col in range(block1.col + 1, block2.col):
if get_block_by_row_col(block1.row, col) is not None:
return False
return True
elif block1.col == block2.col:
for row in range(block1.row + 1, block2.row):
if get_block_by_row_col(row, block1.col) is not None:
return False
return True
else:
return False
```
获取指定行列的方块。
```python
def get_block_by_row_col(row, col):
for block in blocks:
if block.row == row and block.col == col:
return block
return None
```
消除两个方块之间的所有方块。
```python
def remove_blocks(blocks, block1, block2):
for row in range(8):
for col in range(10):
block = get_block_by_row_col(row, col)
if block is not None and block != block1 and block != block2:
if can_connect(block1, block) and can_connect(block, block2):
blocks.remove(block)
```
判断游戏是否结束。
```python
def is_game_over(blocks):
return len(blocks) == 0
```
主函数中实现游戏循环。
```python
def main():
blocks = generate_blocks()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
block = get_clicked_block(blocks, pos)
if block is not None:
if not block.selected:
block.selected = True
else:
for other_block in blocks:
if other_block.selected and can_connect(block, other_block):
remove_blocks(blocks, block, other_block)
block.selected = False
other_block.selected = False
break
screen.fill((0, 0, 0))
draw_blocks(blocks)
pygame.display.update()
if is_game_over(blocks):
pygame.quit()
return
if __name__ == "__main__":
main()
```
这就是一个简单的连连看小游戏的 Python 实现。您可以根据自己的需求进行修改和扩展。
阅读全文