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

📄 daserstespieleng.html

📁 java tutorial all about java game design
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  </ul>
  <p align="justify">In the last step of the method we are testing, if the distance, we calculated in the step before is smaller than a certain value (for example the radius of the ball). I have choosen 15 as a good value although our ball has only the radius 10. But that has no special reason, it just seemed to fit best! </p>
  <ul>
    <em>// If distance is smaller than 15, player has hit the ball <br>
  if (distance &lt; 15) <br>
  { </em>
    <ul>
      <em> player.addScore (10* Math.abs(x_speed) + 10); <br>
    return true; <br>
      </em>
    </ul>
    <em>} <br>
  else return false; <br>
    </em>
  </ul>
  <p align="justify">Now we are able to hit the ball by clicking on it with the mouse pointer. </p>
  <h3>The player- object: Count score and loose lifes </h3>
  <p align="justify">To count the score and loose lifes, we added the methods looseLife() and addScore (int plus) to our player class. Everytime a ball leaves the applet, isOut() calles player.looseLife(), that way the player looses one life. If the player hits a ball, the method userHit() calls player.addScore (10* Math.abs(x_speed) + 10) and adds the score to the score in the player object (the faster the ball has been, the more points the player gets). The methods addScore and looseLife are really simple. <br>
  But there is one important point: The player object is initialized in the Main class! This means that the refference to the player object has to be initialized in the ball object too, to make it possible, that the ball object calls methods of the player object!! <br>
  This is a very important technique because it happens quite often that more than one class needs a refference to a special object. But it means, one has to initialize the object in one class (I'm using the Main class to manage all game objects) and has to give the reference to this object, which is needed in more classes, to the other classes! </p>
  <h3>Change the mouse pointer to a crosshair cursor </h3>
  <p align="justify">In this game it would look much better to have a crosshair mouse pointer than a normal mouse pointer. To get such a cursor, we have to add three lines of code to the Main class of our applet: First of all a instance variable of the class cursor: </p>
  <ul>
    <em>// Crosshair cursor <br>
  Cursor c; <br>
    </em>
  </ul>
  <p align="justify">Now we add the following lines to the init() method: </p>
  <ul>
    <em>// generate a Crosshair cursor <br>
  c = new Cursor (Cursor.CROSSHAIR_CURSOR); <br>
  // set this cursor as the standard cursor of the applet <br>
  this.setCursor (c); <br>
    </em>
  </ul>
  <p align="justify">You can find other mouse pointers in the Java API </p>
  <h3>Start the game with a double click on it <br>
    and finish it if player lost all his lifes: </h3>
  <p align="justify">Now we have almost done it, there is just one thing left to do: We want to start the game when the player clicked two times on the applet (double click) and not earlier. The advantage of this is not only that the player can decide when to start the game but in games where we need the keyboard, we can get the keyboard focus for this applet very easy this way. And of course the game shall be finished if the player lost all of his lifes! </p>
  <p align="justify">As a first step we add a boolean instance variable called isStoped to our Main class. If the value of isStoped is true, our game is not running, if the value is false, the game is running! Now we add a test to the run - method of the main class, which tests if the game is running (isStoped = false) and if the player has more than 0 lifes. If these two things are alright, the two balls are moved. </p>
  <ul>
    <em> // run - method <br>
  while (true) <br>
  { </em>
    <ul>
      <em> if (player.getLives() &gt;= 0 &amp;&amp; !isStoped) <br>
    { </em>
      <ul>
        <em> redball.move(); <br>
      blueball.move(); <br>
        </em>
      </ul>
      <em>} <br>
      <br>
    ... <br>
      </em>
    </ul>
    <em>} </em>
  </ul>
  <p align="justify">In the next step we'll add a second test to the paint() - method of the applet. As long as the player has lifes left, this method paints the two balls, the score and the lifes of the player. If the game is stoped it paints also the information on the screen, that the game can be started with a double click. If the player lost all of his lifes, the paint method writes out the final score and the information, that the game can be restarted by double clicking on the applet. The paint method looks like this: </p>
  <ul>
    <em>// paint() - method <br>
  public void paint (Graphics g) <br>
  { </em>
    <ul>
      <em> // player has lifes left <br>
    if (player.getLives() &gt;= 0) <br>
    { </em>
      <ul>
        <em> // Paint the two balls, score ... <br>
        <br>
      ... <br>
      <br>
      // If game is stopped <br>
      if (isStoped) <br>
      { </em>
        <ul>
          <em> // paint information: "Start game with a double click" <br>
          </em>
        </ul>
        <em>} </em>
      </ul>
      <em>} <br>
    // Player has no lifes left <br>
    else if (player.getLiver() &lt; 0) <br>
    { </em>
      <ul>
        <em> // paint final score, set isStopped true... <br>
      // for details see sourcecode! </em>
      </ul>
      <em>} </em>
    </ul>
    <em>} </em>
  </ul>
  <p align="justify">We have almost made it, but...! At the moment we can't switch between the two game states (game stopped, game running) because we have no way to control the value of isStoped. To get this control, we have to add the following code to the mouseDown - method: </p>
  <ul>
    <em>// listen to mouse clicks <br>
  public void mouseDown (Event e, int x, int y) <br>
  { </em>
    <ul>
      <em> // handle mouse events when game is running <br>
    if (!isStoped) <br>
    { </em>
      <ul>
        <em> // Test if red ball has been hit <br>
      if (redball.userHit (x, y)) <br>
      { </em>
        <ul>
          <em> // play audio file <br>
        hitnoise.play(); <br>
        <br>
        // reset ball <br>
        redball.ballWasHit (); <br>
          </em>
        </ul>
        <em>} <br>
      // Test if blue ball has been hit <br>
      if (blueball.userHit (x, y)) <br>
      { </em>
        <ul>
          <em> // play audio file <br>
        hitnoise.play(); <br>
        <br>
        // reset ball <br>
        blueball.ballWasHit (); <br>
          </em>
        </ul>
        <em>} <br>
      else <br>
      { </em>
        <ul>
          <em> // play normal shot audio file <br>
        shotnoise.play(); <br>
          </em>
        </ul>
        <em>} </em>
      </ul>
      <em>} <br>
    // handle mouse events if game is stopped <br>
    else if (isStoped &amp;&amp; e.clickCount == 2) <br>
    { </em>
      <ul>
        <em> // reset all important values! <br>
      isStoped = false; <br>
      init (); <br>
        </em>
      </ul>
      <em>} <br>
      <br>
    return true; <br>
      </em>
    </ul>
    <em>} </em>
  </ul>
  <h3>That's it!! </h3>
  <p align="justify">Well, you've made it. I hope you understood everything and also these parts, I didn't explain in detail. The next chapters will tell you something about very special solutions to problems I once had. <br>
  Now feel free to program any game you want to program, use the power of Java, which makes almost everything possible, good luck in developing your own games and I hope, that I could help you a little bit with this tutorial! If you have problems, tutorials, games ... please send me a mail! <br>
  Now you can take a look at the game, download the sourcecode and have fun! </p>
  <p><a href="SourceCodes/ErstesSpiel/DasersteSpiel.zip">SourceCode download (*.zip - file) </a><br>
      <a href="Applets/ErstesSpiel/ErstesSpielEng.html">Take a look at the applet </a>
  <h4>Next chapter </h4>
  <a href="PongKIEng.html">AI in a Pong game </a> <!-- InstanceEndEditable -->
</div>
</td>
</tr>

<tr>
<td colspan="11" style="background-color:#990000" align="center">
<table width="100%" style="padding:0"><tr>
  <td width="88" bgcolor="#993300"><div align="center" style="font-size:10px; color: #FFFFFF;">  <a href="#top"> to  top </a></div></td>
  <td><div align="center" style="font-size:10px; color: #FFFFFF;">
<a href="mailto:javacooperation@gmx.de">Fabian Birzele</a>, 2001-2004.<br>
web-design: <a href="http://www.freehand.str.ru/">Vadim Murzagalin</a>, 2004.
</div></td>
<td width="88">
</td>
</tr>
</table>

</td>

</tr>

</table>
</div>

</body>
<!-- InstanceEnd --></html>

⌨️ 快捷键说明

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