基于VB语言的赛车小游戏课程设计论文.docx
《基于VB语言的赛车小游戏课程设计论文.docx》由会员分享,可在线阅读,更多相关《基于VB语言的赛车小游戏课程设计论文.docx(38页珍藏版)》请在冰豆网上搜索。
基于VB语言的赛车小游戏课程设计论文
《VB课程设计》
题目:
基于VB语言的赛车小游戏
姓名:
学号:
201200800031
学院:
机电与信息工程学院
专业:
测控
年级:
一.
系统简介
1.背景与意义
由于现在跑酷游戏非常流行,其快餐的游戏方式符合了当今人们的娱乐思维。
简洁傻瓜的操作方式与记录系统的引入更是刺激了人们对其的热情。
基于此,我便想用VB在PC端上做一个类似的跑酷小游戏,并且加入一些较为有趣的元素,使玩家能在这款小游戏中找到些许乐趣。
基于此想法,我选择了以公路赛车为背景素材,制作一款简介的跑酷小游戏。
游戏中,玩家操作赛车躲避途中随即出现的车辆,行驶的距离越长得分越高,如若撞到途中车辆游戏结束。
在设计程序中,为了实现车辆行驶的效果,使用了计时器,通过计时器短周期移动背景,产生车辆在向前行驶的效果。
在程序中,额外加入了不同的车辆外形供玩家选择,增加了趣味性,并且写入了计分系统与道具系统,使游戏更有目标性与可玩性。
2.需求分析
既然是一个游戏,首先必须能与人互动,因此程序必须引入接受键盘和鼠标信息输入的功能。
其次,游戏中总是要有动画效果的,这个可以通过VB中的定时器实现。
对于其他的一些额外功能,通过VB的一些基础功能、写与读外部文件等方式也均能实现,如记录系统,便通过读、写外部TXT文件内容实现。
二.功能介绍
程序的功能有:
①赛车游戏系统:
玩家可以通过键盘上的左右方向键操作赛车躲避路途中的障碍,在途中赛车的速度会随着时间越来越快;
②计分与记录系统:
随着游戏时间的增长,玩家在游戏中得到得分数会越来越高。
同时游戏中有一个历史分数系统,会记录下玩家所得的最高分与玩家的姓名、创造纪录的时间,并且玩家可以在菜单中的“记录”选项中查看;
③外观系统:
玩家可以选择不同的赛车外观进行游戏;
④道具系统:
游戏途中会随机出现功能不同的道具,不同的道具有不同的功能。
三.程序设计
1.界面设计
主界面:
在游戏制作中,主要想采用比较清新简单的风格,因此主界面整体设计采用黑白简洁的色调,由于对VB自带按钮外观不是很满意,自行通过picturebox控件制作了一主界面按钮。
车库界面:
游戏中内置两种不同风格外貌的车体供玩家选择,单击按键即可完成更换。
游戏说明界面:
记录界面:
显示创造纪录的玩家姓名,最高分与创纪录的时间。
游戏界面:
创新纪录时的界面:
游戏界面也是才有黑白风格,所有车子的外貌风格比较童真(=。
=我自己画的)。
2.功能设计
代码中变量:
Dimlr'左右控制变量
Dimud'上下控制变量
Dima'游戏开始倒计时变量
Dimb'调试变量
Dimmax'最高分记录系统变量
Dimbuff'道具系统变量
Dimmus'音效变量
Dimmuss'音效变量
Label10.Caption:
游戏速度
Label7.Caption:
玩家得分
Label1.Caption:
玩家操作赛车左右移动速度
①基本车辆移动与操作功能:
主要通过TIMER1实现,Label10中的数字为车辆移动速度,通过定时器不断移动程序中的相关图片,产生汽车跑动的效果。
路途上的车辆通过Randomize函数产生随机数,使其出现在随机位置,当然一定是从窗口上方出现,移动到窗口最下方然后消失。
玩家赛车的控制,通过KEYDOWN与KEYUP实现,定义控制全局变量lr,来控制玩家赛车左右移动。
代码如下:
'键盘控制:
PrivateSubForm_KeyDown(KeyCodeAsInteger,ShiftAsInteger)
IfKeyCode=vbKeyLeftThenlr="left"
IfKeyCode=vbKeyRightThenlr="right"
IfKeyCode=vbKeyUpThenud="down"
IfKeyCode=vbKeyDownThenud="up"
EndSub
PrivateSubForm_KeyUp(KeyCodeAsInteger,ShiftAsInteger)
IfKeyCode=vbKeyLeftThenlr="leftstop"
IfKeyCode=vbKeyRightThenlr="rightstop"
IfKeyCode=vbKeyUpThenud=""
IfKeyCode=vbKeyDownThenud=""
EndSub
‘定时器控制:
PrivateSubTimer1_Timer()
Label7.Caption=Label7.Caption+1'计分
Shape1(0).Top=Shape1(0).Top+Label10.Caption+60'道路中心线移动动画
IfShape1(0).Top>Form1.HeightThenShape1(0).Top=0-Shape1(0).Height
Shape1
(1).Top=Shape1
(1).Top+Label10.Caption+60
IfShape1
(1).Top>Form1.HeightThenShape1
(1).Top=0-Shape1
(1).Height
'撞车记录成绩与吃道具代码:
Ifcar.Left+car.Width>Picture3(0).LeftAndcar.LeftPicture3(0).TopAndcar.TopLabel7.Caption=Label7.Caption+50’加分道具
Picture3(0).Left=9000’吃到道具后使道具消失
EndIf
Ifcar.Left+car.Width>Picture3
(1).LeftAndcar.Left(1).Left+Picture3
(1).WidthAndcar.Top+car.Height>Picture3
(1).TopAndcar.Top(1).Top+Picture3
(1).HeightThen
buff=1’加速道具
Picture3
(1).Left=9000’吃到道具后使道具消失
EndIf
Fors=0To2'判断撞车代码
Ifcar.Left+car.Width-100>Picture1(s).LeftAndcar.Left+100Picture1(s).Top+200Andcar.Top+200Timer1.Enabled=False
Timer3.Enabled=False
Frame2.Visible=True
WindowsMediaPlayer1.URL=App.Path&"\MUSIC3.wav"'游戏结束更换音效
Ifmus=0Then'判断背景音乐是开启还是关闭
WindowsMediaPlayer1.Controls.stop
EndIf
Ifmus=1Then
muss=0
mus=0
EndIf
Label6.Caption=Label7.Caption
IfVal(Label6.Caption)>Val(Labelmax.Caption)Then
Labelmax.Caption=Label6.Caption
Label11.Visible=True
max=Labelmax.Caption
OpenApp.Path&"\point.txt"ForOutputAs#1
Write#1,max
Close#1
nn=InputBox("创造了新纪录!
请问您尊姓大名!
","")
tt=Format(Date)
OpenApp.Path&"\name.txt"ForOutputAs#1
Write#1,nn
Write#1,tt
Close#1
EndIf
IfPicture1(s).BackColor=RGB(0,200,0)Then
Timer1.Enabled=False
Timer3.Enabled=False
Frame2.Visible=True
Label6.Caption=Label7.Caption
IfVal(Label6.Caption)>Val(Labelmax.Caption)Then
Labelmax.Caption=Label6.Caption
Label11.Visible=True
max=Labelmax.Caption
OpenApp.Path&"\point.txt"ForOutputAs#1
Write#1,max
Close#1
nn=InputBox("创造了新纪录!
请问您尊姓大名!
","")
tt=Format(Date)
OpenApp.Path&"\name.txt"ForOutputAs#1
Write#1,nn
Write#1,tt
Close#1
EndIf
EndIf
Picture1(s).BackColor=RGB(200,0,0)
EndIf
Nexts
'玩家汽车移动代码:
Iflr="left"Then
Label1.Caption=-55-Val(Label10.Caption)/10
EndIf
Iflr="leftstop"Then
IfLabel1.Caption<0ThenLabel1.Caption=0
Else:
Label1.Caption=Label1.Caption
EndIf
Iflr="right"Then
Label1.Caption=55+Val(Label10.Caption)/10
EndIf
Iflr="rightstop"Then
IfLabel1.Caption>0ThenLabel1.Caption=0
Else:
Label1.Caption=Label1.Caption
EndIf
Ifcar.Left<0Then'判断车子是否在游戏窗口最左右两端
Label1.Caption=0
car.Left=0
Else
car.Left=car.Left+Label1.Caption
EndIf
Ifcar.Left+car.Width>Form1.WidthThen
Label1.Caption=0
car.Left=Form1.Width-car.Width
Else
car.Left=car.Left+Label1.Caption
EndIf
'随机产生道路车辆:
Picture1(0).Top=Picture1(0).Top+Label10.Caption
IfPicture1(0).Top>Form1.HeightThen
Picture1(0).BackColor=RGB(0,200,0)
Picture1(0).Top=-Picture1(0).Height
Randomize
xx=Int(4*(Rnd+0))
Picture1(0).Picture=Image3(xx).Picture
X=Int(6400*(Rnd+0))
Picture1(0).Left=X
EndIf
Picture1
(1).Top=Picture1
(1).Top+Label10.Caption
IfPicture1
(1).Top>Form1.HeightThen
Picture1
(1).BackColor=RGB(0,200,0)
Picture1
(1).Top=-Picture1
(1).Height
Randomize
xx=Int(4*(Rnd+0))
Picture1
(1).Picture=Image3(xx).Picture
X=Int(6400*(Rnd+0))
Picture1
(1).Left=X
EndIf
Picture1
(2).Top=Picture1
(2).Top+Label10.Caption
IfPicture1
(2).Top>Form1.HeightThen
Picture1
(2).BackColor=RGB(0,200,0)
Picture1
(2).Top=-Picture1
(2).Height
Randomize
xx=Int(4*(Rnd+0))
Picture1
(2).Picture=Image3(xx).Picture
X=Int(6400*(Rnd+0))
Picture1
(2).Left=X
EndIf
Picture3(0).Top=Picture3(0).Top+Label10.Caption
IfPicture3(0).Top-1740>Form1.HeightThen
Picture3(0).Top=-Picture3(0).Height
Randomize
X=Int(6400*(Rnd+0))
Picture3(0).Left=X
EndIf
Picture3
(1).Top=Picture3
(1).Top+Label10.Caption
IfPicture3
(1).Top-1740>Form1.HeightThen
Picture3
(1).Top=-Picture3
(1).Height
Randomize
X=Int(6400*(Rnd+0))
Picture3
(1).Left=X
EndIf
EndSub
②计分与记录系统功能:
此部分功能,计分功能通过定时器不断进行加法运算即可,而记录系统,则通过读、写外部文件的功能实现,当玩家查看纪录时,或者结束游戏时,程序会读取外部TXT文件中的内容,里面记录着纪录及其时间等信息,然后显示。
如果游戏结束时,分数大于文件中记录的纪录,那么会出发INPUTBOX窗口,让玩家输入其信息,从新写入纪录数据,代码如下:
'初始化,清零全局变量,读取外部纪录文件数据与背景音乐
PrivateSubForm_Load()
a=0
b=0
buff=0
OpenApp.Path&"\point.txt"ForInputAs#1
Input#1,max
Close#1
Labelmax.Caption=max
Label10.Caption=50
WindowsMediaPlayer1.URL=App.Path&"\MUSIC1.wma"
EndSub
基于TIMER1功能:
Fors=0To2’判断是否撞车代码
Ifcar.Left+car.Width-100>Picture1(s).LeftAndcar.Left+100Picture1(s).Top+200Andcar.Top+200Timer1.Enabled=False
Timer3.Enabled=False
Frame2.Visible=True
Label6.Caption=Label7.Caption‘Label6为记录纪录用,Label7为计分用
IfVal(Label6.Caption)>Val(Labelmax.Caption)Then
Labelmax.Caption=Label6.Caption
Label11.Visible=True’新纪录红色NEW字样
max=Labelmax.Caption
OpenApp.Path&"\point.txt"ForOutputAs#1‘写文件
Write#1,max
Close#1
nn=InputBox("创造了新纪录!
请问您尊姓大名!
","")
tt=Format(Date)
OpenApp.Path&"\name.txt"ForOutputAs#1
Write#1,nn
Write#1,tt
Close#1
EndIf
IfPicture1(s).BackColor=RGB(0,200,0)Then
Timer1.Enabled=False
Timer3.Enabled=False
Frame2.Visible=True
WindowsMediaPlayer1.URL=App.Path&"\MUSIC3.wav"
Ifmus=0Then'判断背景音乐是开启还是关闭
WindowsMediaPlayer1.Controls.stop
EndIf
Ifmus=1Then
muss=0
mus=0
EndIf
Label6.Caption=Label7.Caption
IfVal(Label6.Caption)>Val(Labelmax.Caption)Then
Labelmax.Caption=Label6.Caption
Label11.Visible=True
max=Labelmax.Caption
OpenApp.Path&"\point.txt"ForOutputAs#1
Write#1,max
Close#1
nn=InputBox("创造了新纪录!
请问您尊姓大名!
","")
tt=Format(Date)
OpenApp.Path&"\name.txt"ForOutputAs#1
Write#1,nn
Write#1,tt
Close#1
EndIf
EndIf
PrivateSubMaxp_Click()’打开纪录面板
Form2.Show
EndSub
纪录面板FORM2代码:
DimName1'纪录者姓名
DimTime'纪录时间
Dimmax'纪录分数
PrivateSubCommand1_Click()
UnloadForm2
EndSub
PrivateSubForm_Load()
OpenApp.Path&"\name.txt"ForInputAs#1
Input#1,Name1
Input#1,Time
Close#1
Label2.Caption=Name1
Label4.Caption=Time
OpenApp.Path&"\point.txt"ForInputAs#1
Input#1,max
Close#1
Label3.Caption=max
EndSub
③车库系统:
此部分功能为提供两种萌萌的外貌的车体供玩家选择=。
=:
'车体外貌选择:
PrivateSubCommand5_Click()
car.Picture=Picture2(0).Picture
Frame3.Visible=False
EndSub
PrivateSubCommand6_Click()
car.Picture=Picture2
(1).Picture
Frame3.Visible=False
EndSub
④道具系统:
游戏途中会随机出现两种道具,一种为加分道具,一种为加速道具,代码如下:
基于TIMER1吃道具代码:
Ifcar.Left+car.Width>Picture3(0).LeftAndcar.LeftPicture3(0).TopAndcar.TopLabel7.Caption=Label7.Caption+50’计分系统,吃了道具+50分
Picture3(0).Left=9000’吃到道具后使道具消失
EndIf
Ifcar.Left+car.Width>Picture3
(1).LeftAndcar.Left(1).Left+Picture3
(1).WidthAndcar.Top+car.Height>Picture3
(1).TopAndcar.Top(1).Top+Picture3
(1).HeightThen
buff=1
Picture3
(1).Left=9000’吃到道具后使道具消失
EndIf
游戏速度越来越快的代码,吃了道具会加快增速:
PrivateSubTimer3_Timer()
Ifbuff=0Then
Label10.Caption=Label10.Caption+5
ElseIfbuff=1Then
Label10.Caption=Label10.Caption+15
buff=0
EndIf
IfLabel10.Caption>200Then
Label10.Caption=200
EndIf
EndSub
⑤背景音乐系统:
在主菜单会有背景音乐,开始游戏后与游戏结束时均会有不同的音效,并且在菜单中可以选择开启音乐或者关闭,代码如下:
'初始化,清零全局变量,读取外部纪录文件数据与背景音乐
PrivateSubForm_Load()
a=0
b=0
buff=0
mus=1