Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 7429

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- rogue.qdoc -->
  <title>Qt 4.8: Rogue Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Rogue Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#window-class-definition">Window Class Definition</a></li>
<li class="level1"><a href="#window-class-implementation">Window Class Implementation</a></li>
<li class="level1"><a href="#the-movementtransition-class">The MovementTransition Class</a></li>
<li class="level1"><a href="#the-roguelike-tradition">The Roguelike Tradition</a></li>
</ul>
</div>
<h1 class="title">Rogue Example</h1>
<span class="subtitle"></span>
<!-- $$$statemachine/rogue-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="statemachine-rogue-movementtransition-h.html">statemachine/rogue/movementtransition.h</a></li>
<li><a href="statemachine-rogue-window-cpp.html">statemachine/rogue/window.cpp</a></li>
<li><a href="statemachine-rogue-window-h.html">statemachine/rogue/window.h</a></li>
<li><a href="statemachine-rogue-main-cpp.html">statemachine/rogue/main.cpp</a></li>
<li><a href="statemachine-rogue-rogue-pro.html">statemachine/rogue/rogue.pro</a></li>
</ul>
<p>The Rogue example shows how to use the Qt state machine for event handling.<p class="centerAlign"><img src="images/rogue-example.png" alt="" /></p><p>This example implements a simple text based game. Do you see the <tt>@</tt> in the screenshot? That's you, the rogue. The <tt>#</tt> characters are walls, and the dots represent floor. In a real game, other ASCII characters would represent all kinds of objects and creatures, for instance, ancient dragons (<tt>D</tt>s) or food rations (<tt>%</tt>s). But let's not get carried away. In this game, the rogue is simply running around in an empty room.</p>
<p>The rogue is moved with the keypad (2, 4, 8, 6). That aside, we have implemented a <tt>quit</tt> command that triggers if the player types <tt>q</tt>. The player is then asked if he/she really wants to quit.</p>
<p>Most games have commands that need more than one key press (we think of consecutive presses, i.e&#x2e;, not of several keys being pressed at the same time). In this game, only the <tt>quit</tt> command falls under this category, but for the sake of argument, let's imagine a fully-fledged game with a rich set of commands. If we were to implement these by catching key events in <a href="qwidget.html#keyPressEvent">keyPressEvent()</a>, we would have to keep a lot of class member variables to track the sequence of keys already typed (or find some other way of deducing the current state of a command). This can easily lead to spaghetti, which is--as we all well know, I'm sure--unpleasant. With a state machine, on the other hand, separate states can wait for a single key press, and that makes our lives a lot simpler.</p>
<p>The example consists of two classes:</p>
<ul>
<li><tt>Window</tt> draws the text display of the game and sets up the state machine. The window also has a status bar above the area in which the rouge moves.</li>
<li><tt>MovementTransition</tt> is a transition that carries out a single move of the rogue.</li>
</ul>
<p>Before we embark on a code walkthrough, it is necessary to take a closer look at the design of the machine. Here is a state chart that shows what we want to achieve:</p>
<p class="centerAlign"><img src="images/rogue-statechart.png" alt="" /></p><p>The input state waits for a key press to start a new command. When receiving a key it recognizes, it transitions to one of the two commands of the game; though, as we will see, movement is handled by the transition itself. The quit state waits for the player to answer yes or no (by typing <tt>y</tt> or <tt>n</tt>) when asked whether he/she really wants to quit the game.</p>
<p>The chart demonstrates how we use one state to wait for a single key press. The press received may trigger one of the transitions connected to the state.</p>
<a name="window-class-definition"></a>
<h2>Window Class Definition</h2>
<p>The <tt>Window</tt> class is a widget that draws the text display of the game. It also sets up the state machine, i.e&#x2e;, creates and connects the states in the machine. It is the key events from this widget that are used by the machine.</p>
<pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
     Q_OBJECT
     Q_PROPERTY(<span class="type"><a href="qstring.html">QString</a></span> status READ status WRITE setStatus)

 <span class="keyword">public</span>:
     <span class="keyword">enum</span> Direction { Up<span class="operator">,</span> Down<span class="operator">,</span> Left<span class="operator">,</span> Right };

     Window();

     <span class="type">void</span> movePlayer(Direction direction);
     <span class="type">void</span> setStatus(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>status);
     <span class="type"><a href="qstring.html">QString</a></span> status() <span class="keyword">const</span>;

     <span class="type"><a href="qsize.html">QSize</a></span> sizeHint() <span class="keyword">const</span>;

 <span class="keyword">protected</span>:
     <span class="type">void</span> paintEvent(<span class="type"><a href="qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>event);</pre>
<p><tt>Direction</tt> specifies the direction in which the rogue is to move. We use this in <tt>movePlayer()</tt>, which moves the rogue and repaints the window. The game has a status line above the area in which the rogue moves. The <tt>status</tt> property contains the text of this line. We use a property because the <a href="qstate.html">QState</a> class allows setting any Qt <a href="properties.html#qt-s-property-system">property</a> when entered. More on this later.</p>
<pre class="cpp"> <span class="keyword">private</span>:
     <span class="type">void</span> buildMachine();
     <span class="type">void</span> setupMap();

     <span class="type"><a href="qchar.html">QChar</a></span> map<span class="operator">[</span>WIDTH<span class="operator">]</span><span class="operator">[</span>HEIGHT<span class="operator">]</span>;
     <span class="type">int</span> pX<span class="operator">,</span> pY;

     <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> <span class="operator">*</span>machine;
     <span class="type"><a href="qstring.html">QString</a></span> myStatus;
 };</pre>
<p>The <tt>map</tt> is an array with the characters that are currently displayed. We set up the array in <tt>setupMap()</tt>, and update it when the rogue is moved. <tt>pX</tt> and <tt>pY</tt> is the current position of the rogue. <tt>WIDTH</tt> and <tt>HEIGHT</tt> are macros specifying the dimensions of the map.</p>
<p>The <tt>paintEvent()</tt> function is left out of this walkthrough. We also do not discuss other code that does not concern the state machine (the <tt>setupMap()</tt>, <tt>status()</tt>, <tt>setStatus()</tt>, <tt>movePlayer()</tt>, and <tt>sizeHint()</tt> functions). If you wish to take a look at the code, click on the link for the <tt>window.cpp</tt> file at the top of this page.</p>
<a name="window-class-implementation"></a>
<h2>Window Class Implementation</h2>
<p>Here is the constructor of <tt>Window</tt>:</p>
<pre class="cpp"> Window<span class="operator">::</span>Window()
 {
     pX <span class="operator">=</span> <span class="number">5</span>;
     pY <span class="operator">=</span> <span class="number">5</span>;
     ...
     setupMap();
     buildMachine();
 }</pre>
<p>The player starts off at position (5, 5). We then set up the map and statemachine. Let's proceed with the <tt>buildMachine()</tt> function:</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>buildMachine()
 {
     machine <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstatemachine.html">QStateMachine</a></span>;

     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>inputState <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(machine);
     inputState<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(<span class="keyword">this</span><span class="operator">,</span> <span class="string">&quot;status&quot;</span><span class="operator">,</span> <span class="string">&quot;Move the rogue with 2, 4, 6, and 8&quot;</span>);

     MovementTransition <span class="operator">*</span>transition <span class="operator">=</span> <span class="keyword">new</span> MovementTransition(<span class="keyword">this</span>);
     inputState<span class="operator">-</span><span class="operator">&gt;</span>addTransition(transition);</pre>
<p>We enter <tt>inputState</tt> when the machine is started and from the <tt>quitState</tt> if the user wants to continue playing. We then set the status to a helpful reminder of how to play the game.</p>
<p>First, the <tt>Movement</tt> transition is added to the input state. This will enable the rogue to be moved with the keypad. Notice that we don't set a target state for the movement transition. This will cause the transition to be triggered (and the <a href="qabstracttransition.html#onTransition">onTransition()</a> function to be invoked), but the machine will not leave the <tt>inputState</tt>. If we had set <tt>inputState</tt> as the target state, we would first have left and then entered the <tt>inputState</tt> again.</p>
<pre class="cpp">     <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>quitState <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>(machine);
     quitState<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(<span class="keyword">this</span><span class="operator">,</span> <span class="string">&quot;status&quot;</span><span class="operator">,</span> <span class="string">&quot;Really quit(y/n)?&quot;</span>);

     <span class="type"><a href="qkeyeventtransition.html">QKeyEventTransition</a></span> <span class="operator">*</span>yesTransition <span class="operator">=</span> <span class="keyword">new</span>
         <span class="type"><a href="qkeyeventtransition.html">QKeyEventTransition</a></span>(<span class="keyword">this</span><span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Y);
     yesTransition<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(<span class="keyword">new</span> <span class="type"><a href="qfinalstate.html">QFinalState</a></span>(machine));
     quitState<span class="operator">-</span><span class="operator">&gt;</span>addTransition(yesTransition);

     <span class="type"><a href="qkeyeventtransition.html">QKeyEventTransition</a></span> <span class="operator">*</span>noTransition <span class="operator">=</span>
         <span class="keyword">new</span> <span class="type"><a href="qkeyeventtransition.html">QKeyEventTransition</a></span>(<span class="keyword">this</span><span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_N);
     noTransition<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(inputState);
     quitState<span class="operator">-</span><span class="operator">&gt;</span>addTransition(noTransition);</pre>
<p>When we enter <tt>quitState</tt>, we update the status bar of the window.</p>
<p><tt>QKeyEventTransition</tt> is a utility class that removes the hassle of implementing transitions for <a href="qkeyevent.html">QKeyEvent</a>s. We simply need to specify the key on which the transition should trigger and the target state of the transition.</p>
<pre class="cpp">     <span class="type"><a href="qkeyeventtransition.html">QKeyEventTransition</a></span> <span class="operator">*</span>quitTransition <span class="operator">=</span>
         <span class="keyword">new</span> <span class="type"><a href="qkeyeventtransition.html">QKeyEventTransition</a></span>(<span class="keyword">this</span><span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Q);
     quitTransition<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(quitState);
     inputState<span class="operator">-</span><span class="operator">&gt;</span>addTransition(quitTransition);</pre>
<p>The transition from <tt>inputState</tt> allows triggering the quit state when the player types <tt>q</tt>.</p>
<pre class="cpp">     machine<span class="operator">-</span><span class="operator">&gt;</span>setInitialState(inputState);

     connect(machine<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> qApp<span class="operator">,</span> SLOT(quit()));

     machine<span class="operator">-</span><span class="operator">&gt;</span>start();
 }</pre>
<p>The machine is set up, so it's time to start it.</p>
<a name="the-movementtransition-class"></a>
<h2>The MovementTransition Class</h2>
<p><tt>MovementTransition</tt> is triggered when the player request the rogue to be moved (by typing 2, 4, 6, or 8) when the machine is in the <tt>inputState</tt>.</p>
<pre class="cpp"> <span class="keyword">class</span> MovementTransition : <span class="keyword">public</span> <span class="type"><a href="qeventtransition.html">QEventTransition</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     MovementTransition(Window <span class="operator">*</span>window) :
         <span class="type"><a href="qeventtransition.html">QEventTransition</a></span>(window<span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress) {
         <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>window <span class="operator">=</span> window;
     }</pre>
<p>In the constructor, we tell <a href="qeventtransition.html">QEventTransition</a> to only send <a href="qevent.html#Type-enum">KeyPress</a> events to the <a href="qabstracttransition.html#eventTest">eventTest()</a> function:</p>
<pre class="cpp"> <span class="keyword">protected</span>:
     <span class="type">bool</span> eventTest(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>event) {
         <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>StateMachineWrapped <span class="operator">&amp;</span><span class="operator">&amp;</span>
             <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qstatemachine.html">QStateMachine</a></span><span class="operator">::</span>WrappedEvent <span class="operator">*</span><span class="operator">&gt;</span>(event)<span class="operator">-</span><span class="operator">&gt;</span>event()<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress) {
             <span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>wrappedEvent <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qstatemachine.html">QStateMachine</a></span><span class="operator">::</span>WrappedEvent <span class="operator">*</span><span class="operator">&gt;</span>(event)<span class="operator">-</span><span class="operator">&gt;</span>event();

             <span class="type"><a href="qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span>keyEvent <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(wrappedEvent);
             <span class="type">int</span> key <span class="operator">=</span> keyEvent<span class="operator">-</span><span class="operator">&gt;</span>key();

             <span class="keyword">return</span> key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_2 <span class="operator">|</span><span class="operator">|</span> key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_8 <span class="operator">|</span><span class="operator">|</span> key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_6 <span class="operator">|</span><span class="operator">|</span>
                    key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_4 <span class="operator">|</span><span class="operator">|</span> key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Down <span class="operator">|</span><span class="operator">|</span> key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Up <span class="operator">|</span><span class="operator">|</span>
                    key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Right <span class="operator">|</span><span class="operator">|</span> key <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Left;
         }
         <span class="keyword">return</span> <span class="keyword">false</span>;
     }</pre>
