Dim q As Integer: Dim w As Integer
Dim sq As Integer: Dim sw As Integer
Dim cq As Integer: Dim cw As Integer
Dim ss As Long
Dim c As Integer
Dim a As Integer
Dim t As Integer
Dim sh(350) As Integer '为了偷懒而都弄成了全局变量
Private Sub Form_Load()
Randomize '和随机函数配套的语句,为了刷新第一次的随机数,不然可能每次开始都是同一个数字
Form1.BackColor = RGB(999, 999, 999) '把背景颜色弄成白色
t = 3 '表示蛇头此时的方向为3(不是这个位置上的方向变量),也就是朝上,作为一直在进行的计时器用到的一个参数
ks '创建控件数组函数
sc '随机创建食物的函数
q = 7: w = 9 '初始化刚开始时候蛇的位置,总共有三段,这个是蛇头
sq = 9: sw = 9 '这个是蛇尾
sh(20 * sq + sw) = 3 '设置蛇尾的方向变量为3,也就是朝上
sh(169) = 3 '设置蛇身的方向变量为3,朝上
Picture1(20 * q + w).Visible = True '显示这三个控件
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园上.jpg")
Picture1(169).Visible = True
Picture1(169).Picture = LoadPicture(App.Path + "/园.jpg")
Picture1(20 * sq + sw).Visible = True
Picture1(20 * sq + sw).Picture = LoadPicture(App.Path + "/园.jpg")
End Sub
Public Sub ks() '先把所有的数组都创建好
Picture1(0).Visible = False
Dim i As Integer
Dim j As Integer
Dim count As Integer
For i = 1 To 17 '17X20的尺寸
For j = 1 To 20
If i <> 0 And j <> 0 Then
count = count + 1
Load Picture1(count)
Picture1(count).Left = 400 * j - 400 '400X400
Picture1(count).Top = 400 * i - 400
Picture1(count).Visible = False '设置为不可见
Picture1(count).Picture = LoadPicture(App.Path + "/园上.jpg") '先全部调用一次图片
End If
Next j
Next i
End Sub
Public Sub sc() '随机创建一个食物的坐标
cq = Int((14) * Rnd + 1): cw = Int((19) * Rnd + 1)
While sh(20 * cq + cw) > 0 '当食物创建在了蛇的身上时重新创建,sh表示蛇经过的方向,用1,2,3,4表示,所以它不为0时就表示这个位置有蛇,蛇正在经过
cq = Int((14) * Rnd + 1): cw = Int((20) * Rnd + 1)
Wend 'while语句的结束语句
c = 1 '这个是表示没有吃到食物,下面会说到
Picture1(20 * cq + cw).Visible = True '显示这个控件
Picture1(20 * cq + cw).Picture = LoadPicture(App.Path + "/吃园.jpg") '调用食物这张图片
End Sub
Private Sub Picture1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer) '即时的更改变量t,来更改蛇头的方向
If KeyCode = 37 And t <> 2 And a = 0 Then
t = 1
a = 1 '设置这个变量的原因是保证在计时器中循环过一次操作后才能再次改变蛇头的方向,不然会出现蛇头还没移动就此处乱转的囧境
ElseIf KeyCode = 39 And t <> 1 And a = 0 Then
t = 2
a = 1
ElseIf KeyCode = 38 And t <> 4 And a = 0 Then
t = 3
a = 1
ElseIf KeyCode = 40 And t <> 3 And a = 0 Then
t = 4
a = 1
ElseIf KeyCode = 83 Then '如果按下‘a’就加速
Timer1.Interval = Timer1.Interval + 50
ElseIf KeyCode = 65 Then '按下‘s’ 就减速
If Timer1.Interval <> 50 Then '如果速度等于50了就不能再减了呵呵,速度的上限出于VB的原因倒是有上限的
Timer1.Interval = Timer1.Interval - 50
End If
End If
End Sub
Private Sub Timer1_Timer()
If t = 1 Then '根据变量t来判断蛇头的方向
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园.jpg")
sh(20 * q + w) = 1 '根据蛇头的方向设置他经过的空间的方向变量,为1,朝左的,
If w = 0 Then '如果坐标是最左边的控件,那他再往左就会撞墙了
Print Tab(20); "撞墙了额,你输了"
Timer1.Enabled = Not Timer1.Enabled '如果撞墙了,就停止计时器
End If
w = w - 1 '如果不是最左边的控件,那就把他再往左移一格
If sh(20 * q + w) > 0 Then '前面说过,如果方向变量不为0,那说明此时蛇正经过,如果这里大于0,说明他咬了自己..
Print Tab(20); "你咬了自己,你输了"
Timer1.Enabled = Not Timer1.Enabled '停止计时器
End If
Picture1(20 * q + w).Visible = True '一切正常的话,就显示新的蛇头控件
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园左.jpg")
a = 0
If q = cq And w = cw Then '这个是判断是否吃到食物,如果蛇头的坐标和食物的坐标重合,则c为0,表示吃到,不然的话一直重复上面的循环
c = 0
Else
cy '进行一些更新蛇尾的操作
hh
End If
ElseIf t = 2 Then
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园.jpg")
sh(20 * q + w) = 2
If w = 20 Then
Print Tab(20); "撞墙了额,你输了"
Timer1.Enabled = Not Timer1.Enabled
End If
w = w + 1
If sh(20 * q + w) > 0 Then
Print Tab(20); "你咬了自己,你输了"
Timer1.Enabled = Not Timer1.Enabled
End If
Picture1(20 * q + w).Visible = True
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园右.jpg")
a = 0
If q = cq And w = cw Then
c = 0
Else
cy
hh
End If
ElseIf t = 3 Then
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园.jpg")
sh(20 * q + w) = 3
If q = 1 Then
Print Tab(20); "撞墙了额,你输了"
Timer1.Enabled = Not Timer1.Enabled
End If
q = q - 1
If sh(20 * q + w) > 0 Then
Print Tab(20); "你咬了自己,你输了"
Timer1.Enabled = Not Timer1.Enabled
End If
Picture1(20 * q + w).Visible = True
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园上.jpg")
a = 0
If q = cq And w = cw Then
c = 0
Else
cy
hh
End If
ElseIf t = 4 Then
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园.jpg")
sh(20 * q + w) = 4
If q = 14 Then
Print Tab(20); "撞墙了额,你输了"
Timer1.Enabled = Not Timer1.Enabled
End If
q = q + 1
If sh(20 * q + w) > 0 Then
Print Tab(20); "你咬了自己,你输了"
Timer1.Enabled = Not Timer1.Enabled
End If
Picture1(20 * q + w).Visible = True
Picture1(20 * q + w).Picture = LoadPicture(App.Path + "/园下.jpg")
sh(20 * q + w) = 5
a = 0
If q = cq And w = cw Then
c = 0
Else
cy
hh
End If
End If
If c = 0 Then '如果吃到的话,就重新初始化食物,并增加分数
sc
ss = ss + 100000 / Timer1.Interval * 1.33 '加分,公示里面加入了计时器的参数,所以蛇走的越快加的分数越高
End If
Label1.Caption = "你的得分:" & ss '刷新分数
Label2.Caption = "速度up按a,速度down按s :" & Int(200000 / Timer1.Interval) '显示速度
End Sub
Public Sub cy()
Picture1(20 * sq + sw).Visible = False '隐藏蛇尾
Picture1(20 * sq + sw).Picture = LoadPicture(App.Path + "/园.jpg")
Picture1(20 * cq + cw).Visible = True '初始化食物,不用担心食物会和身体重复,因为cq和cw坐标产生时都是调用了sc函数
Picture1(20 * cq + cw).Picture = LoadPicture(App.Path + "/吃园.jpg")
End Sub
Public Sub hh() '根据蛇尾的方向变量设定下一个蛇尾的位置,然后把之前的方向变量清零
If sh(20 * sq + sw) = 3 Then
sh(20 * sq + sw) = 0
sq = sq - 1
ElseIf sh(20 * sq + sw) = 4 Then
sh(20 * sq + sw) = 0
sq = sq + 1
ElseIf sh(20 * sq + sw) = 1 Then
sh(20 * sq + sw) = 0
sw = sw - 1
ElseIf sh(20 * sq + sw) = 2 Then
sh(20 * sq + sw) = 0
sw = sw + 1
End If
End Sub
大一时候学了vb这门课程,突然想做个玩玩,弄了一下午,过程挺痛苦的,毕竟才大一,代码写的也很臃肿吧,不过那时出来了还是很有成就感的
最近学弟在学vb,就把代码注释了一下,方便阅读,顺便发到这里