用VB设计一个蝴蝶飞动的程序
动画片,程序,肯定
以下代码是实现单张图片的随意飞舞程序,能做到:
1、碰到窗体边缘时,图片会自动改变飞舞的方向(题意中要求的“返回”,只能是在一条线上来回移动,本程序实现蝴蝶出发位置随意,能在窗体内四处飞舞),而且头部也会跟着改变方向
2、窗体改变大小时,蝴蝶不会消失不见,会立即在新的窗体内继续飞舞
'建一个Picture1控件,在它的属性对话框内设置Appearance=0 AutoRedraw=True AutoSize=True BorderStyle=0,然后在它的Picture中加载蝴蝶图片
'再建一个Picture2控件,在它的属性对话框内设置Appearance=0 AutoRedraw=False AutoSize=False BorderStyle=0,其他默认属性都不得改变
'Form的BackColor设为与蝴蝶图片的背景色一致,本程序中,即&H80000009&
'再加一个Timer1控件
Private Const Pi = 3.1415926535
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Dim x0!, y0!, x1!, x2!, y1!, y2!, foot!, zt% 'x0和y0是图片框长高,(x1,y1)-(x2,y2)是窗体坐标系,foot是蝴蝶移动步长,zt是蝴蝶飞舞的线路标记
Dim k! 'k或1/k是飞行轨迹的斜率
Dim flag As Boolean 'flag判断是顺时针还是逆时针飞舞
Private Sub bmp_rotate(pic1 As PictureBox, pic2 As PictureBox, ByVal theta As Single) '自定义角度旋转图片
Dim c1x As Integer, c1y As Integer
Dim c2x As Integer, c2y As Integer
Dim a As Single
Dim p1x As Integer, p1y As Integer
Dim p2x As Integer, p2y As Integer
Dim n As Integer, r As Integer
c1x = pic1.ScaleWidth / 2
c1y = pic1.ScaleHeight / 2
c2x = pic2.ScaleWidth / 2
c2y = pic2.ScaleHeight / 2
If c2x < c2y Then n = c2y Else n = c2x
n = n - 1
pic1hDC& = pic1.hdc
pic2hDC& = pic2.hdc
For p2x = 0 To n
For p2y = 0 To n
If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x)
r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
p1x = r * Cos(a + theta)
p1y = r * Sin(a + theta)
c0& = GetPixel(pic1hDC, c1x + p1x, c1y + p1y)
c1& = GetPixel(pic1hDC, c1x - p1x, c1y - p1y)
c2& = GetPixel(pic1hDC, c1x + p1y, c1y - p1x)
c3& = GetPixel(pic1hDC, c1x - p1y, c1y + p1x)
If c0& -1 Then xret& = SetPixel(pic2hDC, c2x + p2x, c2y + p2y, c0&)
If c1& -1 Then xret& = SetPixel(pic2hDC, c2x - p2x, c2y - p2y, c1&)
If c2& -1 Then xret& = SetPixel(pic2hDC, c2x + p2y, c2y - p2x, c2&)
If c3& -1 Then xret& = SetPixel(pic2hDC, c2x - p2y, c2y + p2x, c3&)
Next
t% = DoEvents()
Next
End Sub
Private Sub Place(ByVal picnum As Integer, ByVal x As Single, ByVal y As Single) '控制两个图片框的位置
If picnum = 1 Then
Picture1.Left = x
Picture1.Top = y
Else
Picture2.Left = x
Picture2.Top = y
End If
End Sub
Private Sub Direct(ByVal angle As Integer) '按指定角度旋转图片,angle是角度
angle = angle Mod 360
hudu = (Pi * angle * 1#) / (180 * 1#) '弧度
bmp_rotate Picture1, Picture2, hudu
End Sub
Private Sub Form_Activate()
x1 = 0!
x2 = 10000!
y1 = 0!
y2 = 10000!
Me.Scale (x1, y1)-(x2, y2)
Randomize
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
x0 = Picture1.Width
Picture2.Width = x0
y0 = Picture1.Height
Picture2.Height = y0
Picture1.Visible = True
Picture2.Visible = False
x = Int(1000 + (x2 - x1 - x0 - 1000) * Rnd) '蝴蝶出发处的横坐标
y = Int(1000 + (y2 - y1 - y0 - 1000) * Rnd) '蝴蝶出发处的纵坐标
k = y / x 'k为撞向水平边框或离开竖直边框的斜率,1/k则为撞向竖直边框或离开水平边框的斜率
Place 1, -x2, y2 - y0 '将图片框的复制源移出窗体的可见范围,但Visible属性又必须是True,否则复制图片会失败
Place 2, x, y '初始化蝴蝶的出发位置
Timer1.Enabled = True
Timer1.Interval = 100 '定义移动周期
zt = 0
End Sub
Private Sub Form_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Form_Resize() '窗体改变大小时,坐标系需要重新定义
x1 = 0!
x2 = 10000!
y1 = 0!
y2 = 10000!
Me.Scale (x1, y1)-(x2, y2)
Cls
x0 = Picture1.Width
Picture2.Width = x0
y0 = Picture1.Height
Picture2.Height = y0
'如果蝴蝶在窗体外,则立即召回
If Picture2.Left < x1 Then Place 2, x1, Picture2.Top
If Picture2.Top < y1 Then Place 2, Picture2.Left, y1
If Picture2.Left > x2 - x1 - x0 Then Place 2, x2 - x1 - x0, Picture2.Top
If Picture2.Top > y2 - y1 - y0 Then Place 2, Picture2.Left, y2 - y1 - y0
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
'zt=0,1,2,3时,分别表示顺时针向右上(简称顺右上)或逆左下,顺右下或逆左上,顺左下或逆右上,顺左上或逆右下
Private Sub Timer1_Timer()
foot = 100
x = Picture2.Left: y = Picture2.Top
Select Case True
Case (flag = True And zt = 0) Or (flag = False And zt = 2)
Place 2, x + foot, y - k * foot
'Line (x, y)-(x + foot, y - k * foot)
Direct 360
If Picture2.Left > x2 - x1 - x0 - foot Then zt = 1: flag = False '撞到垂直边框,将逆左上
If Picture2.Top < foot Then zt = 1: flag = True '撞到水平边框,将顺右下
Case (flag = True And zt = 1) Or (flag = False And zt = 3)
Place 2, x + foot, y + foot / k
'Line (x, y)-(x + foot, y + foot / k)
Direct 270
If Picture2.Left > x2 - x1 - x0 - foot Then zt = 2: flag = True '撞到垂直边框,将顺左下
If Picture2.Top > y2 - y1 - y0 - foot Then zt = 2: flag = False '撞到水平边框,将逆右上
Case (flag = True And zt = 2) Or (flag = False And zt = 0)
Place 2, x - foot, y + k * foot
'Line (x, y)-(x - foot, y + k * foot)
Direct 180
If Picture2.Left < foot Then zt = 3: flag = False '撞到垂直边框,将逆右下
If Picture2.Top > y2 - y1 - y0 - foot Then zt = 3: flag = True '撞到水平边框,将顺左上
Case (flag = True And zt = 3) Or (flag = False And zt = 1)
Place 2, x - foot, y - foot / k
'Line (x, y)-(x - foot, y - foot / k)
Direct 90
If Picture2.Left < foot Then zt = 0: flag = True '撞到垂直边框,将顺右上
If Picture2.Top < foot Then zt = 0: flag = False '撞到水平边框,将逆左下
End Select
Picture2.Visible = True
End Sub
因为这里只能上传一张图片,所以写了一个单张图片的。装载系列图片的我也写了,只是窗体初始化会很麻烦。以下代码是实现单张图片的随意飞舞程序,能做到:
1、碰到窗体边缘时,图片会自动改变飞舞的方向(题意中要求的“返回”,只能是在一条线上来回移动,本程序实现蝴蝶出发位置随意,能在窗体内四处飞舞),而且头部也会跟着改变方向
2、窗体改变大小时,蝴蝶不会消失不见,会立即在新的窗体内继续飞舞
'建一个Picture1控件,在它的属性对话框内设置Appearance=0 AutoRedraw=True AutoSize=True BorderStyle=0,然后在它的Picture中加载蝴蝶图片
'再建一个Picture2控件,在它的属性对话框内设置Appearance=0 AutoRedraw=False AutoSize=False BorderStyle=0,其他默认属性都不得改变
'Form的BackColor设为与蝴蝶图片的背景色一致,本程序中,即&H80000009&
'再加一个Timer1控件
Private Const Pi = 3.1415926535
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Dim x0!, y0!, x1!, x2!, y1!, y2!, foot!, zt% 'x0和y0是图片框长高,(x1,y1)-(x2,y2)是窗体坐标系,foot是蝴蝶移动步长,zt是蝴蝶飞舞的线路标记
Dim k! 'k或1/k是飞行轨迹的斜率
Dim flag As Boolean 'flag判断是顺时针还是逆时针飞舞
Private Sub bmp_rotate(pic1 As PictureBox, pic2 As PictureBox, ByVal theta As Single) '自定义角度旋转图片
Dim c1x As Integer, c1y As Integer
Dim c2x As Integer, c2y As Integer
Dim a As Single
Dim p1x As Integer, p1y As Integer
Dim p2x As Integer, p2y As Integer
Dim n As Integer, r As Integer
c1x = pic1.ScaleWidth / 2
c1y = pic1.ScaleHeight / 2
c2x = pic2.ScaleWidth / 2
c2y = pic2.ScaleHeight / 2
If c2x < c2y Then n = c2y Else n = c2x
n = n - 1
pic1hDC& = pic1.hdc
pic2hDC& = pic2.hdc
For p2x = 0 To n
For p2y = 0 To n
If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x)
r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
p1x = r * Cos(a + theta)
p1y = r * Sin(a + theta)
c0& = GetPixel(pic1hDC, c1x + p1x, c1y + p1y)
c1& = GetPixel(pic1hDC, c1x - p1x, c1y - p1y)
c2& = GetPixel(pic1hDC, c1x + p1y, c1y - p1x)
c3& = GetPixel(pic1hDC, c1x - p1y, c1y + p1x)
If c0& <> -1 Then xret& = SetPixel(pic2hDC, c2x + p2x, c2y + p2y, c0&)
If c1& <> -1 Then xret& = SetPixel(pic2hDC, c2x - p2x, c2y - p2y, c1&)
If c2& <> -1 Then xret& = SetPixel(pic2hDC, c2x + p2y, c2y - p2x, c2&)
If c3& <> -1 Then xret& = SetPixel(pic2hDC, c2x - p2y, c2y + p2x, c3&)
Next
t% = DoEvents()
Next
End Sub
Private Sub Place(ByVal picnum As Integer, ByVal x As Single, ByVal y As Single) '控制两个图片框的位置
If picnum = 1 Then
Picture1.Left = x
Picture1.Top = y
Else
Picture2.Left = x
Picture2.Top = y
End If
End Sub
Private Sub Direct(ByVal angle As Integer) '按指定角度旋转图片,angle是角度
angle = angle Mod 360
hudu = (Pi * angle * 1#) / (180 * 1#) '弧度
bmp_rotate Picture1, Picture2, hudu
End Sub
Private Sub Form_Activate()
x1 = 0!
x2 = 10000!
y1 = 0!
y2 = 10000!
Me.Scale (x1, y1)-(x2, y2)
Randomize
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
x0 = Picture1.Width
Picture2.Width = x0
y0 = Picture1.Height
Picture2.Height = y0
Picture1.Visible = True
Picture2.Visible = False
x = Int(1000 + (x2 - x1 - x0 - 1000) * Rnd) '蝴蝶出发处的横坐标
y = Int(1000 + (y2 - y1 - y0 - 1000) * Rnd) '蝴蝶出发处的纵坐标
k = y / x 'k为撞向水平边框或离开竖直边框的斜率,1/k则为撞向竖直边框或离开水平边框的斜率
Place 1, -x2, y2 - y0 '将图片框的复制源移出窗体的可见范围,但Visible属性又必须是True,否则复制图片会失败
Place 2, x, y '初始化蝴蝶的出发位置
Timer1.Enabled = True
Timer1.Interval = 100 '定义移动周期
zt = 0
End Sub
Private Sub Form_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Form_Resize() '窗体改变大小时,坐标系需要重新定义
x1 = 0!
x2 = 10000!
y1 = 0!
y2 = 10000!
Me.Scale (x1, y1)-(x2, y2)
Cls
x0 = Picture1.Width
Picture2.Width = x0
y0 = Picture1.Height
Picture2.Height = y0
'如果蝴蝶在窗体外,则立即召回
If Picture2.Left < x1 Then Place 2, x1, Picture2.Top
If Picture2.Top < y1 Then Place 2, Picture2.Left, y1
If Picture2.Left > x2 - x1 - x0 Then Place 2, x2 - x1 - x0, Picture2.Top
If Picture2.Top > y2 - y1 - y0 Then Place 2, Picture2.Left, y2 - y1 - y0
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
'zt=0,1,2,3时,分别表示顺时针向右上(简称顺右上)或逆左下,顺右下或逆左上,顺左下或逆右上,顺左上或逆右下
Private Sub Timer1_Timer()
foot = 100
x = Picture2.Left: y = Picture2.Top
Select Case True
Case (flag = True And zt = 0) Or (flag = False And zt = 2)
Place 2, x + foot, y - k * foot
'Line (x, y)-(x + foot, y - k * foot)
Direct 360
If Picture2.Left > x2 - x1 - x0 - foot Then zt = 1: flag = False '撞到垂直边框,将逆左上
If Picture2.Top < foot Then zt = 1: flag = True '撞到水平边框,将顺右下
Case (flag = True And zt = 1) Or (flag = False And zt = 3)
Place 2, x + foot, y + foot / k
'Line (x, y)-(x + foot, y + foot / k)
Direct 270
If Picture2.Left > x2 - x1 - x0 - foot Then zt = 2: flag = True '撞到垂直边框,将顺左下
If Picture2.Top > y2 - y1 - y0 - foot Then zt = 2: flag = False '撞到水平边框,将逆右上
Case (flag = True And zt = 2) Or (flag = False And zt = 0)
Place 2, x - foot, y + k * foot
'Line (x, y)-(x - foot, y + k * foot)
Direct 180
If Picture2.Left < foot Then zt = 3: flag = False '撞到垂直边框,将逆右下
If Picture2.Top > y2 - y1 - y0 - foot Then zt = 3: flag = True '撞到水平边框,将顺左上
Case (flag = True And zt = 3) Or (flag = False And zt = 1)
Place 2, x - foot, y - foot / k
'Line (x, y)-(x - foot, y - foot / k)
Direct 90
If Picture2.Left < foot Then zt = 0: flag = True '撞到垂直边框,将顺右上
If Picture2.Top < foot Then zt = 0: flag = False '撞到水平边框,将逆左下
End Select
Picture2.Visible = True
End Sub
给你一个只移动的例子。(我用的是shape分别代表的蝴蝶和边界)
Private Sub Timer1_Timer()
Static a, b
If Shape2.Left <= Shape1.Left Then
a = 15
End If
If Shape2.Top <= Shape1.Top Then
b = 15
End If
If Shape2.Left >= Shape1.Width + Shape1.Left - Shape2.Width Then
a = -15
End If
If Shape2.Top >= Shape1.Height + Shape1.Top - Shape2.Height Then
b = -15
End If
Shape2.Move Shape2.Left + a, Shape2.Top + b
End Sub
Private Sub Form_Load()
Me.Width = 2900
Me.Height = 3300
With Shape1:
.Top = 100
.Left = 100
.Width = 2500
.Height = 2700
End With
With Shape2:
.Top = 100
.Left = 100
.Shape = 3
.Width = 400
.FillStyle = 0
End With
a = 15: b = 15
End Sub
如果只是移动一个蝴蝶图片还比较简单,但是蝴蝶要飞动就比较麻烦,建议用flash 把flash放到Vb 上
用BitBit,或PaintPicture,或直接用Image1.Picture = Image2.Picture:Image1.Picture = Image3.Picture:Image1.Picture = Image4.Picture......
用Picture循环加载多图片完成动画,然后对Picture进行位置移动并判断。
关于写剪纸的作文
直至闹毕洞房,新郎新娘红晕上飞之时,才始发现,二人实在哭笑不得。此后,每每谈及于此,免不了一番笑骂。 二、剪纸 现在的小女孩,还有多少是喜欢剪纸的,有一次上课时教我的学生们剪纸,他们欢呼雀跃,在自己面前的玻璃上贴满了自己的作品,当时看着这变的非常民间的教室,竟然是感动的。 真的,喜欢的人已经不多了,...
节约用水有什么好处
首先,节约用水,又称节水。是指通过行政、技术、经济等管理手段加强用水管理,调整用水结构,改进用水方式,科学、合理、有计划、有重点的用水,提高水的利用率,避免水资源的浪费。其次,我国是一个缺水国家,在日常生活中,我们一拧水龙头,水就源源不断地流出来,可能丝毫感觉不到水的危机。但...
描写春天的作文
蝴蝶扇动着彩色的翅膀,蜜蜂吟唱着醉人的甜蜜,在花枝上跳舞,在花朵上缠绵,书写着“蝶恋花”的执着与痴情,演绎出“为谁辛苦为谁甜”的酸楚与期盼。小鸟这位主人公这时候也在“千呼万唤”中终于“闪亮登场”了,它开始绞尽脑汁的制造情节,拐弯抹角的“拈花惹树”,谁说名花有主,不到最后一刻,决不轻言放弃。一个...
关于夏天的优美句子大全
水牛早就躲到了池塘里,整个身子埋在了池水中,只露出一个头在水面上透气。 22、那是一个久旱不雨的夏天,炎热的太阳烤得田里的老泥鳅都翻白了,村边的小溪,溪水一下低了几寸,那些露在水面的石头,陡地变大了。 23、盛夏,天热得连蜻蜓都只敢贴着树荫处飞,好像怕阳光伤了自己的翅膀。 24、空中没有一丝云,...
用VB设计一个蝴蝶飞动的程序
因为这里只能上传一张图片,所以写了一个单张图片的。装载系列图片的我也写了,只是窗体初始化会很麻烦。以下代码是实现单张图片的随意飞舞程序,能做到:1、碰到窗体边缘时,图片会自动改变飞舞的方向(题意中要求的“返回”,只能是在一条线上来回移动,本程序实现蝴蝶出发位置随意,能在窗体内四处飞舞...
如何培养学生的观察力与想象力
【案例2】程序设计第一课作业内容及要求 开动脑筋,充分发挥观察力和想象力,应用课堂所学,设计你的第一个VB程序。(提示:不能重复课堂范例,可以在范例基础上加工修改,最好自己设计)具体要求:(1)内容积极向上。(2)要运用所学按钮、标签、图像框等控件及相关属性。(3)要运用赋值语句。(4...
如何培养学生的观察力与想象力
有没有信息处理能力的一个关键点,就是看学生能不能发现能够应用课堂所学来解决的问题,并设法解决这些问题,这也正是培养学生观察力和想象力的关键。 以“开动脑筋,充分发挥观察力和想象力,应用课堂所学,设计你的第X个VB程序。”作为所有程序设计作业的开头,就是让学生自己去发现或想象感兴趣、能够应用课堂所学...
谷陶瑞泰: 首先准备两张图片,,多张也行,,我之前做过一个,代码贴出给你,,所需的控件自己话,,你看得懂的~~ Dim Bj As Integer Private Sub Command1_Click() Bj = 1 Timer1.Enabled = True End SubPrivate Sub Command2_Click() Timer1....
唐县14735581618: VB程序编写,设计一个蝴蝶飞下的动画程序 - ?
谷陶瑞泰: 蝴蝶飞,需要把GIF图片分解成多张图片,然后设2个timer. 1个控制蝴蝶动画,一个控制图片移动就OK了!!!!!!!
唐县14735581618: 怎样在vb中设计从下往上飞的动画蝴蝶?另外写代码时vb上说我子程序或函数没有定义,这是为什么? - ?
谷陶瑞泰: 飞去的蝴蝶可按以下思路做 类.蝴蝶本体,用来表现蝴蝶翅膀的扇动 计时器.移动上边这个类的实例,实现从下往上移动 子程序或函数没有定义可能是因为子程序或函数不是公用的,比如用Private声明且写在了别的模块里
唐县14735581618: 求:用VB实现蝴蝶是可以向右上方飞,蝴蝶飞出窗体程序?
谷陶瑞泰: 很容易,先得到蝴蝶飞的素材动画,Flash的可以,然后嵌入,窗口透明(可以用API什么的实现),Flash背景透明,然后把蝴蝶窗口放到主窗口上面,移动窗口就行了,
唐县14735581618: VB中在编写一个控制蝴蝶在窗体内飞舞的时候,用到 PickBmp,这个怎么用,是什么意思? - ?
谷陶瑞泰: pickbmp是一个静态整形变量 当第一次触发时,值为0 当执行到pickbmp = Not pickbmp时,值为-1 由于是静态变量,所以第二次触发时,值没有重置还是-1 当执行到pickbmp = Not pickbmp时,值为0 继续触发.......你可以把它看成是一个布尔变量,其实vb中布尔变量的true和false值就是-1和0
唐县14735581618: 用图像控件和计时器做蝴蝶飞舞的VB程序 - ?
谷陶瑞泰: 用IMAGE和TIMER配合,每计时一次就随机移动一次坐标
唐县14735581618: vb动画代码一个具有简单动画效果的程序?
谷陶瑞泰: 'Picture1, Timer1 Private Sub Form_Load() Timer1.Interval = 200 End Sub Private Sub Timer1_Timer() If Picture1.Left > Me.Width Then Picture1.Left = -Picture1.Width + 100 Else Picture1.Left = Picture1.Left + 50 End If End Sub
唐县14735581618: 用vb设计一个实现对象上下往返运动的程序 急求!谢谢! - ?
谷陶瑞泰: Private Sub Command1_Click() Do Me.Timer1.Enabled = True Me.Timer1.Interval = 1 Do Until Me.Command1.Top > Me.Frame1.Height - Me.Command1.Height Me.Timer1.Enabled = True Me.Timer1.Interval = 1 DoEvents Loop Me.Timer1....
唐县14735581618: 求VB程序设计 输入两个数,求最大公约数和最小公倍数 - ?
谷陶瑞泰: m = InputBox("输入第一个自然数") n = InputBox("输入第二个自然数") nm = n * m If m < n Then t = m: m = n: n = t r = m Mod n Do While (r <> 0) m = n n = r r = m Mod n Loop MsgBox ("最小公倍数为" & nm / n)
唐县14735581618: 用VB设计一个程序,将输入的字符串以反序显示 - ?
谷陶瑞泰: Private Sub Command1_Click() Dim S As String, T As String S = InputBox("输入一个字符串") Print T Dim I As Integer For I = Len(S) To 1 Step -1 T = T & Mid(S, I, 1) Next Print T End Sub最简单的Private Sub Command1_Click() Dim S As String, T As String S = InputBox("输入一个字符串")'T = StrReverse(S) End Sub