[VB程序设计创新实验教程]Chap1---VB中游戏基本要素的实现方式[1]
[VB程序设计创新实验教程]Chap1---VB中游戏基本要素的实现方式[1] VB中游戏基本要素的实现方式[1]
By EmilMatthew
06/04/14
1游戏画面运动的主体框架:
一个有意思点的游戏的成功关键在于能”动”,而在“动”的实现方法中,最简单易行的就是外层循环加内层的延时及更新的的操作.
为了达到精确的定时,可以用一个WIN32的API GetTickCount来计时,在正常条件下,精度可达1毫秒。
Option Explicit
Public Declare Function GetTickCount Lib 'kernel32' () As Long
Const MS_DELAY = 50 '20FPS
‘valuable declare
Dim mblnRunning As Boolean
Dim mlngTimer As Long
Dim mStartTime As Long
Dim mDelayTime As Long
‘init data
Public Function sample()
mblnRunning = True ‘flag
mDelayTime=20000 ‘20secs
mStartTime= GetTickCount()
mlngTimer= GetTickCount()
‘main part
Do While mblnRunning And GetTickCount()<=mStartTime+mDelayTime
If mlngTimer + MS_DELAY <= GetTickCount() Then
mlngTimer = GetTickCount() ‘update time limit
'------------------------------------------------
‘motion effects doing and updating here
‘-----------------------------------------------
End If
DoEvents 'Important . If not add this ,the effect will be very bad.
Loop
End Function
在motion effects doing and updating here可以加入运动更新的代码
如cmd.left=cmd.left+1,就可以产生一个command控件从左向右的移动效果。
2画点或画线:
最简单的方式,就是使用PictureBox或Form控件提供的Line,Circle,PSet来进行绘制.
Sub Line(Flags As Integer, X1 As Single, Y1 As Single, X2 As Single, Y2 As Single, Color As Long)
Sub Circle(Step As Integer, X As Single, Y As Single, Radius As Single, Color As Long, Start As Single, End As Single, Aspect As Single)
Sub PSet(Step As Integer, X As Single, Y As Single, Color As Long)
下面给出的程序段是关于画函数f(x)= 0.5 * x ^ 3 - 3 * x ^ 2 + 3 * x图像的例子
Private Sub Form_Load()
Me.Show
Dim i As Single
Dim x0 As Integer
Dim y0 As Integer
pic.ScaleMode = 0 'user definitation
pic.ScaleHeight = 20
pic.ScaleWidth = 20
pic.Height = pic.Width
x0 = pic.ScaleWidth / 2
y0 = pic.ScaleHeight / 2
pic.Line (pic.ScaleLeft, y0)-(pic.ScaleWidth, y0), RGB(0, 0, 255)
pic.Line (x0, pic.ScaleTop)-(x0, pic.ScaleHeight), RGB(0, 0, 255)
For i = -pic.ScaleWidth / 2 To pic.ScaleWidth / 2 Step 0.005
pic.PSet (x0 + i, y0 - f_x2(i)), RGB(0, 0, 255)
Next
End Sub
Private Function f_x2(x As Single) As Single
f_x2 = 0.5 * x ^ 3 - 3 * x ^ 2 + 3 * x
End Function
效果
编程练习与思考:
1. 李萨如图形的绘制。
2. 三角函数图像的绘制。
3. 驻波的动画演示。
4. 导弹追击实验。