课程设计报告
题目介绍:“俄罗斯方块”是一款非常经典,而且很受欢迎的风靡全球的电视游戏机和掌上游戏机游戏,玩家既能感受到游戏中的乐趣,也能提供一个展现自己高超技艺的场所。此游戏有很多变种,但大体玩法相同。本题目要求编制俄罗斯方块的计算机版本。
题目要求:当按“开始”键时,游戏开始,期间玩家可以选择“暂停”和“继续”。方块可以在一定的范围内上下左右移动,而且可以根据玩家的指令转动方向,以更好的进行方块之间的组合。当每一行被方块填满时则会自动消去一行,多行都被方块填满时则都消去,同时消去的行数越多,则分数越高。一旦方块达到顶端则游戏自动结束。而且玩家可以根据自身的水平,选择游戏的难度。难度越高,速度越快;难度越低,速度越慢。
解题方案:本题涉及到VB中的很多块知识
使用的模块:数组,变量的命名,菜单编辑器,绘图,键盘事件处理,定时器控制,多模块程序设计,文本文件的读写等。
主要用到的控件有:command控件,image控件,picture控件,label控件,timer控件,text控件,windows media player控件等等
主要实现的功能:
1.可以灵活控制方块在图形框中运动。 2.游戏过程中方块可以自由旋转。
3.当某一行的方块排列满时,将自动将这一行方块消除,然后将上面所有方块向下移动,可以支持连续消行。
4.游戏前可以选择游戏的速度和游戏的等级,游戏速度既为方块下落速度,游戏等级为初始游戏时在基层随机生成一定行数的无规律方块,生成的行数由你来选择,每行至少产生5个以上的无规律方块,这样增加了游戏难度,对于游戏高手来说,无疑不是一个新的挑战。
5.游戏的得分支持积分,并且按照公式:
得分 = 原来分数+ 100 * (2 ^ 同时消除的行数-1)
这样,你同一时间消除的行数越多,你的得分也就越高,当游戏积分到了一定时可以自动升级,这个升级指速度升级
主要步骤为以下9个方面: (1)游戏界面的设计。 (2)俄罗斯方块的造型。 (3)俄罗斯方块的旋转。
(4)俄罗斯方块的运动情况(包括向左,向右和向下)。 (5)俄罗斯方块的自动消行功能。 (6)游戏级别的自由选择。 (7)游戏速度的自由选择。 (8)游戏得分的计算。
(9)游戏菜单选项的设计及功能实现。
分析及大概设计:①方块的造型在旋转的时候存在一个公式,当然首先要判断是否满足旋转的要求,以上是一个长条型由横着变成竖立状态的旋转,我们以它的造型中的第三个小方块n(3)为中心旋转,这样,在开始运动的时候,长条形要发生旋转最少要运动到第三行,才能由横着变成竖立状态,游戏区图形框中第
1
三行的第一个方块的编号为20,所以长条造型的第一个小方块的编号n(0)必须要大于20。同样,长条型方块在下落到底部的时候也有限制。如果长条下落到最后一行也将无法由横着变成竖立状态。
②如何实现方块的运动和自动消除满行的方块:我们的这个俄罗斯
方块游戏主要是利用command控件的visible属性完成效果的,其实在游戏区图形框可以看成是由许多的command小方块组成,方块运动的过程就是造型里方块显示或者隐藏,就像现在的霓虹灯效果一样,由时钟控件控制visible属性改变的速度,上一层的消失,下一层的显示,这样,从视觉效果可以看到方块的下落运动效果。方块在下落的过程中会自动判断每一行方块的visible属性,如果全部为true时,就会将这一行小方块的visible属性全部变成false,在将上面的小方块向下移动,利用for语句进行循环判断,将所有这样情况的行改变小方块visible属性。
③连续消行:当有多行同时出现这样情况时使用递归调用,实现连
续消行。
游戏速度和游戏级别自由选择
④游戏速度选择:就是改变时钟控件的频率,我们在菜单中添加了
选择速度的功能,还有添加了考验功能,将欲览窗中的方块造型隐藏,给玩家提高了难度,如果你不愿意接受考验也可以点击显示还原成原来状态。 游戏级别的自由选择是让用户选择游戏开始时候,游戏区底部出现一定行数的随机方块,同样给玩家增加了难度
⑤游戏得分的计算和游戏菜单的编辑:游戏得分的计算主要是根据
消除的行数来决定的,当然每一次同时消除的行数不一样,每一行的得分也不一样,如果你每次消除的行数为1,则最后得分是100分,如果同时消除2行,则最后得分是300分,同时消除3行,得分为700分,同时消除4行,得分为1500分,这由公式:得分 = 原来分数+ 100 * (2 ^ 同时消除的行数-1)。
效果极其存在问题:游戏自身已经可以运行,可以带来游戏的轻松及其快感,可是界面显得粗糙,缺乏美感,而且就游戏菜单编辑器方面得进行改进
心得:此次对俄罗斯方块的VB程序进行编辑,更加对这个游戏的设计思路以及运行程序得到了解,而且从中学到很多VB的知识,比如怎样对菜单编辑器进行设置,怎样设计产生两个甚至更多的窗体,对一些控件的应用以及数组的应用也有了更深的了解。
2
VB程序:⑴定义产生方块的数组以及消除行数的数组:
Private n(3), m(3) As Integer Private situation, situation2, linenum, t As Integer
⑵为了产生方块,定义使各个小方块可见和不可见的过程: Private Sub hidefang(a As Integer) Select Case a Case 0
cmdfang(n(0)).Visible = False cmdfang(n(1)).Visible = False cmdfang(n(2)).Visible = False cmdfang(n(3)).Visible = False Case 1
cmdfangnext(m(0)).Visible = False cmdfangnext(m(1)).Visible = False cmdfangnext(m(2)).Visible = False cmdfangnext(m(3)).Visible = False End Select
End Sub
Private Sub showfang(a As Integer)
Select Case
Case 0
cmdfang(n(0)).Visible = True cmdfang(n(1)).Visible = True cmdfang(n(2)).Visible = True cmdfang(n(3)).Visible = True Case 1
cmdfangnext(m(0)).Visible = True cmdfangnext(m(1)).Visible = True cmdfangnext(m(2)).Visible = True cmdfangnext(m(3)).Visible = True End Select
End Sub
⑶实现消除一行(实现连续消行,用递归): Private Sub clearline()
For i = 190 To 10 Step -10
If cmdfang(i).Visible = True And _
cmdfang(i + 1).Visible = True And _ cmdfang(i + 2).Visible = True And _ cmdfang(i + 3).Visible = True And _ cmdfang(i + 4).Visible = True And _ cmdfang(i + 5).Visible = True And _ cmdfang(i + 6).Visible = True And _ cmdfang(i + 7).Visible = True And _ cmdfang(i + 8).Visible = True And _
3
cmdfang(i + 9).Visible = True Then For j = i + 4 To i Step -1 t = 1
cmdfang(j).Visible = False
cmdfang(2 * i + 9 - j).Visible = False For k = 1 To 4000 DoEvents Next t = 0 Next
linenum = linenum + 1 For j = i - 1 To 0 Step -1
If cmdfang(j).Visible = True Then cmdfang(j).Visible = False cmdfang(j + 10).Visible = True End If Next clearline End If Next End Sub
⑷定义方块能否下降,左移和右移:
Private Function downable() As Boolean If n(0) < 190 And n(1) < 190 And n(2) < 190 And n(3) < 190 Then If cmdfang(n(0) + 10).Visible = False And _ cmdfang(n(1) + 10).Visible = False And _ cmdfang(n(2) + 10).Visible = False And _ cmdfang(n(3) + 10).Visible = False Then downable = True Else
downable = False End If Else
downable = False End If
End Function
Private Function leftable() As Boolean If n(0) Mod 10 <> 0 And n(1) Mod 10 <> 0 And n(2) Mod 10 <> 0
And n(3) Mod 10 <> 0 Then
If cmdfang(n(0) - 1).Visible = False And _ cmdfang(n(1) - 1).Visible = False And _ cmdfang(n(2) - 1).Visible = False And _ cmdfang(n(3) - 1).Visible = False Then leftable = True
4
Else
leftable = False End If Else
leftable = False End If End Function
Private Function rightable() As Boolean If n(0) Mod 10 <> 9 And n(1) Mod 10 <> 9 And n(2) Mod 10 <> 9 And n(3) Mod 10 <> 9 Then
If cmdfang(n(0) + 1).Visible = False And _ cmdfang(n(1) + 1).Visible = False And _ cmdfang(n(2) + 1).Visible = False And _ cmdfang(n(3) + 1).Visible = False Then rightable = True Else
rightable = False End If Else
rightable = False End If
End Function
⑸定义随机产生的形状: Private Sub loadfangkuai() Select Case Int(Rnd * 6) Case 0
Select Case Int(Rnd * 2) Case 0
m(0) = 3 m(1) = 4 m(2) = 5 m(3) = 6
situation2 = 0 Case 1
m(0) = 5 m(1) = 15 m(2) = 25 m(3) = 35 situation2 = 1 End Select Case 1
m(0) = 4 m(1) = 5 m(2) = 14
5
m(3) = 15 situation2 = 2 Case 2
Select Case Int(Rnd * 2) Case 0
m(0) = 6 m(1) = 5 m(2) = 15 m(3) = 14 situation2 = 3 Case 1
m(0) = 4 m(1) = 14 m(2) = 15 m(3) = 25 situation2 = 4 End Select Case 3
Select Case Int(Rnd * 2)
Case 0
m(0) = 4 m(1) = 5 m(2) = 15 m(3) = 16 situation2 = 5 Case 1
m(0) = 5 m(1) = 15 m(2) = 14 m(3) = 24 situation2 = 6 End Select Case 4
Select Case Int(Rnd * 4) Case 0
m(0) = 4 m(1) = 5 m(2) = 6 m(3) = 15 situation2 = 7 Case 1
m(0) = 5 m(1) = 15 m(2) = 25
6
m(3) = 14 situation2 = 8 Case 2
m(0) = 16 m(1) = 15 m(2) = 14 m(3) = 5
situation2 = 9 Case 3
m(0) = 24 m(1) = 14 m(2) = 4 m(3) = 15
situation2 = 10 End Select Case 5
Select Case Int(Rnd * 4) Case 0
m(0) = 4
m(1) = 5 m(2) = 15 m(3) = 25 situation2 = 11 Case 1
m(0) = 5 m(1) = 15 m(2) = 14 m(3) = 13
situation2 = 12 Case 2
m(0) = 25 m(1) = 24 m(2) = 14 m(3) = 4
situation2 = 13 Case 3
m(0) = 14 m(1) = 4 m(2) = 5 m(3) = 6
situation2 = 14 End Select Case 6
Select Case Int(Rnd * 4)
7
Case 0
m(0) = 5 m(1) = 4 m(2) = 14 m(3) = 24
situation2 = 15 Case 1
m(0) = 15 m(1) = 5 m(2) = 4 m(3) = 3
situation2 = 16 Case 2
m(0) = 24 m(1) = 25 m(2) = 15 m(3) = 5
situation2 = 17 Case 3
m(0) = 4 m(1) = 14 m(2) = 15 m(3) = 16
situation2 = 18 End Select End Select
EndSub ⑹定义各类方块旋转的过程: Private Sub zhuan() Select Case situation Case 0
If n(0) - 18 >= 2 And n(3) + 9 <= 198 Then If cmdfang(n(0) - 18).Visible = False And _ cmdfang(n(1) - 9).Visible = False And _ cmdfang(n(3) + 9).Visible = False Then hidefang 0 n(0) = n(0) - 18 n(1) = n(1) - 9 n(3) = n(3) + 9 showfang 0 situation = 1 End If End If Case 1
8
If (n(0) + 18) Mod 10 < 8 And (n(3) - 9) Mod 10 > 0 Then If cmdfang(n(0) + 18).Visible = False And _ cmdfang(n(1) + 9).Visible = False And _ cmdfang(n(3) - 9).Visible = False Then hidefang 0
n(0) = n(0) + 18 n(1) = n(1) + 9 n(3) = n(3) - 9 showfang 0 situation = 0 End If End If Case 2 Case 3
If n(0) - 11 > 1 Then
If cmdfang(n(0) - 11).Visible = False And _ cmdfang(n(3) + 2).Visible = False Then hidefang 0
n(0) = n(0) - 11
n(2) = n(2) - 9 n(3) = n(3) + 2 showfang 0 situation = 4 End If End If Case 4
If (n(3) - 2) Mod 10 < 9 Then
If cmdfang(n(2) + 9).Visible = False And _ cmdfang(n(3) - 2).Visible = False Then hidefang 0
n(0) = n(0) + 11 n(2) = n(2) + 9 n(3) = n(3) - 2 showfang 0 situation = 3 End If End If Case 5
If n(0) - 9 > 1 Then
If cmdfang(n(0) - 9).Visible = False And _ cmdfang(n(3) - 2).Visible = False Then hidefang 0 n(0) = n(0) - 9 n(2) = n(2) - 11
9
n(3) = n(3) - 2 showfang 0 situation = 6 End If End If Case 6
If (n(3) + 2) Mod 10 > 0 Then
If cmdfang(n(2) + 11).Visible = False And _ cmdfang(n(3) + 2).Visible = False Then hidefang 0 n(0) = n(0) + 9 n(2) = n(2) + 11 n(3) = n(3) + 2 showfang 0 situation = 5 End If End If Case 7
If n(0) - 9 > 0 Then
If cmdfang(n(0) - 9).Visible = False Then hidefang 0 n(0) = n(0) - 9 n(2) = n(2) + 9 n(3) = n(3) - 11 showfang 0 situation = 8 End If End If Case 8
If (n(0) + 11) Mod 10 > 0 Then
If cmdfang(n(0) + 11).Visible = False Then hidefang 0
n(0) = n(0) + 11 n(2) = n(2) - 11 n(3) = n(3) - 9 showfang 0 situation = 9 End If End If Case 9
If n(0) + 9 < 199 Then
If cmdfang(n(0) + 9).Visible = False Then hidefang 0 n(0) = n(0) + 9
10
n(2) = n(2) - 9 n(3) = n(3) + 11 showfang 0 situation = 10 End If End If Case 10
If (n(0) - 11) Mod 10 < 9 Then
If cmdfang(n(0) - 11).Visible = False Then hidefang 0 n(0) = n(0) - 11 n(2) = n(2) + 11 n(3) = n(3) + 9 showfang 0 situation = 7 End If End If Case 11
If n(0) - 9 > 1 And (n(3) - 22) Mod 10 < 9 Then If cmdfang(n(0) - 9).Visible = False And _ cmdfang(n(3) - 22).Visible = False Then hidefang 0 n(0) = n(0) - 9 n(2) = n(2) - 11 n(3) = n(3) - 22 showfang 0 situation = 12 End If End If Case 12
If (n(0) + 11) Mod 10 > 0 And n(3) - 18 > 1 Then If cmdfang(n(0) + 11).Visible = False And _ cmdfang(n(3) - 18).Visible = False Then hidefang 0
n(0) = n(0) + 11 n(2) = n(2) - 9 n(3) = n(3) - 18 showfang 0 situation = 13 End If End If Case 13
If n(0) + 9 < 198 And (n(3) + 22) Mod 10 > 0 Then If cmdfang(n(0) + 9).Visible = False And _
11
cmdfang(n(3) + 22).Visible = False Then hidefang 0 n(0) = n(0) + 9 n(2) = n(2) + 11 n(3) = n(3) + 22 showfang 0 situation = 14 End If End If Case 14
If (n(0) - 11) Mod 10 < 9 And n(3) + 18 < 198 Then If cmdfang(n(0) - 11).Visible = False And _ cmdfang(n(3) + 18).Visible = False Then hidefang 0 n(0) = n(0) - 11 n(2) = n(2) + 9 n(3) = n(3) + 18 showfang 0 situation = 11 End If End If Case 15
If (n(3) - 22) Mod 10 < 8 Then
If cmdfang(n(2) - 11).Visible = False And _ cmdfang(n(3) - 22).Visible = False Then hidefang 0 n(0) = n(0) + 9 n(2) = n(2) - 11 n(3) = n(3) - 22 showfang 0 situation = 16 End If End If Case 16
If n(3) - 18 > 1 Then
If cmdfang(n(2) - 9).Visible = False And _ cmdfang(n(3) - 18).Visible = False Then hidefang 0 n(0) = n(0) - 11 n(2) = n(2) - 9 n(3) = n(3) - 18 showfang 0 situation = 17 End If
12
End If Case 17
If (n(3) + 22) Mod 10 > 1 Then
If cmdfang(n(2) + 11).Visible = False And _ cmdfang(n(3) + 22).Visible = False Then hidefang 0 n(0) = n(0) - 9 n(2) = n(2) + 11 n(3) = n(3) + 22 showfang 0 situation = 18 End If End If Case 18
If n(3) + 18 < 198 Then
If cmdfang(n(2) + 9).Visible = False And _ cmdfang(n(3) + 18).Visible = False Then hidefang 0
n(0) = n(0) + 11 n(2) = n(2) + 9 n(3) = n(3) + 18 showfang 0 situation = 15 End If End If End Select End Sub
⑺对暂停按钮进行编辑:
Private Sub cmdpause_Click() Select Case cmdpause.Caption Case \"暂停\"
cmdpause.Caption = \"继续\" tmrmain.Enabled = False Case \"继续\"
cmdpause.Caption = \"暂停\" tmrmain.Enabled = True picmain.SetFocus End Select
End Sub
⑻游戏开始:
Private Sub cmdstart_Click()
If cmdstart.Caption = \"开始\" Then picmain.SetFocus
tmrmain.Interval = 1000 / Val(txtlevel.Text)
13
For i = 0 To 3 n(i) = m(i) Next
showfang 0
situation = situation2
tmrmain.Enabled = True
cmdpause.Enabled = True cmdoption.Enabled = False hidefang 1 loadfangkuai showfang 1
cmdstart.Caption = \"结束\"
Else
tmrmain.Enabled = False cmdpause.Enabled = False cmdoption.Enabled = True For i = 1 To 199
cmdfang(i).Visible = False
Next
cmdstart.Caption = \"开始\" txtscore.Text = \"0\" linenum = 0 End If End Sub
⑼对游戏下左右及旋转进行控制:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If t = 0 Then hidefang 0
Select Case KeyCode Case vbKeyLeft
If leftable() = True Then For j = 0 To 3
cmdfang(n(j) - 1).Visible = True n(j) = n(j) - 1 Next j End If
showfang 0
Case vbKeyDown
If downable() = True Then For j = 0 To 3
cmdfang(n(j) + 10).Visible = True n(j) = n(j) + 10 Next j End If
14
showfang 0 Case vbKeyRight
If rightable() = True Then For j = 0 To 3
cmdfang(n(j) + 1).Visible = True n(j) = n(j) + 1 Next j End If
showfang 0 Case vbKeyUp showfang 0 zhuan Case Else showfang 0 End Select End If
End Sub
⑽主界面的产生:
Private Sub Form_Load() picmain = LoadPicture(\"\") Randomize For i = 1 To 199 Load cmdfang(i) Next i
For i = 0 To 199
cmdfang(i).Left = (i Mod 10) cmdfang(i).Top = i \\ 10 cmdfang(i).Visible = False Next i
For i = 1 To 39
Load cmdfangnext(i) Next i
For i = 0 To 39
cmdfangnext(i).Left = (i Mod 10) - 3 cmdfangnext(i).Top = i \\ 10 cmdfangnext(i).Visible = False Next i
loadfangkuai showfang 1 End Sub
⑾对时钟控件以及游戏失败结束和得分升级的计算的程序: Private Sub tmrmain_Timer() hidefang 0
If downable() = True Then
15
For j = 0 To 3 n(j) = n(j) + 10 Next j
showfang 0 Else
showfang 0 clearline For i = 0 To 9
If cmdfang(i).Visible = True Then Exit For Next
If i < 10 Then cmdstart_Click: Exit Sub
For i = 0 To 3
n(i) = m(i) Next
showfang 0
situation = situation2
txtscore.Text = Str(Val(txtscore.Text) + 100 * (2 ^ linenum - 1))
If Val(Right(txtscore.Text, 4)) < Val(Right(Str(Val(txtscore.Text) - 100 *
(2 ^ linenum - 1)), 4)) Then
txtlevel.Text = Str(Val(txtlevel.Text) + 1) tmrmain.Interval = 1000 / Val(txtlevel.Text) End If
linenum = 0
hidefang 1 loadfangkuai showfang 1 End If End Sub
⑿菜单编辑器
Private Sub cmdoption_Click() Form1.Show
End Sub
Private Sub xx_Click()
Form2.Show End Sub
16
因篇幅问题不能全部显示,请点此查看更多更全内容