HeadBallGame/README.md
2025-03-01 00:59:12 +08:00

108 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# AI头控球球大逃亡
## 简介
这是一个基于 Pygame 和 MediaPipe 实现的小游戏。玩家通过控制自己的头部移动来避开下落的障碍物,同时收集时间值。使用计算机的摄像头进行人脸检测,动态控制游戏中的小球进行左右移动。
## 特性
- 使用 **MediaPipe** 进行实时人脸检测
-**Pygame** 构建游戏界面
- 障碍物动态生成,增加游戏挑战性
- 实时计时显示,记录游戏时间
- 支持窗口化操作和实时反馈
## 环境要求
- Python 3.12.x
- 使用 PDM 进行依赖管理
## 使用 PDM 安装依赖
首先,确保你的系统上已经安装了 PDM。然后在项目根目录下通过以下命令安装项目依赖
```bash
pdm install
```
指定 Python 版本为 3.12 的依赖将会被安装,如下所列:
```toml
[project]
name = "HeadBallGame"
version = "0.1.0"
description = "Default template for PDM package"
authors = [
{name = "Zengtudor", email = "Zengtudor@outlook.com"},
]
dependencies = ["opencv-python>=4.11.0.86", "mediapipe>=0.10.21", "numpy>=1.26.4", "pygame>=2.6.1"]
requires-python = "==3.12.*"
readme = "README.md"
license = {text = "MIT"}
[tool.pdm]
distribution = false
```
## 使用方法
1. 确保你拥有正常工作的摄像头。
2. 运行游戏的主程序:
```bash
pdm run python main.py
```
3. 根据摄像头的反馈,移动头部来控制游戏中的小球左右移动,避开障碍物。
## 代码示例
以下是游戏的核心代码片段,用于说明主要功能的实现:
```python
import cv2
import mediapipe as mp
import pygame
import time
import random
# 游戏初始化
pygame.init()
font = pygame.font.Font(None, 36)
screen = pygame.display.set_mode((600, 800))
ball_radius = 20
ball_color = (255, 105, 180)
# 摄像头初始化
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# MediaPipe初始化
mp_face = mp.solutions.face_detection
face_detection = mp_face.FaceDetection(min_detection_confidence=0.5)
# 游戏逻辑和绘制函数
clock = pygame.time.Clock()
obstacles = []
# ...(省略一些代码)...
def draw_ball(normalized_x, normalized_y):
# ...(绘制小球及障碍物的逻辑)...
```
## 注意事项
- 由于该游戏需要使用摄像头进行人脸检测和控制,请确保在良好的光线条件下进行游戏,以获得最佳效果。
- 如果检测效果不佳,请尝试调整摄像头的位置。
## 贡献
欢迎任何对项目功能和性能的改进或反馈。您可以通过提交 issue 或 pull requests 的方式贡献。
## License
本项目采用 MIT 许可证,具体内容请查阅 LICENSE 文件。
---
希望这个游戏能为您带来乐趣!尽情享受游戏吧!