Vb课程设计报告.docx

上传人:b****8 文档编号:8999831 上传时间:2023-05-16 格式:DOCX 页数:25 大小:20.21KB
下载 相关 举报
Vb课程设计报告.docx_第1页
第1页 / 共25页
Vb课程设计报告.docx_第2页
第2页 / 共25页
Vb课程设计报告.docx_第3页
第3页 / 共25页
Vb课程设计报告.docx_第4页
第4页 / 共25页
Vb课程设计报告.docx_第5页
第5页 / 共25页
Vb课程设计报告.docx_第6页
第6页 / 共25页
Vb课程设计报告.docx_第7页
第7页 / 共25页
Vb课程设计报告.docx_第8页
第8页 / 共25页
Vb课程设计报告.docx_第9页
第9页 / 共25页
Vb课程设计报告.docx_第10页
第10页 / 共25页
Vb课程设计报告.docx_第11页
第11页 / 共25页
Vb课程设计报告.docx_第12页
第12页 / 共25页
Vb课程设计报告.docx_第13页
第13页 / 共25页
Vb课程设计报告.docx_第14页
第14页 / 共25页
Vb课程设计报告.docx_第15页
第15页 / 共25页
Vb课程设计报告.docx_第16页
第16页 / 共25页
Vb课程设计报告.docx_第17页
第17页 / 共25页
Vb课程设计报告.docx_第18页
第18页 / 共25页
Vb课程设计报告.docx_第19页
第19页 / 共25页
Vb课程设计报告.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Vb课程设计报告.docx

《Vb课程设计报告.docx》由会员分享,可在线阅读,更多相关《Vb课程设计报告.docx(25页珍藏版)》请在冰点文库上搜索。

Vb课程设计报告.docx

Vb课程设计报告

课程设计题目贪吃蛇(四星)

一、功能

“贪吃蛇”是一种很流行的电子游戏。

游戏方法如下:

一条蛇在棋盘上爬行,由键盘控制其转弯;棋盘上随机地出现5个数字(1~5),当蛇的头部碰到数字时(吃数字),蛇的身体会变长。

当蛇的头部撞到障碍时或碰到自己身体时,游戏结束,如果蛇头撞到边界时会从另一边钻出。

游戏结束时,根据蛇身体的长度计算得分。

程序会自己记录最高得分和所用时间,然后提示留下玩家姓名,其默认名为无名侠。

二、课程设计的详细设计

⑴程序启动时界面上棋盘由20*20个方格构成。

其间有障碍物,障碍物的形状由游戏者自己开始时选取,由程序内置设定。

开始时,蛇静止不动,位于窗体的正中央,头向右。

身体由一串共五个圆圈表示,代表头部的圆圈为红色,其中有两个小的绿圆圈代表眼睛,当蛇转向时两只眼睛也会转向。

⑵按回车键开始游戏,蛇自动在棋盘上向前爬行,用户可以使用“←↑↓→”4个方向键控制其拐弯。

蛇只能向左右转90º的弯,不能后退。

蛇吃掉一个数字后,身体伸长的幅度与数字的大小有关,而且不是每次都突然间伸长,而是逐渐的长长。

例如,吃掉“3”后,蛇身体伸长3个圆圈;吃掉“5”后身体伸长5个圆圈。

⑶5个数字(1~5)的位置是随机的,并且互不重叠,且不会出现在蛇身上也不会出现在障碍上,也不会和已经出现的数字重复。

当蛇吃掉一个数字时,同样的数字随机地出现在其他位置上,即棋盘上总保持有“1~5”5个数字。

⑷使用回车键可以暂停或继续游戏。

游戏进行时,窗口标题栏上显示出已使用的时间和得分,得分是当前蛇身体的长度。

⑸蛇在爬行时,如果其头部碰到障碍或自己的身体,则被“撞死”,游戏结束。

程序回到游戏开始状态,用户可以重新开始玩。

如果成绩超过程序的原有记录,程序将会提示游戏者保存记录,并且输入玩家姓名,其默认的是无名侠。

(6)玩家在开始游戏时可以自己设置游戏模式,比如游戏为玩家提供了六种游戏模式,他们的难度都不同,主要表现在他们的障碍物的分布上。

玩家可以通过游戏界面上的设置来自己定义游戏的模式。

本游戏还提供了对游戏的说明和帮助。

三、调试程序的过程

