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

📄 shootingspaceshipeng.html

📁 java tutorial all about java game design
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Java Cooperation: shooting spaceship</title>
<!-- InstanceEndEditable --><meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<link href="jc_style.css" rel="stylesheet" type="text/css">
<script src="klayers.js"></script>

<script>

function cancelCloseMenu(){
        if(self.tm) clearTimeout(tm) // cancel delayed closing
}

function showMenu(){
        if(!self.menu || !self.submenu){
                menu=layer("menulayer")
                submenu=layer("submenulayer")
        }

        cancelCloseMenu()

        submenu.moveTo(menu.getAbsoluteLeft(), menu.getAbsoluteTop() + menu.getHeight()) // move second menu relatively
        submenu.show()
}

function initiateHideMenu(){
        tm=setTimeout("hideMenu()",400) // 1 second delay to close a submenu
}

function hideMenu(){
        submenu.hide()
}

</script>

<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>
<body bgcolor="#ffffff" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
<div align="center">
  <a name="top"></a>
  <table bgcolor="#737373" border="0" cellpadding="0" cellspacing="0" width="618">
  <tr>
<td rowspan="8" bgcolor="#000000"><img src="Pics/spacer.gif" width="1" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="131" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="13" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="58" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="94" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="81" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="48" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="23" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="38" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="19" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="108" height="1" border="0" alt=""></td>
   <td bgcolor="#000000"><img src="Pics/spacer.gif" width="5" height="1" border="0" alt=""></td>

  </tr>

  <tr>
   <td rowspan="5"><a href="index.html"><img name="fab_r1_c1" src="Pics/fab_r1_c1.gif" width="131" height="71" border="0" alt="Java cooperation home"></a></td>
   <td rowspan="6" bgcolor="#737373"></td>
   <td colspan="9" height="15" bgcolor="#737373"></td>
   <td rowspan="8" bgcolor="#000000"><img src="Pics/spacer.gif" width="1" height="1" border="0" alt=""></td>
  </tr>
  <tr>
   <td colspan="5"><div class="colontitul"><!-- InstanceBeginEditable name="Colontitul" -->Tutorial<!-- InstanceEndEditable --></div></td>
   <td colspan="2" bgcolor="#737373"><img name="fab_r2_c8" src="Pics/fab_r2_c8.gif" width="57" height="19" border="0" alt="type and press Enter"></td>
   <td colspan="2">
<FORM method=GET action="http://www.google.com/search">
<TABLE bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" height="10px"><tr>
<td height="10" valign="middle">
<INPUT TYPE=text name=q size=18 maxlength=60 value="" style="font-size:12px; color:#000000; border-width: 0px;">
<input type=hidden name=domains value="http://javacooperation.gmxhome.de/">
<input type=hidden name=sitesearch value="http://javacooperation.gmxhome.de/" checked>
</td></tr></TABLE>
</td>
</form>
   <td><img src="Pics/spacer.gif" width="1" height="19" border="0" alt=""></td>
  </tr>
  <tr>
   <td colspan="9" bgcolor="#666666"><img name="fab_r3_c3" src="Pics/fab_r3_c3.gif" width="474" height="8" border="0" alt=""></td>
   <td><img src="Pics/spacer.gif" width="1" height="8" border="0" alt=""></td>
  </tr>
  <tr>
   <td colspan="6"><a href="TutorialStartEng.html"><img src="Pics/engpanel_01.gif" width="56" height="22" border="0"></a><a href="OnlineSpieleStartEng.html"><img src="Pics/engpanel_02.gif" width="94" height="22" border="0"></a><a href="DownloadsEng.html"><img src="Pics/engpanel_03.gif" width="76" height="22" border="0"></a><a href="LinksEng.html"><img src="Pics/engpanel_04.gif" width="52" height="22" border="0"></a><a href="KontaktEng.html"><img src="Pics/engpanel_05.gif" width="64" height="22" border="0"></a></td>
   <td><img name="fab_r4_c9" src="Pics/fab_r4_c9.gif" width="19" height="22" border="0" alt=""></td>
   <td width="108" height="22" bgcolor="#666666"><div align="center"><span id="menulayer" style="position: relative; background-color:#666666; width:108; height:20; vertical-align:baseline; border:1px solid #FFFFFF"><a href="#" class="text" onmouseover="showMenu(); return true" onmouseout="initiateHideMenu(); return true">Select language</a></span>
   </div>
     <div id="submenulayer" style="position: absolute; visibility: hidden; background-color: #666666; width:108px;">
         <div class="text" style="border:1px solid #FFFFFF; border-top-width:0; padding:5px">
