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

📄 platformgamebasicseng.html

📁 java tutorial all about java game design
💻 HTML
📖 第 1 页 / 共 3 页
字号:
public static final String row19 = ":::::::::::::::::::::::gggg::::::::::::::::"; <br>
public static final String row20 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row21 = "::::::gggg:::::::::::::::::::::::::::::::::"; <br>
public static final String row22 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row23 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row24 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row25 = "ggggggggggggggggggggggggggggggggggggggggggg"; <br>
</code>
<p align="justify">As you can see here it is really simple to define new levels (see the <a href="../../../Downloads/J-RioLevelEditor.zip">level editor version </a> of J-rio too). But how is this simple definition translated to the internal computer readable data representation? This is implemented in the method initializeLevel(String [] definitions) of the class Level. This method parses the level definition strings and generates a two dimensional array of level elements out of them. This array is mainly used for the collision control. Afterwards all pointers to levelelements are also stored in a one dimensional array to make scrolling and painting of the level more efficient. </p>
// Method to parse the level definition strings and generate 2D and 1D arrays. <br>
public void initializeLevel(String [] definitions) <br>
{ <br>
<ul>
  // Initialize collision array <br>
  collision_array = new LevelElement [C_Jump.number_of_level_lines] [definitions[0].length()]; <br>
  <br>
  // Initialize some level information: level length and left and right border <br>
  level_length = definitions[0].length() * C_Jump.level_element_width; <br>
  left_level_border = 0; <br>
  right_level_border = C_Jump.applet_width; <br>
  <br>
  // Counter to count the number of level elements in the level, important <br>
  // for the initialisation of the 1D array <br>
  int elements_counter = 0; <br>
  <br>
  // For all level definition strings do: <br>
  for(int i=0; i&lt;definitions.length; i++) <br>
  { <br>
  <ul>
    // generate a char array of the current level definition string <br>
    char [] definition_line = definitions[i].toCharArray(); <br>
    <br>
    // for all elements in the char array do: <br>
    for(int j=0; j&lt;definition_line.length; j++) <br>
    { <br>
    <ul>
      // Translate the chars to level elements <br>
      if(definition_line[j] == ':') <br>
      { <br>
      <ul>
        collision_array[i][j] = null;
      </ul>
      } <br>
      // Generate a Ground element <br>
      else if(definition_line[j] == 'g') <br>
      { <br>
      <ul>
        // Important: position in the string definition (i, j) <br>
        // is translated to concrete pixel position <br>
        Ground element = new Ground(j*C_Jump.level_element_width, <br>
        <ul>
          i*C_Jump.level_element_height, ground, parent, C_Jump.ground_id);
        </ul>
        <br>
        // Store element in collision array <br>
        collision_array[i][j] = element; <br>
        <br>
        // increase element counter <br>
        elements_counter ++;
      </ul>
      } <br>
    </ul>
    } <br>
  </ul>
  } <br>
  <br>
  // Copy levelelement pointers to 1D array <br>
  // Code's missing because nothing special happens
</ul>
}
<h4>Test for collisions between player and level elements </h4>
<p align="justify">Let's get to the last and maybe most comprehensive topic of this chapter. All the things we did and talked about before are important for the collision control of the game because the collision control has quite a lot to to with the movement control and the internal data representation of the game. <br>
  Well, the idea is the following: The only thing we have to do, when we want to test if the player collides with any level element is to determine the position of the player in the 2D level array, which means the players row and column and test if there is a level element (which means not a null pointer) or if there ist no element (= null pointer). If we find a levelelement at the players position, we have to do something with the movement flags of the player, in our case we have to set them to false. As you might remember, we have four movement flags so we have to test for four possible collisions: collison up (important for jumping), collision down (important for falling), collision left and collision right. Therefore I implemented some methods. The method <strong>testForPlayerCollisions() </strong> is used to manage the collision control and to decide what to do when a certain collision between a level element and the player occurs. There are also four specialized methods ( <strong>testCollisionUp </strong>, <strong>-Down </strong>, ...) that are used to make a lookup within the 2D array if a level element exists at a given player position or not. Ok, here comes the sourcecode of the method testForPlayerCollisions(Player player) as well as for the method testCollisionDown (as a example for one of those methods that test for the existence of a level element at a given player position and return this element to the calling method). </p>
