资源描述
基于VB语言的清新风格赛车小游戏——2012级
《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 Sub
Private 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).Height
Shape1(1).Top = Shape1(1).Top + Label10.Caption + 60
If 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 If
If 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 If
For 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 Then
Timer1.Enabled = False
Timer3.Enabled = False
Frame2.Visible = True
WindowsMediaPlayer1.URL = App.Path & "\MUSIC3.wav" '游戏结束更换音效
If mus = 0 Then '判断背景音乐是开启还是关闭
WindowsMediaPlayer1.Controls.stop
End If
If mus = 1 Then
muss = 0
mus = 0
End If
Label6.Caption = Label7.Caption
If Val(Label6.Caption) > Val(Labelmax.Caption) Then
Labelmax.Caption = Label6.Caption
Label11.Visible = True
max = Labelmax.Caption
Open App.Path & "\point.txt" For Output As #1
Write #1, max
Close #1
nn = InputBox("创造了新纪录!请问您尊姓大名!", " ")
tt = Format(Date)
Open App.Path & "\name.txt" For Output As #1
Write #1, nn
Write #1, tt
Close #1
End If
If Picture1(s).BackColor = RGB(0, 200, 0) Then
Timer1.Enabled = False
Timer3.Enabled = False
Frame2.Visible = True
Label6.Caption = Label7.Caption
If Val(Label6.Caption) > Val(Labelmax.Caption) Then
Labelmax.Caption = Label6.Caption
Label11.Visible = True
max = Labelmax.Caption
Open App.Path & "\point.txt" For Output As #1
Write #1, max
Close #1
nn = InputBox("创造了新纪录!请问您尊姓大名!", " ")
tt = Format(Date)
Open App.Path & "\name.txt" For Output As #1
Write #1, nn
Write #1, tt
Close #1
End If
End If
Picture1(s).BackColor = RGB(200, 0, 0)
End If
Next s
'玩家汽车移动代码:
If lr = "left" Then
Label1.Caption = -55 - Val(Label10.Caption) / 10
End If
If lr = "leftstop" Then
If Label1.Caption < 0 Then Label1.Caption = 0
Else: Label1.Caption = Label1.Caption
End If
If lr = "right" Then
Label1.Caption = 55 + Val(Label10.Caption) / 10
End If
If lr = "rightstop" Then
If Label1.Caption > 0 Then Label1.Caption = 0
Else: Label1.Caption = Label1.Caption
End If
If car.Left < 0 Then '判断车子是否在游戏窗口最左右两端
Label1.Caption = 0
car.Left = 0
Else
car.Left = car.Left + Label1.Caption
End If
If car.Left + car.Width > Form1.Width Then
Label1.Caption = 0
car.Left = Form1.Width - car.Width
Else
car.Left = car.Left + Label1.Caption
End If
'随机产生道路车辆:
Picture1(0).Top = Picture1(0).Top + Label10.Caption
If Picture1(0).Top > Form1.Height Then
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
End If
Picture1(1).Top = Picture1(1).Top + Label10.Caption
If Picture1(1).Top > Form1.Height Then
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
End If
Picture1(2).Top = Picture1(2).Top + Label10.Caption
If Picture1(2).Top > Form1.Height Then
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
End If
Picture3(0).Top = Picture3(0).Top + Label10.Caption
If Picture3(0).Top - 1740 > Form1.Height Then
Picture3(0).Top = -Picture3(0).Height
Randomize
X = Int(6400 * (Rnd + 0))
Picture3(0).Left = X
End If
Picture3(1).Top = Picture3(1).Top + Label10.Caption
If Picture3(1).Top - 1740 > Form1.Height Then
Picture3(1).Top = -Picture3(1).Height
Randomize
X = Int(6400 * (Rnd + 0))
Picture3(1).Left = X
End If
End Sub
②计分与记录系统功能:
此部分功能,计分功能通过定时器不断进行加法运算即可,而记录系统,则通过读、写外部文件的功能实现,当玩家查看纪录时,或者结束游戏时,程序会读取外部TXT文件中的内容,里面记录着纪录及其时间等信息,然后显示。如果游戏结束时,分数大于文件中记录的纪录,那么会出发INPUTBOX窗口,让玩家输入其信息,从新写入纪录数据,代码如下:
'初始化,清零全局变量,读取外部纪录文件数据与背景音乐
Private Sub Form_Load()
a = 0
b = 0
buff = 0
Open App.Path & "\point.txt" For Input As #1
Input #1, max
Close #1
Labelmax.Caption = max
Label10.Caption = 50
WindowsMediaPlayer1.URL = App.Path & "\MUSIC1.wma"
End Sub
基于TIMER1功能:
For 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 Then
Timer1.Enabled = False
Timer3.Enabled = False
Frame2.Visible = True
Label6.Caption = Label7.Caption ‘Label6为记录纪录用,Label7为计分用
If Val(Label6.Caption) > Val(Labelmax.Caption) Then
Labelmax.Caption = Label6.Caption
Label11.Visible = True’新纪录红色NEW字样
max = Labelmax.Caption
Open App.Path & "\point.txt" For Output As #1 ‘写文件
Write #1, max
Close #1
nn = InputBox("创造了新纪录!请问您尊姓大名!", " ")
tt = Format(Date)
Open App.Path & "\name.txt" For Output As #1
Write #1, nn
Write #1, tt
Close #1
End If
If Picture1(s).BackColor = RGB(0, 200, 0) Then
Timer1.Enabled = False
Timer3.Enabled = False
Frame2.Visible = True
WindowsMediaPlayer1.URL = App.Path & "\MUSIC3.wav"
If mus = 0 Then '判断背景音乐是开启还是关闭
WindowsMediaPlayer1.Controls.stop
End If
If mus = 1 Then
muss = 0
mus = 0
End If
Label6.Caption = Label7.Caption
If Val(Label6.Caption) > Val(Labelmax.Caption) Then
Labelmax.Caption = Label6.Caption
Label11.Visible = True
max = Labelmax.Caption
Open App.Path & "\point.txt" For Output As #1
Write #1, max
Close #1
nn = InputBox("创造了新纪录!请问您尊姓大名!", " ")
tt = Format(Date)
Open App.Path & "\name.txt" For Output As #1
Write #1, nn
Write #1, tt
Close #1
End If
End If
Private Sub Maxp_Click()’打开纪录面板
Form2.Show
End Sub
纪录面板FORM2代码:
Dim Name1 '纪录者姓名
Dim Time '纪录时间
Dim max '纪录分数
Private Sub Command1_Click()
Unload Form2
End Sub
Private Sub Form_Load()
Open App.Path & "\name.txt" For Input As #1
Input #1, Name1
Input #1, Time
Close #1
Label2.Caption = Name1
Label4.Caption = Time
Open App.Path & "\point.txt" For Input As #1
Input #1, max
Close #1
Label3.Caption = max
End Sub
③车库系统:
此部分功能为提供两种萌萌的外貌的车体供玩家选择=。=:
'车体外貌选择:
Private Sub Command5_Click()
car.Picture = Picture2(0).Picture
Frame3.Visible = False
End Sub
Private Sub Command6_Click()
car.Picture = Picture2(1).Picture
Frame3.Visible = False
End Sub
④道具系统:
游戏途中会随机出现两种道具,一种为加分道具,一种为加速道具,代码如下:
基于TIMER1吃道具代码:
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’计分系统,吃了道具+50分
Picture3(0).Left = 9000’ 吃到道具后使道具消失
End If
If 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 If
游戏速度越来越快的代码,吃了道具会加快增速:
Private Sub Timer3_Timer()
If buff = 0 Then
Label10.Caption = Label10.Caption + 5
ElseIf buff = 1 Then
Label10.Caption = Label10.Caption + 15
buff = 0
End If
If Label10.Caption > 200 Then
Label10.Caption = 200
End If
End Sub
⑤背景音乐系统:
在主菜单会有背景音乐,开始游戏后与游戏结束时均会有不同的音效,并且在菜单中可以选择开启音乐或者关闭,代码如下:
'初始化,清零全局变量,读取外部纪录文件数据与背景音乐
Private Sub Form_Load()
a = 0
b = 0
buff = 0
mus = 1
muss = 1
Open App.Path & "\point.txt" For Input As #1
Input #1, max
Close #1
Labelmax.Caption = max
Label10.Caption = 50
WindowsMediaPlayer1.URL = App.Path & "\MUSIC1.wma"
End Sub
'音乐循环
Private Sub WindowsMediaPlayer1_PlayStateChange(ByVal NewState As Long)
If NewState = 1 And mus = 1 Then
WindowsMediaPlayer1.Controls.play
End If
End Sub
'打开背景音乐:
Private Sub OPENMUSIC_Click()
WindowsMediaPlayer1.Controls.play
mus = 1
End Sub
'关闭背景音乐:
Private Sub CLOSEMUSIC_Click()
WindowsMediaPlayer1.Controls.stop
mus = 0
End Sub
‘结束时音效:
For s = 0 To 2
If car.Left + car.Width - 50 > Picture1(s).Left And car.Left + 50 < Picture1(s).Left + Picture1(s).Width And car.Top + car.Height > Picture1(s).Top + 50 And car.Top + 50 < Picture1(s).Top + Picture1(s).Height Then
Timer1.Enabled = False
Timer3.Enabled = False
Frame2.Visible = True
WindowsMediaPlayer1.URL = App.Path & "\MUSIC3.wav"
If mus = 0 Then
WindowsMediaPlayer1.Controls.stop
End If
If mus = 1 Then
muss = 0
mus = 0
End If
‘再来一次时重启背景音乐
Private Sub Command4_Click()
…………
If mus = 0 And muss = 0 Then
mus = 1
muss = 1
End If
WindowsMediaPlayer1.URL = App.Path & "\MUSIC2.mp3"
If mus = 0 Then
WindowsMediaPlayer1.Controls.stop
End If
四、使用说明书
在主界面上单击“开始游戏”即可进行游戏,使用键盘上左右方向键操作赛车躲避途中的车辆,撞到其他车辆游戏结束。随着游戏进行车速会越来越快,得分也会越来越高,系统将会记录最高分,最高分可以通过窗口左上角菜单中的“纪录”选项查询,同时背景音乐的开与关可以通过菜单中的音乐选项选择。。车库中有两种车体外貌供玩家选择。游戏途中会随机出现两种道具,吃到他们会产生不同的效果。左上方菜单中还可以重新启动游戏或者退出游戏。
五、总结及进一步完善建议
虽然只经过短短的三天时间学习VB语言,但是由于其精简的风格,我已经掌握了其一些基本的语法及其编程思想,可以编写出一些比较基础的小程序。由于学过C、汇编等语言,不难发现,其实不同的编程语言在其编程思想、逻辑与语法等方面都有相似之处,结合之前对C和汇编语言的一些学习与理解,在学习与运用VB语言上,还是较为上手的。
当然,由于是第一次较为严格、深入地去接触与学习VB语言,难免在一开始会感到不适与遇到一些困难,但是这也是编程的乐趣之一。在遇到困难与问题时,通过不断地对程序进行调试与修改,并通过网络查询信息与资料,最终解决问题,这不仅对自己是一种锻炼,也是一种能力的提高。
在排查与解决程序问题中,我自身感觉,单步运行与调试是很好的一种办法,通过单步运行与调试,观察程序运行的逻辑、变量的值或者其他效果,对比自身的期望与程序出现的错误情况,便能很快地找出问题所在,对症下药,去修改与完善程序。同时,在编写程序的时候,命名与备注十分重要,无论是变量或者控件的命名,都会有助于后期的测试与修改。
在制作本次的课程设计中,其实有过不同的版本程序,如图:
游戏整体风格与外貌没有最后版本的好看和萌=。= ,功能也没有如今丰富,一开始并没有加入纪录系统与道具系统,总体感觉比较山寨。
其实学习也就是这样一个过程,在过程中不断克服困难,而后又不断产生好玩甚至疯狂的想法,然后再努力去实现,自己都会感觉很开心(/ 3 \)。
完善的建议,我觉得可以加入更多的道具与车体,甚至可以加入不懂难度的赛道供玩家选择,使游戏更加有趣,同时,由于TXT文件外部读入并不是非常合适,可以使用数据库替换,使玩家不能轻易更改纪录。
六.参考资料
1.《Visual Basic程序设计教程(第3版)》
2.百度知道
附:
源程序完全代码:
Dim lr '左右控制变量
Dim ud '上下控制变量
Dim a '游戏开始倒计时变量
Dim b '调试变量
Dim max '最高分记录系统变量
Dim buff '道具系统变量
Dim mus '音效变量
Dim muss '音效变量
'主界面四个按钮代码:
Private Sub anniu1_Click()
a = 0
b = 0
Frame1.Visible = False
Label10.Caption = 50
Timer2.Enabled = True
WindowsMediaPlayer1.URL = App.Path & "\MUSIC2.mp3"
If mus = 0 Then
WindowsMediaPlayer1.Controls.stop
End If
End Sub
Private Sub anniu1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
anniu1.Picture = an1
End Sub
Private Sub anniu2_Click()
Frame3.Visible = True
End Sub
Private Sub anniu2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
anniu2.Picture = an3
End Sub
Private Sub anniu3_Click()
Unload Form1
End Sub
Private Sub anniu3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
anniu3.Picture = an5
End Sub
Private Sub anniu4_Click()
Frame4.Visible = True
End Sub
Private Sub anniu4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
anniu4.Picture = an7
End Sub
'关闭背景音乐:
Private Sub CLOSEMUSIC_Click()
WindowsMediaPlayer1.Controls.stop
mus = 0
End Sub
Private Sub Command8_Click()
Frame4.Visible = False
End Sub
'初始化,清零全局变量,读取外部纪录文件数据与背景音乐
Private Sub Form_Load()
a = 0
b = 0
buff = 0
mus = 1
muss = 1
Open App.Path & "\point.txt" For Input As #1
Input #1, max
Close #1
Labelmax.Caption = max
Label10.Caption = 50
WindowsMediaPlayer1.URL = App.Path & "\MUSIC1.wma"
End Sub
'再来一次按钮代码
Private Sub Command4_Click()
Frame2.Visible = False
Label11.Visible = False
Label7.Caption = 0
Picture1(0).Top = 240
Picture1(0).Left = 1920
Picture1(1).Top = 2520
Picture1(1).Left = 9000
Picture1(2).Top = 5160
Picture1(2).Left = 9000
Picture3(0).Top = 4560
Picture3(0).Left = 9000
Picture3(1).Top = 7680
Picture3(1).Left = 9000
Label2.Caption = 3
Label1.Caption = 0
Label10.Caption = 50
b = 0
a = 0
Label2.Visible = True
Timer2.Enabled = True
If mus = 0 And muss = 0 Then
mus = 1
muss = 1
End If
WindowsMediaPlayer1.URL = App.Path & "\MUSIC2.mp3"
If mus = 0 Then
WindowsMediaPlayer1.Controls.stop
End If
End Sub
'车体外貌选择:
Private Sub Command5_Click()
car.Picture = Picture2(0).Picture
Frame3.Visible = False
End Sub
Private Sub Command6_Click()
car.Picture = Picture2(1).Picture
Frame3.Visible = False
End Sub
Private Sub Exit_Click()
Unload Form1
End Sub
'键盘控制:
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 Th
展开阅读全文