求用VB编程一贪食蛇小游戏,本人新手,求带详细注释加窗体!
答案:2 悬赏:60 手机版
解决时间 2021-02-25 23:25
- 提问者网友:不爱我么
- 2021-02-25 09:09
最好打包压缩发上来
最佳答案
- 五星知识达人网友:老鼠爱大米
- 2021-02-25 10:05
这个代码不要任何控件,你只需要把以下代码复制到你的窗体代码中就行了,代码很简单,相信你能看懂!仅仅用了一个结构(ShenTi)一个数组(ZhuangTai(23, 23) As Long) 一个变量(GFangXiang)
代码中自动生成了Timer1,Label1控件。以及几个函数(Randomize,Circle,Line,Erase...) 相信这些对于你来说很简单,只是没想到用这些方法来实现而已,现在你开始看看效果吧!
代码如下:
'贪吃蛇代码(无控件、全代码)
Private WithEvents Timer1 As Timer
Private WithEvents Label1 As Label
Dim GFangXiang As Boolean
Dim HWB As Single
Dim She() As ShenTi
Dim X As Long, Y As Long
Dim ZhuangTai(23, 23) As Long
Private Type ShenTi
F As Long
X As Long
Y As Long
End Type
'按键反应 ←↑↓→
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim C As Long
If KeyCode = 27 Then End 'ESC退出
If KeyCode = 32 Then
If Timer1.Enabled = True Then '空格暂停
Timer1.Enabled = False
Label1.Visible = True
Else '空格开始
Timer1.Enabled = True
Label1.Visible = False
End If
End If
C = UBound(She)
If GFangXiang = True Then Exit Sub
Select Case KeyCode
Case 37 '←
If She(C).F = 2 Then Exit Sub
She(C).F = 0
GFangXiang = True
Case 38 '↑
If She(C).F = 3 Then Exit Sub
She(C).F = 1
GFangXiang = True
Case 39 '↑
If She(C).F = 0 Then Exit Sub
She(C).F = 2
GFangXiang = True
Case 40 '→
If She(C).F = 1 Then Exit Sub
She(C).F = 3
GFangXiang = True
End Select
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
Me.BackColor = &HC000&
Me.FillColor = 255
Me.FillStyle = 0
Me.WindowState = 2
Set Timer1 = Controls.Add("VB.Timer", "Timer1")
Set Label1 = Controls.Add("VB.Label", "Label1")
Label1.AutoSize = True
Label1.BackStyle = 0
Label1 = "暂停"
Label1.ForeColor = RGB(255, 255, 0)
Label1.FontSize = 50
ChuShiHua '初始化
End Sub
Private Sub Form_Resize()
On Error GoTo 1:
With Me
If .WindowState <> 1 Then
.Cls
.ScaleMode = 3
HWB = .ScaleHeight / .ScaleWidth
.ScaleWidth = 24
.ScaleHeight = 24
Label1.Move (Me.ScaleWidth - Label1.Width) / 2, (Me.ScaleHeight - Label1.Height) / 2
HuaTu
Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF
End If
End With
1:
End Sub
Private Sub Timer1_Timer()
Dim C As Long, I As Long
On Error GoTo 2:
QingChu '清图
C = UBound(She)
Select Case She(C).F
Case 0
If ZhuangTai(She(C).X - 1, She(C).Y) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X - 1
She(C).Y = She(C - 1).Y
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X - 1, She(C).Y) = 1 Then
GoTo 2:
End If
Case 1
If ZhuangTai(She(C).X, She(C).Y - 1) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X
She(C).Y = She(C - 1).Y - 1
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X, She(C).Y - 1) = 1 Then
GoTo 2:
End If
Case 2
If ZhuangTai(She(C).X + 1, She(C).Y) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X + 1
She(C).Y = She(C - 1).Y
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X + 1, She(C).Y) = 1 Then
GoTo 2:
End If
Case 3
If ZhuangTai(She(C).X, She(C).Y + 1) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X
She(C).Y = She(C - 1).Y + 1
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X, She(C).Y + 1) = 1 Then
GoTo 2:
End If
End Select
ZhuangTai(She(0).X, She(0).Y) = 0
For I = 0 To C
Select Case She(I).F
Case 0
She(I).X = She(I).X - 1
Case 1
She(I).Y = She(I).Y - 1
Case 2
She(I).X = She(I).X + 1
Case 3
She(I).Y = She(I).Y + 1
End Select
Next
TiaoZheng
1:
GFangXiang = False
ZhuangTai(She(C).X, She(C).Y) = 1
HuaTu
Exit Sub
2: '游戏结束
If MsgBox("得分:" & UBound(She) - 2 & "分 " & vbCrLf & "游戏结束,点“是”重新开始游戏,点“否”", vbYesNo, "贪吃蛇") = vbYes Then
ChuShiHua
Else
End
End If
End Sub
'初始化
Private Sub ChuShiHua()
Me.Cls
Timer1.Enabled = True
Timer1.Interval = 50
Erase ZhuangTai
ReDim She(2)
She(0).F = 2
She(0).X = 9
She(0).Y = 11
ZhuangTai(9, 11) = 1
She(1).F = 2
She(1).X = 10
She(1).Y = 11
ZhuangTai(10, 11) = 1
She(2).F = 2
She(2).X = 11
She(2).Y = 11
ZhuangTai(11, 11) = 1
HuaTu '画图
ChanShengShiWu
End Sub
'清图
Private Sub QingChu()
Dim I As Long
For I = 0 To UBound(She)
Me.Line (She(I).X, She(I).Y)-(She(I).X + 1, She(I).Y + 1), Me.BackColor, BF
Next
End Sub
'画图 蛇
Private Sub HuaTu()
Dim I As Long
For I = 0 To UBound(She)
Me.Circle (She(I).X + 0.5, She(I).Y + 0.5), 0.49, RGB(255, 255, 0), , , HWB
Next
End Sub
Private Sub TiaoZheng()
Dim I As Long
For I = 0 To UBound(She) - 1
She(I).F = She(I + 1).F
Next
End Sub
'随机产生食物
Private Sub ChanShengShiWu()
Randomize Timer
1:
X = Int(Rnd * 24)
Y = Int(Rnd * 24)
If ZhuangTai(X, Y) > 0 Then GoTo 1:
ZhuangTai(X, Y) = 2
Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF
End Sub
代码中自动生成了Timer1,Label1控件。以及几个函数(Randomize,Circle,Line,Erase...) 相信这些对于你来说很简单,只是没想到用这些方法来实现而已,现在你开始看看效果吧!
代码如下:
'贪吃蛇代码(无控件、全代码)
Private WithEvents Timer1 As Timer
Private WithEvents Label1 As Label
Dim GFangXiang As Boolean
Dim HWB As Single
Dim She() As ShenTi
Dim X As Long, Y As Long
Dim ZhuangTai(23, 23) As Long
Private Type ShenTi
F As Long
X As Long
Y As Long
End Type
'按键反应 ←↑↓→
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim C As Long
If KeyCode = 27 Then End 'ESC退出
If KeyCode = 32 Then
If Timer1.Enabled = True Then '空格暂停
Timer1.Enabled = False
Label1.Visible = True
Else '空格开始
Timer1.Enabled = True
Label1.Visible = False
End If
End If
C = UBound(She)
If GFangXiang = True Then Exit Sub
Select Case KeyCode
Case 37 '←
If She(C).F = 2 Then Exit Sub
She(C).F = 0
GFangXiang = True
Case 38 '↑
If She(C).F = 3 Then Exit Sub
She(C).F = 1
GFangXiang = True
Case 39 '↑
If She(C).F = 0 Then Exit Sub
She(C).F = 2
GFangXiang = True
Case 40 '→
If She(C).F = 1 Then Exit Sub
She(C).F = 3
GFangXiang = True
End Select
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
Me.BackColor = &HC000&
Me.FillColor = 255
Me.FillStyle = 0
Me.WindowState = 2
Set Timer1 = Controls.Add("VB.Timer", "Timer1")
Set Label1 = Controls.Add("VB.Label", "Label1")
Label1.AutoSize = True
Label1.BackStyle = 0
Label1 = "暂停"
Label1.ForeColor = RGB(255, 255, 0)
Label1.FontSize = 50
ChuShiHua '初始化
End Sub
Private Sub Form_Resize()
On Error GoTo 1:
With Me
If .WindowState <> 1 Then
.Cls
.ScaleMode = 3
HWB = .ScaleHeight / .ScaleWidth
.ScaleWidth = 24
.ScaleHeight = 24
Label1.Move (Me.ScaleWidth - Label1.Width) / 2, (Me.ScaleHeight - Label1.Height) / 2
HuaTu
Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF
End If
End With
1:
End Sub
Private Sub Timer1_Timer()
Dim C As Long, I As Long
On Error GoTo 2:
QingChu '清图
C = UBound(She)
Select Case She(C).F
Case 0
If ZhuangTai(She(C).X - 1, She(C).Y) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X - 1
She(C).Y = She(C - 1).Y
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X - 1, She(C).Y) = 1 Then
GoTo 2:
End If
Case 1
If ZhuangTai(She(C).X, She(C).Y - 1) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X
She(C).Y = She(C - 1).Y - 1
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X, She(C).Y - 1) = 1 Then
GoTo 2:
End If
Case 2
If ZhuangTai(She(C).X + 1, She(C).Y) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X + 1
She(C).Y = She(C - 1).Y
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X + 1, She(C).Y) = 1 Then
GoTo 2:
End If
Case 3
If ZhuangTai(She(C).X, She(C).Y + 1) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X
She(C).Y = She(C - 1).Y + 1
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X, She(C).Y + 1) = 1 Then
GoTo 2:
End If
End Select
ZhuangTai(She(0).X, She(0).Y) = 0
For I = 0 To C
Select Case She(I).F
Case 0
She(I).X = She(I).X - 1
Case 1
She(I).Y = She(I).Y - 1
Case 2
She(I).X = She(I).X + 1
Case 3
She(I).Y = She(I).Y + 1
End Select
Next
TiaoZheng
1:
GFangXiang = False
ZhuangTai(She(C).X, She(C).Y) = 1
HuaTu
Exit Sub
2: '游戏结束
If MsgBox("得分:" & UBound(She) - 2 & "分 " & vbCrLf & "游戏结束,点“是”重新开始游戏,点“否”", vbYesNo, "贪吃蛇") = vbYes Then
ChuShiHua
Else
End
End If
End Sub
'初始化
Private Sub ChuShiHua()
Me.Cls
Timer1.Enabled = True
Timer1.Interval = 50
Erase ZhuangTai
ReDim She(2)
She(0).F = 2
She(0).X = 9
She(0).Y = 11
ZhuangTai(9, 11) = 1
She(1).F = 2
She(1).X = 10
She(1).Y = 11
ZhuangTai(10, 11) = 1
She(2).F = 2
She(2).X = 11
She(2).Y = 11
ZhuangTai(11, 11) = 1
HuaTu '画图
ChanShengShiWu
End Sub
'清图
Private Sub QingChu()
Dim I As Long
For I = 0 To UBound(She)
Me.Line (She(I).X, She(I).Y)-(She(I).X + 1, She(I).Y + 1), Me.BackColor, BF
Next
End Sub
'画图 蛇
Private Sub HuaTu()
Dim I As Long
For I = 0 To UBound(She)
Me.Circle (She(I).X + 0.5, She(I).Y + 0.5), 0.49, RGB(255, 255, 0), , , HWB
Next
End Sub
Private Sub TiaoZheng()
Dim I As Long
For I = 0 To UBound(She) - 1
She(I).F = She(I + 1).F
Next
End Sub
'随机产生食物
Private Sub ChanShengShiWu()
Randomize Timer
1:
X = Int(Rnd * 24)
Y = Int(Rnd * 24)
If ZhuangTai(X, Y) > 0 Then GoTo 1:
ZhuangTai(X, Y) = 2
Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF
End Sub
全部回答
- 1楼网友:英雄的欲望
- 2021-02-25 10:37
贪吃蛇
private sub form_keydown(keycode as integer, shift as integer)
dim c as long
if keycode = 27 then end
if keycode = 32 then
if timer1.enabled = true then
timer1.enabled = false
label1.visible = true
else
timer1.enabled = true
label1.visible = false
end if
end if
c = ubound(she)
if gfangxiang = true then exit sub
select case keycode
case 37
if she(c).f = 2 then exit sub
she(c).f = 0
gfangxiang = true
case 38
if she(c).f = 3 then exit sub
she(c).f = 1
gfangxiang = true
case 39
if she(c).f = 0 then exit sub
she(c).f = 2
gfangxiang = true
case 40
if she(c).f = 1 then exit sub
she(c).f = 3
gfangxiang = true
end select
end sub
private sub form_load()
me.autoredraw = true
me.backcolor = &hc000&
me.fillcolor = 255
me.fillstyle = 0
me.scalewidth = 24
me.scaleheight = 24
me.windowstate = 2
set timer1 = controls.add("vb.timer", "timer1")
set label1 = controls.add("vb.label", "label1")
label1.autosize = true
label1.backstyle = 0
label1 = "暂停"
label1.forecolor = rgb(255, 255, 0)
label1.fontsize = 50
chushihua
end sub
private sub form_resize()
on error goto 1:
with me
if .windowstate <> 1 then
.cls
.scalemode = 3
hwb = .scaleheight / .scalewidth
.scalewidth = 24
.scaleheight = 24
label1.move (me.scalewidth - label1.width) / 2, (me.scaleheight - label1.height) / 2
huatu
me.line (x, y)-(x + 1, y + 1), rgb(255, 255, 0), bf
end if
end with
1:
end sub
private sub timer1_timer()
dim c as long, i as long
on error goto 2:
qingchu
c = ubound(she)
select case she(c).f
case 0
if zhuangtai(she(c).x - 1, she(c).y) = 2 then
c = c + 1
redim preserve she(c)
she(c).f = she(c - 1).f
she(c).x = she(c - 1).x - 1
she(c).y = she(c - 1).y
chanshengshiwu
goto 1:
elseif zhuangtai(she(c).x - 1, she(c).y) = 1 then
goto 2:
end if
case 1
if zhuangtai(she(c).x, she(c).y - 1) = 2 then
c = c + 1
redim preserve she(c)
she(c).f = she(c - 1).f
she(c).x = she(c - 1).x
she(c).y = she(c - 1).y - 1
chanshengshiwu
goto 1:
elseif zhuangtai(she(c).x, she(c).y - 1) = 1 then
goto 2:
end if
case 2
if zhuangtai(she(c).x + 1, she(c).y) = 2 then
c = c + 1
redim preserve she(c)
she(c).f = she(c - 1).f
she(c).x = she(c - 1).x + 1
she(c).y = she(c - 1).y
chanshengshiwu
goto 1:
elseif zhuangtai(she(c).x + 1, she(c).y) = 1 then
goto 2:
end if
case 3
if zhuangtai(she(c).x, she(c).y + 1) = 2 then
c = c + 1
redim preserve she(c)
she(c).f = she(c - 1).f
she(c).x = she(c - 1).x
she(c).y = she(c - 1).y + 1
chanshengshiwu
goto 1:
elseif zhuangtai(she(c).x, she(c).y + 1) = 1 then
goto 2:
end if
end select
zhuangtai(she(0).x, she(0).y) = 0
for i = 0 to c
select case she(i).f
case 0
she(i).x = she(i).x - 1
case 1
she(i).y = she(i).y - 1
case 2
she(i).x = she(i).x + 1
case 3
she(i).y = she(i).y + 1
end select
next
tiaozheng
1:
gfangxiang = false
zhuangtai(she(c).x, she(c).y) = 1
huatu
exit sub
2:
if msgbox("游戏结束,点“是”重新开始游戏,点“否”", vbyesno, "贪吃蛇") = vbyes then
chushihua
else
end
end if
end sub
private sub chushihua()
me.cls
timer1.enabled = true
timer1.interval = 200
erase zhuangtai
redim she(2)
she(0).f = 2
she(0).x = 9
she(0).y = 11
zhuangtai(9, 11) = 1
she(1).f = 2
she(1).x = 10
she(1).y = 11
zhuangtai(10, 11) = 1
she(2).f = 2
she(2).x = 11
she(2).y = 11
zhuangtai(11, 11) = 1
huatu
chanshengshiwu
end sub
private sub qingchu()
dim i as long
for i = 0 to ubound(she)
me.line (she(i).x, she(i).y)-(she(i).x + 1, she(i).y + 1), me.backcolor, bf
next
end sub
private sub huatu()
dim i as long
for i = 0 to ubound(she)
me.circle (she(i).x + 0.5, she(i).y + 0.5), 0.49, rgb(255, 255, 0), , , hwb
next
end sub
private sub tiaozheng()
dim i as long
for i = 0 to ubound(she) - 1
she(i).f = she(i + 1).f
next
end sub
private sub chanshengshiwu()
randomize timer
1:
x = int(rnd * 24)
y = int(rnd * 24)
if zhuangtai(x, y) > 0 then goto 1:
zhuangtai(x, y) = 2
me.line (x, y)-(x + 1, y + 1), rgb(255, 255, 0), bf
end sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