// method tests for collisions between player and level elements <br>
public void testForPlayerCollisions(Player player) <br>
{ <br>
<ul>
  // get some player specific position values <br>
  int player_game_pos = player.getGameXPosition(); <br>
  int player_down_pos = player.getYPosDown(); <br>
  int player_up_pos = player.getYPosUp(); <br>
  <br>
  int player_left = player_game_pos - (C_Jump.player_image_width/2); <br>
  int player_right = player_game_pos + (C_Jump.player_image_width/2); <br>
  <br>
  // Test for collisions down <br>
  LevelElement down_element = testCollisionDown(player_game_pos, player_down_pos); <br>
  <br>
  // If there is a element below the player, the player does not fall <br>
  if(down_element != null) <br>
  { <br>
  <ul>
    player.playerFall(false);
  </ul>
  } <br>
  // if null is returned the player is falling down <br>
  else <br>
  { <br>
  <ul>
    player.playerFall(true);
  </ul>
  } <br>
  <br>
  // Test for collisions up <br>
  LevelElement upper_element = testCollisionUp(player_game_pos, player_up_pos); <br>
  <br>
  // stop jumping if there is a element <br>
  if(upper_element != null) <br>
  { <br>
  <ul>
    player.playerJump(false);
  </ul>
  } <br>
  <br>
  // Test for collisions at the left side of the player <br>
  LevelElement left_element = testCollisionLeft(player_left, player_down_pos); <br>
  <br>
  // stop movement of the player to the left <br>
  if(left_element != null) <br>
  { <br>
  <ul>
    player.playerWalkLeft(false);
  </ul>
  } <br>
  <br>
  // Test for collisions at the right side of the player <br>
  LevelElement right_element = testCollisionRight(player_right, player_down_pos); <br>
  <br>
  // stop movement of the player to the right <br>
  if(right_element != null) <br>
  { <br>
  <ul>
    player.playerWalkRight(false);
  </ul>
  } <br>
</ul>
} <br>
<br>
// Method tests for a given player position if there is a level element, this element is returned to the calling class <br>
public LevelElement testCollisionDown(int game_pos, int player_y_down) <br>
{ <br>
<ul>
  // Translate player position into array position (row and col) <br>
  int col = game_pos / C_Jump.level_element_width; <br>
  int row = player_y_down / C_Jump.level_element_height; <br>
  <br>
  try <br>
  { <br>
  <ul>
    // Return element at this position or null <br>
    if(collision_array[row][col] != null) <br>
    { <br>
    <ul>
      return collision_array[row][col];
    </ul>
    } <br>
    else <br>
    { <br>
    <ul>
      return null;
    </ul>
    } <br>
  </ul>
  } <br>
  catch (ArrayIndexOutOfBoundsException ex) <br>
  { <br>
  <ul>
    return null;
  </ul>
  } <br>
</ul>
} <br>
<h3>That's it! </h3>
<p align="justify">Well, here I'd like to finish this chapter and I hope that I could help you a little bit. I know that this chapter is not that simple as some other chapters of this tutorial (it was neither easy for me to write, nor easy for you to understand, I think), but as the topic is not so simple this is not such a big surprise. Also I'd really like to hear what was useful and what was not with this chapter because maybe I should explain some things in more detail or so. Please write me a mail if you used this chapter and tell me what was useful and what was too fuzzy (a word my professor is always using so I wanted to use it too once in my life ;-)! Well then, as always you can download the sourcecode of this chapter and you can take a look at the example applet we've just implemented but you can play <a href="Applets/Games/JrioGame/Jrio.html">J-Rio </a> as well, because it is "almost" the same. Good luck with the implementation of your own platform games! </p>
<a href="SourceCodes/PlatformGameBasics/PlatformGameBasics.zip">Sourcecode download </a><br>
<a href="Applets/PlatformGameBasics/PlatformGameBasics.html">Take a look at the applet </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 + -