程序在编制过程中由于变量过多,而且使用的语句反复,出现了很多的问题,特别是在程序的调试过程中,经常会出现变量未定义,或者变量益出的错误,还有很多编制的程序段都没有达到设计功能。

比如在用line1()和line2()进行绘制格线时就没能达到初始要求,后经多次改进才达到设计要求。

再就是对一些控件的应用上也有很多错误,比如说对于蛇身的shape控件时,对于蛇身的前进方向就调试的很多次,因为蛇身方向的传递是通过Shape控件来实现的,其后一个延续前一个的方向,在判断蛇是否撞到自己或者障碍时,主要是看蛇头控件的边界属性值是否和蛇身或障碍相同等等。

四、课程设计总结

在课程设计之初我对很多地方都是很迷茫的,几乎可以说是无从下手的,于是我首先重新阅读了教材,使得对VB有一个更深的了解,然后又请教学长,得到了很多的帮助才完成了这个题目。

通过这个程序,使我对VISUALBASIC的部分内容有了一定的了解,能更加熟练地运用一些定义和控件…明白了很多的知识,特别是以前上课时只了解到那些控件和数组还有语句的基本知识,但并没有实际操作多少,很多的地方都不明白到底要怎么做,比如关于判断产生的数字“1,2,3,4,5”是否出现在已有的数字上,或者是障碍上还是蛇身上的这类问题的解决,都是通过这次的课程设计才有所了解的,虽然如次,依旧还有很多的不懂的地方,这些都是靠请教同学和老师才知道的,最另我高兴的是通过本次的课程设计,我懂得了怎样应用VISUALBASIC来解决一些生活中的常见问题,也更加深刻的理解了计算机程序解决问题的方便。

五、附件(源程序)

frmmain

OptionExplicit

OptionBase1

Dimintdirect()AsInteger

PublicmAsInteger

DimintnumAsInteger

DimtimepastAsInteger

DiminteatAsInteger

PublicrestartAsBoolean

PrivateSubForm_Load()

ReDimintdirect(1To5)

DimiAsInteger,jAsInteger,nAsInteger

Dimmaze(1To20,1To20)AsInteger

Dimmazehead(1To4)AsString

DimnumberAsInteger

Callopenmaze

Callopenrecord

m=0

Picture1.Scale(0,0)-(20,20)

Fori=1To20

LoadLine1(i)

Line1(i).Visible=True

LoadLine2(i)

Line2(i).Visible=True

WithLine1(i)

.X1=0

.Y1=i

.X2=20

.Y2=i

EndWith

WithLine2(i)

.X1=i

.Y1=0

.X2=i

.Y2=20

EndWith

Next

Calldrawmaze

Calldrawsnake

Fornumber=1To5

Callshownumber(number)

Next

EndSub

PublicSubdraweye()

Shapeeyel.Height=1/2

Shapeeyel.Width=1/2

Shapeeyer.Height=1/2

Shapeeyer.Width=1/2

SelectCaseintdirect

(1)

Case0

Shapeeyel.Top=Shapes

(1).Top

Shapeeyel.Left=Shapes

(1).Left+1/2

Shapeeyer.Top=Shapes

(1).Top+1/2

Shapeeyer.Left=Shapes

(1).Left+1/2

Case2

Shapeeyer.Top=Shapes

(1).Top

Shapeeyer.Left=Shapes

(1).Left

Shapeeyel.Top=Shapes

(1).Top+1/2

Shapeeyel.Left=Shapes

(1).Left

Case3

Shapeeyer.Top=Shapes

(1).Top+1/2

Shapeeyer.Left=Shapes

(1).Left

Shapeeyel.Top=Shapes

(1).Top+1/2

Shapeeyel.Left=Shapes

(1).Left+1/2

Case1

Shapeeyer.Top=Shapes

(1).Top

Shapeeyer.Left=Shapes

(1).Left+1/2

Shapeeyel.Top=Shapes

(1).Top

Shapeeyel.Left=Shapes

(1).Left

EndSelect

EndSub

PrivateSubshownumber(aAsInteger)

DimiAsInteger,jAsInteger

DimkAsInteger

DimfAsBoolean

Randomize

Do

i=Int(Rnd*20)

j=Int(Rnd*20)

f=True

Fork=1Tointnum

Ifi=CInt(Shapes(k).Left)Andj=CInt(Shapes(k).Top)Then

f=False

ExitFor

EndIf

Next

Iff=TrueThen

Fork=1To5