<a href="indexDeu.html" onmouseover="cancelCloseMenu(); return true" onmouseout="initiateHideMenu(); return true">German</a><br>
<a href="#" onmouseover="cancelCloseMenu(); return true" onmouseout="initiateHideMenu(); return true">English</a><br>
<a href="http://javacooperation.dev.juga.ru/index.html" onmouseover="cancelCloseMenu(); return true" onmouseout="initiateHideMenu(); return true">Russian</a>
</div>
</div>
</td>
   <td><img name="fab_r4_c11" src="Pics/fab_r4_c11.gif" width="5" height="22" border="0" alt=""></td>
   </tr>
  <tr>
   <td colspan="9"><img name="fab_r5_c3" src="Pics/fab_r5_c3.gif" width="474" height="7" border="0" alt=""></td>
  </tr>
<tr>
<td colspan="11" style="padding:18; background-color:#333333; vertical-align:top;">
<div class="text">
  <!-- InstanceBeginEditable name="EditRegion" -->
  <h2>A shooting spaceship </h2>
  <p align="justify">In this chapter I want to talk about the "problem" of how to make a starship (or any other game object) shoot. This behaviour is used in nearly every action game like MarsAttacks or my game <a href="Applets/Games/JrioGame/JrioEng.html">J-Rio </a> and even though it's really simple to implement it, I got many mails from people who didn't know how to start this (which is absolutly no shame;-). Well, let's start this chapter! </p>
  <h3>Outline of the classes and the important steps </h3>
  <p align="justify">As always I want to start this chapter by explaining the outline of the classes and the main steps and techniques I'm going to use. As Java is a object oriented programming language and a shot has many attributes and behaviours (movement, tests for collisions with enemies, walls..., ...) it should be clear that we will implement a class <em>Shot </em> to implement all this behaviour. This class will hold all the attribute variables (x and y position, speed...) of the shot and will have methods to represent behaviour like movement... . In most cases a shot is generated by a player object, so we will have a player object with a method called generateShot. This method will generate a shot (no wonder with this name ;-) at the position of the player, with the right direction... and will return this object to the calling class. In my example applet the calling class will be the class Main. This class stores the generated shot in a array of shot objects. Everytime the run - method of this main class is called, we will iterate over this array to move the shots, destroy them if they leave the game area and we can also test for collisions with enemies or level elements (which is not implemented in my example applet). The paint - method of the applet will also run through the shots array and will draw every existing shot in it. <br>
  If you understand everything so far, which means that you got the message, you don't have to read on but you can download the example source code and take a look at it on your own. The classes are really simple and nothing special. But of course you may read on, I'll show you the sourcecode and I'll also give some explanations to it. </p>
  <h3>The class Shot </h3>
  <p align="justify">As I already said, this class stores the coordinates of the shot, has a moveShot - method to implement moving behaviour in y - direction and a drawShot() - method, to paint the shot to the screen. Ok, here comes the code: </p>
  <ul>
  import java.awt.Graphics; <br>
  import java.awt.Color; <br>
  <br>
  public class Shot <br>
  { <br>
  <ul>
    // variables <br>
    private int x_pos; <br>
    private int y_pos; <br>
    <br>
    // size of the shot <br>
    private final int radius = 3; <br>
    <br>
    // constructor <br>
    public Shot(int x, int y) <br>
    { <br>
    <ul>
      x_pos = x; <br>
      y_pos = y; <br>
    </ul>
    } <br>
    <br>
    // returns y - position, needed for testing if shot has left the game area <br>
    public int getYPos() <br>
    { <br>
    <ul>
      return y_pos; <br>
    </ul>
    } <br>
    <br>
    // moving shot in y - direction <br>
    public void moveShot(int speed) <br>
    { <br>
    <ul>
      y_pos += speed; <br>
    </ul>
    } <br>
    <br>
    // draw the shot to the screen <br>
    public void drawShot(Graphics g) <br>
    { <br>
    <ul>
      g.setColor(Color.yellow); <br>
      g.fillOval(x_pos, y_pos, radius, radius); <br>
    </ul>
    } <br>
  </ul>
  } <br>
  </ul>
  <h3>The class Player </h3>
  <p align="justify">This class is simple, too. It also has a move - method (to move player in x - direction), a draw - method and stores the coordinates of the spaceship. The only "interesting" method is the generateShot - method. </p>
  <ul>
    <br>
  import java.awt.Graphics; <br>
  import java.awt.Color; <br>
  <br>
  public class Player <br>
  { <br>
  <ul>
    // variables <br>
    private int x_pos; <br>
    private int y_pos; <br>
    <br>
    // constructor <br>
    public Player(int x, int y) <br>
    { <br>
    <ul>
      x_pos = x; <br>
      y_pos = y; <br>
    </ul>
    } <br>
    <br>
    // move spaceship in x - direction <br>
    public void moveX(int speed) <br>
    { <br>
    <ul>
      x_pos += speed; <br>
    </ul>
    } <br>

⌨️ 快捷键说明

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