用VB6.0设计简易赛车游戏

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 yaozheng 的 blog

简单的游戏往往更耐玩,就比如伴我度过高考的赛车游戏:一切都是方块,所谓的赛车也只是四个方块。

  第一步,绘制对象:用函数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 

本文关键:VB 游戏
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top