📄 platformgamebasicseng.html
字号:
<ul>
<em> // Method to set the value of the falling flag <br>
public void playerFall(boolean value) <br>
{ <br>
</em>
<ul>
<em> // we want to stop falling <br>
if(!value) <br>
{ <br>
</em>
<ul>
<em> // reset the jump flags to make new jump possible <br>
if(jump_lock) <br>
{ <br>
</em>
<ul>
<em> jump_lock = false; <br>
jumping = false; </em>
</ul>
<em>} <br>
<br>
// we have to correct the player position so that the player always, <br>
// stands on the surface of a platform. In this case, the lower <br>
// position of the player modulo the Height of a level element <br>
// is equal 0. If this is not the case, the player is moved up <br>
// until this is the case and the player stands on the surface! <br>
while(y_pos_down%C_Jump.level_element_height != 0) <br>
{ <br>
</em>
<ul>
<em> y_pos_down --; <br>
y_pos_up--; </em>
</ul>
<em>} <br>
</em>
</ul>
<em>} <br>
<br>
// set value of falling <br>
falling = value; <br>
</em>
</ul>
<em>} <br>
</em>
</ul>
<h4>Movement and animation of the player </h4>
<p align="justify">Now as we know about the four movement flags and how they are set we will take a look at the method that actually moves the player according to the values of the flags. This happens in the playerMove() - method but before we take a look at the sourcecode of the method I have to say something about two variables I'll use in the method: </p>
<ul>
<li>
<p align="justify"><strong>step_counter </strong> and <strong>picture_counter </strong>: Those variables are only necessary to animate the player. When the player made 15 "steps" we want to change the player image and this means that we have to change the value of picture_counter that tells the paint() - method which image of the player should be painted to the screen. </p>
<li>
<p align="justify"><strong>jump_counter </strong>: This counter is used to determine how far the player can jump and when a jump of the player stops. If the jump_counter value reaches a certain maximal value, the value of the jumping flag is set to false. </p>
</li>
</ul>
<ul>
<em> // This method moves the player according to the values of the movement flags <br>
public void playerMove() <br>
{ <br>
</em>
<ul>
<em> // walking_left flag is true <br>
if(walking_left) <br>
{ <br>
</em>
<ul>
<em> // Change the x - position of the player <br>
x_pos_left -= walk_x_speed; <br>
x_pos_right -= walk_x_speed; <br>
<br>
// change the game position (important for scrolling) <br>
game_x_position -= walk_x_speed; <br>
<br>
// change the player image after 15 steps <br>
if (step_counter%15 == 0) <br>
{ <br>
</em>
<ul>
<em> // change value of the picture_counter <br>
picture_counter ++; <br>
<br>
// reset counter if value is 2 (there are only 2 pictures) <br>
if(picture_counter == 2) <br>
{ <br>
</em>
<ul>
<em> picture_counter = 0; </em>
</ul>
<em>} <br>
<br>
// reset step counter <br>
step_counter = 1; <br>
</em>
</ul>
<em>} <br>
// else increase value of the step counter <br>
else <br>
{ <br>
</em>
<ul>
<em> step_counter ++; </em>
</ul>
<em>} <br>
<br>
// tell if player looks left (only important for animation) <br>
look_left = true; <br>
</em>
</ul>
<em>} <br>
// walking_right flag is true <br>
else if(walking_right) <br>
{ <br>
</em>
<ul>
<em> ... All the same as for walking_left only to the other side... </em>
</ul>
<em>} <br>
<br>
// Value of jumping is true <br>
if(jumping) <br>
{ <br>
</em>
<ul>
<em> // This variable exists to avoid that the player can jump one more time <br>
// if he's already jumping <br>
jump_lock = true; <br>
<br>
// Make sure that the value of falling is false becaues the collision <br>
// control sets the falling value to true even if the player is jumping <br>
falling = false; <br>
<br>
// jump_counter still smaller than 30 <br>
if(jump_counter < 30) <br>
{ <br>
</em>
<ul>
<em> // player jumps by the speed of 2 <br>
y_pos_up -= jump_y_speed; <br>
y_pos_down -= jump_y_speed; <br>
jump_counter ++; </em>
</ul>
<em>} <br>
// if the value of the jump_counter is smaller than 30 but greater than <br>
// 40 jump by the speed of 1 <br>
else if (jump_counter < 40) <br>
{ <br>
</em>
<ul>
<em> y_pos_up -= jump_y_speed2; <br>
y_pos_down -= jump_y_speed2; <br>
jump_counter++; </em>
</ul>
<em>} <br>
// if the value of the jump_counter is greater than 40, player can't <br>
// jump further so set the value of jumping to false <br>
else <br>
{ <br>
</em>
<ul>
<em> jumping = false; </em>
</ul>
<em>} <br>
</em>
</ul>
<em>} <br>
<br>
// if player is falling move playe down <br>
if(falling) <br>
{ <br>
</em>
<ul>
<em> y_pos_up += fall_y_speed; <br>
y_pos_down += fall_y_speed; </em>
</ul>
<em>} <br>
</em>
</ul>
<em>} <br>
</em>
</ul>
<p align="justify">I hope that I could show you now the most important and new parts of the class player, scrolling and painting of the player should be no problem anymore. But I think you should really take a look at the sourcecode of the class to understand everything because this class is not so simple. Well, ok, lets start with a even more complex class, the class <strong>Level </strong></p>
<h3><a name="PlatformGameLevelObjekt">Structure and function of the class <em>Level </em></a></h3>
<p align="justify">In this last part of the tutorial we'll talk about the class <strong>Level </strong> and the related classes <strong>LevelOne </strong> and <strong>LevelElement </strong>. First of all we'll take a look at the "definition language" of a platform game level and we'll see how this definition language is translated to the internal data representation of the level. Those are mainly ideas I already introduced in the chapter about the level editor, if you have any problems please read this chapter first. Afterwards I'll show you some details of the collision control between level elements and the player. I won't talk about things like scrolling and painting because those things should be clear now (at least I hope so). </p>
<h4>Definition language and internal data structure of a level </h4>
<p align="justify">One major goal of our class design was to allow ourselfs to write new levels for our game easily. That's the reason why we define our levels with a certain "definition language" which can easily be used by humans and then we translate this level definition into a new data structure which can be used by the computer. In our design language a level consists of 25 rows represented as 25 strings. The length of those strings is variable but they all need to be of the same length. Every level element we want to use in our level is implemented as a child class of LevelElement, has a unique character identifier and at every position where this character appears in our level definition strings this level element will be generated in the internal data structure of the level. The parser in the class Level (method initializeLevel()) has to be capable to translate those string definitions into the internal data structure of our level. This internal data structure consists of a two dimensional array with null pointers at places where no level element exists in our level and instances of child classes of the class LevelElement where a level element exists in the level. This idea is explained in more detail in the chapter aboue the <a href="Leveleditor.html">level editor </a>. Ok, before we get to the collision control here comes the sourcecode: </p>
<p align="justify">The level definition can be found in the class <strong>LevelOne </strong>. This class is used to define all the level specific things like the background color, the level itself and so on where the real functionality of the level is implemented in the inherited methods from the parent class Level. Here comes a level in the level definition language: </p>
<code>
/** <br>
Legend: <br>
":": represents a position in the level where no element should be generated <br>
"g": represents a position in the level where a ground element should be generated <br>
*/ <br>
<br>
// String definition of the level <br>
// rows 1 - 10 are missing, because they don't contain any important data <br>
public static final String row11 = "::::::::::::::::::::::::::::::::::::::::g::"; <br>
public static final String row12 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row13 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row14 = "::::::::::::::::::::::::::::::::::::g::::::"; <br>
public static final String row15 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row16 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
public static final String row17 = "::::::::::::gggg::::::::::::::::g::::::::::"; <br>
public static final String row18 = ":::::::::::::::::::::::::::::::::::::::::::"; <br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -