如何设计一个很简单的VB程序小游戏

如何设计一个很简单的VB程序小游戏,第1张

利用vb控件做个坦克大战类的游戏即可,炮d和坦克都用控件实现就行。唯一难点是控制控件移动以及炮d击中目标的碰撞检测判断。给你一个简单实现代码

这是一种碰撞检测方法,下述属于简化的矩形碰撞检测,若是需要复杂碰撞可以用一个数组来记录大量需要碰撞检测的物体

image1里读念则谨入坦克的图片

image2里读入地雷的图片

然后用下面代码即可实现

Private

Sub

Form_KeyPress(KeyAscii

As

Integer)

'按键盘A和D键控制猫图片image1左盯唤右移动

If

KeyAscii

=

97

Then

Image1.Left

=

Image1.Left

-

10

If

KeyAscii

=

100

Then

Image1.Left

=

Image1.Left

+

10

'如果坦克图片与地雷图片相遇则提示碰撞到了

If

Image1.Left

+

Image1.Width

>

Image2.Left

Then

If

Image1.Left

<

Image2.Left

+

Image2.Width

Then

If

Image1.Top

+

Image1.Height

>

Image2.Top

Then

If

Image1.Top

<

Image2.Top

+

Image2.Height

Then

MsgBox

"坦克碰到地雷,已经被炸毁了"仔基

End

If

End

If

End

If

End

If

End

Sub

假设坦克是方形,有4个数性Left Right Top Bottom

子d是点的情况很简单只要判断点是否在坦克方形樱茄内部就可以了

子d.x >坦克.left and 子d.x <坦克.right

子d.y同理

例如子d是圆,圆宴颂判心(x,y),半径r

只要判断圆心和坦克中心连线交于圆上的点是否在坦克内部

设交点tx,ty

坦克中心cx,cy

用极坐标好算晌改一些,tx=x+rcos(夹角),ty=y+rsin(夹角)

给你个算幅角的函数

Function arg(ByVal x As Double, ByVal y As Double)

If x = 0 Then

If y = 0 Then arg = 0: Exit Function

If y >0 Then arg = pi / 2: Exit Function

arg = 3 * pi / 2: Exit Function

ElseIf x >0 Then

If y >0 Then arg = Atn(y / x): Exit Function

If y = 0 Then arg = 0: Exit Function

arg = 2 * pi - Atn(-y / x): Exit Function

Else

If y >0 Then arg = pi - Atn(-y / x): Exit Function

If y = 0 Then arg = pi: Exit Function

arg = pi + Atn(y / x)

End If

End Function

上面的例子中夹角=arg(cx-x,cy-y)

Private Sub Timer2_Timer()

Select Case a

Case 1

 pd.Left = pd.Left - v1

 If pd.Left <= 0 Then

  pd.Left = pd.Left + (pt.Width - pd.Width) / 2

  pd.Top = pt.Top + (pt.Height + pd.Height) / 2

 End If   '少了end if

  Case 2

pd.Top = pd.Top - v1

If pd.Top <= 0 Then

 pd.Left = pd.Left + (pt.Width - pd.Width) / 2

  pd.Top = pt.Top + (pt.Height + pd.Height) / 2

 End If '少了end if

Case 3

 pd.Left = pd.Left + v1

If pd.Left >= 10000 Then

 pd.Left = pd.Left + (pt.Width - pd.Width) / 2

  pd.Top = pt.Top + (pt.Height + pd.Height) / 2

 End If '少了end if

Case 4

pd.Top = pd.Top + v1

If pd.Top >= 11000 Then

 pd.Left = pd.Left + (pt.Width - pd.Width) / 2

  pd.Top = pt.Top + (pt.Height + pd.Height) / 2

End If

End Select

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12336289.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存