VB课程设计

  • 格式:doc
  • 大小:590.50 KB
  • 文档页数:29

《VB课程设计》题目:基于VB语言的赛车小游戏姓名:学号:201200800031学院:机电与信息工程学院专业:测控年级:日期:一.系统简介1.背景与意义由于现在跑酷游戏非常流行,其快餐的游戏方式符合了当今人们的娱乐思维。

简洁傻瓜的操作方式与记录系统的引入更是刺激了人们对其的热情。

基于此,我便想用VB在PC端上做一个类似的跑酷小游戏,并且加入一些较为有趣的元素,使玩家能在这款小游戏中找到些许乐趣。

基于此想法,我选择了以公路赛车为背景素材,制作一款简介的跑酷小游戏。

游戏中,玩家操作赛车躲避途中随即出现的车辆,行驶的距离越长得分越高,如若撞到途中车辆游戏结束。

在设计程序中,为了实现车辆行驶的效果,使用了计时器,通过计时器短周期移动背景,产生车辆在向前行驶的效果。

在程序中,额外加入了不同的车辆外形供玩家选择,增加了趣味性,并且写入了计分系统与道具系统,使游戏更有目标性与可玩性。

2.需求分析既然是一个游戏,首先必须能与人互动,因此程序必须引入接受键盘和鼠标信息输入的功能。

其次,游戏中总是要有动画效果的,这个可以通过VB中的定时器实现。

对于其他的一些额外功能,通过VB的一些基础功能、写与读外部文件等方式也均能实现,如记录系统,便通过读、写外部TXT文件内容实现。

二.功能介绍程序的功能有:①赛车游戏系统:玩家可以通过键盘上的左右方向键操作赛车躲避路途中的障碍,在途中赛车的速度会随着时间越来越快;②计分与记录系统:随着游戏时间的增长,玩家在游戏中得到得分数会越来越高。

同时游戏中有一个历史分数系统,会记录下玩家所得的最高分与玩家的姓名、创造纪录的时间,并且玩家可以在菜单中的“记录”选项中查看;③外观系统:玩家可以选择不同的赛车外观进行游戏;④道具系统:游戏途中会随机出现功能不同的道具,不同的道具有不同的功能。

三.程序设计1.界面设计主界面:在游戏制作中,主要想采用比较清新简单的风格,因此主界面整体设计采用黑白简洁的色调,由于对VB自带按钮外观不是很满意,自行通过picturebox 控件制作了一主界面按钮。

车库界面:游戏中内置两种不同风格外貌的车体供玩家选择,单击按键即可完成更换。

游戏说明界面:记录界面:显示创造纪录的玩家姓名,最高分与创纪录的时间。

游戏界面:创新纪录时的界面:游戏界面也是才有黑白风格,所有车子的外貌风格比较童真(=。

= 我自己画的)。

2.功能设计代码中变量:Dim lr '左右控制变量Dim ud '上下控制变量Dim a '游戏开始倒计时变量Dim b '调试变量Dim max '最高分记录系统变量Dim buff '道具系统变量Dim mus '音效变量Dim muss '音效变量Label10.Caption:游戏速度Label7.Caption:玩家得分Label1.Caption:玩家操作赛车左右移动速度①基本车辆移动与操作功能:主要通过TIMER1实现,Label10中的数字为车辆移动速度,通过定时器不断移动程序中的相关图片,产生汽车跑动的效果。

路途上的车辆通过Randomize 函数产生随机数,使其出现在随机位置,当然一定是从窗口上方出现,移动到窗口最下方然后消失。

玩家赛车的控制,通过KEYDOWN与KEYUP实现,定义控制全局变量lr,来控制玩家赛车左右移动。

