import cv2
import mediapipe as mp
import numpy as np
import pygame
import os
import random
from orange import Orange
import time
import paho.mqtt.client as mqtt
import json
import threading
# 定义全局变量
idleflag = True #当此标志变量为False时,播放空闲照片和操作示例说明,单手直举起时表示开始
startnum = 2 #开始则设置为10秒倒计时
endnum = 2 #结束则设置为10秒倒计时
checknum = 600 #播放摄像头,没有检查到有人举手,减为0以后,再提示
gamenum = 2000 #游戏水果
gamespeed = 2 #速度倍数
flashnum = 0.5 #碰到水果时延迟的时间再确认为碰到
is_button_click = False
inkIf = False
# 确定PNG图片在视频帧上的位置
sleepy = 0
x = 10 # X坐标
y = 10 # Y坐标
# 生成一个范围在1到10(包括1和10)的随机整数
x = random.randint(1, 600)
myscore = 0
imagesize = 80
# 初始化图片、声音 开始
# 获取当前Python文件的绝对路径
current_script_path = os.path.abspath(__file__)
# 获取当前Python文件所在的目录
current_directory = os.path.dirname(current_script_path)
print("文件:",current_directory+"\\orange_transparent.png")
# 显示游戏示范图片
image_path = 'background.jpg' # 替换为你的图片路径
background_image = cv2.imread(current_directory+"\\footage\\image\\"+image_path, cv2.IMREAD_UNCHANGED)
girlok01 = cv2.imread(current_directory+"\\footage\\image\\"+image_path , cv2.IMREAD_UNCHANGED)
girlfail01 = cv2.imread(current_directory+"\\footage\\image\\"+image_path , cv2.IMREAD_UNCHANGED)
# 加载音效文件
# 初始化pygame mixer
pygame.mixer.init()
sound_fruit = pygame.mixer.Sound(current_directory+"\\footage\\sound\\splatter.mp3")
sound_iceBanana = pygame.mixer.Sound(current_directory+"\\footage\\sound\\iceBanana.mp3")
sound_potion = pygame.mixer.Sound(current_directory+"\\footage\\sound\\potion.ogg")
sound_boom = pygame.mixer.Sound(current_directory+"\\footage\\sound\\boom.mp3")
sound_durian = pygame.mixer.Sound(current_directory+"\\footage\\sound\\durian.wav")
# 加载图像并调整大小。
orange_img0 = cv2.imread(current_directory+"\\footage\\image\\fruit\\orange.png", cv2.IMREAD_UNCHANGED)
orange_explosion = cv2.imread(current_directory+"\\explosion_2.png", cv2.IMREAD_UNCHANGED)
watermelon_img0 = cv2.imread(current_directory+"\\footage\\image\\fruit\\watermelon.png", cv2.IMREAD_UNCHANGED)
watermelon_explosion1 = cv2.imread(current_directory+"\\footage\\image\\fruit\\watermelon-1.png", cv2.IMREAD_UNCHANGED)
watermelon_explosion2= cv2.imread(current_directory+"\\footage\\image\\fruit\\watermelon-2.png", cv2.IMREAD_UNCHANGED)
peach_img0 = cv2.imread(current_directory+"\\footage\\image\\fruit\\peach.png", cv2.IMREAD_UNCHANGED)
peach_explosion = cv2.imread(current_directory+"\\footage\\image\\fruit\\peach-1.png", cv2.IMREAD_UNCHANGED)
iceBanana_img0 = cv2.imread(current_directory+"\\footage\\image\\item\\iceBanana.png", cv2.IMREAD_UNCHANGED)
potion_img0 = cv2.imread(current_directory+"\\footage\\image\\item\\potion.png", cv2.IMREAD_UNCHANGED)
durian_img0 = cv2.imread(current_directory+"\\footage\\image\\fruit\\durian.png", cv2.IMREAD_UNCHANGED)
ink_img0 = cv2.imread(current_directory+"\\footage\\image\\item\\ink.png", cv2.IMREAD_UNCHANGED)
boom1 = cv2.imread(current_directory+"\\footage\\image\\fruit\\boom.png", cv2.IMREAD_UNCHANGED)
wuya2 = cv2.imread(current_directory+"\\footage\\image\\fruit\\boom.png", cv2.IMREAD_UNCHANGED)
orange_img = orange_img0 #表示当前显示的水果图像
# 调整橘子图像大小
orange_img0 = cv2.resize(orange_img0, (imagesize, imagesize)) # 将橘子图像调整为imagesizeximagesize像素
orange_explosion = cv2.resize(orange_explosion, (imagesize, imagesize)) # 将橘子图像调整为imagesizeximagesize像素
watermelon_img0 = cv2.resize(watermelon_img0 , (imagesize, imagesize))
watermelon_explosion1 = cv2.resize(watermelon_explosion1 , (imagesize, imagesize))
watermelon_explosion2 = cv2.resize(watermelon_explosion2 , (imagesize, imagesize))
peach_img0 = cv2.resize(peach_img0, (imagesize, imagesize))
peach_explosion = cv2.resize(peach_explosion, (imagesize, imagesize))
iceBanana_img0 = cv2.resize(iceBanana_img0,(imagesize,imagesize))
potion_img0 = cv2.resize(potion_img0,(imagesize,imagesize))
durian_img0 = cv2.resize(durian_img0,(imagesize,imagesize))
boom1 = cv2.resize(boom1, (imagesize, imagesize))
wuya2 = cv2.resize(wuya2, (imagesize, imagesize))
# 获取叠加图像的尺寸
overlay_height, overlay_width = orange_img0.shape[:2]
# 初始化摄像头
cap = cv2.VideoCapture(0)
# 确保摄像头被成功打开
if not cap.isOpened():
print("无法打开摄像头")
exit()
# 你需要根据你的摄像头支持的分辨率列表来设置
desired_width = 1024
desired_height = 1024
# 设置宽度
cap.set(cv2.CAP_PROP_FRAME_WIDTH, desired_width)
# 设置高度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, desired_height)
# 获取实际设置的分辨率
actual_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
actual_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# print(actual_width,actual_height)
# background_image = cv2.resize(background_image, (actual_width,actual_height )) # 将橘子图像调整为imagesizeximagesize像素
ink_img0 = cv2.resize(ink_img0,(actual_width,actual_height))
#使用算法包进行姿态估计时设置的参数
mp_pose = mp.solutions.pose
# 创建计算图
mp_drawing = mp.solutions.drawing_utils
# 定义姿态对象
pose = mp_pose.Pose(min_detection_confidence=0.5,min_tracking_confidence=0.8)
# 随机数,以权重的方式决定生成的下一个实体是什么
fruit_kind = 0
# 初始化实体列表
entities = []
#新增一个掉落物
def createfruit():
#初始化一个实体
# 随机生成一个数,根据权重决定实体类型
fruit_kind = random.randint(0, 100)
if fruit_kind < 30:
newobj_img = orange_img0
image = "orange"
elif fruit_kind < 45:
newobj_img = watermelon_img0
image = "watermelon"
elif fruit_kind < 60:
newobj_img = peach_img0
image = "peach"
elif fruit_kind < 70:
newobj_img = iceBanana_img0
image = "iceBanana"
# 原:70 测试中
elif fruit_kind < 80:
newobj_img = potion_img0
image = "potion"
elif fruit_kind < 90:
newobj_img = durian_img0
image = "durian"
else:
newobj_img = boom1
image = "boom"
# 上述代码在掉落生成时被复用
newobj = Orange(newobj_img, orange_explosion, imagesize, random.randint(1, actual_width - imagesize), 0, actual_width, actual_height, image)
# 将这个实体添加到列表里
entities.append(newobj)
def update_game_logic():
global entities, gamenum, myscore, gamespeed,inkIf
ret, frame = cap.read()
if not ret:
return # 如果没有成功读取帧,直接返回
frame = cv2.flip(frame, 1) # 翻转图像
results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # 姿态估计
for entity in entities:
entity.addyy(gamespeed) # 更新实体的垂直位置
entity.checkoranger(frame, results) # 检查是否碰到水果
if entity.explosionflag:
explose_floatimae(entity) # 如果实体被击中,处理爆炸逻辑
addoranger(frame, entity)
addscore(frame) # 将积分显示在帧中
# 其他游戏逻辑...
def show_ink_effect(frame, duration=3000):
# 确保墨水效果可以独立于游戏主循环运行
global actual_width, actual_height
ink_img0 = cv2.imread(current_directory + "\\footage\\image\\item\\ink.png", cv2.IMREAD_UNCHANGED)
ink_img_resized = cv2.resize(ink_img0, (actual_width, actual_height))
start_time = time.time()
while time.time() - start_time < duration / 1000:
# 更新游戏逻辑
update_game_logic()
if ink_img_resized.shape[2] == 4:
ink_img_resized = cv2.cvtColor(ink_img_resized, cv2.COLOR_BGRA2BGR)
# 显示墨水

