📄 leveleditoreng.html
字号:
<br>
// Get number of levels in total <br>
levels_in_total = Integer.parseInt (((Applet)parent).getParameter (C_LevelEditor.total_levels)); <br>
<br>
// Initialize level_array <br>
level_array = new Level [levels_in_total]; <br>
</ul>
} <br>
<br>
/* This method reads every level in the HTML - Page, generates a <br>
instance of the class Level (for every level) and stores it in the <br>
level_array*/ <br>
public Level [] readLevels () <br>
{ <br>
<ul>
for (int i = 1; i <= levels_in_total; i++) <br>
{ <br>
<ul>
// generate new level <br>
Level level = new Level (); <br>
<br>
// get and set information parameters <br>
level.setAuthor (((Applet)parent).getParameter ("Level" + i + "_" + C_LevelEditor.author)); <br>
level.setName (((Applet)parent).getParameter ("Level" + i + "_" + C_LevelEditor.name)); <br>
level.setComment (((Applet)parent).getParameter ("Level" + i + "_" + C_LevelEditor.comment)); <br>
<br>
// read in all the lines and store them in the level <br>
for (int j = 0; j < C_LevelEditor.number_of_lines; j++) <br>
{ <br>
<ul>
level.setLine (((Applet)parent).getParameter ("Level" + i + "_Line" + j), j);
</ul>
} <br>
<br>
// store level <br>
level_array [i-1] = level; <br>
</ul>
} <br>
return level_array; <br>
</ul>
}
</ul>
}
</ul>
<h4>The class Level </h4>
<p align="justify">Now we are able to read in the levels and to store them in Level instances but we still don't know anything about the Level class in detail. Now we have to generate a real level mainly the stone_map which holds the different level elements out of the string information we get out of the level definition in the HTML - page. The stone_map 2D array stores the level elements at the same position where they appear in the level definition (for example a "r" occurs at line 3 as the third character of the string, then a red stone object is generated in the array in row 3 and column 3). First of all this class has some set and get methods to get and set the values of the level information parameters. Much more interesting is the method setLine. This method gets one line of the level definition (a string) and translates this string to stone objects and stores these stone instances in the stone_map. Last but not least the class has its own paint method. </p>
<ul>
import java.util.*; <br>
import java.awt.*; <br>
<br>
public class Level <br>
{ <br>
<ul>
// Variables <br>
private String author; <br>
private String name; <br>
private String comment; <br>
<br>
// Levelmatrix, stores the stone objects <br>
private Stone [] [] stone_map; <br>
public Level () <br>
{ <br>
<ul>
// Initialize the stone_map, all fields are initialized with null <br>
stone_map = new Stone [C_LevelEditor.number_of_lines] <br>
[C_LevelEditor.number_of_cols]; <br>
</ul>
} <br>
<br>
// Method translates information of one line of the level definition to <br>
stone objects <br>
public void setLine (String line, int line_index) <br>
{ <br>
<ul>
char [] entrys = line.toCharArray(); <br>
<br>
// go thourgh all chars and translate them to stone objects <br>
for (int i = 0; i < C_LevelEditor.number_of_cols; i++) <br>
{ <br>
<ul>
Stone stone = null; <br>
<br>
// generate red stone if char equals "r" <br>
if (entrys[i] == 'r') <br>
{ <br>
<ul>
stone = new Stone (line_index, i, Color.red); <br>
</ul>
} <br>
<br>
// generate different coloured stones the same way <br>
... <br>
<br>
// If char is unknown, generate no stone, which means that this <br>
// array field stays null <br>
else <br>
{ <br>
<ul>
// do nothing <br>
</ul>
} <br>
<br>
// store stone in array if it is not null <br>
if (stone == null) <br>
{ <br>
<ul>
// do nothing
</ul>
} <br>
else <br>
{ <br>
<ul>
stone_map [line_index] [i] = stone; <br>
</ul>
} <br>
</ul>
} <br>
</ul>
} <br>
<br>
// set and get methods for the information strings <br>
... <br>
<br>
// Method paints level <br>
public void paintLevel (Graphics g) <br>
{ <br>
<ul>
// go through the whole stone map and paint stones <br>
for (int i = 0; i < stone_map.length; i++) <br>
{ <br>
<ul>
for (int j = 0; j < stone_map[i].length; j++) <br>
{ <br>
<ul>
Stone stone = stone_map [i][j]; <br>
<br>
// paint stone or do nothing if stone is null <br>
if (stone == null) <br>
{ <br>
<ul>
// draw nothing <br>
</ul>
} <br>
else <br>
{ <br>
<ul>
stone.drawStone(g); <br>
</ul>
} <br>
</ul>
} <br>
</ul>
} <br>
<br>
// paint level information <br>
g.setColor (Color.yellow); <br>
g.drawString (comment, 50, 250); <br>
g.drawString (name, 50, 270); <br>
g.drawString (author, 50, 290); <br>
</ul>
} <br>
</ul>
}
</ul>
<h3>Conclusion </h3>
<p align="justify">In this chapter I showed you one way to define a level in a HTML - page, to read in this level using a LevelReader and one method to represent this level in our applet (2D array). As always there are many ways to do this maybe much better ones than mine and even though we might have helped you, because you can use the methods to read in and store the level in the applet in almost every arraybased game the much harder work is still in front of you. You have to make your game work with <em>every </em> level someone defined (which is really hard) and of course you have to generate your own level elements, change the number of level lines... . Ok, I hope I could help you a little bit, if you wrote a game using this editor, I would be glad if you would send it to me. Well, we are finished, here comes the link to download the sourcecode and the link to the working level editor applet (take a look a the sourcecode of the HTML - page to see what the levels look like). </p>
<p><a href="SourceCodes/Leveleditor/Leveleditor.zip">SourceCode download </a><br>
<a href="Applets/Leveleditor/Leveleditor.html">Take a look at the applet </a>
<h4>Next chapter </h4>
<a href="ScrollingEng.html">Scrolling </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 + -