VB实现贪吃蛇小游戏

更新时间:2024-05-11 10:27:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

VB实现贪吃蛇小游戏

——比较适合VB初学者,属原创作品

图1

图2

图3

如图1所示,布置控件,其中有4个timer控件,1个picture控件(底图),4个command控件(上下左右),10个label控件。

其他功能都在程序里实现,其中贪吃蛇都是由点来组成的,初学者主要学习一下timer控件的应用,还有贪吃蛇拐弯怎么实现的。图2、图3是游戏视图。

主程序:

Public a, b, f As Double: Public i, j, c, d, e, g, h, z As Integer Dim aa(100) As Double: Dim bb(100) As Double

Private Sub Form_Load()

a = 100: b = 300: c = 4 'a、b定义第一个点(红点)位置,c能控制蓝点数量 g = 2000: h = 2000

Timer1.Interval = (200 - z * 30) '定义每个定时器间隔时间 0.2S Timer2.Interval = (200 - z * 30) Timer3.Interval = (200 - z * 30) Timer4.Interval = (200 - z * 30) Timer5.Interval = 300

Timer1.Enabled = True '初设一开始向右走 Timer2.Enabled = False Timer3.Enabled = False Timer4.Enabled = False

End Sub

Private Sub Timer1_Timer() '向右走

Picture1.Cls '没循环一次就清除上一次画的图像,要不就看不出运动了

Picture1.DrawWidth = 10 '定义画图粗细

b = b + 100 'b每次都加100,画图后坐标就变了 aa(i) = a bb(j) = b

Picture1.PSet (b, a), vbRed '绘制第一个点(红)

For k = 1 To c '此k-for循环从1到c,绘制c个蓝色点

If aa(99) = 0 Then '如果游戏刚开始,以下绘制方法,一开始j=0,j-k为负,bb(j-k)与aa(j-k)数组无效,因此在j<=c时单独绘制 If j <= c And i <= c Then f = 400 - 100 * (k - j)

Picture1.PSet (f, 100), vbBlue Else

Picture1.PSet (bb(j - k), aa(i - k)), vbBlue '其他情况时,蓝点走过红点的轨迹 End If End If

If aa(99) <> 0 Or bb(99) <> 0 Then '如果时j和i运行到99后变为0,则bb(0)为红点时,蓝点必须走bb(99)、bb(98).... If j <= c And i <= c Then

d = 100 - k + j: e = 100 - k + i '此行与以下两行作用就是为了实现蓝点绘制时混合了...bb(0)、bb(99)...的情况 If d >= 100 Then d = d - 100 If e >= 100 Then e = e - 100

Picture1.PSet (bb(d), aa(e)), vbBlue Else

Picture1.PSet (bb(j - k), aa(i - k)), vbBlue End If End If Next k

If b > 4900 Or b < 100 Or a > 4900 Or a < 100 Then '碰墙则重新开始,以下进行了数据重置和清零

j = -1: i = -1: b = 300: a = 100: c = 4: z = 0 '如果碰墙了,j=-1时因为运行完了 后面有j=j+1,这样j就等于0了,把a、b重置

MsgBox (\对不起!您碰墙了!接下来将重新开始游戏!\ For o = 0 To 99

aa(o) = 0: bb(o) = 0 '把aa()、bb()数组中

所有的数清零 Next o End If

If j > c Then '咬尾了就重新开始游戏,以下进行了数据重置和清零 For p = 1 To c

If bb(j) = bb(j - p) And aa(i) = aa(i - p) Then j = -1: i = -1: b = 300: a = 100: c = 4: z = 0

MsgBox (\对不起!您咬尾了!接下来将重新开始游戏!\ For o = 0 To 99

aa(o) = 0: bb(o) = 0 Next o Exit For End If Next p End If

j = j + 1 i = i + 1

If j = 100 Or i = 100 Then j = 0: i = 0 'j不能持续增长,有可能会溢出,现在另j到了100就变0 End Sub

Private Sub Timer2_Timer() '向左走 Picture1.Cls

Picture1.DrawWidth = 10 b = b - 100 aa(i) = a bb(j) = b

Picture1.PSet (b, a), vbRed

For k = 1 To c

If aa(99) = 0 Then

If j <= c And i <= c Then

Picture1.PSet (400 - 100 * (k - j), 100), vbBlue Else

Picture1.PSet (bb(j - k), aa(i - k)), vbBlue End If End If

If aa(99) <> 0 Or bb(99) <> 0 Then If j <= c And i <= c Then

d = 100 - k + j: e = 100 - k + i

If d >= 100 Then d = d - 100 If e >= 100 Then e = e - 100

Picture1.PSet (bb(d), aa(e)), vbBlue Else

Picture1.PSet (bb(j - k), aa(i - k)), vbBlue End If End If Next k

If b > 4900 Or b < 100 Or a > 4900 Or a < 100 Then j = -1: i = -1: b = 300: a = 100: c = 4: z = 0

MsgBox (\对不起!您碰墙了!接下来将重新开始游戏!\ For o = 0 To 99

aa(o) = 0: bb(o) = 0 Next o

Timer2.Enabled = False Timer1.Enabled = True End If

If j > c Then

For p = 1 To c

If bb(j) = bb(j - p) And aa(i) = aa(i - p) Then j = -1: i = -1: b = 300: a = 100: c = 4: z = 0

MsgBox (\对不起!您咬尾了!接下来将重新开始游戏!\ For o = 0 To 99

aa(o) = 0: bb(o) = 0 Next o

Timer2.Enabled = False Timer1.Enabled = True Exit For End If Next p End If

j = j + 1 i = i + 1

If j = 100 Or i = 100 Then j = 0: i = 0 End Sub

Private Sub Timer3_Timer() '向下走 Picture1.Cls

Picture1.DrawWidth = 10 a = a + 100 aa(i) = a

本文来源:https://www.bwwdw.com/article/gugg.html

Top