Ifi=CInt(lblnumber(k).Left)Andj=CInt(lblnumber(k).Top)Then

f=False

ExitFor

EndIf

Next

EndIf

Iff=TrueThen

Fork=1Tom

Ifi=CInt(Shapezi(k).Left)Andj=CInt(Shapezi(k).Top)Then

f=False

ExitFor

EndIf

Next

EndIf

Iff=TrueThen

ExitDo

EndIf

Loop

lblnumber(a).Left=i

lblnumber(a).Top=j

lblnumber(a).Width=1

lblnumber(a).Height=1

EndSub

PrivateSubmnuexit_Click()

UnloadMe

EndSub

PrivateSubmnuhelpful_Click()

frmhelp.Show1

EndSub

PrivateSubmnunew_Click()

DimiAsInteger

Timer1.Enabled=False

Timer2.Enabled=False

Me.Caption="贪吃蛇(按回车键开始)"

Picture1.BackColor=lngbackcolor

Fori=intnumTo2Step-1

UnloadShapes(i)

Next

Calldrawmaze

Calldrawsnake

Fori=1To5

Callshownumber(i)

Next

timepast=0

intnum=5

EndSub

PrivateSubmnuplaypause_Click()

SendKeys"{ENTER}"

EndSub

PrivateSubmnurecord_Click()

frmrecord.Show1

EndSub

PrivateSubmnuset_Click()

DimiAsInteger

restart=False

Timer1.Enabled=False

Timer2.Enabled=False

frmset.Show1,Me

Ifrestart=TrueThen

Me.Caption="贪吃蛇(按回车键开始)"

Picture1.BackColor=lngbackcolor

Fori=intnumTo2Step-1

UnloadShapes(i)

Next

Calldrawsnake

Fori=1To5

Callshownumber(i)

Next

timepast=0

intnum=5

EndIf

Fori=mTo1Step-1

UnloadShapezi(i)

Next

Calldrawmaze

EndSub

PrivateSubPicture1_KeyDown(KeyCodeAsInteger,ShiftAsInteger)

SelectCaseKeyCode

Case37

Ifintdirect

(1)<>0Thenintdirect

(1)=2

Case38

Ifintdirect

(1)<>3Thenintdirect

(1)=1

Case39

Ifintdirect

(1)<>2Thenintdirect

(1)=0

Case40

Ifintdirect

(1)<>1Thenintdirect

(1)=3

Case13

Timer1.Enabled=NotTimer1.Enabled

IfTimer1.Enabled=FalseThen

frmmain.Caption="贪吃蛇(暂停),回车键开始"

Timer2.Enabled=False

Else

frmmain.Caption="贪吃蛇(运行),回车键暂停"

Timer2.Enabled=True

EndIf

EndSelect

Calldraweye

EndSub

PrivateSubTimer1_Timer()

DimsAsInteger

DimiAsInteger

DimnAsInteger

DimdAsInteger

DimgameoverAsBoolean

DimLastLeftAsInteger

DimLastTopAsInteger

DimLastDirectAsInteger

Forn=1To5

IfCInt(Shapes

(1).Left)=CInt(lblnumber(n).Left)AndCInt(Shapes

(1).Top)=CInt(lblnumber(n).Top)Then'如果吃到了数字

inteat=inteat+n

Callshownumber(n)

ExitFor

EndIf

Next

LastLeft=Shapes(intnum).Left

LastTop=Shapes(intnum).Top

LastDirect=intdirect(intnum)

Fori=1Tointnum

SelectCaseintdirect(i)

Case0

Shapes(i).Left=Shapes(i).Left+1

IfShapes(i).Left>20ThenShapes(i).Left=0

Case1

Shapes(i).Top=Shapes(i).Top-1

IfShapes(i).Top<0ThenShapes(i).Top=20

Case2

Shapes(i).Left=Shapes(i).Left-1

IfShapes(i).Left<0ThenShapes(i).Left=20

Case3

Shapes(i).Top=Shapes(i).Top+1

IfShapes(i).Top>20ThenShapes(i).Top=0

EndSelect

Next

Calldraweye

Fori=intnumTo2Step-1

intdirect(i)=intdirect(i-1)

Next

Ifinteat>0Then

inteat=inteat-1

intnum=intnum+1

LoadShapes(intnum)

ReDimPreserveintdirect(intnum)

Shapes(intnum).FillColor=vbYellow

Shapes(intnum).Left=LastLeft

Shapes(intnum).Top=LastTop

