python连连看小游戏代码
时间: 2023-11-28 18:05:44 浏览: 209
Python连连看小游戏源代码
5星 · 资源好评率100%
以下是一个简单的Python连连看小游戏代码,它使用了Pygame库来实现游戏界面和交互逻辑。游戏规则是点击两个相同的图标,如果它们之间的路径不超过两个拐角,那么它们就会消失。游戏结束的条件是所有的图标都被消除或者没有可消除的图标了。
```python
import pygame
import random
# 游戏界面参数
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
ICON_SIZE = 40
ICON_ROWS = 10
ICON_COLS = 15
ICON_MARGIN = 5
BG_COLOR = (255, 255, 255)
FONT_COLOR = (0, 0, 0)
FONT_SIZE = 30
# 图标种类和数量
ICON_TYPES = 25
ICON_COUNT = ICON_ROWS * ICON_COLS // 2
# 加载图标图片
ICON_IMAGES = []
for i in range(ICON_TYPES):
image = pygame.image.load(f"icon{i+1}.png")
image = pygame.transform.scale(image, (ICON_SIZE, ICON_SIZE))
ICON_IMAGES.append(image)
# 初始化Pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("连连看")
font = pygame.font.SysFont(None, FONT_SIZE)
# 随机生成图标矩阵
icons = []
for i in range(ICON_COUNT):
icon = random.randint(0, ICON_TYPES-1)
icons.append(icon)
icons.append(icon)
random.shuffle(icons)
grid = [[-1] * ICON_COLS for _ in range(ICON_ROWS)]
for i in range(ICON_ROWS):
for j in range(ICON_COLS):
grid[i][j] = icons[i*ICON_COLS+j]
# 计算图标位置
icon_width = ICON_SIZE + ICON_MARGIN
icon_height = ICON_SIZE + ICON_MARGIN
grid_width = icon_width * ICON_COLS
grid_height = icon_height * ICON_ROWS
grid_x = (SCREEN_WIDTH - grid_width) // 2
grid_y = (SCREEN_HEIGHT - grid_height) // 2
# 绘制图标
def draw_icon(i, j):
icon = grid[i][j]
if icon >= 0:
x = grid_x + j * icon_width + ICON_MARGIN // 2
y = grid_y + i * icon_height + ICON_MARGIN // 2
screen.blit(ICON_IMAGES[icon], (x, y))
# 绘制界面
def draw():
screen.fill(BG_COLOR)
for i in range(ICON_ROWS):
for j in range(ICON_COLS):
draw_icon(i, j)
pygame.display.update()
# 判断两个图标是否可消除
def can_connect(i1, j1, i2, j2):
if i1 == i2 and j1 == j2:
return False
if grid[i1][j1] != grid[i2][j2]:
return False
if i1 == i2:
if abs(j1-j2) == 1:
return True
for k in range(min(j1, j2)+1, max(j1, j2)):
if grid[i1][k] != -1:
return False
return True
if j1 == j2:
if abs(i1-i2) == 1:
return True
for k in range(min(i1, i2)+1, max(i1, i2)):
if grid[k][j1] != -1:
return False
return True
return False
# 查找两个图标之间的路径
def find_path(i1, j1, i2, j2):
if i1 == i2 and j1 == j2:
return []
if grid[i1][j1] != grid[i2][j2]:
return []
if i1 == i2:
if abs(j1-j2) == 1:
return [(i1, j1), (i2, j2)]
for k in range(min(j1, j2)+1, max(j1, j2)):
if grid[i1][k] != -1:
return []
return [(i1, j1), (i1, j2), (i2, j2)]
if j1 == j2:
if abs(i1-i2) == 1:
return [(i1, j1), (i2, j2)]
for k in range(min(i1, i2)+1, max(i1, i2)):
if grid[k][j1] != -1:
return []
return [(i1, j1), (i2, j1), (i2, j2)]
return []
# 处理鼠标点击事件
def handle_click(pos):
x, y = pos
if x < grid_x or x >= grid_x + grid_width:
return
if y < grid_y or y >= grid_y + grid_height:
return
i = (y - grid_y) // icon_height
j = (x - grid_x) // icon_width
if grid[i][j] == -1:
return
if selected:
if can_connect(selected[0], selected[1], i, j):
path = find_path(selected[0], selected[1], i, j)
if path:
grid[selected[0]][selected[1]] = -1
grid[i][j] = -1
draw()
pygame.time.wait(500)
selected.clear()
check_win()
else:
selected.clear()
else:
selected.append(i)
selected.append(j)
# 检查是否胜利
def check_win():
for i in range(ICON_ROWS):
for j in range(ICON_COLS):
if grid[i][j] != -1:
return
text = font.render("You Win!", True, FONT_COLOR)
x = (SCREEN_WIDTH - text.get_width()) // 2
y = (SCREEN_HEIGHT - text.get_height()) // 2
screen.blit(text, (x, y))
pygame.display.update()
pygame.time.wait(3000)
pygame.quit()
exit()
# 主循环
selected = []
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
handle_click(pygame.mouse.get_pos())
draw()
# 退出Pygame
pygame.quit()
```
阅读全文