python实现满天樱花飘落效果
import randomimport cv2
import numpy as np
def cal(point,here):
# 计算樱花中的坐标在屏幕中投影的坐标
# here不断增大,delta不断减少
delta = point - here
if delta==0:
return [-1, -1, 1, 0, 0]
x = point/delta # 投影在屏幕上x坐标
y = point/delta # 投影在屏幕上y坐标
r = point/delta # 星球半径
return
def display_all(sakura,here):
result=[]
for i in range(sakura_num):
position = cal(sakura,here)
result.append(position)
return result
if __name__ == "__main__":
height=1080
width=1920
step = 0.5
min_r = 1
max_r = 200
sakura_num = 150
x_far = 100*2
y_far = width*x_far
z_far = height*x_far
here = 0
sakura = [ for i in range(sakura_num)]
cv2.namedWindow('sakura',0)
while True:
canvas=np.zeros((height,width,3),dtype=np.uint8)
here+=step
for i in range(sakura_num):
while True:
position = cal(sakura,here)
if (0 <= position < width and
0 <= position < height and sakura>here):
break
else:
sakura = random.randint(x_far, 2*x_far)+here
sakura = random.randint(0, 2*y_far)
sakura = random.randint(0, 2*z_far)
sakura = random.randint(min_r, max_r)
result=display_all(sakura,here)
result=np.uint16(result).tolist()
for i in range(sakura_num):
radius=np.random.randint(3,15,size=(1,2)).tolist()
cv2.ellipse(canvas,tuple(result),tuple(radius),30,0,360,color=(233,192,255),thickness = -1)
cv2.imshow('sakura',canvas)
if cv2.waitKey(10)==27:
cv2.destroyAllWindows()
break
本帖最后由 wjl 于 2022-5-27 22:00 编辑
我的python版本是3.10,没法安装cv2模块
ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none)
ERROR: No matching distribution found for cv2
后来查了一下原来安装命令是 pip install opencv-python,而不是 pip install cv2 if cv2.waitKey(10)==27:
cv2.destroyAllWindows()
break
应该是按esc退出 不然是死循环 正准备找个代码,就看到楼主分享了,试一下{:1_893:} 感谢楼主分享!! 还可以这样玩 感谢楼主分享 效果不错啊,感谢楼主分享! 有bug吧,程序无法关闭,单击右上角的X也无法关闭 numpy 这个是个神级的库~ 关都关不掉,而且不太像樱花