Shapes(intnum).Visible=True

intdirect(intnum)=LastDirect

EndIf

Fori=1Tom

IfCInt(Shapes

(1).Left)=CInt(Shapezi(i).Left)AndCInt(Shapes

(1).Top)=CInt(Shapezi(i).Top)Then

gameover=True

ExitFor

EndIf

Next

Ifgameover=FalseThen

Fori=2Tointnum

IfCInt(Shapes

(1).Left)=CInt(Shapes(i).Left)AndCInt(Shapes

(1).Top)=CInt(Shapes(i).Top)Then'如果撞到自已,撞死

gameover=True

ExitFor

EndIf

Next

EndIf

Ifgameover=TrueThen

Timer1.Enabled=False

Timer2.Enabled=False

Ifintnum>mazeInfo(4,curmaze)Then

mazename(2,curmaze)=InputBox("GAMEOVER!

"&Chr(10)&Chr(13)&"你的成绩为"&intnum-5&"分,用时"&timepast&"秒。

"&Chr(10)&Chr(13)&"请留下大名:

","贪吃蛇","无名侠")

mazeInfo(4,curmaze)=intnum

mazeInfo(5,curmaze)=timepast

Else

MsgBox"GAMEOVER!

",64,"贪吃蛇"

EndIf

Me.Caption="贪吃蛇(按回车键开始)"

Fori=intnumTo2Step-1

UnloadShapes(i)

Next

Calldrawmaze

Calldrawsnake

Fori=1To5

Callshownumber(i)

Next

timepast=0

intnum=5

EndIf

EndSub

PrivateSubTimer2_Timer()

timepast=timepast+1

Caption="贪吃蛇(运行):

"&timepast&"秒,"&intnum-5&"分"

EndSub

PrivateSubopenmaze()

DimiAsInteger,jAsInteger

IfDir(App.Path&"\maze.def")=""ThenMsgBox"找不到迷宫定义文件:

maze.def,程序终止。

",vbCritical,"贪吃蛇":

UnloadMe

OpenApp.Path&"\maze.def"ForInputAs1

Erasemaze,mazename

mazenum=0

DoWhileNotEOF

(1)

mazenum=mazenum+1

ReDimPreservemaze(20,20,mazenum),mazename(2,mazenum),mazeInfo(5,mazenum)

Input#1,mazename(1,mazenum)

mazename(2,mazenum)="无名氏"

Fori=1To3

Input#1,mazeInfo(i,mazenum)

Next

mazeInfo(4,mazenum)=0

Fori=1To20

Forj=1To20

Input#1,maze(i,j,mazenum)

Next

Next

Loop

Close1

EndSub

PrivateSubdrawmaze()

DimiAsInteger,jAsInteger

Fori=1To20

Forj=1To20

Ifmaze(i,j,curmaze)=1Then

m=m+1

LoadShapezi(m)

Shapezi(m).Visible=True

WithShapezi(m)

.Left=j-1

.Top=i-1

.Width=1

.Height=1

EndWith

Shapezi(m).FillColor=lngmazecolor

EndIf

Next

Next

EndSub

PrivateSubdrawsnake()

DimiAsInteger

DimpAsInteger

Shapes

(1).Left=mazeInfo(1,curmaze)

Shapes

(1).Top=mazeInfo(2,curmaze)

Shapes

(1).Width=1

Shapes

(1).Height=1

intdirect

(1)=mazeInfo(3,curmaze)

Forp=2To5

intdirect(p)=intdirect

(1)

Next

Calldraweye

intnum=5

Fori=2Tointnum

LoadShapes(i)

Shapes(i).FillColor=vbYellow

SelectCaseintdirect

(1)

Case0

Shapes(i).Left=Shapes(i-1).Left-1

Case2

Shapes(i).Left=Shapes(i-1).Left+1

Case3

Shapes(i).Top=Shapes(i-1).Top-1

Case1

Shapes(i).Top=Shapes(i-1).Top+1

EndSelect

Shapes(i).Visible=True

Next

Calldraweye

EndSub

 

PrivateSubopenrecord()

DimiAsInteger

IfDir(App.Path&"\snake.dat")=""Then

curmaze=1

Else

OpenApp.Path&"\snake.dat"ForInputAs3

Input#3,curmaze,lngbackcolor,lngmazecolor

i=0

DoWhileNotEOF(3)

i=

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

当前位置:首页 > 党团工作 > 入党转正申请

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

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