diff --git a/AI/Game/Convert.py b/AI/Game/Convert.py new file mode 100644 index 00000000..bbf913b1 --- /dev/null +++ b/AI/Game/Convert.py @@ -0,0 +1,24 @@ +import cv2 +import os + +# 创建输出文件夹 +output_folder = "frames" +os.makedirs(output_folder, exist_ok=True) + +# 打开视频文件 +video = cv2.VideoCapture("./Movie/vid_0.mov") +frame_count = 0 + +while video.isOpened(): + ret, frame = video.read() + if not ret: + break + + # 保存帧为 PNG 图片 + frame_path = os.path.join(output_folder, f"frame_{frame_count:04d}.png") + cv2.imwrite(frame_path, frame) + frame_count += 1 + +# 释放资源 +video.release() +print(f"Saved {frame_count} frames to {output_folder}") \ No newline at end of file diff --git a/AI/Game/KeyBoard.py b/AI/Game/KeyBoard.py new file mode 100644 index 00000000..39e0c96f --- /dev/null +++ b/AI/Game/KeyBoard.py @@ -0,0 +1,32 @@ +import sys +import pygame + +pygame.init() +screen = pygame.display.set_mode((640, 480)) # 设置屏幕大小 +pygame.display.set_caption('keyboard ctrl') # 设置窗口标题 +screen.fill((0, 0, 0)) # 屏幕填充RGB颜色 + +while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + + keys = pygame.key.get_pressed() + for i in range(26): + if keys[pygame.K_a + i]: + print(chr(pygame.K_a + i)) + + # 检测上下左右键 # + if keys[pygame.K_UP]: + print("Up arrow") + if keys[pygame.K_DOWN]: + print("Down arrow") + if keys[pygame.K_LEFT]: + print("Left arrow") + if keys[pygame.K_RIGHT]: + print("Right arrow") + + # ESC to quit # + if keys[pygame.K_ESCAPE]: + pygame.quit() + sys.exit() \ No newline at end of file diff --git a/AI/Game/Mouse.py b/AI/Game/Mouse.py new file mode 100644 index 00000000..81285dd5 --- /dev/null +++ b/AI/Game/Mouse.py @@ -0,0 +1,25 @@ +import sys +import pygame + +pygame.init() +screen = pygame.display.set_mode((640, 480)) # 设置屏幕大小 +pygame.display.set_caption('mouse ctrl') # 设置窗口标题 +screen.fill((0, 0, 0)) # 屏幕填充RGB颜色 + +while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + + # 处理鼠标事件 # + if event.type == pygame.MOUSEBUTTONDOWN: + print("Mouse Down:", event) + if event.type == pygame.MOUSEBUTTONUP: + print("Mouse Up:", event) + if event.type == pygame.MOUSEMOTION: + print("Mouse is moving now:", event) + + # 处理鼠标事件 # + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_RETURN: + print("keyboard event:", event) \ No newline at end of file diff --git a/AI/Game/Movie/data.json b/AI/Game/Movie/data.json new file mode 100644 index 00000000..df467c68 --- /dev/null +++ b/AI/Game/Movie/data.json @@ -0,0 +1 @@ +{"v":"5.12.1","fr":30.0003051757812,"ip":0,"op":160.001627604167,"w":713,"h":1080,"nm":"人物动画02","ddd":0,"assets":[{"id":"video_0","w":713,"h":1080,"u":"images/","p":"vid_0.mov","e":0}],"layers":[{"ddd":0,"ind":1,"ty":9,"nm":"vid_0.mov","cl":"mov","refId":"video_0","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":0,"k":0,"ix":10},"p":{"a":0,"k":[356.5,540,0],"ix":2,"l":2},"a":{"a":0,"k":[356.5,540,0],"ix":1,"l":2},"s":{"a":0,"k":[100,100,100],"ix":6,"l":2}},"ao":0,"ip":0,"op":160.001627604167,"st":0,"bm":0}],"markers":[],"props":{}} \ No newline at end of file diff --git a/AI/Game/Movie/vid_0.mov b/AI/Game/Movie/vid_0.mov new file mode 100644 index 00000000..cad624d8 Binary files /dev/null and b/AI/Game/Movie/vid_0.mov differ diff --git a/AI/Game/Play.py b/AI/Game/Play.py new file mode 100644 index 00000000..edb735e0 --- /dev/null +++ b/AI/Game/Play.py @@ -0,0 +1,74 @@ +import pygame +import os + +# 初始化 Pygame +pygame.init() +window_width = 800 +window_height = 600 +window = pygame.display.set_mode((window_width, window_height)) +pygame.display.set_caption("小人动画") +clock = pygame.time.Clock() + +# 加载帧序列 +frames = [] +frame_folder = "frames" +for frame_file in sorted(os.listdir(frame_folder)): + if frame_file.endswith(".png"): + frame = pygame.image.load(os.path.join(frame_folder, frame_file)) + # 等比例缩放帧图片 + frame_width, frame_height = frame.get_size() + scale_factor = min(window_width / frame_width, window_height / frame_height) + scaled_frame = pygame.transform.scale(frame, (int(frame_width * scale_factor), int(frame_height * scale_factor))) + frames.append(scaled_frame) + +# 设置字体(使用支持中文的字体文件) +font_path = "c:/Windows/Fonts/simhei.ttf" # 替换为你的中文字体文件路径 +font = pygame.font.Font(font_path, 36) # 使用中文字体,字号 36 + +# 播放动画 +current_frame = 0 +running = True +show_message = False # 是否显示提示框 +message = "别乱点!" # 提示框内容 +message_timer = 0 # 提示框显示时间 + +while running: + # 处理事件 + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.MOUSEBUTTONDOWN: # 检测鼠标点击 + mouse_pos = pygame.mouse.get_pos() # 获取鼠标点击位置 + print(f"Mouse clicked at: {mouse_pos}") # 打印鼠标点击位置 + show_message = True # 显示提示框 + message_timer = pygame.time.get_ticks() # 记录当前时间 + + # 绘制当前帧 + window.fill((0, 0, 0)) # 清空屏幕 + # 将帧居中显示 + frame_width, frame_height = frames[current_frame].get_size() + x = (window_width - frame_width) // 2 + y = (window_height - frame_height) // 2 + window.blit(frames[current_frame], (x, y)) + + # 显示提示框 + if show_message: + # 绘制提示框背景 + message_surface = font.render(message, True, (255, 255, 255)) # 白色文字 + message_rect = message_surface.get_rect(center=(window_width // 2, window_height - 50)) # 提示框位置在窗口底部 + pygame.draw.rect(window, (0, 0, 0), message_rect.inflate(20, 10)) # 黑色背景 + window.blit(message_surface, message_rect) + + # 5 秒后隐藏提示框 + if pygame.time.get_ticks() - message_timer > 5000: # 检查是否超过 5 秒 + show_message = False + + # 更新帧索引 + current_frame = (current_frame + 1) % len(frames) + + # 更新屏幕 + pygame.display.flip() + clock.tick(24) # 控制帧率 + +# 退出 Pygame +pygame.quit() \ No newline at end of file diff --git a/AI/Game/Sample.py b/AI/Game/Sample.py new file mode 100644 index 00000000..65a0eb47 --- /dev/null +++ b/AI/Game/Sample.py @@ -0,0 +1,117 @@ +import pygame +import sys +import random + +# 初始化 Pygame +pygame.init() + +# 设置窗口尺寸 +window_width = 800 +window_height = 600 +window = pygame.display.set_mode((window_width, window_height)) +pygame.display.set_caption("弹球小游戏") + +# 设置颜色 +WHITE = (255, 255, 255) +RED = (255, 0, 0) +BLUE = (0, 0, 255) + +# 设置小球初始位置和速度 +ball_radius = 10 +ball_x = random.randint(ball_radius, window_width - ball_radius) +ball_y = ball_radius +ball_speed_x = random.choice([-5, 5]) +ball_speed_y = 5 + +# 设置挡板初始位置和大小 +paddle_width = 100 +paddle_height = 20 +paddle_x = (window_width - paddle_width) // 2 +paddle_y = window_height - paddle_height - 10 +paddle_speed = 10 + +# 设置时钟 +clock = pygame.time.Clock() + +# 游戏状态 +game_over = False + +# 重置游戏 +def reset_game(): + global ball_x, ball_y, ball_speed_x, ball_speed_y, paddle_x, game_over + ball_x = random.randint(ball_radius, window_width - ball_radius) + ball_y = ball_radius + ball_speed_x = random.choice([-5, 5]) + ball_speed_y = 5 + paddle_x = (window_width - paddle_width) // 2 + game_over = False + +# 主循环 +running = True +while running: + # 处理事件 + for event in pygame.event.get(): + if event.type == pygame.QUIT: # 检测窗口关闭事件 + running = False + elif event.type == pygame.KEYDOWN: # 检测键盘按下事件 + if event.key == pygame.K_RETURN and game_over: # 按下回车键重新开始 + reset_game() + + # 检测键盘持续按下状态 + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT]: # 按下左箭头键 + paddle_x -= paddle_speed + if keys[pygame.K_RIGHT]: # 按下右箭头键 + paddle_x += paddle_speed + + # 限制挡板在窗口内移动 + paddle_x = max(0, min(paddle_x, window_width - paddle_width)) + + # 更新小球位置 + if not game_over: + ball_x += ball_speed_x + ball_y += ball_speed_y + + # 小球碰到左右边界时反弹 + if ball_x <= ball_radius or ball_x >= window_width - ball_radius: + ball_speed_x = -ball_speed_x + + # 小球碰到上边界时反弹 + if ball_y <= ball_radius: + ball_speed_y = -ball_speed_y + + # 小球碰到挡板时反弹 + if (paddle_x <= ball_x <= paddle_x + paddle_width and + paddle_y <= ball_y + ball_radius <= paddle_y + paddle_height): + ball_speed_y = -ball_speed_y + + # 小球碰到下边界时游戏结束 + if ball_y >= window_height - ball_radius: + game_over = True + + # 清空屏幕 + window.fill(WHITE) + + # 绘制小球 + pygame.draw.circle(window, RED, (ball_x, ball_y), ball_radius) + + # 绘制挡板 + pygame.draw.rect(window, BLUE, (paddle_x, paddle_y, paddle_width, paddle_height)) + + # 显示游戏结束信息 + if game_over: + font_path = "c:/Windows/Fonts/simhei.ttf" + font = pygame.font.Font(font_path, 74) + text = font.render("游戏结束", True, (0, 0, 0)) + text_rect = text.get_rect(center=(window_width // 2, window_height // 2)) + window.blit(text, text_rect) + + # 更新屏幕 + pygame.display.flip() + + # 控制帧率 + clock.tick(60) + +# 退出 Pygame +pygame.quit() +sys.exit() \ No newline at end of file diff --git a/AI/Game/__init__.py b/AI/Game/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/AI/Game/frames/frame_0000.png b/AI/Game/frames/frame_0000.png new file mode 100644 index 00000000..640fb63c Binary files /dev/null and b/AI/Game/frames/frame_0000.png differ diff --git a/AI/Game/frames/frame_0001.png b/AI/Game/frames/frame_0001.png new file mode 100644 index 00000000..0ccb673e Binary files /dev/null and b/AI/Game/frames/frame_0001.png differ diff --git a/AI/Game/frames/frame_0002.png b/AI/Game/frames/frame_0002.png new file mode 100644 index 00000000..51da70ef Binary files /dev/null and b/AI/Game/frames/frame_0002.png differ diff --git a/AI/Game/frames/frame_0003.png b/AI/Game/frames/frame_0003.png new file mode 100644 index 00000000..828b2a0f Binary files /dev/null and b/AI/Game/frames/frame_0003.png differ diff --git a/AI/Game/frames/frame_0004.png b/AI/Game/frames/frame_0004.png new file mode 100644 index 00000000..4eaecc50 Binary files /dev/null and b/AI/Game/frames/frame_0004.png differ diff --git a/AI/Game/frames/frame_0005.png b/AI/Game/frames/frame_0005.png new file mode 100644 index 00000000..c68679f5 Binary files /dev/null and b/AI/Game/frames/frame_0005.png differ diff --git a/AI/Game/frames/frame_0006.png b/AI/Game/frames/frame_0006.png new file mode 100644 index 00000000..02505159 Binary files /dev/null and b/AI/Game/frames/frame_0006.png differ diff --git a/AI/Game/frames/frame_0007.png b/AI/Game/frames/frame_0007.png new file mode 100644 index 00000000..bc5b0d34 Binary files /dev/null and b/AI/Game/frames/frame_0007.png differ diff --git a/AI/Game/frames/frame_0008.png b/AI/Game/frames/frame_0008.png new file mode 100644 index 00000000..4a3c17c5 Binary files /dev/null and b/AI/Game/frames/frame_0008.png differ diff --git a/AI/Game/frames/frame_0009.png b/AI/Game/frames/frame_0009.png new file mode 100644 index 00000000..1289cdcb Binary files /dev/null and b/AI/Game/frames/frame_0009.png differ diff --git a/AI/Game/frames/frame_0010.png b/AI/Game/frames/frame_0010.png new file mode 100644 index 00000000..b2a66137 Binary files /dev/null and b/AI/Game/frames/frame_0010.png differ diff --git a/AI/Game/frames/frame_0011.png b/AI/Game/frames/frame_0011.png new file mode 100644 index 00000000..b3ac478d Binary files /dev/null and b/AI/Game/frames/frame_0011.png differ diff --git a/AI/Game/frames/frame_0012.png b/AI/Game/frames/frame_0012.png new file mode 100644 index 00000000..56673acb Binary files /dev/null and b/AI/Game/frames/frame_0012.png differ diff --git a/AI/Game/frames/frame_0013.png b/AI/Game/frames/frame_0013.png new file mode 100644 index 00000000..9635f371 Binary files /dev/null and b/AI/Game/frames/frame_0013.png differ diff --git a/AI/Game/frames/frame_0014.png b/AI/Game/frames/frame_0014.png new file mode 100644 index 00000000..fbc6b892 Binary files /dev/null and b/AI/Game/frames/frame_0014.png differ diff --git a/AI/Game/frames/frame_0015.png b/AI/Game/frames/frame_0015.png new file mode 100644 index 00000000..c973fd5f Binary files /dev/null and b/AI/Game/frames/frame_0015.png differ diff --git a/AI/Game/frames/frame_0016.png b/AI/Game/frames/frame_0016.png new file mode 100644 index 00000000..460e4a35 Binary files /dev/null and b/AI/Game/frames/frame_0016.png differ diff --git a/AI/Game/frames/frame_0017.png b/AI/Game/frames/frame_0017.png new file mode 100644 index 00000000..51f82c23 Binary files /dev/null and b/AI/Game/frames/frame_0017.png differ diff --git a/AI/Game/frames/frame_0018.png b/AI/Game/frames/frame_0018.png new file mode 100644 index 00000000..f2f6460d Binary files /dev/null and b/AI/Game/frames/frame_0018.png differ diff --git a/AI/Game/frames/frame_0019.png b/AI/Game/frames/frame_0019.png new file mode 100644 index 00000000..71344a96 Binary files /dev/null and b/AI/Game/frames/frame_0019.png differ diff --git a/AI/Game/frames/frame_0020.png b/AI/Game/frames/frame_0020.png new file mode 100644 index 00000000..f539dda6 Binary files /dev/null and b/AI/Game/frames/frame_0020.png differ diff --git a/AI/Game/frames/frame_0021.png b/AI/Game/frames/frame_0021.png new file mode 100644 index 00000000..8faa446d Binary files /dev/null and b/AI/Game/frames/frame_0021.png differ diff --git a/AI/Game/frames/frame_0022.png b/AI/Game/frames/frame_0022.png new file mode 100644 index 00000000..81ea1792 Binary files /dev/null and b/AI/Game/frames/frame_0022.png differ diff --git a/AI/Game/frames/frame_0023.png b/AI/Game/frames/frame_0023.png new file mode 100644 index 00000000..7f8f5547 Binary files /dev/null and b/AI/Game/frames/frame_0023.png differ diff --git a/AI/Game/frames/frame_0024.png b/AI/Game/frames/frame_0024.png new file mode 100644 index 00000000..ffe7baad Binary files /dev/null and b/AI/Game/frames/frame_0024.png differ diff --git a/AI/Game/frames/frame_0025.png b/AI/Game/frames/frame_0025.png new file mode 100644 index 00000000..51d9b791 Binary files /dev/null and b/AI/Game/frames/frame_0025.png differ diff --git a/AI/Game/frames/frame_0026.png b/AI/Game/frames/frame_0026.png new file mode 100644 index 00000000..c3a14d10 Binary files /dev/null and b/AI/Game/frames/frame_0026.png differ diff --git a/AI/Game/frames/frame_0027.png b/AI/Game/frames/frame_0027.png new file mode 100644 index 00000000..a27ccdad Binary files /dev/null and b/AI/Game/frames/frame_0027.png differ diff --git a/AI/Game/frames/frame_0028.png b/AI/Game/frames/frame_0028.png new file mode 100644 index 00000000..b6867413 Binary files /dev/null and b/AI/Game/frames/frame_0028.png differ diff --git a/AI/Game/frames/frame_0029.png b/AI/Game/frames/frame_0029.png new file mode 100644 index 00000000..ff6a368a Binary files /dev/null and b/AI/Game/frames/frame_0029.png differ diff --git a/AI/Game/frames/frame_0030.png b/AI/Game/frames/frame_0030.png new file mode 100644 index 00000000..3b817dc5 Binary files /dev/null and b/AI/Game/frames/frame_0030.png differ diff --git a/AI/Game/frames/frame_0031.png b/AI/Game/frames/frame_0031.png new file mode 100644 index 00000000..a828a7a8 Binary files /dev/null and b/AI/Game/frames/frame_0031.png differ diff --git a/AI/Game/frames/frame_0032.png b/AI/Game/frames/frame_0032.png new file mode 100644 index 00000000..2296cbcd Binary files /dev/null and b/AI/Game/frames/frame_0032.png differ diff --git a/AI/Game/frames/frame_0033.png b/AI/Game/frames/frame_0033.png new file mode 100644 index 00000000..0d27e64f Binary files /dev/null and b/AI/Game/frames/frame_0033.png differ diff --git a/AI/Game/frames/frame_0034.png b/AI/Game/frames/frame_0034.png new file mode 100644 index 00000000..d305dbbc Binary files /dev/null and b/AI/Game/frames/frame_0034.png differ diff --git a/AI/Game/frames/frame_0035.png b/AI/Game/frames/frame_0035.png new file mode 100644 index 00000000..071f83f6 Binary files /dev/null and b/AI/Game/frames/frame_0035.png differ diff --git a/AI/Game/frames/frame_0036.png b/AI/Game/frames/frame_0036.png new file mode 100644 index 00000000..1eee31a2 Binary files /dev/null and b/AI/Game/frames/frame_0036.png differ diff --git a/AI/Game/frames/frame_0037.png b/AI/Game/frames/frame_0037.png new file mode 100644 index 00000000..ead4be6c Binary files /dev/null and b/AI/Game/frames/frame_0037.png differ diff --git a/AI/Game/frames/frame_0038.png b/AI/Game/frames/frame_0038.png new file mode 100644 index 00000000..0bcd91b4 Binary files /dev/null and b/AI/Game/frames/frame_0038.png differ diff --git a/AI/Game/frames/frame_0039.png b/AI/Game/frames/frame_0039.png new file mode 100644 index 00000000..b49c8717 Binary files /dev/null and b/AI/Game/frames/frame_0039.png differ diff --git a/AI/Game/frames/frame_0040.png b/AI/Game/frames/frame_0040.png new file mode 100644 index 00000000..85482f62 Binary files /dev/null and b/AI/Game/frames/frame_0040.png differ diff --git a/AI/Game/frames/frame_0041.png b/AI/Game/frames/frame_0041.png new file mode 100644 index 00000000..76c5b330 Binary files /dev/null and b/AI/Game/frames/frame_0041.png differ diff --git a/AI/Game/frames/frame_0042.png b/AI/Game/frames/frame_0042.png new file mode 100644 index 00000000..c2cdbbbb Binary files /dev/null and b/AI/Game/frames/frame_0042.png differ diff --git a/AI/Game/frames/frame_0043.png b/AI/Game/frames/frame_0043.png new file mode 100644 index 00000000..ede1f767 Binary files /dev/null and b/AI/Game/frames/frame_0043.png differ diff --git a/AI/Game/frames/frame_0044.png b/AI/Game/frames/frame_0044.png new file mode 100644 index 00000000..85fe8867 Binary files /dev/null and b/AI/Game/frames/frame_0044.png differ diff --git a/AI/Game/frames/frame_0045.png b/AI/Game/frames/frame_0045.png new file mode 100644 index 00000000..f5a1166b Binary files /dev/null and b/AI/Game/frames/frame_0045.png differ diff --git a/AI/Game/frames/frame_0046.png b/AI/Game/frames/frame_0046.png new file mode 100644 index 00000000..37afe020 Binary files /dev/null and b/AI/Game/frames/frame_0046.png differ diff --git a/AI/Game/frames/frame_0047.png b/AI/Game/frames/frame_0047.png new file mode 100644 index 00000000..346a04a3 Binary files /dev/null and b/AI/Game/frames/frame_0047.png differ diff --git a/AI/Game/frames/frame_0048.png b/AI/Game/frames/frame_0048.png new file mode 100644 index 00000000..5002c388 Binary files /dev/null and b/AI/Game/frames/frame_0048.png differ diff --git a/AI/Game/frames/frame_0049.png b/AI/Game/frames/frame_0049.png new file mode 100644 index 00000000..1234b975 Binary files /dev/null and b/AI/Game/frames/frame_0049.png differ diff --git a/AI/Game/frames/frame_0050.png b/AI/Game/frames/frame_0050.png new file mode 100644 index 00000000..7cd0b3a6 Binary files /dev/null and b/AI/Game/frames/frame_0050.png differ diff --git a/AI/Game/frames/frame_0051.png b/AI/Game/frames/frame_0051.png new file mode 100644 index 00000000..a2857322 Binary files /dev/null and b/AI/Game/frames/frame_0051.png differ diff --git a/AI/Game/frames/frame_0052.png b/AI/Game/frames/frame_0052.png new file mode 100644 index 00000000..e8dfc34e Binary files /dev/null and b/AI/Game/frames/frame_0052.png differ diff --git a/AI/Game/frames/frame_0053.png b/AI/Game/frames/frame_0053.png new file mode 100644 index 00000000..f7fe002b Binary files /dev/null and b/AI/Game/frames/frame_0053.png differ diff --git a/AI/Game/frames/frame_0054.png b/AI/Game/frames/frame_0054.png new file mode 100644 index 00000000..30718710 Binary files /dev/null and b/AI/Game/frames/frame_0054.png differ diff --git a/AI/Game/frames/frame_0055.png b/AI/Game/frames/frame_0055.png new file mode 100644 index 00000000..8ec7a842 Binary files /dev/null and b/AI/Game/frames/frame_0055.png differ diff --git a/AI/Game/frames/frame_0056.png b/AI/Game/frames/frame_0056.png new file mode 100644 index 00000000..dbf2141e Binary files /dev/null and b/AI/Game/frames/frame_0056.png differ diff --git a/AI/Game/frames/frame_0057.png b/AI/Game/frames/frame_0057.png new file mode 100644 index 00000000..251d1657 Binary files /dev/null and b/AI/Game/frames/frame_0057.png differ diff --git a/AI/Game/frames/frame_0058.png b/AI/Game/frames/frame_0058.png new file mode 100644 index 00000000..3a7243b9 Binary files /dev/null and b/AI/Game/frames/frame_0058.png differ diff --git a/AI/Game/frames/frame_0059.png b/AI/Game/frames/frame_0059.png new file mode 100644 index 00000000..a02487ef Binary files /dev/null and b/AI/Game/frames/frame_0059.png differ diff --git a/AI/Game/frames/frame_0060.png b/AI/Game/frames/frame_0060.png new file mode 100644 index 00000000..a07a327b Binary files /dev/null and b/AI/Game/frames/frame_0060.png differ diff --git a/AI/Game/frames/frame_0061.png b/AI/Game/frames/frame_0061.png new file mode 100644 index 00000000..b3ca8c9d Binary files /dev/null and b/AI/Game/frames/frame_0061.png differ diff --git a/AI/Game/frames/frame_0062.png b/AI/Game/frames/frame_0062.png new file mode 100644 index 00000000..e8bf9864 Binary files /dev/null and b/AI/Game/frames/frame_0062.png differ diff --git a/AI/Game/frames/frame_0063.png b/AI/Game/frames/frame_0063.png new file mode 100644 index 00000000..17c0f395 Binary files /dev/null and b/AI/Game/frames/frame_0063.png differ diff --git a/AI/Game/frames/frame_0064.png b/AI/Game/frames/frame_0064.png new file mode 100644 index 00000000..8db07f07 Binary files /dev/null and b/AI/Game/frames/frame_0064.png differ diff --git a/AI/Game/frames/frame_0065.png b/AI/Game/frames/frame_0065.png new file mode 100644 index 00000000..1b6ff6ff Binary files /dev/null and b/AI/Game/frames/frame_0065.png differ diff --git a/AI/Game/frames/frame_0066.png b/AI/Game/frames/frame_0066.png new file mode 100644 index 00000000..932caf30 Binary files /dev/null and b/AI/Game/frames/frame_0066.png differ diff --git a/AI/Game/frames/frame_0067.png b/AI/Game/frames/frame_0067.png new file mode 100644 index 00000000..bacfe2f8 Binary files /dev/null and b/AI/Game/frames/frame_0067.png differ diff --git a/AI/Game/frames/frame_0068.png b/AI/Game/frames/frame_0068.png new file mode 100644 index 00000000..013b3cf6 Binary files /dev/null and b/AI/Game/frames/frame_0068.png differ diff --git a/AI/Game/frames/frame_0069.png b/AI/Game/frames/frame_0069.png new file mode 100644 index 00000000..729f3032 Binary files /dev/null and b/AI/Game/frames/frame_0069.png differ diff --git a/AI/Game/frames/frame_0070.png b/AI/Game/frames/frame_0070.png new file mode 100644 index 00000000..d66b1a4d Binary files /dev/null and b/AI/Game/frames/frame_0070.png differ diff --git a/AI/Game/frames/frame_0071.png b/AI/Game/frames/frame_0071.png new file mode 100644 index 00000000..a7e436a2 Binary files /dev/null and b/AI/Game/frames/frame_0071.png differ diff --git a/AI/Game/frames/frame_0072.png b/AI/Game/frames/frame_0072.png new file mode 100644 index 00000000..66dea111 Binary files /dev/null and b/AI/Game/frames/frame_0072.png differ diff --git a/AI/Game/frames/frame_0073.png b/AI/Game/frames/frame_0073.png new file mode 100644 index 00000000..7f32d54e Binary files /dev/null and b/AI/Game/frames/frame_0073.png differ diff --git a/AI/Game/frames/frame_0074.png b/AI/Game/frames/frame_0074.png new file mode 100644 index 00000000..8872fbf7 Binary files /dev/null and b/AI/Game/frames/frame_0074.png differ diff --git a/AI/Game/frames/frame_0075.png b/AI/Game/frames/frame_0075.png new file mode 100644 index 00000000..ca80780f Binary files /dev/null and b/AI/Game/frames/frame_0075.png differ diff --git a/AI/Game/frames/frame_0076.png b/AI/Game/frames/frame_0076.png new file mode 100644 index 00000000..b03a41ce Binary files /dev/null and b/AI/Game/frames/frame_0076.png differ diff --git a/AI/Game/frames/frame_0077.png b/AI/Game/frames/frame_0077.png new file mode 100644 index 00000000..ae9d70b8 Binary files /dev/null and b/AI/Game/frames/frame_0077.png differ diff --git a/AI/Game/frames/frame_0078.png b/AI/Game/frames/frame_0078.png new file mode 100644 index 00000000..c657e79c Binary files /dev/null and b/AI/Game/frames/frame_0078.png differ diff --git a/AI/Game/frames/frame_0079.png b/AI/Game/frames/frame_0079.png new file mode 100644 index 00000000..395fb66c Binary files /dev/null and b/AI/Game/frames/frame_0079.png differ diff --git a/AI/Game/frames/frame_0080.png b/AI/Game/frames/frame_0080.png new file mode 100644 index 00000000..22ddb6a2 Binary files /dev/null and b/AI/Game/frames/frame_0080.png differ diff --git a/AI/Game/frames/frame_0081.png b/AI/Game/frames/frame_0081.png new file mode 100644 index 00000000..0ccb673e Binary files /dev/null and b/AI/Game/frames/frame_0081.png differ diff --git a/AI/Game/frames/frame_0082.png b/AI/Game/frames/frame_0082.png new file mode 100644 index 00000000..51da70ef Binary files /dev/null and b/AI/Game/frames/frame_0082.png differ diff --git a/AI/Game/frames/frame_0083.png b/AI/Game/frames/frame_0083.png new file mode 100644 index 00000000..828b2a0f Binary files /dev/null and b/AI/Game/frames/frame_0083.png differ diff --git a/AI/Game/frames/frame_0084.png b/AI/Game/frames/frame_0084.png new file mode 100644 index 00000000..4eaecc50 Binary files /dev/null and b/AI/Game/frames/frame_0084.png differ diff --git a/AI/Game/frames/frame_0085.png b/AI/Game/frames/frame_0085.png new file mode 100644 index 00000000..c68679f5 Binary files /dev/null and b/AI/Game/frames/frame_0085.png differ diff --git a/AI/Game/frames/frame_0086.png b/AI/Game/frames/frame_0086.png new file mode 100644 index 00000000..02505159 Binary files /dev/null and b/AI/Game/frames/frame_0086.png differ diff --git a/AI/Game/frames/frame_0087.png b/AI/Game/frames/frame_0087.png new file mode 100644 index 00000000..bc5b0d34 Binary files /dev/null and b/AI/Game/frames/frame_0087.png differ diff --git a/AI/Game/frames/frame_0088.png b/AI/Game/frames/frame_0088.png new file mode 100644 index 00000000..4a3c17c5 Binary files /dev/null and b/AI/Game/frames/frame_0088.png differ diff --git a/AI/Game/frames/frame_0089.png b/AI/Game/frames/frame_0089.png new file mode 100644 index 00000000..1289cdcb Binary files /dev/null and b/AI/Game/frames/frame_0089.png differ diff --git a/AI/Game/frames/frame_0090.png b/AI/Game/frames/frame_0090.png new file mode 100644 index 00000000..b2a66137 Binary files /dev/null and b/AI/Game/frames/frame_0090.png differ diff --git a/AI/Game/frames/frame_0091.png b/AI/Game/frames/frame_0091.png new file mode 100644 index 00000000..b3ac478d Binary files /dev/null and b/AI/Game/frames/frame_0091.png differ diff --git a/AI/Game/frames/frame_0092.png b/AI/Game/frames/frame_0092.png new file mode 100644 index 00000000..56673acb Binary files /dev/null and b/AI/Game/frames/frame_0092.png differ diff --git a/AI/Game/frames/frame_0093.png b/AI/Game/frames/frame_0093.png new file mode 100644 index 00000000..9635f371 Binary files /dev/null and b/AI/Game/frames/frame_0093.png differ diff --git a/AI/Game/frames/frame_0094.png b/AI/Game/frames/frame_0094.png new file mode 100644 index 00000000..fbc6b892 Binary files /dev/null and b/AI/Game/frames/frame_0094.png differ diff --git a/AI/Game/frames/frame_0095.png b/AI/Game/frames/frame_0095.png new file mode 100644 index 00000000..c973fd5f Binary files /dev/null and b/AI/Game/frames/frame_0095.png differ diff --git a/AI/Game/frames/frame_0096.png b/AI/Game/frames/frame_0096.png new file mode 100644 index 00000000..460e4a35 Binary files /dev/null and b/AI/Game/frames/frame_0096.png differ diff --git a/AI/Game/frames/frame_0097.png b/AI/Game/frames/frame_0097.png new file mode 100644 index 00000000..51f82c23 Binary files /dev/null and b/AI/Game/frames/frame_0097.png differ diff --git a/AI/Game/frames/frame_0098.png b/AI/Game/frames/frame_0098.png new file mode 100644 index 00000000..f2f6460d Binary files /dev/null and b/AI/Game/frames/frame_0098.png differ diff --git a/AI/Game/frames/frame_0099.png b/AI/Game/frames/frame_0099.png new file mode 100644 index 00000000..71344a96 Binary files /dev/null and b/AI/Game/frames/frame_0099.png differ diff --git a/AI/Game/frames/frame_0100.png b/AI/Game/frames/frame_0100.png new file mode 100644 index 00000000..f539dda6 Binary files /dev/null and b/AI/Game/frames/frame_0100.png differ diff --git a/AI/Game/frames/frame_0101.png b/AI/Game/frames/frame_0101.png new file mode 100644 index 00000000..8faa446d Binary files /dev/null and b/AI/Game/frames/frame_0101.png differ diff --git a/AI/Game/frames/frame_0102.png b/AI/Game/frames/frame_0102.png new file mode 100644 index 00000000..81ea1792 Binary files /dev/null and b/AI/Game/frames/frame_0102.png differ diff --git a/AI/Game/frames/frame_0103.png b/AI/Game/frames/frame_0103.png new file mode 100644 index 00000000..7f8f5547 Binary files /dev/null and b/AI/Game/frames/frame_0103.png differ diff --git a/AI/Game/frames/frame_0104.png b/AI/Game/frames/frame_0104.png new file mode 100644 index 00000000..ffe7baad Binary files /dev/null and b/AI/Game/frames/frame_0104.png differ diff --git a/AI/Game/frames/frame_0105.png b/AI/Game/frames/frame_0105.png new file mode 100644 index 00000000..51d9b791 Binary files /dev/null and b/AI/Game/frames/frame_0105.png differ diff --git a/AI/Game/frames/frame_0106.png b/AI/Game/frames/frame_0106.png new file mode 100644 index 00000000..c3a14d10 Binary files /dev/null and b/AI/Game/frames/frame_0106.png differ diff --git a/AI/Game/frames/frame_0107.png b/AI/Game/frames/frame_0107.png new file mode 100644 index 00000000..a27ccdad Binary files /dev/null and b/AI/Game/frames/frame_0107.png differ diff --git a/AI/Game/frames/frame_0108.png b/AI/Game/frames/frame_0108.png new file mode 100644 index 00000000..b6867413 Binary files /dev/null and b/AI/Game/frames/frame_0108.png differ diff --git a/AI/Game/frames/frame_0109.png b/AI/Game/frames/frame_0109.png new file mode 100644 index 00000000..ff6a368a Binary files /dev/null and b/AI/Game/frames/frame_0109.png differ diff --git a/AI/Game/frames/frame_0110.png b/AI/Game/frames/frame_0110.png new file mode 100644 index 00000000..3b817dc5 Binary files /dev/null and b/AI/Game/frames/frame_0110.png differ diff --git a/AI/Game/frames/frame_0111.png b/AI/Game/frames/frame_0111.png new file mode 100644 index 00000000..a828a7a8 Binary files /dev/null and b/AI/Game/frames/frame_0111.png differ diff --git a/AI/Game/frames/frame_0112.png b/AI/Game/frames/frame_0112.png new file mode 100644 index 00000000..2296cbcd Binary files /dev/null and b/AI/Game/frames/frame_0112.png differ diff --git a/AI/Game/frames/frame_0113.png b/AI/Game/frames/frame_0113.png new file mode 100644 index 00000000..0d27e64f Binary files /dev/null and b/AI/Game/frames/frame_0113.png differ diff --git a/AI/Game/frames/frame_0114.png b/AI/Game/frames/frame_0114.png new file mode 100644 index 00000000..d305dbbc Binary files /dev/null and b/AI/Game/frames/frame_0114.png differ diff --git a/AI/Game/frames/frame_0115.png b/AI/Game/frames/frame_0115.png new file mode 100644 index 00000000..071f83f6 Binary files /dev/null and b/AI/Game/frames/frame_0115.png differ diff --git a/AI/Game/frames/frame_0116.png b/AI/Game/frames/frame_0116.png new file mode 100644 index 00000000..1eee31a2 Binary files /dev/null and b/AI/Game/frames/frame_0116.png differ diff --git a/AI/Game/frames/frame_0117.png b/AI/Game/frames/frame_0117.png new file mode 100644 index 00000000..ead4be6c Binary files /dev/null and b/AI/Game/frames/frame_0117.png differ diff --git a/AI/Game/frames/frame_0118.png b/AI/Game/frames/frame_0118.png new file mode 100644 index 00000000..0bcd91b4 Binary files /dev/null and b/AI/Game/frames/frame_0118.png differ diff --git a/AI/Game/frames/frame_0119.png b/AI/Game/frames/frame_0119.png new file mode 100644 index 00000000..b49c8717 Binary files /dev/null and b/AI/Game/frames/frame_0119.png differ diff --git a/AI/Game/frames/frame_0120.png b/AI/Game/frames/frame_0120.png new file mode 100644 index 00000000..85482f62 Binary files /dev/null and b/AI/Game/frames/frame_0120.png differ diff --git a/AI/Game/frames/frame_0121.png b/AI/Game/frames/frame_0121.png new file mode 100644 index 00000000..76c5b330 Binary files /dev/null and b/AI/Game/frames/frame_0121.png differ diff --git a/AI/Game/frames/frame_0122.png b/AI/Game/frames/frame_0122.png new file mode 100644 index 00000000..c2cdbbbb Binary files /dev/null and b/AI/Game/frames/frame_0122.png differ diff --git a/AI/Game/frames/frame_0123.png b/AI/Game/frames/frame_0123.png new file mode 100644 index 00000000..ede1f767 Binary files /dev/null and b/AI/Game/frames/frame_0123.png differ diff --git a/AI/Game/frames/frame_0124.png b/AI/Game/frames/frame_0124.png new file mode 100644 index 00000000..85fe8867 Binary files /dev/null and b/AI/Game/frames/frame_0124.png differ diff --git a/AI/Game/frames/frame_0125.png b/AI/Game/frames/frame_0125.png new file mode 100644 index 00000000..f5a1166b Binary files /dev/null and b/AI/Game/frames/frame_0125.png differ diff --git a/AI/Game/frames/frame_0126.png b/AI/Game/frames/frame_0126.png new file mode 100644 index 00000000..37afe020 Binary files /dev/null and b/AI/Game/frames/frame_0126.png differ diff --git a/AI/Game/frames/frame_0127.png b/AI/Game/frames/frame_0127.png new file mode 100644 index 00000000..346a04a3 Binary files /dev/null and b/AI/Game/frames/frame_0127.png differ diff --git a/AI/Game/frames/frame_0128.png b/AI/Game/frames/frame_0128.png new file mode 100644 index 00000000..5002c388 Binary files /dev/null and b/AI/Game/frames/frame_0128.png differ diff --git a/AI/Game/frames/frame_0129.png b/AI/Game/frames/frame_0129.png new file mode 100644 index 00000000..1234b975 Binary files /dev/null and b/AI/Game/frames/frame_0129.png differ diff --git a/AI/Game/frames/frame_0130.png b/AI/Game/frames/frame_0130.png new file mode 100644 index 00000000..7cd0b3a6 Binary files /dev/null and b/AI/Game/frames/frame_0130.png differ diff --git a/AI/Game/frames/frame_0131.png b/AI/Game/frames/frame_0131.png new file mode 100644 index 00000000..a2857322 Binary files /dev/null and b/AI/Game/frames/frame_0131.png differ diff --git a/AI/Game/frames/frame_0132.png b/AI/Game/frames/frame_0132.png new file mode 100644 index 00000000..e8dfc34e Binary files /dev/null and b/AI/Game/frames/frame_0132.png differ diff --git a/AI/Game/frames/frame_0133.png b/AI/Game/frames/frame_0133.png new file mode 100644 index 00000000..f7fe002b Binary files /dev/null and b/AI/Game/frames/frame_0133.png differ diff --git a/AI/Game/frames/frame_0134.png b/AI/Game/frames/frame_0134.png new file mode 100644 index 00000000..30718710 Binary files /dev/null and b/AI/Game/frames/frame_0134.png differ diff --git a/AI/Game/frames/frame_0135.png b/AI/Game/frames/frame_0135.png new file mode 100644 index 00000000..8ec7a842 Binary files /dev/null and b/AI/Game/frames/frame_0135.png differ diff --git a/AI/Game/frames/frame_0136.png b/AI/Game/frames/frame_0136.png new file mode 100644 index 00000000..dbf2141e Binary files /dev/null and b/AI/Game/frames/frame_0136.png differ diff --git a/AI/Game/frames/frame_0137.png b/AI/Game/frames/frame_0137.png new file mode 100644 index 00000000..251d1657 Binary files /dev/null and b/AI/Game/frames/frame_0137.png differ diff --git a/AI/Game/frames/frame_0138.png b/AI/Game/frames/frame_0138.png new file mode 100644 index 00000000..3a7243b9 Binary files /dev/null and b/AI/Game/frames/frame_0138.png differ diff --git a/AI/Game/frames/frame_0139.png b/AI/Game/frames/frame_0139.png new file mode 100644 index 00000000..a02487ef Binary files /dev/null and b/AI/Game/frames/frame_0139.png differ diff --git a/AI/Game/frames/frame_0140.png b/AI/Game/frames/frame_0140.png new file mode 100644 index 00000000..a07a327b Binary files /dev/null and b/AI/Game/frames/frame_0140.png differ diff --git a/AI/Game/frames/frame_0141.png b/AI/Game/frames/frame_0141.png new file mode 100644 index 00000000..b3ca8c9d Binary files /dev/null and b/AI/Game/frames/frame_0141.png differ diff --git a/AI/Game/frames/frame_0142.png b/AI/Game/frames/frame_0142.png new file mode 100644 index 00000000..e8bf9864 Binary files /dev/null and b/AI/Game/frames/frame_0142.png differ diff --git a/AI/Game/frames/frame_0143.png b/AI/Game/frames/frame_0143.png new file mode 100644 index 00000000..17c0f395 Binary files /dev/null and b/AI/Game/frames/frame_0143.png differ diff --git a/AI/Game/frames/frame_0144.png b/AI/Game/frames/frame_0144.png new file mode 100644 index 00000000..8db07f07 Binary files /dev/null and b/AI/Game/frames/frame_0144.png differ diff --git a/AI/Game/frames/frame_0145.png b/AI/Game/frames/frame_0145.png new file mode 100644 index 00000000..1b6ff6ff Binary files /dev/null and b/AI/Game/frames/frame_0145.png differ diff --git a/AI/Game/frames/frame_0146.png b/AI/Game/frames/frame_0146.png new file mode 100644 index 00000000..932caf30 Binary files /dev/null and b/AI/Game/frames/frame_0146.png differ diff --git a/AI/Game/frames/frame_0147.png b/AI/Game/frames/frame_0147.png new file mode 100644 index 00000000..bacfe2f8 Binary files /dev/null and b/AI/Game/frames/frame_0147.png differ diff --git a/AI/Game/frames/frame_0148.png b/AI/Game/frames/frame_0148.png new file mode 100644 index 00000000..013b3cf6 Binary files /dev/null and b/AI/Game/frames/frame_0148.png differ diff --git a/AI/Game/frames/frame_0149.png b/AI/Game/frames/frame_0149.png new file mode 100644 index 00000000..729f3032 Binary files /dev/null and b/AI/Game/frames/frame_0149.png differ diff --git a/AI/Game/frames/frame_0150.png b/AI/Game/frames/frame_0150.png new file mode 100644 index 00000000..d66b1a4d Binary files /dev/null and b/AI/Game/frames/frame_0150.png differ diff --git a/AI/Game/frames/frame_0151.png b/AI/Game/frames/frame_0151.png new file mode 100644 index 00000000..a7e436a2 Binary files /dev/null and b/AI/Game/frames/frame_0151.png differ diff --git a/AI/Game/frames/frame_0152.png b/AI/Game/frames/frame_0152.png new file mode 100644 index 00000000..66dea111 Binary files /dev/null and b/AI/Game/frames/frame_0152.png differ diff --git a/AI/Game/frames/frame_0153.png b/AI/Game/frames/frame_0153.png new file mode 100644 index 00000000..7f32d54e Binary files /dev/null and b/AI/Game/frames/frame_0153.png differ diff --git a/AI/Game/frames/frame_0154.png b/AI/Game/frames/frame_0154.png new file mode 100644 index 00000000..8872fbf7 Binary files /dev/null and b/AI/Game/frames/frame_0154.png differ diff --git a/AI/Game/frames/frame_0155.png b/AI/Game/frames/frame_0155.png new file mode 100644 index 00000000..ca80780f Binary files /dev/null and b/AI/Game/frames/frame_0155.png differ diff --git a/AI/Game/frames/frame_0156.png b/AI/Game/frames/frame_0156.png new file mode 100644 index 00000000..b03a41ce Binary files /dev/null and b/AI/Game/frames/frame_0156.png differ diff --git a/AI/Game/frames/frame_0157.png b/AI/Game/frames/frame_0157.png new file mode 100644 index 00000000..ae9d70b8 Binary files /dev/null and b/AI/Game/frames/frame_0157.png differ diff --git a/AI/Game/frames/frame_0158.png b/AI/Game/frames/frame_0158.png new file mode 100644 index 00000000..c657e79c Binary files /dev/null and b/AI/Game/frames/frame_0158.png differ diff --git a/AI/Game/frames/frame_0159.png b/AI/Game/frames/frame_0159.png new file mode 100644 index 00000000..395fb66c Binary files /dev/null and b/AI/Game/frames/frame_0159.png differ diff --git a/AI/Game/map.py b/AI/Game/map.py new file mode 100644 index 00000000..1cc355e5 --- /dev/null +++ b/AI/Game/map.py @@ -0,0 +1,180 @@ +import pygame +import random + +# 初始化 Pygame +pygame.init() + +# 设置窗口尺寸 +cell_size = 20 # 单元格大小 +grid_width = 30 # 网格宽度 +grid_height = 20 # 网格高度 +window_width = grid_width * cell_size +window_height = grid_height * cell_size +window = pygame.display.set_mode((window_width, window_height)) +pygame.display.set_caption("迷宫游戏") + +# 设置颜色 +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +RED = (255, 0, 0) +GREEN = (0, 255, 0) + +# 设置时钟 +clock = pygame.time.Clock() + + +# 生成随机迷宫 +def generate_maze(width, height): + # 初始化迷宫,所有单元格都是墙壁 + maze = [[1 for _ in range(width)] for _ in range(height)] + + # 确保入口和出口是通道 + maze[0][0] = 0 + maze[height - 1][width - 1] = 0 + + # 使用深度优先搜索生成迷宫 + def carve_passages(x, y): + # 随机选择方向 + directions = [(0, 2), (2, 0), (0, -2), (-2, 0)] + random.shuffle(directions) + + for dx, dy in directions: + nx, ny = x + dx, y + dy + + # 检查是否在边界内 + if 0 <= nx < width and 0 <= ny < height: + # 如果目标单元格是墙壁 + if maze[ny][nx] == 1: + # 打通墙壁 + maze[ny][nx] = 0 + # 打通中间的墙壁 + maze[y + dy // 2][x + dx // 2] = 0 + # 递归处理下一个单元格 + carve_passages(nx, ny) + + # 从入口开始生成迷宫 + carve_passages(0, 0) + + # 确保出口附近有通道 + maze[height - 2][width - 1] = 0 + maze[height - 1][width - 2] = 0 + + return maze + + +# 生成迷宫 +maze = generate_maze(grid_width, grid_height) + + +# 定义小人类 +class Character: + def __init__(self, grid_x, grid_y): + self.grid_x = grid_x # 小人的网格 x 坐标 + self.grid_y = grid_y # 小人的网格 y 坐标 + self.x = grid_x * cell_size + cell_size // 2 # 小人的像素 x 坐标 + self.y = grid_y * cell_size + cell_size // 2 # 小人的像素 y 坐标 + self.radius = cell_size // 3 # 小人的半径 + self.speed = 3 # 移动速度 + + def move(self, dx, dy): + # 计算目标位置 + new_x = self.x + dx + new_y = self.y + dy + + # 检测是否与墙壁碰撞 + if self.will_collide(new_x, new_y): + return + + # 更新位置 + self.x = new_x + self.y = new_y + self.grid_x = self.x // cell_size + self.grid_y = self.y // cell_size + + def will_collide(self, x, y): + # 检测小球是否会与墙壁碰撞 + # 计算小球边缘的四个点 + points = [ + (x - self.radius, y), # 左 + (x + self.radius, y), # 右 + (x, y - self.radius), # 上 + (x, y + self.radius) # 下 + ] + + # 检测每个点是否在墙壁内 + for px, py in points: + # 计算点所在的网格位置 + grid_x = px // cell_size + grid_y = py // cell_size + + # 检测是否超出边界 + if grid_x < 0 or grid_x >= grid_width or grid_y < 0 or grid_y >= grid_height: + return True + + # 检测是否与墙壁碰撞 + if maze[grid_y][grid_x] == 1: + return True + + return False + + def draw(self, window): + # 绘制小人(红色小球) + pygame.draw.circle(window, RED, (self.x, self.y), self.radius) + + +# 创建小人对象(初始位置在左上角) +character = Character(0, 0) + +# 设置出口位置 +exit_grid_x = grid_width - 1 +exit_grid_y = grid_height - 1 + +# 主循环 +running = True +while running: + # 处理事件 + for event in pygame.event.get(): + if event.type == pygame.QUIT: # 检测窗口关闭事件 + running = False + + # 检测键盘持续按下状态 + keys = pygame.key.get_pressed() + if keys[pygame.K_UP]: # 按下上箭头键 + character.move(0, -character.speed) + if keys[pygame.K_DOWN]: # 按下下箭头键 + character.move(0, character.speed) + if keys[pygame.K_LEFT]: # 按下左箭头键 + character.move(-character.speed, 0) + if keys[pygame.K_RIGHT]: # 按下右箭头键 + character.move(character.speed, 0) + + # 清空屏幕 + window.fill(WHITE) + + # 绘制迷宫 + for i in range(grid_height): + for j in range(grid_width): + if maze[i][j] == 1: # 绘制墙壁 + pygame.draw.rect(window, BLACK, (j * cell_size, i * cell_size, cell_size, cell_size)) + + # 绘制出口 + exit_x = exit_grid_x * cell_size + cell_size // 2 + exit_y = exit_grid_y * cell_size + cell_size // 2 + pygame.draw.circle(window, GREEN, (exit_x, exit_y), cell_size // 3) + + # 绘制小人 + character.draw(window) + + # 检测是否到达出口 + if abs(character.x - exit_x) < cell_size // 2 and abs(character.y - exit_y) < cell_size // 2: + print("胜利!") + running = False + + # 更新屏幕 + pygame.display.flip() + + # 控制帧率 + clock.tick(60) + +# 退出 Pygame +pygame.quit() \ No newline at end of file diff --git a/AI/WxMini/Milvus/Config/MulvusConfig.py b/AI/WxMini/Milvus/Config/MulvusConfig.py index 385f09a4..c983c891 100644 --- a/AI/WxMini/Milvus/Config/MulvusConfig.py +++ b/AI/WxMini/Milvus/Config/MulvusConfig.py @@ -8,7 +8,7 @@ MS_PORT = "19530" MS_COLLECTION_NAME = "ds_collection" # Milvus 连接池的最大连接数 -MS_MAX_CONNECTIONS = 5 +MS_MAX_CONNECTIONS = 50 # 腾讯 AI Lab 中文词向量模型的路径 MS_MODEL_PATH = "D:/Tencent_AILab_ChineseEmbedding/Tencent_AILab_ChineseEmbedding.txt" diff --git a/AI/WxMini/Milvus/Utils/MilvusConnectionPool.py b/AI/WxMini/Milvus/Utils/MilvusConnectionPool.py index a8416104..d7b4ab05 100644 --- a/AI/WxMini/Milvus/Utils/MilvusConnectionPool.py +++ b/AI/WxMini/Milvus/Utils/MilvusConnectionPool.py @@ -1,8 +1,12 @@ +import logging import threading from queue import Queue from pymilvus import connections +# 配置日志 +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") +logger = logging.getLogger(__name__) # 1. 手动实现 Milvus 连接池 class MilvusConnectionPool: @@ -31,6 +35,7 @@ class MilvusConnectionPool: return connections.connect(host=self.host, port=self.port, alias="default") def get_connection(self): + logger.info(f"获取连接,当前可用连接数: {self._pool.qsize()}") """ 从连接池中获取一个连接 :return: Milvus 连接对象 @@ -51,6 +56,7 @@ class MilvusConnectionPool: self._pool.put(connection) else: connections.disconnect("default") + logger.info(f"释放连接,当前可用连接数: {self._pool.qsize()}") def close(self): """ diff --git a/AI/WxMini/Start.py b/AI/WxMini/Start.py index e08aca42..06b248ed 100644 --- a/AI/WxMini/Start.py +++ b/AI/WxMini/Start.py @@ -1,5 +1,4 @@ import base64 -import base64 import datetime import json import time @@ -97,24 +96,29 @@ async def on_session_end(person_id): # 使用 asyncio.create_task 异步执行大模型调用 async def analyze_mental_health(): - response = await client.chat.completions.create( - model=MODEL_NAME, - messages=[ - {"role": "system", "content": "你是一个心理健康分析助手,负责分析用户的心理健康状况。"}, - {"role": "user", "content": prompt} - ], - max_tokens=1000 - ) + try: + response = await client.chat.completions.create( + model=MODEL_NAME, + messages=[ + {"role": "system", "content": "你是一个心理健康分析助手,负责分析用户的心理健康状况。"}, + {"role": "user", "content": prompt} + ], + max_tokens=1000 + ) - # 处理分析结果 - if response.choices and response.choices[0].message.content: - analysis_result = response.choices[0].message.content.strip() - if analysis_result.startswith("NO"): - # 异步执行 update_risk - await update_risk(app.state.mysql_pool, person_id, analysis_result) - logger.info(f"已异步更新 person_id={person_id} 的风险状态。") - else: - logger.info(f"AI大模型没有发现任何心理健康问题,用户会话 {person_id} 没有风险。") + # 处理分析结果 + if response.choices and response.choices[0].message.content: + analysis_result = response.choices[0].message.content.strip() + if analysis_result.startswith("NO"): + # 异步执行 update_risk + await update_risk(app.state.mysql_pool, person_id, analysis_result) + logger.info(f"已异步更新 person_id={person_id} 的风险状态。") + else: + logger.info(f"AI大模型没有发现任何心理健康问题,用户会话 {person_id} 没有风险。") + except Exception as e: + logger.error(f"AI大模型调用失败: {e}") + finally: + pass # 创建异步任务 asyncio.create_task(analyze_mental_health()) @@ -237,6 +241,7 @@ async def reply(person_id: str = Form(...), :return: 大模型的回复 """ logger.info(f"current_user= {current_user}") + connection = None try: logger.info(f"收到用户输入: {prompt}") @@ -295,18 +300,36 @@ async def reply(person_id: str = Form(...), # 限制历史交互提示词长度 history_prompt = history_prompt[:3000] - # 拼接交互提示词 + # 1.天气 if '天气' in prompt or '降温' in prompt or '气温' in prompt or '下雨' in prompt or '下雪' in prompt or '风' in prompt: weather_info = await get_weather('长春') history_prompt = f"天气信息: {weather_info}\n" logger.info(f"历史交互提示词: {history_prompt}") - # NBA与CBA - # result = await get_news(client, prompt) - # if result is not None: - # history_prompt = result - # print("新闻返回了下面的内容:" + result) + # 2.新闻 + result = await get_news(client, prompt) + if result is not None: + history_prompt = result + print("新闻返回了下面的内容:" + result) + + # 3.生图 + if '生成图片' in prompt or '生图' in prompt: + success, key = generate_image(prompt) + if success: + image_url = f"{OSS_PREFIX}{key}" + image_width, image_height = getImgWidthHeight(image_url) + # 记录聊天数据到 MySQL + await save_chat_to_mysql(app.state.mysql_pool, person_id, prompt, + key, '', 0, input_type=2, output_type=4, + input_image_type=0, image_width=image_width, image_height=image_height) + # 返回数据 + return { + "success": True, + "url": None, + "duration": 0, # 返回大模型的回复时长 + "response": key, # 返回大模型的回复 + } # 调用大模型,将历史交互作为提示词 try: @@ -315,7 +338,7 @@ async def reply(person_id: str = Form(...), model=MODEL_NAME, messages=[ {"role": "system", - "content": "你是一个和你聊天人的好朋友,疏导情绪,让他开心,亲切一些,不要使用哎呀这样的语气词。聊天的回复内容不要超过100字。"}, + "content": "你是聊天人的好朋友,你认识深刻,知识渊博,不要使用哎呀这样的语气词。聊天的回复内容不要超过150字。"}, {"role": "user", "content": f"历史对话记录:{history_prompt},本次用户问题: {prompt}"} ], max_tokens=4000 @@ -324,6 +347,7 @@ async def reply(person_id: str = Form(...), ) except asyncio.TimeoutError: logger.error("大模型调用超时") + milvus_pool.release_connection(connection) raise HTTPException(status_code=500, detail="大模型调用超时") # 提取生成的回复 @@ -373,7 +397,7 @@ async def reply(person_id: str = Form(...), # 调用会话检查机制,异步执行 asyncio.create_task(on_session_end(person_id)) - + milvus_pool.release_connection(connection) # 返回数据 return { "success": True, @@ -383,13 +407,13 @@ async def reply(person_id: str = Form(...), "response": result, # 返回大模型的回复 } else: + milvus_pool.release_connection(connection) raise HTTPException(status_code=500, detail="大模型未返回有效结果") except Exception as e: logger.error(f"调用大模型失败: {str(e)}") raise HTTPException(status_code=500, detail=f"调用大模型失败: {str(e)}") finally: - # 释放连接 - if 'connection' in locals() and connection: # 检查 connection 是否已定义且不为 None + if connection: milvus_pool.release_connection(connection) @@ -656,7 +680,7 @@ async def web_recognize_content(image_url: str, "message": f"生成内容失败: {str(e)}" }) yield error_response.encode("utf-8") - #yield error_response + # yield error_response # 使用 StreamingResponse 返回流式结果 return StreamingResponse( diff --git a/AI/WxMini/Utils/NewsUtil.py b/AI/WxMini/Utils/NewsUtil.py index 6cb9cfc3..b34036b0 100644 --- a/AI/WxMini/Utils/NewsUtil.py +++ b/AI/WxMini/Utils/NewsUtil.py @@ -17,7 +17,8 @@ def extract_keywords(user_input): # 使用 jieba 进行分词 words = jieba.lcut(user_input) # 过滤掉无意义的词(如标点符号、停用词等) - stop_words = ['的', '了', '吗', '呢', '是', '在', '啊', '呀', '怎么', '怎么样', '最近', '今天', '今日', '?', ',', + stop_words = ['的', '了', '吗', '呢', '是','你','知不知道','了解','多少', '在', '啊', '呀', '怎么', '怎么样', '有没有', '关于', '最近', '最新', + '今天', '今日', '?', ',', '。'] keywords = [word for word in words if word not in stop_words] return keywords @@ -88,6 +89,10 @@ async def format_results_with_ai(client, results): # 主函数 async def get_news(client, user_input): keywords = extract_keywords(user_input) + if '新闻' not in keywords: + return None + # 移除新闻两个字 + keywords.remove('新闻') results = search_based_on_keywords(keywords) if results: formatted_response = await format_results_with_ai(client, results) diff --git a/AI/WxMini/安装.txt b/AI/WxMini/安装.txt index 50721070..997a0720 100644 --- a/AI/WxMini/安装.txt +++ b/AI/WxMini/安装.txt @@ -7,19 +7,3 @@ pip show jieba pip show pymilvus - if '生成图片' in prompt or '生图' in prompt: - success, key = generate_image(prompt) - if success: - image_url = f"{OSS_PREFIX}{key}" - image_width, image_height = getImgWidthHeight(image_url) - # 记录聊天数据到 MySQL - await save_chat_to_mysql(app.state.mysql_pool, person_id, prompt, - key, '', 0, input_type=2, output_type=4, - input_image_type=0, image_width=image_width, image_height=image_height) - # 返回数据 - return { - "success": True, - "url": None, - "duration": 0, # 返回大模型的回复时长 - "response": key, # 返回大模型的回复 - } \ No newline at end of file