📄 programming a memory game in delphi - part 2 - starting to code.htm
字号:
i : integer;
begin
for i := 0 to 19 do
ImageOfCell[RandomPermutationArray[i]] := i div 2
end;
</pre>
<p>This procedure assigns the 0th image to the first couple, 1st image to the second couple, and so on.</p>
<p>We have to call the followings at start of each new game:</p>
<ul>
<li>RandomizeThePermutationArray</li>
<li>AssignPartnerships</li>
<li>AssignImagesToCells</li>
</ul>
<p><strong>NOTE</strong>: We used the built-in function Random() in the RandomizeThePermutationArray procedure. But before using it, we have to call the built-in Randomize procedure once, in order to assign the randomization seed. We can call it in the OnCreate event of the main form:</p>
<pre>
procedure TfrmMain.FormCreate(Sender: TObject);
var
i : integer;
begin
LoadImages;
for i := 0 to 19 do
RandomPermutationArray[i] := i;
Randomize
end;
</pre>
<h2>Starting a New Game</h2>
<p>Let's add a main menu to our application and add an item with a caption "New Game" to it. I will name this item as itemNewGame. When clicked, this item will start a new game.</p>
<img src="../Images/MemoryGame_4.jpg" alt="Delphi Game with menu">
<p>We should call the three procedures mentioned above in the OnClick event of this item in order to make the necessary initializations</p>
<pre>
procedure TfrmMain.itemNewGameClick(Sender: TObject);
begin
RandomizeThePermutationArray;
AssignPartnerships;
AssignImagesToCells
end;
</pre>
<p>In the third part of this tutorial we will move onto drawing the playing board using the DrawGrid component, if you wish to be informed when part 3 is available please send us your email address via the update section on the left of this page.</p>
<p><a href="memory_game.aspx">Delphi Tutorial - Programming a Memory Game - Part 1</a><br>
<a href="memory_game_2.aspx">Programming a Memory Game in Delphi - Part 2 - Starting to Code</a><br>
<a href="memory_game_3.aspx">Programming a Memory Game in Delphi - Part 3 - Drawing the Grid</a><br>
<a href="memory_game_4.aspx">Programming a Memory Game in Delphi - Part 4 - Coding the Algorithm</a><br>
</p>
<!-- InstanceEndEditable --> <br>
<center>
<!-- SiteSearch Google -->
<form method="get" action="http://www.google.com/custom" target="google_window">
<table border="0" bgcolor="#ffffff">
<tr>
<td nowrap="nowrap" valign="top" align="left" height="32"><a href="http://www.google.com/"> <img src="http://www.google.com/logos/Logo_25wht.gif"
border="0" alt="Google"></a> <br/>
<input type="hidden" name="domains" value="www.Delphi-Central.com">
<input type="text" name="q" size="31" maxlength="255" value="">
<input type="submit" name="sa" value="Search">
</td>
</tr>
<tr>
<td nowrap="nowrap"><table>
<tr>
<td><input type="radio" name="sitesearch" value="">
<font size="-1" color="#000000">Web</font> </td>
<td><input type="radio" name="sitesearch" value="www.Delphi-Central.com" checked="checked">
<font size="-1" color="#000000">www.Delphi-Central.com</font> </td>
</tr>
</table>
<input type="hidden" name="client" value="pub-8027051536030207">
<input type="hidden" name="forid" value="1">
<input type="hidden" name="channel" value="0901509782">
<input type="hidden" name="ie" value="ISO-8859-1">
<input type="hidden" name="oe" value="ISO-8859-1">
<input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:F9EFDD;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;LH:50;LW:189;L:http://www.delphi-central.com/images/logoline.gif;S:http://www.delphi-central.com/;FORID:1;">
<input type="hidden" name="hl" value="en">
</td>
</tr>
</table>
</form>
<!-- SiteSearch Google -->
</center>
<div id="pagefooter">
<br>
<a href="http://www.dirspace.com/">Internet Guide</a>
<br>
<br>
[<A HREF="/">Home</A>] [<a href="/search.aspx">Search</a>] [<A HREF="/tutorials/">Delphi Tutorials</A>] [<A HREF="/hints/">Hints & Tips</A>] [<A HREF="/components/">Freeware Components</A>] [<A HREF="/privacy.aspx">Privacy</A>] | [<A HREF="http://forums.hostmatrix.org">Webmaster Discussions</A>]<br>
<br>
Copyright 1997-2005 Delphi Central. Delphi is a trademark of <a href="http://www.borland.com" target="_blank">Borland</a> International, Inc.
</div>
</div>
<div id="leftEdge">
<div class="leftBox"> <A HREF="http://www.delphi-central.com"><img src="/images/logo3.gif" border=0 height=92 width=103 hspace=2 vspace=2 align="middle" alt="Delphi Central - Delphi Programming Tutorials, Hints and Tips"></A> </div>
<div class="leftBox">
<div class="Nav">
<h4><a href="http://www.delphi-central.com" target="_top">Home</a></h4><div class="line"></div>
<p><a href="/tutorials1.aspx">Delphi Tutorials</a></p>
<p><a href="/components/">Delphi Components</a></p>
<p><a href="/hints/">Hints and Tips</a></p>
<p><a href="/delphi-books.aspx">Delphi Books</a></p>
<p><a href="/search.aspx">Search</a></p>
<p><a href="/links.aspx">Links</a></p>
<p><a href="/feedback.aspx">Feedback</a></p>
<p><a href="/about.aspx">About</a></p>
<hr>
<p><a href="http://www.coolest-gadgets.com">Coolest Gadgets</a></p>
</div>
</div>
<script type="text/javascript"><!--
ch_client = "gadgets";
ch_width = 120;
ch_height = 600;
ch_sid = 'dcleft';
ch_non_contextual = 1;
ch_nosearch = 1;
var ch_queries = new Array('gadget', 'ipod nano', 'Borland Delphi', 'C++', 'SQL', 'psp', 'gagets', 'nano', 'Creative Zen Vision');
var ch_selected=Math.floor((Math.random()*ch_queries.length));
ch_query = ch_queries[ch_selected];//-->
</script>
<script src="http://scripts.chitika.net/eminimalls/mm.js" type="text/javascript">
</script>
</div>
<div id="rightEdge">
<div class="rightBox">
<div class="Nav">
<h4>Most Popular</h4><div class="line"></div>
<p><a href="/mcsd_training.aspx">Training for MCSD</a></p>
<p><A href="/dynamicdll.aspx">Dynamically Loading DLL's</A></p>
<p><A href="/iconex.aspx">An Icon Extractor and Viewer</A></p>
<p><A href="/movecontrun.aspx">How to Move Controls and Save their Positions</A></p>
<p><A href="/runtime.aspx">Creating Controls at Run Time</A></p>
<p><A href="/wierd.aspx">How to Implement Weird Shaped Windows</A></p>
</div>
</div>
<div class="leftcentered">
<p><iframe width="120" height="213" scrolling="no" frameborder=0 src="http://rcm-uk.amazon.co.uk/e/cm?t=delphicentral-21&l=st1&search=delphi&mode=software-uk&p=6&o=2&f=ifr">
<table border='0' cellpadding='0' cellspacing='0' width='120' height='213'>
<tr>
<td><p><A HREF='http://www.amazon.co.uk/exec/obidos/redirect-home/delphicentral-21' target=_blank><img src="http://images-eu.amazon.com/images/G/02/associates/recommends/default_120x213.gif" width=120 height=213 border="0" access=regular></a></p></td>
</tr>
</table>
</iframe>
</p>
</div
><div class="rightBox" align="center">
<!-- BEGIN RICH-MEDIA BURST! CODE -->
<script language="JavaScript">
rnum=Math.round(Math.random() * 100000);
document.write('<scr'+'ipt src="http://www.burstnet.com/cgi-bin/ads/sk2422a.cgi/v=2.0S/sz=120x600A/'+rnum+'/RETURN-CODE/JS/"></scr'+'ipt>');
</script>
<noscript>
<a href="http://www.burstnet.com/ads/sk2422a-map.cgi/ns/v=2.0S/sz=120x600A/" target="_top"> <img src="http://www.burstnet.com/cgi-bin/ads/sk2422a.cgi/ns/v=2.0S/sz=120x600A/" border="0" alt="Click Here"></a>
</noscript>
<!-- END BURST CODE -->
</div>
<div class="rightBox">
<div class="Nav">
<h4>Latest Delphi Hints</h4><div class="line"></div>
<p><a href="/Hints/PolyLine.aspx">Use PolyLine rather than MoveTo and LineTo</a></p>
<p><a href="/Hints/HideButtonTaskbar.aspx">Hiding your application from the Taskbar</a></p>
<p><a href="/Hints/HideWindowsTaskBar.aspx">How to hide and show the Windows Taskbar</a></p>
<p><a href="/Hints/ShowStartMenu.aspx">How to programmatically press the "Start" Button</a></p>
</div>
</div>
</div>
<script language="javascript" type="text/javascript"
src="http://www.hostmatrix.org/asrep/record.php"></script>
<noscript><img width="1" height="1" border="0"
src="http://www.hostmatrix.org/asrep/record.php?rep=v&transport=img"></noscript>
<br>
<!-- InstanceBeginEditable name="BelowLeftSide" --> <!-- InstanceEndEditable -->
</body>
<!-- InstanceEnd --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -