You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.7 KiB
87 lines
2.7 KiB
6 months ago
|
import pygame
|
||
|
import random
|
||
|
import os
|
||
|
pygame.mixer.init()
|
||
|
# 修改音乐的 file 路径
|
||
|
file ='h.ogg'
|
||
|
track = pygame.mixer.music.load(file)
|
||
|
pygame.mixer.music.play()
|
||
|
# 初始化 Pygame
|
||
|
pygame.init()
|
||
|
|
||
|
# 设置窗口大小和标题
|
||
|
window_width =600
|
||
|
window_height = 600
|
||
|
window_title = "拼图游戏"
|
||
|
screen = pygame.display.set_mode((window_width, window_height))
|
||
|
pygame.display.set_caption(window_title)
|
||
|
|
||
|
# 加载拼图图片
|
||
|
puzzle_image = pygame.image.load("3.jpg")
|
||
|
|
||
|
# 分割拼图图片
|
||
|
puzzle_width, puzzle_height = puzzle_image.get_size()
|
||
|
tile_size = 200
|
||
|
num_tiles_x = puzzle_width // tile_size
|
||
|
num_tiles_y = puzzle_height // tile_size
|
||
|
puzzle_tiles = []
|
||
|
for x in range(num_tiles_x):
|
||
|
for y in range(num_tiles_y):
|
||
|
tile_rect = (x * tile_size, y * tile_size, tile_size, tile_size)
|
||
|
puzzle_tiles.append(puzzle_image.subsurface(tile_rect))
|
||
|
|
||
|
# 随机打乱拼图顺序
|
||
|
random.shuffle(puzzle_tiles)
|
||
|
|
||
|
# 游戏初始化状态
|
||
|
is_game_over = False
|
||
|
current_tile = 0
|
||
|
|
||
|
# 游戏主循环
|
||
|
while not is_game_over:
|
||
|
for event in pygame.event.get():
|
||
|
# 窗口关闭事件
|
||
|
if puzzle_tiles == puzzle_tiles[current_tile]:
|
||
|
is_game_over = True
|
||
|
if event.type == pygame.QUIT:
|
||
|
is_game_over = True
|
||
|
|
||
|
# 鼠标点击事件
|
||
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||
|
if pygame.mouse.get_pressed()[0] and not is_game_over:
|
||
|
mx, my = pygame.mouse.get_pos()
|
||
|
x = int(mx // tile_size)
|
||
|
y = int(my // tile_size)
|
||
|
|
||
|
if 0 <= x < num_tiles_x and 0 <= y < num_tiles_y:
|
||
|
# 检查点击的位置是否有拼图块
|
||
|
if puzzle_tiles[y * num_tiles_x + x] is not None:
|
||
|
# 交换当前拼图块和点击位置的拼图块
|
||
|
puzzle_tiles[current_tile], puzzle_tiles[y * num_tiles_x + x] = puzzle_tiles[y * num_tiles_x + x], puzzle_tiles[current_tile]
|
||
|
|
||
|
# 更新当前拼图块的位置
|
||
|
current_tile = (y * num_tiles_x + x) % len(puzzle_tiles)
|
||
|
|
||
|
if all(puzzle_tiles[i] is None for i in range(len(puzzle_tiles) - 1)):
|
||
|
print("恭喜你")
|
||
|
screen.fill((255, 255, 255))
|
||
|
import cs
|
||
|
# 检查是否完成拼图
|
||
|
|
||
|
|
||
|
|
||
|
# 清除屏幕
|
||
|
screen.fill((0, 0, 0))
|
||
|
|
||
|
# 绘制拼图块
|
||
|
for i, tile in enumerate(puzzle_tiles):
|
||
|
if tile is not None:
|
||
|
x = i % num_tiles_x * tile_size
|
||
|
y = i // num_tiles_x * tile_size
|
||
|
cs=screen.blit(tile, (x, y))
|
||
|
|
||
|
# 刷新屏幕
|
||
|
pygame.display.flip()
|
||
|
|
||
|
# 退出游戏
|
||
|
pygame.quit()
|