如果你对经典贪吃蛇游戏背后的编程逻辑感兴趣,这篇文章详细展示了如何使用Visual Basic进行游戏开发的关键代码段。从窗口初始化到游戏逻辑实现,这里有你需要的全部信息!快来学习如何制作你自己的贪吃蛇游戏吧。
PrivateSubForm_KeyDown(KeyCodeAsInteger,ShiftAsInteger)
DimCAsLong
IfKeyCode=27ThenEnd
IfKeyCode=32Then
IfTimer1.Enabled=TrueThen
Timer1.Enabled=False
Label1.Visible=True
Else
Timer1.Enabled=True
Label1.Visible=False
EndIf
EndIf
C=UBound(She)
IfGFangXiang=TrueThenExitSub
SelectCaseKeyCode
Case37
IfShe(C).F=2ThenExitSub
She(C).F=0
GFangXiang=True
Case38
IfShe(C).F=3ThenExitSub
She(C).F=1
GFangXiang=True
Case39
IfShe(C).F=0ThenExitSub
She(C).F=2
GFangXiang=True
Case40
IfShe(C).F=1ThenExitSub
She(C).F=3
GFangXiang=True
EndSelect
EndSub
PrivateSubForm_Load()
Me.AutoRedraw=True
Me.BackColor=&HC000&
Me.FillColor=255
Me.FillStyle=0
Me.ScaleWidth=24
Me.ScaleHeight=24
Me.WindowState=2
SetTimer1=Controls.Add(VB.Timer,Timer1)
SetLabel1=Controls.Add(VB.Label,Label1)
Label1.AutoSize=True
Label1.BackStyle=0
Label1=暂停
Label1.ForeColor=RGB(255,255,0)
Label1.FontSize=50
ChuShiHua
EndSub
PrivateSubForm_Resize()
OnErrorGoTo1:
WithMe
If.WindowState1Then
.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
EndIf
EndWith
1:
EndSub
PrivateSubTimer1_Timer()
DimCAsLong,IAsLong
OnErrorGoTo2:
QingChu
C=UBound(She)
SelectCaseShe(C).F
Case0
IfZhuangTai(She(C).X-1,She(C).Y)=2Then
C=C+1
ReDimPreserveShe(C)
She(C).F=She(C-1).F
She(C).X=She(C-1).X-1
She(C).Y=She(C-1).Y
ChanShengShiWu
GoTo1:
ElseIfZhuangTai(She(C).X-1,She(C).Y)=1Then
GoTo2:
EndIf
Case1
IfZhuangTai(She(C).X,She(C).Y-1)=2Then
C=C+1
ReDimPreserveShe(C)
She(C).F=She(C-1).F
She(C).X=She(C-1).X
She(C).Y=She(C-1).Y-1
ChanShengShiWu
GoTo1:
ElseIfZhuangTai(She(C).X,She(C).Y-1)=1Then
GoTo2:
EndIf
Case2
IfZhuangTai(She(C).X+1,She(C).Y)=2Then
C=C+1
ReDimPreserveShe(C)
She(C).F=She(C-1).F
She(C).X=She(C-1).X+1
She(C).Y=She(C-1).Y
ChanShengShiWu
GoTo1:
ElseIfZhuangTai(She(C).X+1,She(C).Y)=1Then
GoTo2:
EndIf
Case3
IfZhuangTai(She(C).X,She(C).Y+1)=2Then
C=C+1
ReDimPreserveShe(C)
She(C).F=She(C-1).F
She(C).X=She(C-1).X
She(C).Y=She(C-1).Y+1
ChanShengShiWu
GoTo1:
ElseIfZhuangTai(She(C).X,She(C).Y+1)=1Then
GoTo2:
EndIf
EndSelect
ZhuangTai(She(0).X,She(0).Y)=0
ForI=0ToC
SelectCaseShe(I).F
Case0
She(I).X=She(I).X-1
Case1
She(I).Y=She(I).Y-1
Case2
She(I).X=She(I).X+1
Case3
She(I).Y=She(I).Y+1
EndSelect
Next
TiaoZheng
1:
GFangXiang=False
ZhuangTai(She(C).X,She(C).Y)=1
HuaTu
ExitSub
2:
IfMsgBox(游戏结束,点“是”重新开始游戏,点“否”,vbYesNo,贪吃蛇)=vbYesThen
ChuShiHua
Else
End
EndIf
EndSub
PrivateSubChuShiHua()
Me.Cls
Timer1.Enabled=True
Timer1.Interval=200
EraseZhuangTai
ReDimShe(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
EndSub
PrivateSubQingChu()
DimIAsLong
ForI=0ToUBound(She)
Me.Line(She(I).X,She(I).Y)-(She(I).X+1,She(I).Y+1),Me.BackColor,BF
Next
EndSub
PrivateSubHuaTu()
DimIAsLong
ForI=0ToUBound(She)
Me.Circle(She(I).X+0.5,She(I).Y+0.5),0.49,RGB(255,255,0),,,HWB
Next
EndSub
PrivateSubTiaoZheng()
DimIAsLong
ForI=0ToUBound(She)-1
She(I).F=She(I+1).F
Next
EndSub
PrivateSubChanShengShiWu()
RandomizeTimer
1:
X=Int(Rnd*24)
Y=Int(Rnd*24)
IfZhuangTai(X,Y)>0ThenGoTo1:
ZhuangTai(X,Y)=2
Me.Line(X,Y)-(X+1,Y+1),RGB(255,255,0),BF
EndSub
直接复制到空代码窗口