前言
用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()