[VB程序设计创新实验教程]Chap1---VB中游戏基本要素的实现方式[1]

本文介绍使用Visual Basic进行游戏开发的基本技巧,包括实现游戏画面运动的主体框架、利用Win32 API进行精确定时、画点与画线等绘图操作,并通过实例展示了如何绘制函数图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VB中游戏基本要素的实现方式[1]

                                                                                                                By EmilMatthew

                                                                                                                          06/04/14

1游戏画面运动的主体框架:

一个有意思点的游戏的成功关键在于能”,而在“动”的实现方法中,最简单易行的就是外层循环加内层的延时及更新的的操作.

为了达到精确的定时,可以用一个WIN32API 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画点或画线:

       最简单的方式,就是使用PictureBoxForm控件提供的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.   导弹追击实验。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值