vb怎么让蝴蝶飞起来的编程
举个例子吧,我有两幅蝴蝶的图片,一副翅膀往下扇,一副翅膀往上扇
用定时器隔一秒切换一下,看起来就像“飞舞”一样
这个静态变量就是表示应该换哪副图片了,
Image1.Picture是用来显示蝴蝶飞舞的效果的
Image2.Picture和 Image3.Picture是用来保存一上一下的图片的
因为这里只能上传一张图片,所以写了一个单张图片的。装载系列图片的我也写了,只是窗体初始化会很麻烦。以下代码是实现单张图片的随意飞舞程序,能做到:
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
如何让纸蝴蝶飞起来?
详情请查看视频回答
用VB设计一个蝴蝶飞动的程序
1、碰到窗体边缘时,图片会自动改变飞舞的方向(题意中要求的“返回”,只能是在一条线上来回移动,本程序实现蝴蝶出发位置随意,能在窗体内四处飞舞),而且头部也会跟着改变方向 2、窗体改变大小时,蝴蝶不会消失不见,会立即在新的窗体内继续飞舞 '建一个Picture1控件,在它的属性对话框内设置Appeara...
怎么让蝴蝶飞在身边
3. 了解不同蝴蝶的生命周期和习性,提供适宜的环境,比如在特定季节种植它们需要的食物植物。4. 避免使用杀虫剂和化学剂,这些物质会伤害蝴蝶,让它们远离。5. 在户外阅读或休息时,选择有蝴蝶出没的区域,这样就可能会有蝴蝶飞到你身边。6. 保持耐心和安静,蝴蝶通常对安静的环境和缓慢的动作的人更感...
怎样让蝴蝶飞到人身上
1、将风油精与水以一比十的比例调配一点点,滴在花蜜料中,到蝴蝶飞舞之时,把花蜜料涂在手上,站到稍微有风的地方,会有蝴蝶来找你的。2、资料上有说蝴蝶比较喜欢红、粉、紫等颜色鲜艳的颜色,而蜜蜂则喜欢颜色比较浅的花朵,因为花的颜色越浅,花蜜越丰富,色彩—主要是花朵大颜色鲜艳的植物,如...
怎样让蝴蝶飞到人身上
4. 在风水学中,蝴蝶飞到家中被认为是一种吉祥的征兆。蝴蝶与“福”谐音,因此常被视为“福气”的象征。如果家中出现了蝴蝶,这通常意味着家里将会有好事发生,家庭成员会迎来不断的福气。通常情况下,如果不是在蝴蝶活跃的季节,它们不会飞到人身边,除非那个地方充满了福气,让蝴蝶忍不住前来感受。
怎么让PPT的蝴蝶飞起来
我正巧刚做了一个,有需要我传你。用自定义动画的“动作路径”,但如果你选曲线的话还要同时用“强调”里的“陀螺旋”旋转一点角度,以使蝴蝶的头部总是跟着飞行方向,而不是僵直着平移。
为何蝴蝶飞不起来了
原来蝴蝶破茧一点点的外挤这个过程,是老天特意设计的安排,因为只有一点点住外挤,受尽折磨和苦难,把它的体液挤到翅膀上。让翅膀变得丰满,才会有力,才会飞起来。这个人看到蝴蝶那么的难受,出于好心,而帮助蝴蝶,其实是在害蝴蝶,这只蝴蝶就是因为他的好心,永远都飞不起来了,像虫一样悲怜在地...
幼儿园中班游戏课教案《过山车、让蝴蝶飞起来等》
游戏二:让蝴蝶飞起来(摩擦起电)。师:(出示塑料棒和飞满蝴蝶的小花园)这是什么?(塑料尺子),你想让它变长魔术棒吗?你可以把它放在不同的地方摩擦,看,可以在布上(教师示范操作),你们还想放在哪里?(头发上、身上)你要用力不断地摩擦,记住要用力要久一些哦,然后对准小蝴蝶和小星星...
如何让你满肚子的蝴蝶飞出来这一典故
用来形容坠入爱河的感觉。这一短语最早出现于美国女作家Florence Converse (1871_1967)的《祷告之殿》一书中。我问他,当他第一次遇到自己的女友时是什么样的感觉。“当时的感觉,”他回答说,“就好像千万只蝴蝶在我肚中翩翩起舞!。
中班科学活动《神奇的静电》教案
2.怎样才能让蝴蝶飞起来呢?(请幼儿尝试,教师及时鼓励。) 3.听说有一位魔法师能让蝴蝶飞起来,让我们一起来看一看吧!(出示小动物手偶操作) a.拿梳子直接在蝴蝶上面吸。(现象:蝴蝶没有动。) b.用塑料梳子和毛皮摩擦后在蝴蝶上面吸。(现象:蝴蝶飞起来了。) 4.提问: ?你看到了什么?(引导幼儿大胆表达。) ?为...
宰韩富马: 首先准备两张图片,,多张也行,,我之前做过一个,代码贴出给你,,所需的控件自己话,,你看得懂的~~ Dim Bj As Integer Private Sub Command1_Click() Bj = 1 Timer1.Enabled = True End SubPrivate Sub Command2_Click() Timer1....
襄城区18412385596: VB编写蝴蝶在窗体来回飞舞的程序怎么写??
宰韩富马: 动画用FLASH控件吧.VB做这个很垃圾的 用计时器不停的改变蝴蝶(图片控件)的坐标(.left和.top),同时改变装载图片的路径切换蝴蝶连续飞行的动作
襄城区18412385596: VB作业:设计一个蝴蝶在窗体内自动飞舞的的程序.(提示:使用Timer和图像框控件) 我是初学者,最好不要永恒太难的方法 - ?
宰韩富马: 代码如下:Private Sub Timer1_Timer()RandomizeImage1.Left = Image1.Left + Int(-2000 * Rnd + 1000)Image1.Top = Image1.Top + Int(-2000 * Rnd + 1000)If Image1.Left < 0 Then Image1.Left = 0If Image1.Top < 0 Then Image1.Top = 0If Image1.Left ...
襄城区18412385596: VB 关于蝴蝶飞舞的代码问题 - ?
宰韩富马: 举个例子吧,我有两幅蝴蝶的图片,一副翅膀往下扇,一副翅膀往上扇 用定时器隔一秒切换一下,看起来就像“飞舞”一样 这个静态变量就是表示应该换哪副图片了, Image1.Picture是用来显示蝴蝶飞舞的效果的 Image2.Picture和 Image3.Picture是用来保存一上一下的图片的
襄城区18412385596: VB程序编写,设计一个蝴蝶飞下的动画程序 - ?
宰韩富马: 蝴蝶飞,需要把GIF图片分解成多张图片,然后设2个timer. 1个控制蝴蝶动画,一个控制图片移动就OK了!!!!!!!
襄城区18412385596: 怎样在vb中设计从下往上飞的动画蝴蝶?另外写代码时vb上说我子程序或函数没有定义,这是为什么? - ?
宰韩富马: 飞去的蝴蝶可按以下思路做 类.蝴蝶本体,用来表现蝴蝶翅膀的扇动 计时器.移动上边这个类的实例,实现从下往上移动 子程序或函数没有定义可能是因为子程序或函数不是公用的,比如用Private声明且写在了别的模块里
襄城区18412385596: 求:用VB实现蝴蝶是可以向右上方飞,蝴蝶飞出窗体程序?
宰韩富马: 很容易,先得到蝴蝶飞的素材动画,Flash的可以,然后嵌入,窗口透明(可以用API什么的实现),Flash背景透明,然后把蝴蝶窗口放到主窗口上面,移动窗口就行了,
襄城区18412385596: 怎样用VB作蝴蝶效应图?
宰韩富马: Public Function fx(x, y, z) fx = -10 * x + 10 * y End Function Public Function fy(x, y, z) fy = 28# * x - y - x * z End Function Public Function fz(x, y, z) fz = x * y - 8 * z / 3 End Function Private Sub Form_click() Scale (-25, 50)-(25, 0) h = 0.01 x = 1 y = 1 z = ...
襄城区18412385596: 请用VB设计程序2只会飞的蝴蝶 - ?
宰韩富马: 你用个image和几张图片就行了~~ 用个timer 控制image的位置
襄城区18412385596: 什么类型的数据接受的变量定义为无功 - ?
宰韩富马: 有用功率吗?有功无功只是名称,看数据类型;整数,不带小数点,可以定义为int; int noPower;带小数点的数字类型,定义为float或double; float noPower;也可以定为...