From da5183deeb67d82b463da38c26bfed04b6c87a35 Mon Sep 17 00:00:00 2001 From: p8hrgqm6a <2069279050@qq.com> Date: Sun, 2 Jun 2024 22:13:05 +0800 Subject: [PATCH] ADD file via upload --- pt.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 pt.py diff --git a/pt.py b/pt.py new file mode 100644 index 0000000..c6ea12a --- /dev/null +++ b/pt.py @@ -0,0 +1,87 @@ +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() \ No newline at end of file