VB贪吃蛇代码
的有关信息介绍如下:问题补充说明:我要的是很带的那种 只要可以动 可以吃东西 可以死的那种 要初学者就好编的那种 多谢啦
'定义蛇的运动速度枚举值
Pri够委修观命春氢灯vateEnumtpsSpeed
QUICKLY=0
SLOWLY=1
EndEnum
'定义蛇的运动方向枚举值
PrivateEnumtpsDirection
D_UP=38
D_DOWN=40
D_LEFT=37
D_RIGHT=39
EndEnum
'定义运动区域4个禁区的枚举值
PrivateEnumtpsForbiddenZone
FZ_TOP=30
FZ_BOTTOM=5330
FZ_LEFT=30
FZ_RIGHT=5730
EndEnum
'定义蛇头及身体初始化数枚举值
PrivateEnumtps来自Snake
SNAKEONE=1
SNAKETWO=2
SNAKETHREE=3
SNAKEFOUR=4
EndEnum
'定义蛇宽度的常量
PrivateConstSNAKEWIDTHAsInteger=100
'该过程用于显示游戏信息
PrivateSubForm_Load()
***.Show
Me.lbl&App.Major&"."&App.Minor&"."&App.Revision&")"
Me.Caption=Me.lblTitle.Caption
***.Show1
EndSub
'该过程用于使窗体恢复原始大小
PrivateSubForm_Resize()
率员节盟IfMe.Win兰似dowState<>1Then
Me.Caption=""
Me.Height=6405'窗体高度为6405缇
Me.Width=8535'窗体宽度为8535缇
Me.Left=(Screen.Width-Width)\2
Me.Top=(Screen.Height-Height式酒凯假才皇上扩空)\2
EndIf
EndSub
'该过程用于重新开始开始编微也础历游戏
PrivateSubcmdGameStart_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
B袁边断才棉充eep
msg=MsgBox("您跟庆确认要重新开始游戏吗?",4+32,"BS贪食蛇")
Ifmsg=6ThenCallm_subGameI表nitialize
EndSub
'该过程用于暂停/运行游戏
PrivateSubchkPause_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
IfMe.chkPause.Caption="暂停游戏(&P)"Then
Me.tmrSnakeMove.Enabl协成到选看地境迅划吧ed=False
Me器斯生全.tmrGameTime.Enabl力宽眼鲜木项需式商黄ed=False
Me.留卫极强标picMoveArea.Enabled=False
Me.lblPauseLab.Visible=True
Me.chkPause.Cap互从银战径tion="继续游戏(&R)"
Else
Me.tmrS永科四病nakeMove.Enabled=True
Me.tmrGameTime.裂题须还备重船诗Enabled=True
Me.picMoveArea.Enabled=True
Me.lblPauseLab.Visible=False
Me.ch程期风项氢己省活部kPause.Caption="暂停游戏(&P)"
EndIf
EndSub
'该过程用于显示游戏规则
PrivateSubcmdGameRules_MouseDown(ButtonAs副去所固Integer,ShiftAsInteger,XAsSingle,YAsSingle)
Beep
MsgBox"B消官任静航都始解落S贪食蛇:一个规则最简单的趣味游戏,您将用键盘"&Chr(13)&_
"上的4个方向键来控制蛇的运动方向。在运动过程中蛇"&Chr(13)&_
"不能后退,蛇的头部也不能接触到运动区域的边线以外"&Chr(13)&_
"和蛇自己的身体,否则就游戏失败。在吃掉随机出现的"&Chr(13)&_
"果子后,蛇的身体会变长,越长难度越大。祝您好运!!",0+64,"游戏规则"
EndSub
'该过程用于显示游戏开发信息
PrivateSubcmdAbout_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
Beep
MsgBox"BS贪食蛇"&"(V-"&App.Major&"."&App.Minor&"版本)"&Chr(13)&Chr(13)&_
""&Chr(13)&Chr(13)&_
"由PigheadPrince设计制作"&Chr(13)&_
"CopyRight(C)2002,BestSoft.TCG",0,"关于本游戏"
EndSub
'该过程用于退出游戏
PrivateSubcmdExit_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
Beep
msg=MsgBox("您要退出本游戏吗?",4+32,"BS贪食蛇")
SelectCasemsg
Case6
End
Case7
Me.chkWindowButton(2).Value=0
ExitSub
EndSelect
EndSub
'该过程用于拖动窗体_(点击图标)
PrivateSubimgWindowTop_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
ReleaseCapture
SendMessageMe.hwnd,WM_SYSCOMMAND,SC_MOVE,0
EndSub
'该共用过程用于处理窗体控制按钮组的相关操作_(锁定、最小化、退出)
PrivateSubchkWindowButton_MouseUp(IndexAsInteger,ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
IfButton<>1ThenExitSub
SelectCaseIndex
Case0'锁定窗体
IfMe.chkWindowButton(0).Value=1Then
Me.imgWindowTop.BorderStyle=0
Me.imgWindowTop.Enabled=False
Else
Me.imgWindowTop.BorderStyle=1
Me.imgWindowTop.Enabled=True
EndIf
Case1'最小化
Me.WindowState=1
Me.chkWindowButton(1).Value=0
Me.Caption="BS贪食蛇—(V-"&App.Major&"."&App.Minor&"版本)"
Case2'退出
Beep
msg=MsgBox("您要退出本游戏吗?",4+32,"BS贪食蛇")
SelectCasemsg
Case6
End
Case7
Me.chkWindowButton(2).Value=0
ExitSub
EndSelect
EndSelect
EndSub
'该过程用于设置蛇运动速度的快慢
PrivateSubhsbGameSpeed_Change()
Me.tmrSnakeMove.Interval=Me.hsbGameSpeed.Value
EndSub
'该过程用于通过键盘的方向键改变蛇的运动方向
PrivateSubpicMoveArea_KeyDown(KeyCodeAsInteger,ShiftAsInteger)
SelectCaseg_intDirection
CaseD_UP
IfKeyCode=D_DOWNThenExitSub
CaseD_DOWN
IfKeyCode=D_UPThenExitSub
CaseD_LEFT
IfKeyCode=D_RIGHTThenExitSub
CaseD_RIGHT
IfKeyCode=D_LEFTThenExitSub
EndSelect
g_intDirection=KeyCode
EndSub
'该计时循环过程用于计算游戏耗费的秒数并显示
PrivateSubtmrGameTime_Timer()
g_lngGameTime=g_lngGameTime+1
Me.lblGameTime.Caption=g_lngGameTime&"秒"
EndSub
'该计时循环过程用于控制蛇的行动轨迹
PrivateSubtmrSnakeMove_Timer()
DimlngSnakeXAsLong,lngSnakeYAsLong,lngSnakeColorAsLong
DimlngPointXAsLong,lngPointYAsLong,lngPointColorAsLong
Randomize
Me.picMoveArea.SetFocus
Me.picMoveArea.Cls
'确认蛇头的运动方向并获取新的位置
SelectCaseg_intDirection
CaseD_UP'向上运动
g_udtSnake(SNAKEONE).Snake_CurX=g_udtSnake(SNAKEONE).Snake_OldX
g_udtSnake(SNAKEONE).Snake_CurY=g_udtSnake(SNAKEONE).Snake_OldY
g_udtSnake(SNAKEONE).Snake_CurY=g_udtSnake(SNAKEONE).Snake_CurY-SNAKEWIDTH
CaseD_DOWN'向下运动
g_udtSnake(SNAKEONE).Snake_CurX=g_udtSnake(SNAKEONE).Snake_OldX
g_udtSnake(SNAKEONE).Snake_CurY=g_udtSnake(SNAKEONE).Snake_OldY
g_udtSnake(SNAKEONE).Snake_CurY=g_udtSnake(SNAKEONE).Snake_CurY+SNAKEWIDTH
CaseD_LEFT'向左运动
g_udtSnake(SNAKEONE).Snake_CurX=g_udtSnake(SNAKEONE).Snake_OldX
g_udtSnake(SNAKEONE).Snake_CurX=g_udtSnake(SNAKEONE).Snake_CurX-SNAKEWIDTH
g_udtSnake(SNAKEONE).Snake_CurY=g_udtSnake(SNAKEONE).Snake_OldY
CaseD_RIGHT'向右运动
g_udtSnake(SNAKEONE).Snake_CurX=g_udtSnake(SNAKEONE).Snake_OldX
g_udtSnake(SNAKEONE).Snake_CurX=g_udtSnake(SNAKEONE).Snake_CurX+SNAKEWIDTH
g_udtSnake(SNAKEONE).Snake_CurY=g_udtSnake(SNAKEONE).Snake_OldY
EndSelect
'根据新的位置绘制蛇头
lngSnakeX=g_udtSnake(SNAKEONE).Snake_CurX
lngSnakeY=g_udtSnake(SNAKEONE).Snake_CurY
lngSnakeColor=g_udtSnake(SNAKEONE).Snake_Color
Me.picMoveArea.PSet(lngSnakeX,lngSnakeY),lngSnakeColor
'移动蛇身体其他部分的位置
Fori=2Tog_intSnakeLength
g_udtSnake(i).Snake_CurX=g_udtSnake(i-1).Snake_OldX
g_udtSnake(i).Snake_CurY=g_udtSnake(i-1).Snake_OldY
lngSnakeX=g_udtSnake(i).Snake_CurX
lngSnakeY=g_udtSnake(i).Snake_CurY
lngSnakeColor=g_udtSnake(i).Snake_Color
Me.picMoveArea.PSet(lngSnakeX,lngSnakeY),lngSnakeColor
Nexti
'更新蛇旧的坐标位置
Forj=1Tog_intSnakeLength
g_udtSnake(j).Snake_OldX=g_udtSnake(j).Snake_CurX
g_udtSnake(j).Snake_OldY=g_udtSnake(j).Snake_CurY
Nextj
'判断蛇在移动中是否到了禁区而导致游戏失败
Ifm_funMoveForbiddenZone(g_udtSnake(SNAKEONE).Snake_CurX,g_udtSnake(SNAKEONE).Snake_CurY)Then
Beep
MsgBox"您的蛇移动到了禁区,游戏失败!",0+16,"BS贪食蛇"
Me.tmrSnakeMove.Enabled=False
Me.tmrGameTime.Enabled=False
Me.picMoveArea.Visible=False
ExitSub
EndIf
'判断蛇在移动中是否碰到了自己的身体而导致游戏失败
Ifm_funTouchSnakeBody(g_udtSnake(SNAKEONE).Snake_CurX,g_udtSnake(SNAKEONE).Snake_CurY)Then
Beep
MsgBox"您的蛇在移动中碰到了自己的身体,游戏失败!",0+16,"BS贪食蛇"
Me.tmrSnakeMove.Enabled=False
Me.tmrGameTime.Enabled=False
Me.picMoveArea.Visible=False
ExitSub
EndIf
'判断蛇是否吃到了果子
Ifm_funEatPoint(g_udtSnake(SNAKEONE).Snake_CurX,g_udtSnake(SNAKEONE).Snake_CurY)Then
'累加玩家的得分并刷新得分显示
g_intPlayerScore=g_intPlayerScore+1
Me.lblYourScore.Caption=g_intPlayerScore&"分"
Callm_subAddSnake'加长蛇的身体
Callm_subGetPoint'获取下一个果子的位置和颜色
Else
'绘制果子
lngPointX=g_udtPoint.Point_X
lngPointY=g_udtPoint.Point_Y
lngPointColor=g_udtPoint.Point_Color
Me.picMoveArea.PSet(lngPointX,lngPointY),lngPointColor
EndIf
EndSub
'该私有子过程用于初始化游戏
PrivateSubm_subGameInitialize()
Eraseg_udtSnake'清空蛇的结构数组
g_intPlayerScore=0'清空玩家的得分
g_lngGameTime=0'清空游戏耗费的秒数
g_intDirection=D_DOWN'设定蛇的初始运动方向为下
g_intSnakeLength=4'设定蛇的初始长度
ReDimg_udtSnake(1Tog_intSnakeLength)'重新定义蛇的长度
'定义蛇头部的数据
Withg_udtSnake(SNAKEONE)
.Snake_OldX=530
.Snake_OldY=530
.Snake_Color=vbBlack
EndWith
'定义蛇身第2节的数据
Withg_udtSnake(SNAKETWO)
.Snake_OldX=530
.Snake_OldY=430
.Snake_Color=vbGreen
EndWith
'定义蛇身第3节的数据
Withg_udtSnake(SNAKETHREE)
.Snake_OldX=530
.Snake_OldY=330
.Snake_Color=vbYellow
EndWith
'定义蛇身第4节的数据
Withg_udtSnake(SNAKEFOUR)
.Snake_OldX=530
.Snake_OldY=230
.Snake_Color=vbRed
EndWith
Me.picMoveArea.Visible=True
Me.lblYourScore.Caption=g_intPlayerScore&"分"
Me.lblGameTime.Caption=g_lngGameTime&"秒"
Me.tmrSnakeMove.Interval=Me.hsbGameSpeed.Value
Me.tmrSnakeMove.Enabled=True
Me.tmrGameTime.Enabled=True
Callm_subGetPoint'获取第一个果子的位置和颜色
EndSub
'该私有子过程用于返回获取的果子的位置和颜色信息
PrivateSubm_subGetPoint()
DimlngRedValueAsLong,lngGreenValueAsLong,lngBlueValueAsLong
DimlngPointXAsLong,lngPointYAsLong,lngPointColorAsLong
'随机获取果子的颜色
lngRedValue=Int((255-0+1)*Rnd+0)
lngGreenValue=Int((255-0+1)*Rnd+0)
lngBlueValue=Int((255-0+1)*Rnd+0)
lngPointColor=RGB(lngRedValue,lngGreenValue,lngBlueValue)
'随机获取果子的位置
lngPointX=Int((FZ_LEFT-FZ_RIGHT+1)*Rnd+FZ_RIGHT)
lngPointY=Int((FZ_TOP-FZ_BOTTOM+1)*Rnd+FZ_BOTTOM)
Me.PSet(lngPointX,lngPointY),lngPointColor
'设置函数返回值
Withg_udtPoint
.Point_X=lngPointX
.Point_Y=lngPointY
.Point_Color=lngPointColor
EndWith
EndSub
'该私有子过程用于加长蛇的身体
PrivateSubm_subAddSnake()
DimudtSnakeTemp()AsSnake
DimlngSnakeXAsLong,lngSnakeYAsLong,lngSnakeColorAsLong
'备份蛇原先身体的数据并使蛇的身体加长
ReDimudtSnakeTemp(1Tog_intSnakeLength)
Fork=1Tog_intSnakeLength
WithudtSnakeTemp(k)
.Snake_CurX=g_udtSnake(k).Snake_CurX
.Snake_CurY=g_udtSnake(k).Snake_CurY
.Snake_OldX=g_udtSnake(k).Snake_OldX
.Snake_OldY=g_udtSnake(k).Snake_OldY
.Snake_Color=g_udtSnake(k).Snake_Color
EndWith
Nextk
g_intSnakeLength=g_intSnakeLength+1
ReDimg_udtSnake(g_intSnakeLength)
'将备份蛇身体的数据返回到加长的蛇身数组中
Forl=1Tog_intSnakeLength-1
Withg_udtSnake(l)
.Snake_CurX=udtSnakeTemp(l).Snake_CurX
.Snake_CurY=udtSnakeTemp(l).Snake_CurY
.Snake_OldX=udtSnakeTemp(l).Snake_OldX
.Snake_OldY=udtSnakeTemp(l).Snake_OldY
.Snake_Color=udtSnakeTemp(l).Snake_Color
EndWith
Nextl
'写入新加入的身体数据
SelectCaseg_intDirection
CaseD_UP
Withg_udtSnake(g_intSnakeLength)
.Snake_OldX=g_udtSnake(g_intSnakeLength-1).Snake_CurX+SNAKEWIDTH
.Snake_OldY=g_udtSnake(g_intSnakeLength-1).Snake_CurY
.Snake_Color=g_udtPoint.Point_Color
EndWith
CaseD_DOWN
Withg_udtSnake(g_intSnakeLength)
.Snake_OldX=g_udtSnake(g_intSnakeLength-1).Snake_CurX-SNAKEWIDTH
.Snake_OldY=g_udtSnake(g_intSnakeLength-1).Snake_CurY
.Snake_Color=g_udtPoint.Point_Color
EndWith
CaseD_LEFT
Withg_udtSnake(g_intSnakeLength)
.Snake_OldX=g_udtSnake(g_intSnakeLength-1).Snake_CurX
.Snake_OldY=g_udtSnake(g_intSnakeLength-1).Snake_CurY+SNAKEWIDTH
.Snake_Color=g_udtPoint.Point_Color
EndWith
CaseD_RIGHT
Withg_udtSnake(g_intSnakeLength)
.Snake_OldX=g_udtSnake(g_intSnakeLength-1).Snake_CurX
.Snake_OldY=g_udtSnake(g_intSnakeLength-1).Snake_CurY-SNAKEWIDTH
.Snake_Color=g_udtPoint.Point_Color
EndWith
EndSelect
lngSnakeX=g_udtSnake(g_intSnakeLength).Snake_CurX
lngSnakeY=g_udtSnake(g_intSnakeLength).Snake_CurY
lngSnakeColor=g_udtSnake(g_intSnakeLength).Snake_Color
Me.picMoveArea.PSet(lngSnakeX,lngSnakeY),lngSnakeColor
EndSub
'该自定义函数用于返回运动的蛇是否到达禁区而导致游戏失败
PrivateFunctionm_funMoveForbiddenZone(SnakeXAsLong,SnakeYAsLong)AsBoolean
If(SnakeX>=FZ_LEFTAndSnakeX<=FZ_RIGHT)And(SnakeY>=FZ_TOPAndSnakeY<=FZ_BOTTOM)Then
m_funMoveForbiddenZone=False
Else
m_funMoveForbiddenZone=True
EndIf
EndFunction
'该自定义函数用于返回运动的蛇是否碰到自己的身体而导致游戏失败
PrivateFunctionm_funTouchSnakeBody(SnakeXAsLong,SnakeYAsLong)AsBoolean
Form=2Tog_intSnakeLength
IfSnakeX=g_udtSnake(m).Snake_CurXAndSnakeY=g_udtSnake(m).Snake_CurYThen
m_funTouchSnakeBody=True
ExitFor
Else
m_funTouchSnakeBody=False
EndIf
Nextm
EndFunction
'该自定义函数用于返回运动的蛇是否吃到了果子
PrivateFunctionm_funEatPoint(SnakeXAsLong,SnakeYAsLong)AsBoolean
IfAbs(SnakeX-g_udtPoint.Point_X)<=SNAKEWIDTHAndAbs(SnakeY-g_udtPoint.Point_Y)<=SNAKEWIDTHThen
m_funEatPoint=True
Else
m_funEatPoint=False
EndIf
EndFunction
'(API函数调用过程_用以实现无标题窗体的拖动操作)---------------------------------
'RleaseCapture函数用以释放鼠标捕获
PublicDeclareFunctionReleaseCaptureLib"user32"()AsLong
'SendMessage函数用作向Windows发送移动窗体的消息
PublicDeclareFunctionSendMessageLib"user32"Alias"SendMessageA"(ByValhwndAs_
Long,ByValwMsgAsLong,ByValwParamAsLong,IParamAsAny)AsLong
PublicConstWM_SYSCOMMAND=&H112'声明向Windows发送消息的常量
PublicConstSC_MOVE=&HF012'声明控制移动窗体常量
'(游戏变量声明部分)-------------------------------------------------------------
'定义蛇的数据类型结构
PublicTypeSnake
Snake_OldXAsLong
Snake_OldYAsLong
Snake_CurXAsLong
Snake_CurYAsLong
Snake_ColorAsLong
EndType
'定义果子的数据类型结构
PublicTypePoint
Point_XAsLong
Point_YAsLong
Point_ColorAsLong
EndType
'定义蛇的动态数组
Publicg_udtSnake()AsSnake
'定义果子
Publicg_udtPointAsPoint
'定义蛇的长度
Publicg_intSnakeLengthAsInteger
'定义蛇的颜色
Publicg_lngSnakeColorAsLong
'定义蛇的运动方向
Publicg_intDirectionAsInteger
'定义玩家的得分
Publicg_intPlayerScoreAsInteger
'定义游戏耗费的秒数
Publicg_lngGameTimeAsLong