LeonDL168
- 粉丝: 3262
- 资源: 895
最新资源
- xmlpull-1.2.0-2.el8.x64-86.rpm.tar.gz
- 孟加拉国航班票价数据集,合成数据集,包含57,000条航班记录
- xml-common-0.6.3-50.el8.x64-86.rpm.tar.gz
- Truffle、Ganache、MetaMask、Vue+Web3完成的一个简单区块链项目
- 科大讯飞语音TTS3.0.zip
- 3D场景下RRT、RRT*算法、实现绕过两个圆柱障碍物到达目标点,输出路径图和路径点与障碍物最小距离变化图,可用于移动机器人 也可用于机械臂做参考 RRT.m标准随机树 RRTstar.m改进随机数
- 基于springboot框架的Javaweb图书进销存管理系统的设计与实现(完整Java源码+数据库sql文件+项目文档+Java项目编程实战+编程练手好项目).zip
- xmakemol-5.16-10.el8.x64-86.rpm.tar.gz
- 全栈vue第二天vday02包
- 多电机均匀磨损控制子程序FB,程序版本博图V16 V1.0 V1.1 -~~-考虑到实际应用中原V1.0程序在轮时,会先停止运行一台,在轮另一台,在一台设备运行时,也会先停止,这
- RS422通信协议verilog代码实现
- Unity 编辑器 简体中文包 zh-hans.po
- Java核心技术之常见数据结构与概念详解-助力高级程序员提升面试竞争力 面试 面经
- xl2tpd-1.3.17-1.el8.x64-86.rpm.tar.gz
- 带隙基准Bandgap,低压差稳压器Ldo电路 ,带隙基准Bandgap与低压差稳压器Ldo电路在电路设计中的重要性与应用分析,带隙基准与低压差稳压器Ldo电路的深度解析与应用,核心关键词:带隙基准(
- xkeyboard-config-devel-2.28-1.el8.x64-86.rpm.tar.gz
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