代码如下:'键盘控制:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyLeft Then lr = "left"If KeyCode = vbKeyRight Then lr = "right"If KeyCode = vbKeyUp Then ud = "down"If KeyCode = vbKeyDown Then ud = "up"End SubPrivate Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)If KeyCode = vbKeyLeft Then lr = "leftstop"If KeyCode = vbKeyRight Then lr = "rightstop"If KeyCode = vbKeyUp Then ud = ""If KeyCode = vbKeyDown Then ud = ""End Sub‘定时器控制:Private Sub Timer1_Timer()Label7.Caption = Label7.Caption + 1 '计分Shape1(0).Top = Shape1(0).Top + Label10.Caption + 60 '道路中心线移动动画If Shape1(0).Top > Form1.Height Then Shape1(0).Top = 0 -Shape1(0).HeightShape1(1).Top = Shape1(1).Top + Label10.Caption + 60If Shape1(1).Top > Form1.Height Then Shape1(1).Top = 0 -Shape1(1).Height'撞车记录成绩与吃道具代码:If car.Left + car.Width > Picture3(0).Left And car.Left <Picture3(0).Left + Picture3(0).Width And car.Top + car.Height > Picture3(0).Top And car.Top < Picture3(0).Top + Picture3(0).Height Then Label7.Caption = Label7.Caption + 50’加分道具Picture3(0).Left = 9000’吃到道具后使道具消失End IfIf car.Left + car.Width > Picture3(1).Left And car.Left <Picture3(1).Left + Picture3(1).Width And car.Top + car.Height > Picture3(1).Top And car.Top < Picture3(1).Top + Picture3(1).Height Then buff = 1’加速道具Picture3(1).Left = 9000’吃到道具后使道具消失End IfFor s = 0 To 2 '判断撞车代码If car.Left + car.Width - 100 > Picture1(s).Left And car.Left + 100 < Picture1(s).Left + Picture1(s).Width And car.Top + car.Height > Picture1(s).Top + 200 And car.Top + 200 < Picture1(s).Top +Picture1(s).Height ThenTimer1.Enabled = FalseTimer3.Enabled = FalseFrame2.Visible = TrueWindowsMediaPlayer1.URL = App.Path & "\MUSIC3.wav"'游戏结束更换音效If mus = 0 Then'判断背景音乐是开启还是关闭WindowsMediaPlayer1.Controls.stopEnd IfIf mus = 1 Thenmuss = 0mus = 0End IfLabel6.Caption = Label7.CaptionIf Val(Label6.Caption) > Val(Labelmax.Caption) ThenLabelmax.Caption = Label6.CaptionLabel11.Visible = Truemax = Labelmax.CaptionOpen App.Path & "\point.txt" For Output As #1Write #1, maxClose #1nn = InputBox("创造了新纪录!请问您尊姓大名!", " ")tt = Format(Date)Open App.Path & "\name.txt" For Output As #1Write #1, nnWrite #1, ttClose #1End IfIf Picture1(s).BackColor = RGB(0, 200, 0) Then Timer1.Enabled = FalseTimer3.Enabled = FalseFrame2.Visible = TrueLabel6.Caption = Label7.CaptionIf Val(Label6.Caption) > Val(Labelmax.Caption) Then Labelmax.Caption = Label6.CaptionLabel11.Visible = Truemax = Labelmax.CaptionOpen App.Path & "\point.txt" For Output As #1Write #1, maxClose #1nn = InputBox("创造了新纪录!请问您尊姓大名!", " ") tt = Format(Date)Open App.Path & "\name.txt" For Output As #1Write #1, nnWrite #1, ttClose #1End IfEnd IfPicture1(s).BackColor = RGB(200, 0, 0)End IfNext s'玩家汽车移动代码:If lr = "left" ThenLabel1.Caption = -55 - Val(Label10.Caption) / 10 End IfIf lr = "leftstop" ThenIf Label1.Caption < 0 Then Label1.Caption = 0 Else: Label1.Caption = Label1.CaptionEnd IfIf lr = "right" ThenLabel1.Caption = 55 + Val(Label10.Caption) / 10 End IfIf lr = "rightstop" ThenIf Label1.Caption > 0 Then Label1.Caption = 0Else: Label1.Caption = Label1.CaptionEnd IfIf car.Left < 0 Then'判断车子是否在游戏窗口最左右两端Label1.Caption = 0car.Left = 0Elsecar.Left = car.Left + Label1.CaptionEnd IfIf car.Left + car.Width > Form1.Width ThenLabel1.Caption = 0car.Left = Form1.Width - car.WidthElsecar.Left = car.Left + Label1.CaptionEnd If'随机产生道路车辆:Picture1(0).Top = Picture1(0).Top + Label10.Caption If Picture1(0).Top > Form1.Height ThenPicture1(0).BackColor = RGB(0, 200, 0)Picture1(0).Top = -Picture1(0).HeightRandomizexx = Int(4 * (Rnd + 0))Picture1(0).Picture = Image3(xx).PictureX = Int(6400 * (Rnd + 0))Picture1(0).Left = XEnd IfPicture1(1).Top = Picture1(1).Top + Label10.Caption If Picture1(1).Top > Form1.Height ThenPicture1(1).BackColor = RGB(0, 200, 0)Picture1(1).Top = -Picture1(1).HeightRandomizexx = Int(4 * (Rnd + 0))Picture1(1).Picture = Image3(xx).PictureX = Int(6400 * (Rnd + 0))Picture1(1).Left = XEnd IfPicture1(2).Top = Picture1(2).Top + Label10.CaptionIf Picture1(2).Top > Form1.Height ThenPicture1(2).BackColor = RGB(0, 200, 0)Picture1(2).Top = -Picture1(2).HeightRandomizexx = Int(4 * (Rnd + 0))Picture1(2).Picture = Image3(xx).PictureX = Int(6400 * (Rnd + 0))Picture1(2).Left = XEnd IfPicture3(0).Top = Picture3(0).Top + Label10.CaptionIf Picture3(0).Top - 1740 > Form1.Height ThenPicture3(0).Top = -Picture3(0).HeightRandomizeX = Int(6400 * (Rnd + 0))Picture3(0).Left = XEnd IfPicture3(1).Top = Picture3(1).Top + Label10.CaptionIf Picture3(1).Top - 1740 > Form1.Height ThenPicture3(1).Top = -Picture3(1).HeightRandomizeX = Int(6400 * (Rnd + 0))Picture3(1).Left = XEnd IfEnd Sub②计分与记录系统功能:此部分功能,计分功能通过定时器不断进行加法运算即可,而记录系统,则通过读、写外部文件的功能实现,当玩家查看纪录时,或者结束游戏时,程序会读取外部TXT文件中的内容,里面记录着纪录及其时间等信息,然后显示。