Quellcode
<SCRIPT type="text/javascript">
var level;
if (window.name=="") level=1;
else level=window.name;
var level_string='<SCRIPT type="text/javascript" src="' + level + '.js"><\/SCRIPT>';
document.write(level_string);
</SCRIPT> <SCRIPT type="text/javascript"> var box_image=new Image;
box_image.src="box.gif";
var player_image=new Image;
player_image.src="player.gif";
var wall_image=new Image;
wall_image.src="wall.gif";
var free_image=new Image;
free_image.src="free.gif";
var target_image=new Image;
target_image.src="target.gif";
var box_in_target_image=new Image;
box_in_target_image.src="box_in_target.gif";
var player_x;
var player_y;
var num_targets;
var copyright="";
var num_rows;
var num_columns; load_level_size();
var field=new Array();
for (var i=0; i < num_rows; i++)
{
field[i]=new Array();
for (var j=0; j < num_rows; j++)
{
field[i][j]=0;
}
}
load_level(); var box_in_target=0; var game_status=0; var turns=0; function Bildwechsel(Bildnr,Bildobjekt)
{
window.document.images[Bildnr].src = Bildobjekt.src;
} function Tastendruck(Druck)
{
if (document.all) k = window.event.keyCode;
else k = Druck.which;
if (k == 38 || k == 87) move_player(0,-1);
if (k == 37 || k == 65) move_player(-1,0);
if (k == 39 || k == 68) move_player(1,0);
if (k == 40 || k == 83) move_player(0,1);
} function change_image(y,x)
{
var image_number='f'+y+'_'+x;
switch (field[y][x])
{
case 0:
Bildwechsel(image_number,free_image);
break;
case 10:
Bildwechsel(image_number,target_image);
break;
case 20:
Bildwechsel(image_number,box_image);
break;
case 30:
Bildwechsel(image_number,box_in_target_image);
break;
default:
if (field[y][x]>=100) Bildwechsel(image_number,player_image);
break;
}
} if (game_status==0)
{ turns++; if ((player_y+y>-1)&&(player_x+x>-1)&&(player_y+y<num_rows)&&(player_x+x<num_columns))
{ switch(field[player_y+y][player_x+x])
{ case 0:
field[player_y][player_x]-=100;
change_image(player_y,player_x);
player_x += x;
player_y += y;
field[player_y][player_x]+=100;
change_image(player_y,player_x);
break; case 10:
field[player_y][player_x]-=100;
change_image(player_y,player_x);
player_x += x;
player_y += y;
field[player_y][player_x]+=100;
change_image(player_y,player_x);
break; case 20:
if ((field[player_y+2*y][player_x+2*x] != 1) && (field[player_y+2*y][player_x+2*x] != 20) &&
(field[player_y+2*y][player_x+2*x] != 30) && (player_y+2*y > -1) &&
(player_x+2*x > -1) && (player_y+2*y < num_rows) && (player_x+2*x < num_columns))
{
field[player_y][player_x]-=100;
change_image(player_y,player_x);
player_x += x;
player_y += y;
field[player_y][player_x]+=80;
change_image(player_y,player_x);
field[player_y+y][player_x+x]+=20;
change_image(player_y+y,player_x+x);
if (field[player_y+y][player_x+x]==30) box_in_target++;
}
break;
case 30:
if ((field[player_y+2*y][player_x+2*x] != 1) && (field[player_y+2*y][player_x+2*x] != 20) &&
(field[player_y+2*y][player_x+2*x] != 30) && (player_y+2*y > -1) &&
(player_x+2*x > -1) && (player_y+2*y < num_rows) && (player_x+2*x < num_columns))
{
field[player_y][player_x]-=100;
change_image(player_y,player_x);
player_x += x;
player_y += y;
field[player_y][player_x]+=80;
change_image(player_y,player_x);
field[player_y+y][player_x+x]+=20;
change_image(player_y+y,player_x+x);
if (field[player_y+y][player_x+x]!=30) box_in_target--;
}
break;
} if (box_in_target == num_targets)game_status=1;
}
} if (game_status==1)
{
alert('You won the game in '+turns+' moves!');
level++;
window.name=level;
}}
</SCRIPT> <SCRIPT type="text/javascript"> document.onkeydown = Tastendruck; for (var i=0; i < num_rows; i++)
{
for (var j=0; j < num_columns; j++)
{
var temp = field[i][j];
switch (temp)
{
case 0:
document.write('<IMG alt="f" src="'+free_image.src+'" name="f'+i+'_'+j+'">');
break;
case 1:
document.write('<IMG alt="w" src="'+wall_image.src+'" name="f'+i+'_'+j+'">');
break;
case 10:
document.write('<IMG alt="f" src="'+target_image.src+'" name="f'+i+'_'+j+'">');
break;
case 20:
document.write('<IMG alt="w" src="'+box_image.src+'" name="f'+i+'_'+j+'">');
break;
case 30:
document.write('<IMG alt="w" src="'+box_in_target_image.src+'" name="f'+i+'_'+j+'">');
box_in_target++;
break;
default:
document.write('<IMG alt="f" src="'+free_image.src+'" name="f'+i+'_'+j+'">');
break;
}
}
document.write('<BR>');
} Bildwechsel('f'+player_y+'_'+player_x,player_image);
</SCRIPT> <SCRIPT type="text/javascript">
document.write("<SMALL>"+copyright+"<\/SMALL>");
</SCRIPT> <TABLE>
<TR>
<TD> </TD>
<TD>
<INPUT type="button" value="⇑ " onclick="move_player(0,-1); ">
</TD>
<TD> </TD>
</TR>
<TR>
<TD>
<INPUT type="button" value="⇐ " onclick="move_player(-1,0); ">
</TD>
<TD>
<INPUT type="button" value="⇓ " onclick="move_player(0,1); ">
</TD>
<TD>
<INPUT type="button" value="⇒ " onclick="move_player(1,0); ">
</TD>
</TR>
</TABLE> <FORM name="test" action="">Choose your Maze:
<SELECT name="level" size="1" onChange="window.name=document.test.level.options[document.test.level.selectedIndex].text; location.reload(); ">
<OPTION>1</OPTION>
<OPTION>2</OPTION>
.
.
.
<OPTION>19</OPTION>
<OPTION>20</OPTION>
</SELECT>
<BR>
<BR>
<INPUT type="button" name="text" value="New Game" onClick="location.reload(); ">
</FORM> function load_level_size()
{
num_rows=9;
num_columns=9;
} function load_level()
{ player_x=4;
player_y=4; field[0][0]=0;
field[0][1]=0;
field[0][2]=1;
field[0][3]=1;
field[0][4]=1;
.
.
.
field[8][5]=1;
field[8][6]=1;
field[8][7]=0;
field[8][8]=0; num_targets=5; copyright="This is an example level";
} |
Dieses Script lädt das Level. Die Levelnummer ist im Fensternamen abgelegt. Die Daten des Raumes werden extern in Maze-Files abgelegt, um das Spiel flexibler zu machen.
Um einen Raum zu erstellen, kann man entweder den Leveleditor verwenden oder einfach mit einem ASCII-Editor die Datei manuell erstellen, wenn man die Struktur dieser Dateien verstanden hat (siehe Ende dieses Dokumentes).