mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
270 lines
18 KiB
HTML
270 lines
18 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"/>
|
|
<title>ArduinoLibs: Snake Video Game Using an Infrared Remote Control</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body onload='searchBox.OnSelectItem(0);'>
|
|
<!-- Generated by Doxygen 1.7.4 -->
|
|
<script type="text/javascript"><!--
|
|
var searchBox = new SearchBox("searchBox", "search",false,'Search');
|
|
--></script>
|
|
<div id="top">
|
|
<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>
|
|
<div id="navrow1" class="tabs">
|
|
<ul class="tablist">
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li class="current"><a href="pages.html"><span>Related 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 id="searchli">
|
|
<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>
|
|
</div>
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">Snake Video Game Using an Infrared Remote Control </div> </div>
|
|
</div>
|
|
<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'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"><pre class="fragment"><span class="comment">/*</span>
|
|
<span class="comment"> * Copyright (C) 2012 Southern Storm Software, Pty Ltd.</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</span>
|
|
<span class="comment"> * copy of this software and associated documentation files (the "Software"),</span>
|
|
<span class="comment"> * to deal in the Software without restriction, including without limitation</span>
|
|
<span class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</span>
|
|
<span class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</span>
|
|
<span class="comment"> * Software is furnished to do so, subject to the following conditions:</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * The above copyright notice and this permission notice shall be included</span>
|
|
<span class="comment"> * in all copies or substantial portions of the Software.</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS</span>
|
|
<span class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
|
|
<span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
|
|
<span class="comment"> * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
|
|
<span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</span>
|
|
<span class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</span>
|
|
<span class="comment"> * DEALINGS IN THE SOFTWARE.</span>
|
|
<span class="comment"> */</span>
|
|
|
|
<span class="preprocessor">#include <IRreceiver.h></span>
|
|
<span class="preprocessor">#include <DMD.h></span>
|
|
<span class="preprocessor">#include <Mono5x7.h></span>
|
|
|
|
<span class="preprocessor">#define SNAKE_ADVANCE_TIME 150</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define SNAKE_BLINK_TIME 500</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define SNAKE_INC_STEPS 5</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define SNAKE_MAX_LENGTH 50</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define SNAKE_START_LENGTH 10</span>
|
|
<span class="preprocessor"></span>
|
|
<span class="keyword">struct </span>Point
|
|
{
|
|
<span class="keywordtype">int</span> x, y;
|
|
};
|
|
|
|
<a class="code" href="classDMD.html" title="Handle large dot matrix displays composed of LED's.">DMD</a> display;
|
|
<a class="code" href="classIRreceiver.html" title="Manages the reception of RC-5 commands from an infrared remote control.">IRreceiver</a> ir;
|
|
<span class="keywordtype">bool</span> paused;
|
|
<span class="keywordtype">bool</span> gameOver;
|
|
<span class="keywordtype">bool</span> waitForStart;
|
|
<span class="keywordtype">bool</span> snakeDrawn;
|
|
<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> lastChange;
|
|
Point direction;
|
|
Point snakeParts[SNAKE_MAX_LENGTH];
|
|
<span class="keywordtype">int</span> snakeLength;
|
|
<span class="keywordtype">int</span> incStep;
|
|
|
|
ISR(TIMER1_OVF_vect)
|
|
{
|
|
display.<a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8" title="Refresh the display.">refresh</a>();
|
|
}
|
|
|
|
<span class="keywordtype">void</span> setup() {
|
|
display.<a class="code" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2" title="Enables Timer1 overflow interrupts for updating this display.">enableTimer1</a>();
|
|
display.<a class="code" href="classBitmap.html#abf7adfceb267080aa9806388e96358c4" title="Sets the font for use with drawText() and drawChar().">setFont</a>(Mono5x7);
|
|
startGame();
|
|
}
|
|
|
|
<span class="keywordtype">void</span> drawSnake(<a class="code" href="classBitmap.html#a88d386944a7017aa776a177b10d8b2ba" title="Type that represents the color of a pixel in a bitmap.">Bitmap::Color</a> color) {
|
|
<span class="keywordflow">for</span> (<span class="keywordtype">int</span> index = 0; index < snakeLength; ++index)
|
|
display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114" title="Sets the pixel at (x, y) to color.">setPixel</a>(snakeParts[index].x, snakeParts[index].y, color);
|
|
}
|
|
|
|
<span class="keywordtype">void</span> loop() {
|
|
<span class="comment">// Handle the "Game Over" state. Any key press starts a new game.</span>
|
|
<span class="keywordtype">int</span> cmd = ir.<a class="code" href="classIRreceiver.html#a4b021592a2b089dc2f1e138a38506fda" title="Returns the next command from the remote control.">command</a>();
|
|
<span class="keywordflow">if</span> (gameOver) {
|
|
<span class="keywordflow">if</span> (cmd != -1 && (cmd & IRreceiver::AUTO_REPEAT) == 0)
|
|
startGame();
|
|
<span class="keywordflow">return</span>;
|
|
}
|
|
|
|
<span class="comment">// Pause the game if waiting for the first start. While waiting,</span>
|
|
<span class="comment">// blink the location of the snake to help the player get their bearings.</span>
|
|
<span class="keywordflow">if</span> (waitForStart) {
|
|
<span class="keywordflow">if</span> (cmd == -1) {
|
|
<span class="keywordflow">if</span> ((millis() - lastChange) >= SNAKE_BLINK_TIME) {
|
|
snakeDrawn = !snakeDrawn;
|
|
drawSnake(snakeDrawn ? <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7" title="Color value corresponding to "white". If the bitmap is displayed on a LED array, then it may have a d...">Bitmap::White</a> : <a class="code" href="classBitmap.html#a2c7faeeb89d3624b5bbca58871785adc" title="Color value corresponding to "black".">Bitmap::Black</a>);
|
|
lastChange += SNAKE_BLINK_TIME;
|
|
}
|
|
<span class="keywordflow">return</span>;
|
|
}
|
|
drawSnake(<a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7" title="Color value corresponding to "white". If the bitmap is displayed on a LED array, then it may have a d...">Bitmap::White</a>);
|
|
waitForStart = <span class="keyword">false</span>;
|
|
snakeDrawn = <span class="keyword">true</span>;
|
|
lastChange = millis();
|
|
}
|
|
|
|
<span class="comment">// Process commands from the player.</span>
|
|
<span class="keywordflow">switch</span> (cmd) {
|
|
|
|
<span class="comment">// Both arrow keys and numbers can be used to control the direction,</span>
|
|
<span class="comment">// in case the remote control does not have arrow keys.</span>
|
|
<span class="keywordflow">case</span> RC5_LEFT: <span class="keywordflow">case</span> RC5_4:
|
|
changeDirection(-1, 0);
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> RC5_RIGHT: <span class="keywordflow">case</span> RC5_6:
|
|
changeDirection(1, 0);
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> RC5_UP: <span class="keywordflow">case</span> RC5_2:
|
|
changeDirection(0, -1);
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> RC5_DOWN: <span class="keywordflow">case</span> RC5_8:
|
|
changeDirection(0, 1);
|
|
<span class="keywordflow">break</span>;
|
|
|
|
<span class="keywordflow">case</span> RC5_PAUSE: <span class="keywordflow">case</span> RC5_PLAY: <span class="keywordflow">case</span> RC5_0:
|
|
<span class="comment">// Pause or resume the game.</span>
|
|
paused = !paused;
|
|
lastChange = millis();
|
|
<span class="keywordflow">break</span>;
|
|
|
|
<span class="keywordflow">case</span> RC5_STOP: <span class="keywordflow">case</span> RC5_STANDBY:
|
|
<span class="comment">// Stop the game and start a new one.</span>
|
|
startGame();
|
|
<span class="keywordflow">break</span>;
|
|
}
|
|
|
|
<span class="comment">// Advance the snake position if not paused and the timeout has expired.</span>
|
|
<span class="keywordflow">if</span> (!paused && (millis() - lastChange) >= SNAKE_ADVANCE_TIME) {
|
|
++incStep;
|
|
advanceSnake(incStep >= SNAKE_INC_STEPS);
|
|
lastChange += SNAKE_ADVANCE_TIME;
|
|
}
|
|
}
|
|
|
|
<span class="keywordtype">void</span> startGame() {
|
|
randomSeed(micros() + analogRead(A0)); <span class="comment">// Analog read adds some noise.</span>
|
|
display.<a class="code" href="classBitmap.html#a839dc8fab05a5ebf7a6b2e61436b2fa1" title="Clears the entire bitmap to the specified color.">clear</a>();
|
|
display.<a class="code" href="classBitmap.html#aac61e3f7f625db568e37d88b52b3b2fc" title="Draws a rectangle from (x1, y1) to (x2, y2), with the outline in borderColor and the interior filled ...">drawRect</a>(0, 0, display.<a class="code" href="classBitmap.html#a76c3b49e535761f07c553e7336daf523" title="Returns the width of the bitmap in pixels.">width</a>() - 1, display.<a class="code" href="classBitmap.html#adcd4e3dc7594421e647b0f52da9a41a3" title="Returns the height of the bitmap in pixels.">height</a>() - 1);
|
|
<span class="keywordflow">for</span> (<span class="keywordtype">int</span> count = 0; count < 10; ++count) {
|
|
<span class="keywordtype">int</span> x, y;
|
|
<span class="keywordflow">if</span> (random(0, 2) == 0) {
|
|
x = random(1, display.<a class="code" href="classBitmap.html#a76c3b49e535761f07c553e7336daf523" title="Returns the width of the bitmap in pixels.">width</a>() - 5);
|
|
y = random(1, display.<a class="code" href="classBitmap.html#adcd4e3dc7594421e647b0f52da9a41a3" title="Returns the height of the bitmap in pixels.">height</a>() - 1);
|
|
display.<a class="code" href="classBitmap.html#aa0a84f3694e343d68e7021552c69f767" title="Draws a line from (x1, y1) to (x2, y2) in color.">drawLine</a>(x, y, x + 4, y);
|
|
} <span class="keywordflow">else</span> {
|
|
x = random(1, display.<a class="code" href="classBitmap.html#a76c3b49e535761f07c553e7336daf523" title="Returns the width of the bitmap in pixels.">width</a>() - 1);
|
|
y = random(1, display.<a class="code" href="classBitmap.html#adcd4e3dc7594421e647b0f52da9a41a3" title="Returns the height of the bitmap in pixels.">height</a>() - 3);
|
|
display.<a class="code" href="classBitmap.html#aa0a84f3694e343d68e7021552c69f767" title="Draws a line from (x1, y1) to (x2, y2) in color.">drawLine</a>(x, y, x, y + 2);
|
|
}
|
|
}
|
|
paused = <span class="keyword">false</span>;
|
|
gameOver = <span class="keyword">false</span>;
|
|
waitForStart = <span class="keyword">true</span>;
|
|
snakeDrawn = <span class="keyword">true</span>;
|
|
lastChange = millis();
|
|
direction.x = 1;
|
|
direction.y = 0;
|
|
incStep = 0;
|
|
snakeLength = SNAKE_START_LENGTH;
|
|
<span class="keywordflow">for</span> (<span class="keywordtype">int</span> index = 0; index < snakeLength; ++index) {
|
|
snakeParts[index].x = 3 + index;
|
|
snakeParts[index].y = 4;
|
|
display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114" title="Sets the pixel at (x, y) to color.">setPixel</a>
|
|
(snakeParts[index].x, snakeParts[index].y, <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7" title="Color value corresponding to "white". If the bitmap is displayed on a LED array, then it may have a d...">Bitmap::White</a>);
|
|
}
|
|
}
|
|
|
|
<span class="keywordtype">void</span> changeDirection(<span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y) {
|
|
direction.x = x;
|
|
direction.y = y;
|
|
}
|
|
|
|
<span class="keywordtype">void</span> advanceSnake(<span class="keywordtype">bool</span> increase) {
|
|
<span class="keywordtype">int</span> x = snakeParts[snakeLength - 1].x + direction.x;
|
|
<span class="keywordtype">int</span> y = snakeParts[snakeLength - 1].y + direction.y;
|
|
<span class="keywordflow">if</span> (display.<a class="code" href="classBitmap.html#a35aa38b377d509d6c4f061a0b988d203" title="Returns the color of the pixel at (x, y); either Black or White.">pixel</a>(x, y) == <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7" title="Color value corresponding to "white". If the bitmap is displayed on a LED array, then it may have a d...">Bitmap::White</a>) {
|
|
gameOver = <span class="keyword">true</span>;
|
|
display.<a class="code" href="classBitmap.html#a839dc8fab05a5ebf7a6b2e61436b2fa1" title="Clears the entire bitmap to the specified color.">clear</a>();
|
|
display.<a class="code" href="classBitmap.html#a3e9bcbfb584d5020bd6f0a313ee275f0" title="Draws the len characters of str at (x, y).">drawText</a>(5, 0, <span class="stringliteral">"Game"</span>);
|
|
display.<a class="code" href="classBitmap.html#a3e9bcbfb584d5020bd6f0a313ee275f0" title="Draws the len characters of str at (x, y).">drawText</a>(3, 8, <span class="stringliteral">"Over!"</span>);
|
|
<span class="keywordflow">return</span>;
|
|
}
|
|
<span class="keywordflow">if</span> (!increase || snakeLength >= SNAKE_MAX_LENGTH) {
|
|
display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114" title="Sets the pixel at (x, y) to color.">setPixel</a>(snakeParts[0].x, snakeParts[0].y, <a class="code" href="classBitmap.html#a2c7faeeb89d3624b5bbca58871785adc" title="Color value corresponding to "black".">Bitmap::Black</a>);
|
|
<span class="keywordflow">for</span> (<span class="keywordtype">int</span> index = 0; index < snakeLength - 1; ++index)
|
|
snakeParts[index] = snakeParts[index + 1];
|
|
} <span class="keywordflow">else</span> {
|
|
++snakeLength;
|
|
}
|
|
snakeParts[snakeLength - 1].x = x;
|
|
snakeParts[snakeLength - 1].y = y;
|
|
display.<a class="code" href="classBitmap.html#aac994b75418e7d37ec66829437329114" title="Sets the pixel at (x, y) to color.">setPixel</a>(x, y, <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7" title="Color value corresponding to "white". If the bitmap is displayed on a LED array, then it may have a d...">Bitmap::White</a>);
|
|
<span class="keywordflow">if</span> (increase)
|
|
incStep = 0;
|
|
}
|
|
</pre></div> </div></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"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</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>
|
|
|
|
<hr class="footer"/><address class="footer"><small>Generated on Tue Jun 12 2012 11:29:25 for ArduinoLibs by 
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
|
|
</body>
|
|
</html>
|