<p>The KeyPress events come wrapped in <a href="qstatemachine-wrappedevent.html">QStateMachine::WrappedEvent</a>s. <tt>event</tt> must be confirmed to be a wrapped event because Qt uses other events internally. After that, it is simply a matter of checking which key has been pressed.</p>
<p>Let's move on to the <tt>onTransition()</tt> function:</p>
<pre class="cpp">     <span class="type">void</span> onTransition(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>event) {
         <span class="type"><a href="qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span>keyEvent <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(
             <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qstatemachine.html">QStateMachine</a></span><span class="operator">::</span>WrappedEvent <span class="operator">*</span><span class="operator">&gt;</span>(event)<span class="operator">-</span><span class="operator">&gt;</span>event());

         <span class="type">int</span> key <span class="operator">=</span> keyEvent<span class="operator">-</span><span class="operator">&gt;</span>key();
         <span class="keyword">switch</span> (key) {
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Left:
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_4:
                 window<span class="operator">-</span><span class="operator">&gt;</span>movePlayer(Window<span class="operator">::</span>Left);
                 <span class="keyword">break</span>;
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Up:
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_8:
                 window<span class="operator">-</span><span class="operator">&gt;</span>movePlayer(Window<span class="operator">::</span>Up);
                 <span class="keyword">break</span>;
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Right:
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_6:
                 window<span class="operator">-</span><span class="operator">&gt;</span>movePlayer(Window<span class="operator">::</span>Right);
                 <span class="keyword">break</span>;
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Down:
             <span class="keyword">case</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_2:
                 window<span class="operator">-</span><span class="operator">&gt;</span>movePlayer(Window<span class="operator">::</span>Down);
                 <span class="keyword">break</span>;
             <span class="keyword">default</span>:
                 ;
         }
     }</pre>
<p>When <tt>onTransition()</tt> is invoked, we know that we have a <a href="qevent.html#Type-enum">KeyPress</a> event with 2, 4, 6, or 8, and can ask <tt>Window</tt> to move the player.</p>
<a name="the-roguelike-tradition"></a>
<h2>The Roguelike Tradition</h2>
<p>You might have been wondering why the game features a rogue. Well, these kinds of text based dungeon exploration games date back to a game called, yes, &quot;Rogue&quot;. Although outflanked by the technology of modern 3D computer games, roguelikes have a solid community of hard-core, devoted followers.</p>
<p>Playing these games can be surprisingly addictive (despite the lack of graphics). Angband, the perhaps most well-known rougelike, is found here: <a href="http://rephial.org/">http://rephial.org/</a>.</p>
</div>
<!-- @@@statemachine/rogue -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>