对于下雪的景象大家可能都不陌生,我们还是用vb来制作一个下雪的景象吧。其实制作这样一个下雪的景象并不复杂,它的原理是首先在底色为黑色的屏幕上随机画出许多白点(雪花),然后使这些雪花不断地向下移动(重画),反复循环,就成功地模拟了下雪的景象。下面是这个小程序,你可以修改其中的一些数据调整雪花的密度和雪花落下的快慢。
双击窗体写如下代码:
dim snow(1000, 2), amounty as integer
private sub form_load()
form1.show
doevents
randomize
amounty = 325
for j = 1 to amounty
snow(j, 0) = int(rnd * form1.width)
snow(j, 1) = int(rnd * form1.height)
snow(j, 2) = 10 + (rnd * 20)
next j
do while not (doevents = 0)
for ls = 1 to 10
for i = 1 to amounty
oldx = snow(i, 0): oldy = snow(i, 1)
snow(i, 1) = snow(i, 1) + snow(i, 2)
if snow(i, 1) > form1.height then
snow(i, 1) = 0: snow(i, 2) = 5 + (rnd * 30)
snow(i, 0) = int(rnd * form1.width)
oldx = 0: oldy = 0
end if
coloury = 8 * (snow(i, 2) - 10): coloury = 60 + coloury
pset (oldx, oldy), qbcolor(0)
pset (snow(i, 0), snow(i, 1)), rgb(coloury, coloury, coloury)
next i
next ls
loop
end
end sub
编写窗体的鼠标按下代码:
private sub form_mousedown(button as integer,shift as integer, x as single, y as single)
unload me
end sub
运行上面的小程序,就可以看到漫天的雪花在缓缓落下,地面上还会有积雪!鼠标单击可结束程序。