⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tetris2008.aspx.htm

📁 有是一个用VB写的俄罗斯方块啊!!是2005的! 相信也很好的!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<tr id="ctl00_ArticleRating_PopularityRow">
			<td colspan="2" class="SmallText" align="right">
		<a id="ctl00_ArticleRating_PopularityLnk" title="Calculated as rating x Log10(# votes)" href="http://www.codeproject.com/script/Articles/TopArticles.aspx?ta_so=1">Popularity: 3.87</a><span id="ctl00_ArticleRating_PopularityLbl"></span>
		Rating: <b>4.58</b> out of 5
	</td>
		</tr>
		

</tbody></table>





				</td>
			</tr>
			</tbody></table>
			
			
			
			<div id="ctl00_UneditedRow" class="SmallText" style="border-top: 1px dashed rgb(255, 153, 0); border-bottom: 1px dashed rgb(255, 153, 0); margin: 5px; padding: 5px;">
				<b>Note:</b> This is an unedited contribution. If this article is inappropriate,
				needs attention or copies someone else's work without reference then please
				<a id="ctl00_ReportProblem" href="http://www.codeproject.com/script/Articles/Report.aspx?aid=23510">Report This Article</a>
			</div>
			
			
			
			
			
			<span name="intelliTxt" id="intelliTXT">
			<div id="contentdiv">
			
			<!-- Main Page Contents Start -->
			

<!-- Article Starts -->


<p><a href="http://www.codeproject.com/KB/vb/Tetris2008/Tetris2008.zip"></a></p>

<ul class="download">
<li><a href="http://www.codeproject.com/KB/vb/Tetris2008/Tetris2008.zip">Download solution and executable - 209 Kb </a></li>
</ul>

<p><img alt="Sample Image - maximum width is 600 pixels" src="Tetris2008.aspx_files/tetris.gif" height="338" width="431"></p>

<h2>Introduction</h2>

<p>The beloved game Tetris was once a very popular game, but now it has
been overshadowed by a never ending line of games that just keep
coming. I first started this program just last month when my Advanced
Visual Basic programming course decided to re-create this classic.
After spending quite a few class hours looking for code samples and
brainstorming aspects of the program, the task was deemed too difficult
for the class as a whole. Their were many examples of the game, but
none were open source or explained how the program could be built.
However, I could not just let this great program go without a fight.
The result is what you see before you, a basic two player Tetris the
encompasses all the basic functionality of the classic. This tutorial
will guide you through the creation of this program from start to
finish, explaining every detail possible.</p>

<h2>The Concept</h2>

<p>You may believe that the concept to Tetris is very simple, however
their are many aspects that need to be covered. Some of the concepts
that need to be thought of:</p>

<ul>
<li>What dimensions must the Tetris board be? </li>

<li>Do you inter-mingle the logical and graphical aspects of the program? </li>

<li>How do you toggle the pieces? </li>

<li>How do you store the pieces on the board? </li>

<li>How do you draw the different pieces on to the board? </li>

<li>How do you animate the pieces? </li>

<li>How do you manage completed lines? </li>
</ul>

<p>This is just a sampling of all the questions that must be asked
before you can start the Tetris making process. A few of the questions
can be answered with simple Google searches. For example, the official
dimension of a Tetris board is 10x20. For storing pieces, I can only
think of one way to store everything...arrays...or for a more
maneuverable approach, a collection. For this program I use the <code>Collection(Of type)</code> class in the <code>System.Collections.ObjectModel</code>
namespace. The last question that can really be answered right now is
the inter-mingling of the logical and graphical aspects of the program.
When my programming class initially chose Tetris, one of the goals was
to move the program to XNA so the game could be transferred to the XBOX
360. Of course this will not happen as the class has moved to a
different program, but to help facilitate this I wrote this program
with the logical and graphical aspects separate. This way only the
graphical layer had to be changed when the program was transferred to
XNA.</p>

<h2>Logical Layer</h2>

<p>To begin the logical layer we must now answer the question 'How do
you store pieces on the board?' As a class, we started by adding one
point at a time to the board and then manipulating the four points for
the movement of the pieces. This almost immediately lead to cumbersome
code that would inevitably lead to long confusing code statements. To
correct this issue we decided to create a class called <code>Shape</code>. This class is basically the heart of the logical layer. Here is the basic outline for the shape class:</p>

<p><img alt="prop" src="Tetris2008.aspx_files/PropertiesHS.png" height="16" width="16">
Points as Collection(Of Point) - This property stores a logical grid of
all the points for the current shape. Every shape has a total of four
points, and each point is stored in this property. Since this is in the
logical portion of the program, these points do not need to abide by
the pixel coordinates throughout the board. The logical grid is a 10x20
grid where the top of the board is at coordinate 20 and the far right
of the board is at coordinate 10.</p>

<p><img alt="prop" src="Tetris2008.aspx_files/PropertiesHS.png" height="16" width="16">
IsFrozen as boolean - This property is used to prevent pieces from
moving after they have landed and should not be able to move anymore.</p>

