1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
arduinolibs/ir_snake.html
Rhys Weatherley 77f9295247 Update docs
2016-02-13 07:01:47 +10:00

282 lines
22 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>ArduinoLibs: Snake Video Game Using an Infrared Remote Control</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">ArduinoLibs
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Snake Video Game Using an Infrared Remote Control </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This example demonstrates the use of the <a class="el" href="classDMD.html" title="Handle large dot matrix displays composed of LED&#39;s. ">DMD</a> and <a class="el" href="classIRreceiver.html" title="Manages the reception of RC-5 commands from an infrared remote control. ">IRreceiver</a> classes. The full source code follows:</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Copyright (C) 2012 Southern Storm Software, Pty Ltd.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</span></div>
<div class="line"><span class="comment"> * copy of this software and associated documentation files (the &quot;Software&quot;),</span></div>
<div class="line"><span class="comment"> * to deal in the Software without restriction, including without limitation</span></div>
<div class="line"><span class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</span></div>
<div class="line"><span class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</span></div>
<div class="line"><span class="comment"> * Software is furnished to do so, subject to the following conditions:</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * The above copyright notice and this permission notice shall be included</span></div>
<div class="line"><span class="comment"> * in all copies or substantial portions of the Software.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS</span></div>
<div class="line"><span class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span></div>
<div class="line"><span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span></div>
<div class="line"><span class="comment"> * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span></div>
<div class="line"><span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</span></div>
<div class="line"><span class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</span></div>
<div class="line"><span class="comment"> * DEALINGS IN THE SOFTWARE.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;IRreceiver.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;DMD.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Mono5x7.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define SNAKE_ADVANCE_TIME 150</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define SNAKE_BLINK_TIME 500</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define SNAKE_INC_STEPS 5</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define SNAKE_MAX_LENGTH 50</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define SNAKE_START_LENGTH 10</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="keyword">struct </span>Point</div>
<div class="line">{</div>
<div class="line"> <span class="keywordtype">int</span> x, y;</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><a class="code" href="classDMD.html">DMD</a> display;</div>
<div class="line"><a class="code" href="classIRreceiver.html">IRreceiver</a> ir;</div>
<div class="line"><span class="keywordtype">bool</span> paused;</div>
<div class="line"><span class="keywordtype">bool</span> gameOver;</div>
<div class="line"><span class="keywordtype">bool</span> waitForStart;</div>
<div class="line"><span class="keywordtype">bool</span> snakeDrawn;</div>
<div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> lastChange;</div>
<div class="line">Point direction;</div>
<div class="line">Point snakeParts[SNAKE_MAX_LENGTH];</div>
<div class="line"><span class="keywordtype">int</span> snakeLength;</div>
<div class="line"><span class="keywordtype">int</span> incStep;</div>
<div class="line"></div>
<div class="line">ISR(TIMER1_OVF_vect)</div>
<div class="line">{</div>
<div class="line"> display.<a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8">refresh</a>();</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> setup() {</div>
<div class="line"> display.<a class="code" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2">enableTimer1</a>();</div>
<div class="line"> display.<a class="code" href="classBitmap.html#a64d7a9651d5c385a044cc910a3b82837">setFont</a>(Mono5x7);</div>
<div class="line"> startGame();</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> drawSnake(<a class="code" href="classBitmap.html#a88d386944a7017aa776a177b10d8b2ba">Bitmap::Color</a> color) {</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> index = 0; index &lt; snakeLength; ++index)</div>
<div class="line"> display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114">setPixel</a>(snakeParts[index].x, snakeParts[index].y, color);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> loop() {</div>
<div class="line"> <span class="comment">// Handle the &quot;Game Over&quot; state. Any key press starts a new game.</span></div>
<div class="line"> <span class="keywordtype">int</span> cmd = ir.<a class="code" href="classIRreceiver.html#a4b021592a2b089dc2f1e138a38506fda">command</a>();</div>
<div class="line"> <span class="keywordflow">if</span> (gameOver) {</div>
<div class="line"> <span class="keywordflow">if</span> (cmd != -1 &amp;&amp; (cmd &amp; IRreceiver::AUTO_REPEAT) == 0)</div>
<div class="line"> startGame();</div>
<div class="line"> <span class="keywordflow">return</span>;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Pause the game if waiting for the first start. While waiting,</span></div>
<div class="line"> <span class="comment">// blink the location of the snake to help the player get their bearings.</span></div>
<div class="line"> <span class="keywordflow">if</span> (waitForStart) {</div>
<div class="line"> <span class="keywordflow">if</span> (cmd == -1) {</div>
<div class="line"> <span class="keywordflow">if</span> ((millis() - lastChange) &gt;= SNAKE_BLINK_TIME) {</div>
<div class="line"> snakeDrawn = !snakeDrawn;</div>
<div class="line"> drawSnake(snakeDrawn ? <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7">Bitmap::White</a> : <a class="code" href="classBitmap.html#a2c7faeeb89d3624b5bbca58871785adc">Bitmap::Black</a>);</div>
<div class="line"> lastChange += SNAKE_BLINK_TIME;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">return</span>;</div>
<div class="line"> }</div>
<div class="line"> drawSnake(<a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7">Bitmap::White</a>);</div>
<div class="line"> waitForStart = <span class="keyword">false</span>;</div>
<div class="line"> snakeDrawn = <span class="keyword">true</span>;</div>
<div class="line"> lastChange = millis();</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Process commands from the player.</span></div>
<div class="line"> <span class="keywordflow">switch</span> (cmd) {</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Both arrow keys and numbers can be used to control the direction,</span></div>
<div class="line"> <span class="comment">// in case the remote control does not have arrow keys.</span></div>
<div class="line"> <span class="keywordflow">case</span> RC5_LEFT: <span class="keywordflow">case</span> RC5_4:</div>
<div class="line"> changeDirection(-1, 0);</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">case</span> RC5_RIGHT: <span class="keywordflow">case</span> RC5_6:</div>
<div class="line"> changeDirection(1, 0);</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">case</span> RC5_UP: <span class="keywordflow">case</span> RC5_2:</div>
<div class="line"> changeDirection(0, -1);</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">case</span> RC5_DOWN: <span class="keywordflow">case</span> RC5_8:</div>
<div class="line"> changeDirection(0, 1);</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">case</span> RC5_PAUSE: <span class="keywordflow">case</span> RC5_PLAY: <span class="keywordflow">case</span> RC5_0:</div>
<div class="line"> <span class="comment">// Pause or resume the game.</span></div>
<div class="line"> paused = !paused;</div>
<div class="line"> lastChange = millis();</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">case</span> RC5_STOP: <span class="keywordflow">case</span> RC5_STANDBY:</div>
<div class="line"> <span class="comment">// Stop the game and start a new one.</span></div>
<div class="line"> startGame();</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Advance the snake position if not paused and the timeout has expired.</span></div>
<div class="line"> <span class="keywordflow">if</span> (!paused &amp;&amp; (millis() - lastChange) &gt;= SNAKE_ADVANCE_TIME) {</div>
<div class="line"> ++incStep;</div>
<div class="line"> advanceSnake(incStep &gt;= SNAKE_INC_STEPS);</div>
<div class="line"> lastChange += SNAKE_ADVANCE_TIME;</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> startGame() {</div>
<div class="line"> randomSeed(micros() + analogRead(A0)); <span class="comment">// Analog read adds some noise.</span></div>
<div class="line"> display.<a class="code" href="classBitmap.html#a839dc8fab05a5ebf7a6b2e61436b2fa1">clear</a>();</div>
<div class="line"> display.<a class="code" href="classBitmap.html#aac61e3f7f625db568e37d88b52b3b2fc">drawRect</a>(0, 0, display.<a class="code" href="classBitmap.html#a76c3b49e535761f07c553e7336daf523">width</a>() - 1, display.<a class="code" href="classBitmap.html#adcd4e3dc7594421e647b0f52da9a41a3">height</a>() - 1);</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> count = 0; count &lt; 10; ++count) {</div>
<div class="line"> <span class="keywordtype">int</span> x, y;</div>
<div class="line"> <span class="keywordflow">if</span> (random(0, 2) == 0) {</div>
<div class="line"> x = random(1, display.<a class="code" href="classBitmap.html#a76c3b49e535761f07c553e7336daf523">width</a>() - 5);</div>
<div class="line"> y = random(1, display.<a class="code" href="classBitmap.html#adcd4e3dc7594421e647b0f52da9a41a3">height</a>() - 1);</div>
<div class="line"> display.<a class="code" href="classBitmap.html#aa0a84f3694e343d68e7021552c69f767">drawLine</a>(x, y, x + 4, y);</div>
<div class="line"> } <span class="keywordflow">else</span> {</div>
<div class="line"> x = random(1, display.<a class="code" href="classBitmap.html#a76c3b49e535761f07c553e7336daf523">width</a>() - 1);</div>
<div class="line"> y = random(1, display.<a class="code" href="classBitmap.html#adcd4e3dc7594421e647b0f52da9a41a3">height</a>() - 3);</div>
<div class="line"> display.<a class="code" href="classBitmap.html#aa0a84f3694e343d68e7021552c69f767">drawLine</a>(x, y, x, y + 2);</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> paused = <span class="keyword">false</span>;</div>
<div class="line"> gameOver = <span class="keyword">false</span>;</div>
<div class="line"> waitForStart = <span class="keyword">true</span>;</div>
<div class="line"> snakeDrawn = <span class="keyword">true</span>;</div>
<div class="line"> lastChange = millis();</div>
<div class="line"> direction.x = 1;</div>
<div class="line"> direction.y = 0;</div>
<div class="line"> incStep = 0;</div>
<div class="line"> snakeLength = SNAKE_START_LENGTH;</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> index = 0; index &lt; snakeLength; ++index) {</div>
<div class="line"> snakeParts[index].x = 3 + index;</div>
<div class="line"> snakeParts[index].y = 4;</div>
<div class="line"> display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114">setPixel</a></div>
<div class="line"> (snakeParts[index].x, snakeParts[index].y, <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7">Bitmap::White</a>);</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> changeDirection(<span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y) {</div>
<div class="line"> direction.x = x;</div>
<div class="line"> direction.y = y;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> advanceSnake(<span class="keywordtype">bool</span> increase) {</div>
<div class="line"> <span class="keywordtype">int</span> x = snakeParts[snakeLength - 1].x + direction.x;</div>
<div class="line"> <span class="keywordtype">int</span> y = snakeParts[snakeLength - 1].y + direction.y;</div>
<div class="line"> <span class="keywordflow">if</span> (display.<a class="code" href="classBitmap.html#a35aa38b377d509d6c4f061a0b988d203">pixel</a>(x, y) == <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7">Bitmap::White</a>) {</div>
<div class="line"> gameOver = <span class="keyword">true</span>;</div>
<div class="line"> display.<a class="code" href="classBitmap.html#a839dc8fab05a5ebf7a6b2e61436b2fa1">clear</a>();</div>
<div class="line"> display.<a class="code" href="classBitmap.html#a3e9bcbfb584d5020bd6f0a313ee275f0">drawText</a>(5, 0, <span class="stringliteral">&quot;Game&quot;</span>);</div>
<div class="line"> display.<a class="code" href="classBitmap.html#a3e9bcbfb584d5020bd6f0a313ee275f0">drawText</a>(3, 8, <span class="stringliteral">&quot;Over!&quot;</span>);</div>
<div class="line"> <span class="keywordflow">return</span>;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">if</span> (!increase || snakeLength &gt;= SNAKE_MAX_LENGTH) {</div>
<div class="line"> display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114">setPixel</a>(snakeParts[0].x, snakeParts[0].y, <a class="code" href="classBitmap.html#a2c7faeeb89d3624b5bbca58871785adc">Bitmap::Black</a>);</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> index = 0; index &lt; snakeLength - 1; ++index)</div>
<div class="line"> snakeParts[index] = snakeParts[index + 1];</div>
<div class="line"> } <span class="keywordflow">else</span> {</div>
<div class="line"> ++snakeLength;</div>
<div class="line"> }</div>
<div class="line"> snakeParts[snakeLength - 1].x = x;</div>
<div class="line"> snakeParts[snakeLength - 1].y = y;</div>
<div class="line"> display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114">setPixel</a>(x, y, <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7">Bitmap::White</a>);</div>
<div class="line"> <span class="keywordflow">if</span> (increase)</div>
<div class="line"> incStep = 0;</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Sat Feb 13 2016 07:01:07 for ArduinoLibs by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.6
</small></address>
</body>
</html>