最新整理VB实现贪吃蛇小游戏.docx

上传人:b****3 文档编号:10704015 上传时间:2023-05-27 格式:DOCX 页数:16 大小:101.41KB
下载 相关 举报
最新整理VB实现贪吃蛇小游戏.docx_第1页
第1页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第2页
第2页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第3页
第3页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第4页
第4页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第5页
第5页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第6页
第6页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第7页
第7页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第8页
第8页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第9页
第9页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第10页
第10页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第11页
第11页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第12页
第12页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第13页
第13页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第14页
第14页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第15页
第15页 / 共16页
最新整理VB实现贪吃蛇小游戏.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

最新整理VB实现贪吃蛇小游戏.docx

《最新整理VB实现贪吃蛇小游戏.docx》由会员分享,可在线阅读,更多相关《最新整理VB实现贪吃蛇小游戏.docx(16页珍藏版)》请在冰点文库上搜索。

最新整理VB实现贪吃蛇小游戏.docx

最新整理VB实现贪吃蛇小游戏

(完整)VB实现贪吃蛇小游戏

编辑整理:

 

尊敬的读者朋友们:

这里是精品文档编辑中心,本文档内容是由我和我的同事精心编辑整理后发布的,发布之前我们对文中内容进行仔细校对,但是难免会有疏漏的地方,但是任然希望((完整)VB实现贪吃蛇小游戏)的内容能够给您的工作和学习带来便利。

同时也真诚的希望收到您的建议和反馈,这将是我们进步的源泉,前进的动力。

本文可编辑可修改,如果觉得对您有帮助请收藏以便随时查阅,最后祝您生活愉快业绩进步,以下为(完整)VB实现贪吃蛇小游戏的全部内容。

VB实现贪吃蛇小游戏

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

图1

图2

图3

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

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

主程序:

Publica,b,fAsDouble:

Publici,j,c,d,e,g,h,zAsInteger

Dimaa(100)AsDouble:

Dimbb(100)AsDouble

PrivateSubForm_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

EndSub

PrivateSubTimer1_Timer()'向右走

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

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

b=b+100’b每次都加100,画图后坐标就变了

aa(i)=a

bb(j)=b

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

Fork=1Toc’此k-for循环从1到c,绘制c个蓝色点

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

Ifj〈=cAndi〈=cThen

f=400—100*(k-j)

Picture1。

PSet(f,100),vbBlue

Else

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

EndIf

EndIf

Ifaa(99)<>0Orbb(99)〈〉0Then'如果时j和i运行到99后变为0,则bb(0)为红点时,蓝点必须走bb(99)、bb(98).。

Ifj<=cAndi<=cThen

d=100—k+j:

e=100—k+i’此行与以下两行作用就是为了实现蓝点绘制时混合了...bb(0)、bb(99)。

..的情况

Ifd〉=100Thend=d-100

Ife>=100Thene=e—100

Picture1。

PSet(bb(d),aa(e)),vbBlue

Else

Picture1.PSet(bb(j—k),aa(i—k)),vbBlue

EndIf

EndIf

Nextk

Ifb〉4900Orb<100Ora>4900Ora〈100Then’碰墙则重新开始,以下进行了数据重置和清零

j=—1:

i=-1:

b=300:

a=100:

c=4:

z=0’如果碰墙了,j=—1时因为运行完了后面有j=j+1,这样j就等于0了,把a、b重置

MsgBox("对不起!

您碰墙了!

接下来将重新开始游戏!

")

Foro=0To99

aa(o)=0:

bb(o)=0'把aa()、bb()数组中所有的数清零

Nexto

EndIf

Ifj〉cThen'咬尾了就重新开始游戏,以下进行了数据重置和清零

Forp=1Toc

Ifbb(j)=bb(j-p)Andaa(i)=aa(i—p)Then

j=-1:

i=-1:

b=300:

a=100:

c=4:

z=0

MsgBox(”对不起!

您咬尾了!

接下来将重新开始游戏!

”)

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

ExitFor

EndIf

Nextp

EndIf

j=j+1

i=i+1

Ifj=100Ori=100Thenj=0:

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

EndSub

PrivateSubTimer2_Timer()'向左走

Picture1.Cls

Picture1。

DrawWidth=10

b=b-100

aa(i)=a

bb(j)=b

Picture1.PSet(b,a),vbRed

Fork=1Toc

Ifaa(99)=0Then

Ifj〈=cAndi〈=cThen

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

Else

Picture1。

PSet(bb(j—k),aa(i-k)),vbBlue

EndIf

EndIf

Ifaa(99)<〉0Orbb(99)<>0Then

Ifj〈=cAndi<=cThen

d=100-k+j:

e=100—k+i

Ifd〉=100Thend=d-100

Ife>=100Thene=e—100

Picture1。

PSet(bb(d),aa(e)),vbBlue

Else

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

EndIf

EndIf

Nextk

Ifb>4900Orb〈100Ora〉4900Ora〈100Then

j=-1:

i=-1:

b=300:

a=100:

c=4:

z=0

MsgBox(”对不起!

您碰墙了!

接下来将重新开始游戏!

”)

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

Timer2.Enabled=False

Timer1。

Enabled=True

EndIf

Ifj〉cThen

Forp=1Toc

Ifbb(j)=bb(j-p)Andaa(i)=aa(i—p)Then

j=-1:

i=-1:

b=300:

a=100:

c=4:

z=0

MsgBox(”对不起!

您咬尾了!

接下来将重新开始游戏!

”)

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

Timer2.Enabled=False

Timer1.Enabled=True

ExitFor

EndIf

Nextp

EndIf

j=j+1

i=i+1

Ifj=100Ori=100Thenj=0:

i=0

EndSub

PrivateSubTimer3_Timer()'向下走

Picture1.Cls

Picture1。

DrawWidth=10

a=a+100

aa(i)=a

bb(j)=b

Picture1。

PSet(b,a),vbRed

Fork=1Toc

Ifaa(99)=0Then

Ifj<=cAndi〈=cThen

Picture1.PSet(f,100-100*(k—j)),vbBlue

Else

Picture1。

PSet(bb(j—k),aa(i—k)),vbBlue

EndIf

EndIf

Ifaa(99)<〉0Orbb(99)〈>0Then

Ifj〈=cAndi<=cThen

d=100—k+j:

e=100-k+i

Ifd〉=100Thend=d-100

Ife〉=100Thene=e—100

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

Else

Picture1。

PSet(bb(j-k),aa(i—k)),vbBlue

EndIf

EndIf

Nextk

Ifb〉4900Orb<100Ora>4900Ora<100Then

j=—1:

i=—1:

b=300:

a=100:

c=4:

z=0

MsgBox("对不起!

您碰墙了!

接下来将重新开始游戏!

”)

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

Timer3。

Enabled=False

Timer1。

Enabled=True

EndIf

Ifj>cThen

Forp=1Toc

Ifbb(j)=bb(j-p)Andaa(i)=aa(i—p)Then

j=—1:

i=—1:

b=300:

a=100:

c=4:

z=0

MsgBox(”对不起!

您咬尾了!

接下来将重新开始游戏!

”)

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

Timer3。

Enabled=False

Timer1.Enabled=True

ExitFor

EndIf

Nextp

EndIf

j=j+1

i=i+1

Ifj=100Ori=100Thenj=0:

i=0

EndSub

PrivateSubTimer4_Timer()’向上走

Picture1.Cls

Picture1。

DrawWidth=10

a=a—100

aa(i)=a

bb(j)=b

Picture1。

PSet(b,a),vbRed

Fork=1Toc

Ifaa(99)=0Then

Ifj〈=cAndi<=cThen

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

Else

Picture1。

PSet(bb(j-k),aa(i-k)),vbBlue

EndIf

EndIf

Ifaa(99)<〉0Orbb(99)〈〉0Then

Ifj<=cAndi<=cThen

d=100—k+j:

e=100—k+i

Ifd>=100Thend=d-100

Ife〉=100Thene=e-100

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

Else

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

EndIf

EndIf

Nextk

Ifb>4900Orb<100Ora〉4900Ora〈100Then

j=—1:

i=-1:

b=300:

a=100:

c=4:

z=0

MsgBox(”对不起!

您碰墙了!

接下来将重新开始游戏!

")

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

Timer4.Enabled=False

Timer1。

Enabled=True

EndIf

Ifj>cThen

Forp=1Toc

Ifbb(j)=bb(j-p)Andaa(i)=aa(i-p)Then

j=—1:

i=—1:

b=300:

a=100:

c=4:

z=0

MsgBox(”对不起!

您咬尾了!

接下来将重新开始游戏!

")

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

Timer4。

Enabled=False

Timer1。

Enabled=True

ExitFor

EndIf

Nextp

EndIf

j=j+1

i=i+1

Ifj=100Ori=100Thenj=0:

i=0

EndSub

PrivateSubCommand1_Click()'上

IfTimer3.Enabled=FalseThen

Timer2.Enabled=False

Timer1.Enabled=False

Timer4。

Enabled=True

EndIf

EndSub

PrivateSubCommand2_Click()'下

IfTimer4.Enabled=FalseAndj〉cThen

Timer1.Enabled=False

Timer2。

Enabled=False

Timer3.Enabled=True

EndIf

EndSub

PrivateSubCommand3_Click()'左

IfTimer1.Enabled=FalseThen

Timer3。

Enabled=False

Timer4.Enabled=False

Timer2.Enabled=True

EndIf

EndSub

PrivateSubCommand4_Click()'右

IfTimer2.Enabled=FalseThen

Timer4.Enabled=False

Timer3。

Enabled=False

Timer1。

Enabled=True

EndIf

EndSub

PrivateSubpicture1_KeyDown(KeyCodeAsInteger,ShiftAsInteger)

IfKeyCode=38AndTimer3。

Enabled=FalseThen’上

Timer2.Enabled=False

’Timer3.Enabled=False

Timer1.Enabled=False

Timer4。

Enabled=True

EndIf

IfKeyCode=40AndTimer4.Enabled=FalseAndj〉cThen'下

Timer2.Enabled=False

Timer1.Enabled=False

’Timer4.Enabled=False

Timer3。

Enabled=True

EndIf

IfKeyCode=37AndTimer1.Enabled=FalseThen’左

'Timer1.Enabled=False

Timer3.Enabled=False

Timer4。

Enabled=False

Timer2.Enabled=True

EndIf

IfKeyCode=39AndTimer2。

Enabled=FalseThen’右

’Timer2。

Enabled=False

Timer3.Enabled=False

Timer4。

Enabled=False

Timer1.Enabled=True

EndIf

EndSub

PrivateSubTimer5_Timer()

Picture1。

DrawWidth=7

Picture1.PSet(g,h),RGB(50,50,0)

Ifb=gAnda=hThen

g=(Int(Rnd*48)+1)*100

h=(Int(Rnd*48)+1)*100

c=c+1

EndIf

Ifc=30Then

z=z+1

j=0:

i=0:

b=300:

a=100:

c=4

MsgBox("恭喜您!

进入下一关!

")

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

Timer4。

Enabled=False

Timer3.Enabled=False

Timer2。

Enabled=False

Timer1。

Enabled=True

EndIf

Ifp=6Then

MsgBox(”您真是快手王啊!

重新开始吧!

”)

j=—1:

i=—1:

b=300:

a=100:

c=4:

z=0

MsgBox("对不起!

您咬尾了!

接下来将重新开始游戏!

”)

Foro=0To99

aa(o)=0:

bb(o)=0

Nexto

EndIf

Timer1。

Interval=(200-z*30)'定义每个定时器间隔时间0.2S

Timer2。

Interval=(200-z*30)

Timer3.Interval=(200-z*30)

Timer4.Interval=(200—z*30)

Label3.Caption=g

Label4。

Caption=h

Label5。

Caption=c+1

Label6。

Caption=z+1

EndSub

主程序结束

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2