After a long Christmas and New Year break, it’s back to coding, fortunately that break didn’t wash away all that they had learned and we were able to get on with Dimensional arrays. The best part of the lesson was when we started talking about how the Baddy AI will be implemented next lesson. They were beginning to think laterally, and how the computer doesn’t view the game as the player, but as a maze of 0 and 2’s in this case. The 2’s in the array are solid walls and so could 1,3, and 4 but they will be added later. 0’s are for nothing and is where both the goody and baddy can walk.The first row of 0’s is there to allow for space to draw the number of lives, score and level later on.
Every loop it redraws the level, and when key presses occur it checks if the position the character wants to head is clear, if it’s not it issues a return;. The following code builds upon last lesson here and adds the map drawing and good guy collision with it. The code as is also purposely presented the students with a dilemma, how do you get into the maze.
To get the below example to work
1) Load up processing and save immediately calling it “MazeGame” this will create a folder, example “MazeGame”
2) Within that folder create another folder called “data” so MazeGame\data
3) Within that folder put a graphic called “good.png” 40X40 pixels.
4) Run it
//MazeGame
//Copyright Room 1 Studios 2009
//Matthew C. Applegate – Pixelh8
int goodx, goody;
int level, column, row;
int[][][] mapper = {
//Level 1
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,2,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,2,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,2,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},//End of Level 1
//Level 2
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,9,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}} //End of Level 2
};
PImage hero;
void setup(){
background(0);
size(600, 400); // 15 X 10
hero = loadImage(“good.png“);
}
void draw (){
background(0);
updatescreen();
}
void updatescreen (){
image(hero, goodx*40, goody*40);
for (int row = 0; row < 10; row++){
for (int column = 0; column < 15; column++){
if (mapper [level] [row] [column] == 2){fill(255); rect (column*40,row*40, 40,40);}
}}
}
void keyReleased() {
if (key == CODED) {
//check if collision free
if (keyCode == UP) {
if ((mapper [level] [goody-1][goodx]>0)&& (mapper[level] [goody-1][goodx]<4)){return;}}
if (keyCode == DOWN) {
if ((mapper [level] [goody+1][goodx]>0)&& (mapper[level] [goody+1][goodx]<4)){return;}}
if (keyCode == LEFT) {
if ((mapper [level] [goody][goodx-1]>0)&& (mapper[level] [goody][goodx-1]<4)){return;}}
if (keyCode == RIGHT) {
if ((mapper[level] [goody][goodx+1]>0)&& (mapper[level][goody][goodx+1]<4)){return;}}
//If collision free
if (keyCode == UP) {goody=goody-1;}
if (keyCode == DOWN) {goody=goody+1;}
if (keyCode == LEFT) {goodx=goodx-1;}
if (keyCode == RIGHT) {goodx=goodx+1;}
}
}
