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

📄 shootingspaceshipeng.html

📁 java tutorial all about java game design
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <br>
    // generate a shot at the current position of the spaceship <br>
    // and return this shot to the calling method <br>
    public Shot generateShot() <br>
    { <br>
    <ul>
      Shot shot = new Shot(x_pos, y_pos); <br>
      <br>
      return shot; <br>
    </ul>
    } <br>
    <br>
    // draw the player <br>
    public void drawPlayer(Graphics g) <br>
    { <br>
    <ul>
      g.setColor(Color.red); <br>
      int [] x_poly = {x_pos, x_pos - 10, x_pos, x_pos + 10}; <br>
      int [] y_poly = {y_pos, y_pos + 15, y_pos + 10, y_pos + 15}; <br>
      g.fillPolygon(x_poly, y_poly, 4); <br>
    </ul>
    } <br>
  </ul>
  } <br>
  </ul>
  <h3>The class Main </h3>
  <p align="justify">And now we'll take a look at the class Main. I've deleted all for the shooting behaviour of the spaceship unimportant parts (marked with "..."). You can take y look at those parts if you download the sourcecode. </p>
  <ul>
  import java.applet.*; <br>
  import java.awt.*; <br>
  <br>
  public class Main extends Applet implements Runnable <br>
  { <br>
  <ul>
    // variables <br>
    ... <br>
    private Player player; <br>
    private Shot [] shots; <br>
    <br>
    // constants <br>
    private final int shotSpeed = -2; <br>
    ... <br>
    <br>
    // double buffering <br>
    private Image dbImage; <br>
    private Graphics dbg; <br>
    <br>
    public void init() <br>
    { <br>
    <ul>
      ... <br>
      // generate shot array <br>
      shots = new Shot[5]; <br>
    </ul>
    } <br>
    <br>
    ... <br>
    <br>
    public void run () <br>
    { <br>
    <br>
    <ul>
      while (true) <br>
      { <br>
      <ul>
        // Iterate over the shot array and move shots, <br>
        // test if a shot has left the game area. <br>
        // You can add other tests here (test for collisions <br>
        // of enemies and shots... <br>
        for(int i=0; i&lt;shots.length; i++) <br>
        { <br>
        <ul>
          if(shots[i] != null) <br>
          { <br>
          <ul>
            // move shot <br>
            shots[i].moveShot(shotSpeed); <br>
            <br>
            // test if shot has left the game area <br>
            // if true, delete from array <br>
            if(shots[i].getYPos() &lt; 0) <br>
            { <br>
            <ul>
              // delete shot <br>
              shots[i] = null; <br>
            </ul>
            } <br>
            <br>
            // other operations to be added <br>
            // ... <br>
            // for example collision testing... <br>
            // ... <br>
          </ul>
          } <br>
        </ul>
        } <br>
        <br>
        // move player <br>
        ... <br>
        <br>
        ... <br>
      </ul>
      } <br>
    </ul>
    } <br>
    <br>
    public boolean keyDown(Event e, int key) <br>
    { <br>
    <ul>
      ... <br>
      <br>
      // Spacebar is hit, generate new shot <br>
      else if(key == 32) <br>
      { <br>
      <ul>
        // generate new shot and try to store it in shots array <br>
        for(int i=0; i&lt;shots.length; i++) <br>
        { <br>
        // only store shot if there is a place left in the array <br>
        <ul>
          if(shots[i] == null) <br>
          { <br>
          <ul>
            shots[i] = player.generateShot(); <br>
            // call break to store shot only once, important! <br>
            break; <br>
          </ul>
          } <br>
        </ul>
        } <br>
      </ul>
      } <br>
      <br>
      ... <br>
    </ul>
    } <br>
    <br>
    public void paint (Graphics g) <br>
    { <br>
    <ul>
      // draw player <br>
      ... <br>
      <br>
      // draw shots <br>
      for(int i=0; i&lt;shots.length; i++) <br>
      { <br>
      <ul>
        if(shots[i] != null) <br>
        { <br>
        <ul>
          shots[i].drawShot(g); <br>
        </ul>
        } <br>
      </ul>
      } <br>
    </ul>
    } <br>
  </ul>
  } <br>
  </ul>
  <h3>Conclusion </h3>
  <p align="justify">I hope I could convince you that it is really simple to generate shots. The technique to store game objects of the same kind in a array and iterate over this array everytime the run and paint - methods are called is used very often when programming games and other applications, so don't forget about it if you are in a similar situation. I hope that I could help you a little bit with this chapter and as always you can download the sourcecode and take a look at the example applet. </p>
  <a href="SourceCodes/ShootingSpaceship/ShootingSpaceship.zip">SourceCode download (*.zip - Datei) </a><br>
  <a href="Applets/ShootingSpaceship/ShootingSpaceship.html">Take a look at the applet </a>
  <h4>Next chapter </h4>
  <a href="PlatformGameBasicsEng.html">Platform game basics </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 + -