<p><img alt="prop" src="Tetris2008.aspx_files/PropertiesHS.png" height="16" width="16">Color
as Color - Every piece has a different color and this is where the
color is stored. Even though one color is assigned to this property the
graphical layer can manipulate the usage of of this color. This
application uses this color and a color variations through the <code>ControlPaint.Light</code>(<code>c</code> as Color) as Color method to create a LinearGradientBrush.</p>

<p><img alt="private method" src="Tetris2008.aspx_files/VSObject_Method_Private.gif" height="16" width="16"> <code>GetNewPoints</code>(<code>direction </code>as
NewDirection) as Collection(Of Point) - This function will return a new
set of points of where the shape will be if a shape is to move in any
direction in the NewDirection enumeration. The NewDirection enumeration
contains the following directions: left, right, down, current,
toggleOrientation. Movement in this method will be discussed later in
the article.</p>

<p><img alt="method" src="Tetris2008.aspx_files/VSObject_Method.gif" height="16" width="16"> <code>MoveLeft</code>() - This method calls <code>GetNewPoints </code>and replaces the points currently stored in the shape object with the points returned from <code>GetNewPoints</code>.</p>

<p><img alt="method" src="Tetris2008.aspx_files/VSObject_Method.gif" height="16" width="16"> <code>MoveRight</code>() - This method calls <code>GetNewPoints </code>and replaces the points currently stored in the shape object with the points returned from <code>GetNewPoints</code>.</p>

<p><img alt="method" src="Tetris2008.aspx_files/VSObject_Method.gif" height="16" width="16"> <code>ToggleOrientation</code>() - This method calls <code>GetNewPoints </code>and replaces the points currently stored in the shape object with the points returned from <code>GetNewPoints</code>.</p>

<p><img src="Tetris2008.aspx_files/VSObject_MethodOverload.gif" height="16" width="16"> <code>WillCollide</code>(<code>shp</code> as Shape, <code>direction </code>as
NewDirection) as boolean - I got the inspiration of this function from
my AP programming class' Marine Biology case study. This method will
check the logical grid of two shapes and check for overlaps. If an
overlap in points occurs then the function will return false. Else it
will return true.</p>

<p><img src="Tetris2008.aspx_files/VSObject_MethodOverload.gif" height="16" width="16"> <code>WillCollide</code>(<code>direction </code>as
NewDirection) as boolean - This function is an overload of the first
WillCollide function. This overload only deals with the placement of
the piece on the logical grid which is 10x20. If the new direction
results in the piece going off the board either horizontally or
vertically this function will return false, else it will return true.</p>

<h3>Movement of the Shape object</h3>

<p>As explained in the outline above, the GetNewPoints function is one of the main parts of the shape class. The <code>GetNewPoints </code>function
basically helps answer the question 'How do you toggle the pieces?'.
When the shape class is instantiated a ShapeType is passed through the
constructor, the ShapeType enumeration contains all of the shapes that
are in Tetris. The shapes that are included: <code>square</code>, <code>pyramid</code>, <code>staircaseLeft</code>, <code>staircaseRight</code>, <code>lshapeLeft</code>, <code>lshapeRight</code>, <code>line</code>, <code>null</code>. All the ShapeTypes are pretty much self-explanatory, but <code>null </code>is the exception it will be explained later in this article. <code>GetNewPoints </code>is basically a huge Select Case statement that determines how a piece should be moved. The basic part of <code>GetNewPoints </code>is the moving left, right, and down.</p>

<div class="no-vmads"><pre lang="vb.net"><span class="code-keyword">Dim</span> rval <span class="code-keyword">As</span> <span class="code-keyword">New</span> Collection(<span class="code-keyword">Of</span> Point)
<span class="code-keyword">Select</span> <span class="code-keyword">Case</span> direction
       <span class="code-keyword">Case</span> NewDirection.current
            <span class="code-keyword">Return</span> <span class="code-keyword">Me</span>.Points
       <span class="code-keyword">Case</span> NewDirection.down
            <span class="code-keyword">For</span> <span class="code-keyword">Each</span> pnt <span class="code-keyword">As</span> Point <span class="code-keyword">In</span> <span class="code-keyword">Me</span>.Points
                rval.Add(<span class="code-keyword">New</span> Point(pnt.X, pnt.Y - <span class="code-digit">1</span>))
            <span class="code-keyword">Next</span>
       <span class="code-keyword">Case</span> NewDirection.left
            <span class="code-keyword">For</span> <span class="code-keyword">Each</span> pnt <span class="code-keyword">As</span> Point <span class="code-keyword">In</span> <span class="code-keyword">Me</span>.Points
                rval.Add(<span class="code-keyword">New</span> Point(pnt.X - 1, pnt.Y))
            <span class="code-keyword">Next</span>
       <span class="code-keyword">Case</span> NewDirection.right
            <span class="code-keyword">For</span> <span class="code-keyword">Each</span> pnt <span class="code-keyword">As</span> Point <span class="code-keyword">In</span> <span class="code-keyword">Me</span>.Points
                rval.Add(<span class="code-keyword">New</span> Point(pnt.X + 1, pnt.Y))
            <span class="code-keyword">Next</span>
...
</pre></div>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -