|          
简单的游戏往往更耐玩,就比如伴我度过高考的赛车游戏:一切都是方块,所谓的赛车也只是四个方块。
 第一步,绘制对象:用函数drawcar()画赛车,drawway(n)画跑道的第n层。
 
 第二步,接受控制:Form的KeyPreview属性要设为true,在Form_KeyPress函数中通过改变全局变量cx来控制赛车的位置。
 
 第三步,游戏循环:作为即时游戏,必须要有一个Timer,并在其事件Timer1_Timer()中绘制所有对象和进行碰撞检测。本例中,绘图部分写在了Timer1_Timer()中,碰撞测试放在了test()中。
 
 ……这也是所有即时游戏所共通的框架。当然,我们往往还是根据具体的设计作一些变通,发挥一些技巧……比如这里设计的跑道是随机产生的的,这就要通过一点技巧以便既让玩家感到挑战,又不至于出现不可逾越的难关……
 
 下面是全部源代码,窗体上只需放个按钮Command1就行了!
 
 Const D = 100 '方格的宽度
 Const BT = 3000 '跑道底部的y坐标
 Dim l1(22) As Integer '每层跑道左边有几个方块
 Dim l2(22) As Integer '每层跑道右边有几个方块
 Dim cx As Single '赛车的在x轴的位置
 
 Private Sub Command1_Click()
 
 cx = Width / 2 - 3 * D / 2
 cy = Height - D
 drawcar
 For i = 1 To 20
 l1(i) = 0
 l2(i) = 0
 drawway (i)
 Next i
 Timer1.Enabled = True
 
 End Sub
 
 Private Sub drawcar()
 
 Line (cx, BT - 100)-Step(3 * D, D), BackColor, BF
 Line (cx + D, BT - 200)-Step(D, D), BackColor, BF '先擦
 Line (cx, BT - 100)-Step(3 * D, D), RGB(225, 0, 0), BF
 Line (cx + D, BT - 200)-Step(D, D), RGB(225, 0, 0), BF
 
 End Sub
 
 Private Sub drawway(n)
 
 Line (Width/2-3*D/2-2*D,BT-n*D)-Step(7*D,D),BackColor, BF
 '先擦后画
 Line (Width/2-3*D/2-2*D,BT-n*D)-Step(l1(n)*D, D), ,BF
 Line (Width/2-3*D/2+5*D,BT-n*D)-Step(-l2(n)*D,D), ,BF
 
 End Sub
 
 Private Sub Form_KeyPress(KeyAscii As Integer)
 
 Select Case KeyAscii
 Case Asc("a"), Asc("A")
 cx = cx - D
 Case Asc("s"), Asc("S")
 cx = cx + D
 End Select
 
 End Sub
 
 Private Sub Timer1_Timer()
 Randomize
 For i = 1 To 19
 l1(i) = l1(i + 1)
 l2(i) = l2(i + 1)
 drawway (i)
 Next i
 Do
 l1(20) = Int(Rnd * 5)
 l2(20) = Int(Rnd * 5)
 Loop Until ((l1(20) + l2(20) <= 4) And (l1(20) - l1(19) <= 1) And _
 (l2(20) - l2(19) <= 1) And (l1(19) + l2(20) <= 4) And _
 (l1(20) + l2(19) <= 4)) '这里生成新一层跑道,
 '注意要筛去玩家不可能通过的情况!
 drawway (20)
 '以上画出跑道
 drawcar
 test
 
 End Sub
 
 Private Sub test()
 
 If 3.5*D-Width/2+cx<l1(1)*D Then Timer1.Enabled=False
 If 3.5*D-Width/2+cx+D<l1(2)*D Then Timer1.Enabled=False
 If 3.5*D-(cx+3*D-Width/2)<l2(1)*D Then Timer1.Enabled=False
 If 3.5*D-(cx+2*D-Width/2)<l2(2)*D Then Timer1.Enabled=False
 
 End Sub
 |