因为这里只能上传一张,所以写了一个单张的。装载系列的我也写了,只是窗体初始化会很麻烦。以下代码是实现单张的随意飞舞程序,能做到:
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 = 31415926535
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 = pic1ScaleWidth / 2
c1y = pic1ScaleHeight / 2
c2x = pic2ScaleWidth / 2
c2y = pic2ScaleHeight / 2
If c2x < c2y Then n = c2y Else n = c2x
n = n - 1
pic1hDC& = pic1hdc
pic2hDC& = pic2hdc
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
Picture1Left = x
Picture1Top = y
Else
Picture2Left = x
Picture2Top = 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!
MeScale (x1, y1)-(x2, y2)
Randomize
Picture1ScaleMode = 3
Picture2ScaleMode = 3
x0 = Picture1Width
Picture2Width = x0
y0 = Picture1Height
Picture2Height = y0
Picture1Visible = True
Picture2Visible = 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 '初始化蝴蝶的出发位置
Timer1Enabled = True
Timer1Interval = 100 '定义移动周期
zt = 0
End Sub
Private Sub Form_Click()
Timer1Enabled = Not Timer1Enabled
End Sub
Private Sub Form_Resize() '窗体改变大小时,坐标系需要重新定义
x1 = 0!
x2 = 10000!
y1 = 0!
y2 = 10000!
MeScale (x1, y1)-(x2, y2)
Cls
x0 = Picture1Width
Picture2Width = x0
y0 = Picture1Height
Picture2Height = y0
'如果蝴蝶在窗体外,则立即召回
If Picture2Left < x1 Then Place 2, x1, Picture2Top
If Picture2Top < y1 Then Place 2, Picture2Left, y1
If Picture2Left > x2 - x1 - x0 Then Place 2, x2 - x1 - x0, Picture2Top
If Picture2Top > y2 - y1 - y0 Then Place 2, Picture2Left, 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 = Picture2Left: y = Picture2Top
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 Picture2Left > x2 - x1 - x0 - foot Then zt = 1: flag = False '撞到垂直边框,将逆左上
If Picture2Top < 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 Picture2Left > x2 - x1 - x0 - foot Then zt = 2: flag = True '撞到垂直边框,将顺左下
If Picture2Top > 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 Picture2Left < foot Then zt = 3: flag = False '撞到垂直边框,将逆右下
If Picture2Top > 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 Picture2Left < foot Then zt = 0: flag = True '撞到垂直边框,将顺右上
If Picture2Top < foot Then zt = 0: flag = False '撞到水平边框,将逆左下
End Select
Picture2Visible = True
End Sub
利用vb控件做个坦克大战类的游戏即可,炮d和坦克都用控件实现就行。唯一难点是控制控件移动以及炮d击中目标的碰撞检测判断。给你一个简单实现代码
这是一种碰撞检测方法,下述属于简化的矩形碰撞检测,若是需要复杂碰撞可以用一个数组来记录大量需要碰撞检测的物体
image1里读入坦克的
image2里读入地雷的
然后用下面代码即可实现
Private
Sub
Form_KeyPress(KeyAscii
As
Integer)
'按键盘A和D键控制猫image1左右移动
If
KeyAscii
=
97
Then
Image1Left
=
Image1Left
-
10
If
KeyAscii
=
100
Then
Image1Left
=
Image1Left
+
10
'如果坦克与地雷相遇则提示碰撞到了
If
Image1Left
+
Image1Width
>
Image2Left
Then
If
Image1Left
<
Image2Left
+
Image2Width
Then
If
Image1Top
+
Image1Height
>
Image2Top
Then
If
Image1Top
<
Image2Top
+
Image2Height
Then
MsgBox
"坦克碰到地雷,已经被炸毁了"
End
If
End
If
End
If
End
If
End
Sub
Dim a, b
Private Sub Command1_Click()
End
End Sub
Private Sub Form_Load()
TextText = "Visual Basic程序设计"
Command1Caption = "结束"
TextLeft = 0: TextTop = 0: TextWidth = 05 Form1Width
TextHeight = 05 Form1Height
a = Form1Width - Command1Left
b = Form1Height - Command1Top
End Sub
Private Sub Form_Resize()
TextLeft = 0: TextTop = 0: TextWidth = 05 Form1Width
TextHeight = 05 Form1Height
Command1Left = Form1Width - a
Command1Top = Form1Height - b
End Sub
注意,各变量名,控件名保持一致,你题目中的文本框名称为text
在窗体中建立一个文本框text1,multiline属性为true,scrollBars属性为2-vertical
复制进如下代码:
Private
Sub
Form_Load()
Dim
a&,
b$
For
a
=
100
To
200
If
a
Mod
3
=
0
Then
b
=
b
&
Str(a)
Next
a
Text1Text
=
b
End
Sub
---------我是华丽丽的分割线--------
dim
a(1
to
10)
as
long
dim
Temp()
as
long
‘保存能被3整除的数
dim
s
as
string
dim
n
as
integer
for
i=1
to
10
a(i)=inputbox("Please
input
data!")
next
i
for
i=1
to
10
if
(a(i)
mod
2)
then
n=n+1
s=s
&
cstr(a(i))
&
","
end
if
redim
temp(n)
as
long
dim
v
v=split(s,",")
for
i=0
to
ubound(v)
temp(i)=v(i)
next
i
程序界面如上图所示:一个标签(名称为Label1),三个复选框(名称分别为Check1、Check2和Check3,Caption属性分别为红、绿和蓝)一个命令按钮(名称为Command1,Caption属性为结束),完成程序功能的代码如下:
Dim red As Integer
Dim green As Integer
Dim blue As Integer
Private Sub Check1_Click()
If Check1Value = 1 Then
red = 255
Else
red = 0
End If
Label1BackColor = RGB(red, green, blue)
End Sub
Private Sub Check2_Click()
If Check2Value = 1 Then
green = 255
Else
green = 0
End If
Label1BackColor = RGB(red, green, blue)
End Sub
Private Sub Check3_Click()
If Check3Value = 1 Then
blue = 255
Else
blue = 0
End If
Label1BackColor = RGB(red, green, blue)
End Sub
Private Sub Command1_Click()
End
End Sub
Private Sub Form_Load()
red = 0
green = 0
blue = 0
Label1BackColor = RGB(red, green, blue)
End Sub
tmp = InputBox("随便输入几个数,用逗号隔开就可以了。")
s = Split(tmp, ",")
u = UBound(s)
For i = 0 To u
If Val(s(i)) Mod 2 = 1 Then
qs = qs + 1
Else
os = os + 1
End If
Next
Print qs & "个奇数," & os & "个偶数"
以上就是关于用VB设计一个蝴蝶飞动的程序全部的内容,包括:用VB设计一个蝴蝶飞动的程序、如何设计一个很简单的VB程序小游戏、用VB编写一个小程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)