opencv学习-碰壁小球

文章目录


前言

用opencv实现一个动画,一个小球碰壁后反弹(随机加速或减速)。备忘


代码

  • 以下用了三角函数来计算位置,做复杂了。把速度改成x和y方向上的分量应该会更简单,也不需要计算角度(只用根据撞的是哪边调整速度+/-符号就行了
  • 多球处理部分不够简洁。
  • 以后改进
import numpy as np
import cv2 as cv
import math
import random
import time

def showPic(img1,*args):
    cv.imshow('img',img1)
    for i in range(0,len(args)):
        cv.imshow('img'+str(i+1), args[i])
    cv.waitKey(30000)
    cv.destroyAllWindows()
    
#Calculate current position
def getNewPosition(currentPosition, angle, speed, period):
    x0 = currentPosition[0]
    y0 = currentPosition[1]
    distance = speed * period
    x1 = int(x0 + distance * math.cos(angle * math.pi))
    y1 = int(y0 - distance * math.sin(angle * math.pi))
    return (x1, y1)

def hitBoarder(board, r, torrence, current_status, x1, y1 ):
    #board: x_min, x_max, y_min, y_max
    #current_status: x0, y0, angle, speed
    
    if not( (x1 - (r - torrence)) <= board[0] or  (x1 + (r - torrence)) >= board[1] or (y1 - (r - torrence)) <= board[2] or (y1 + (r - torrence)) >= board[3]):
        return False, current_status[0],current_status[1],current_status[2],current_status[3]
    else:
        speed_new = int(current_status[3] * random.uniform(0.8,2.0))
        angle_new = current_status[2]
        #left side:
        if (x1 - (r - torrence)) <= board[0]:
            #top left
            if (y1 - (r - torrence)) <= board[2]:
                angle_new = random.uniform(1.5,2.0)
            #bottom left
            elif (y1 + (r - torrence)) >= board[3]:
                angle_new = random.uniform(0,0.5)
            else:
                if 1.0 <= current_status[2] <= 1.5:
                    angle_new = 3 - current_status[2]
                elif 0.5 <= current_status[2] <= 1.0:
                    angle_new = 1.0 - current_status[2]
        # right side            
        elif (x1 + (r - torrence)) >= board[1]:
            #top right
            if (y1 - (r - torrence)) <= board[2]:
                angle_new = random.uniform(1.0,1.5)
            #bottom right
            elif (y1 + (r - torrence)) >= board[3]:
                angle_new = random.uniform(0.5,1.0)
            else:
                if 0 <= current_status[2] <= 0.5:
                    angle_new = 1.0 - current_status[2]
                elif 1.5 < current_status[2] < 2.0:
                    angle_new = 3 - current_status[2]
        
        #top / bottom
        #top: angle 0 ~1; bottom: angle 1 ~2, the result is the same
        elif  (y1 - (r - torrence)) <= board[2] or (y1 + (r - torrence)) >= board[3]:
            angle_new = 2.0 - current_status[2]
        
        return True, x1, y1, angle_new, speed_new
 
# create the canvas
canvas = np.zeros((600,600,3),np.uint8)
r = 20
torrence = 3
# Get the initial position, angle (* 2Pi), speed (pit/second)
x0 = random.randint(50,250)
y0 = random.randint(50,250)
angle = random.random() *2.0
speed = random.randint(60,120)

x0_1 = random.randint(50,250)
y0_1 = random.randint(50,250)
angle_1 = random.random() *2.0
speed_1 = random.randint(60,120)

time_counter = 0
time_counter_1 = 0
game_over = False
img = canvas.copy()
while cv.waitKey(50) ==-1 and not(game_over):
    x1, y1 = getNewPosition([x0,y0],angle,speed, 1/60 * time_counter)
    x1_1, y1_1 = getNewPosition([x0_1,y0_1],angle_1,speed_1, 1/60 * time_counter_1)
    img = canvas.copy()
    cv.circle(img, (x1,y1), r,(0,255,0),-1)
    cv.circle(img, (x1_1,y1_1), r,(255,0,0),-1)
    cv.imshow('img', img)
    time.sleep(1/60)
    time_counter +=1
    time_counter_1 +=1
    
    # if hit the board, then change the direction and speed
    hitted, x0, y0, angle, speed = hitBoarder([0,599,0,599],r, torrence,[x0,y0,angle, speed],x1,y1)
    if hitted:
        time_counter = 1
    
    hitted_1, x0_1, y0_1, angle_1, speed_1 = hitBoarder([0,599,0,599],r, torrence,[x0_1,y0_1,angle_1, speed_1],x1_1,y1_1)
    if hitted_1:
        time_counter_1 = 1
        
    # if hit too deep
    if ((x1 - r/4) <= 0 or  (x1 + r/4) >= 599 or (y1 - r/4 ) <= 0 or (y1 + r/4) >= 599) or ((x1_1 - r/4) <= 0 or  (x1_1 + r/4) >= 599 or (y1_1 - r/4 ) <= 0 or (y1_1 + r/4) >= 599):
        game_over = True
        break
        
cv.destroyAllWindows()
if game_over == True:
    #print('Game Over!')
    cv.putText(img,"Game Over!",(100,270),cv.FONT_HERSHEY_TRIPLEX,2,(0,0,255),1)
    cv.putText(img,"Game Over!",(100,330),cv.FONT_HERSHEY_TRIPLEX,2,(0,0,255),1,8,True)
    cv.imshow('img',img)
    cv.waitKey()
cv.destroyAllWindows()   
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要下载OpenCV学习中的运动目标(前景)检测源码,可以按照以下步骤进行。 首先,访问OpenCV的官方网站(https://opencv.org/)或GitHub的OpenCV仓库(https://github.com/opencv/opencv),找到源代码的下载选项。 在官方网站上,可以选择下载最新版本的OpenCV,或者根据特定版本的需求进行选择。在GitHub上,可以浏览仓库的不同分支和版本标签,并选择下载相应的源代码。 一旦选择了合适的源码下载选项,点击下载按钮进行下载。下载完成后,将源代码文件解压缩至本地目录。 接下来,在下载的源代码文件夹中,找到与运动目标检测相关的示例代码或项目。这些示例代码通常位于“samples”或“examples”文件夹中,可以根据名称或说明找到与运动目标检测相关的示例。 打开示例代码文件,使用合适的集成开发环境(IDE)或文本编辑器加载源代码。确保已正确配置编译环境和OpenCV库文件。 阅读示例代码的注释和文档,理解实现运动目标检测的算法和方法。 对于初学者,建议阅读和运行示例代码,以更好地理解和学习运动目标检测的概念和实践。根据需要,可以根据示例代码进行修改和调整,以满足特定的需求。 总之,要下载OpenCV学习中的运动目标(前景)检测源码,首先选择合适的源代码下载选项,然后解压缩源代码文件夹,找到与运动目标检测相关的示例代码或项目,最后阅读和运行示例代码以学习和实践运动目标检测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值