
常用软件类: |
|杀毒安全 | |联络聊天 | |网络软件 | |多媒体类 | |系统工具 | |图形图像 | |系统工具 | |应用软件 | |行业软件 |
开发设计类: |
|动画制作 | |图像处理 | |3D设计 | |操作系统 | |站长学院 | |网络相关 | |WEB设计 | |数据库类 | |程序开发 |
三、程序实现
现在我们已经拥有了着陆程序所有必需的元素,但是正象开药方一样,我们还需要小心的将它们组合在一起。首先生成一个新的工程,在FORM1上放置三个图片控件,分别命名为:picEarth, picLander 和 picSmash。在picLander和 picSmash图片框中分别装载着陆的飞船
和毁坏的飞船
图片,对于这两个图片控件,将BorderStyle属性设置为: 0 - None, ScaleMode 属性设置为 3 - Pixels, Visible属性设置为 False, AutoRedraw 属性设置为 True, AutoSize 属性设置为 True。对于picEarth控件,设置 BorderStyle 属性为 0 -None,ScaleMode属性设置为3 - Pixels。确定该控制大小合适,我程序中使用的是336x568 pixels (5040x8520 twips), 它看上去非常合适。
现在,在窗体上放置一个timer控件,将其命名为tmrGravity,将其Enabled属性设置为False,时间间隔设置为1秒;放置一个按钮控件,命名为cmdGo,"caption"属性设为"Start",另外 在窗体上绘制三个文本控件,并伴有标签。分别命名为txtFuel, txtVSpeed 和txtHeight,它们将用于提供一些飞行参数。好了,界面已经设计好了,下面是添加代码。
首先,我们需要对GetTickCount, BitBlt, SetPixelV and GetAsyncKeyState等API函数及VK_DOWN常量进行声明,将它们剪贴至窗体的声明部分,确定将它们声明为"Private"而不是"Public"类型,另外,我们还需要vSpeed, LandY和 Fuel等三个变量来用于控制飞船,所有的类型都是double,目前为止,我们已有了如下代码:
| Option Explicit ’API Declares Private Declare Function GetTickCount _ Lib "kernel32" () As Long Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, _ ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, _ ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, _ ByVal y As Long, ByVal crColor As Long) As Long Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Private Const VK_DOWN = &H28 ’ Vertical speed of the craft Private vSpeed As Double ’ The y coordinate of the craft Private LandY As Double ’ The amount of fuel left Private Fuel As Double |
| txtvspeed.Text = Format(vSpeed, "0.0") txtfuel.Text = Format(Fuel, "0.0") txtheight.Text = Format(picEarth.ScaleHeight - piclander.ScaleHeight - 30 - LandY, "0.0") |
| Static curtime As Long Dim timenow As Long Dim timediff As Long |
| If curtime = 0 Then ’ Draw the earth picEarth.Line (0, picEarth.ScaleHeight - 30) -(picEarth.ScaleWidth, picEarth.ScaleHeight), vbWhite, BF Randomize Timer Dim starx As Long, stary As Long For starx = 0 To picEarth.ScaleWidth For stary = 0 To picEarth.ScaleHeight - 30 If Rnd * 1000 < 5 Then SetPixelV picEarth.hdc, starx, stary, vbYellow End If Next Next timenow = GetTickCount curtime = timenow End If |
| Destination = Source XOR Destination |
| ’etc...curtime = timenow Else timenow = GetTickCount ’ If it isn’t the first time, put back the previous background BitBlt picEarth.hdc, 150, LandY, piclander.ScaleWidth, _ piclander.ScaleHeight, piclander.hdc, 0, 0, vbSrcInvert ’ End If... |
| timediff = timenow - curtime |
| ’ Calculate new vertical speed based on g vSpeed = vSpeed - ((timediff / 1000) * 10) |
| If GetAsyncKeyState(VK_DOWN) <> 0 Then If Fuel > 0 Then ’ Apply thrust: 15 is the acceleration produced vSpeed = vSpeed + ((timediff / 1000) * 15) Fuel = Fuel - ((timediff / 1000) * 150) ’ Check that fuel does not go below 0 If Fuel < 0 Then Fuel = 0 Else Beep End If End If |
| LandY = LandY - vSpeed ’ Update text boxes txtvspeed.Text = Format(vSpeed, "0.0") txtfuel.Text = Format(Fuel, "0.0") txtheight.Text = Format(picEarth.ScaleHeight _ - piclander.ScaleHeight - 30 - LandY, "0.0") ’ Update the ’last called time’ curtime = timenow |
| ’ If it has touched down... If LandY >= picEarth.ScaleHeight - 30 - piclander.ScaleHeight Then ’ Make sure that it is on the surface LandY = picEarth.ScaleHeight - 30 - piclander.ScaleHeight txtheight.Text = Format(picEarth.ScaleHeight - piclander.ScaleHeight - 30 - LandY, "0.0") ’ Stop the timer and disable the pause button... ’ the game is over! tmrgravity.Enabled = False ’ Figure out if it was a safe landing or not, ’ and paint the appropriate craft If vSpeed > -2 Then ’ If it was safe, then the craft remains intact BitBlt picEarth.hdc, 150, LandY, piclander.ScaleWidth, piclander.ScaleHeight, piclander.hdc, 0, 0, vbSrcInvert MsgBox "Congratulations! You have landed successfully!" Else ’ If it was moving too fast, it blows up! BitBlt picEarth.hdc, 150, LandY, piclander.ScaleWidth, piclander.ScaleHeight, picsmash.hdc, 0, 0, vbSrcInvert MsgBox "Smash! Oooops!" End If Else ’ paint the craft into its new position. BitBlt picEarth.hdc, 150, LandY, piclander.ScaleWidth, _ piclander.ScaleHeight, piclander.hdc, 0, 0, vbSrcInvert End If |