Grid system array?? Help with error please
in
Programming Questions
•
1 year ago
Hello to all who read this thread, and thanks in advance.
I've been experimenting with a way to correspond numbers set up in a grid to a certain square and have that create a maze like game where you can collect coins and such.
When I go to run it i get this error:
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 25
at Grid_Test$World.reload(Grid_Test.java:423)
at Grid_Test.resetGame(Grid_Test.java:41)
at Grid_Test.setup(Grid_Test.java:36)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
I have no idea as to what this may be, or how its coming up. Any help would be greatly appreciated!
Thanks again to all who read this,
Nerial
*Oops forgot the code x.x*
Keyboard movement = new Keyboard();
Player doodMan = new Player();
World game = new World();
PFont font;
PImage splash;
int gameStartTimeSec,gameCurrentTimeSec;//keeps track of time from start to where its at now
int slide = 1;
boolean move = true;
void setup(){
size(500,500);
smooth();
frameRate(60);
font = loadFont("font.vlw");
splash = loadImage("splash.jpg");
resetGame();
}
void resetGame(){
doodMan.reset();
game.reload();
}
Boolean gameWon(){
return (doodMan.coinsCollected >= game.coinsInStage);
}
void outlinedText(String sayThis, float atX, float atY){
textFont(font);
fill(0);
text(sayThis, atX-1,atY);
text(sayThis, atX+1,atY);
text(sayThis, atX, atY-1);
text(sayThis, atX, atY+1);
fill(255);
text(sayThis, atX, atY);
}
void draw(){
if(slide == 1){
image(splash,0,0);
if(mousePressed && move){
slide = 2;
move = false;
}
}
else if (slide == 2){
pushMatrix();
game.render();
doodMan.inputCheck();
doodMan.movement();
doodMan.drawStuff();
popMatrix();
if(focused == false){
textAlign(CENTER);
outlinedText("Click around here to continue play! \n\nUse Arrow Keys to move.",width/2, height/2);
}else{
textAlign(LEFT);
outlinedText("Coins:"+doodMan.coinsCollected + "/" + game.coinsInStage,8,height-10);
if(gameWon()){
textAlign(CENTER);
outlinedText("You've made it though the maze!" +"with:"+doodMan.coinsCollected+ "Of:" + game.coinsInStage,width/2,height/2);
}
}
}
}
void keyPressed(KeyEvent evt){
movement.pressKey(evt.getKeyCode());
}
void keyReleased(KeyEvent evt){
movement.releaseKey(evt.getKeyCode());
}
void mouseReleased(){
move = true;
}
class Keyboard{
boolean hU,hR,hL,hD;
Keyboard(){
hU=hR=hL=hD = false;
}
void pressKey(int key){
if (key == KeyEvent.VK_R){ //will never be held down so no boolean
if(gameWon()){ //if the game has been won
resetGame(); //then R key will reset it
}
}
if(key == KeyEvent.VK_UP){
hU = true;
println("up is working");
}
if(key == KeyEvent.VK_LEFT){
hL = true;
println("left is working");
}
if(key == KeyEvent.VK_RIGHT){
hR = true;
println("right is working");
}
if(key == KeyEvent.VK_DOWN){
hD = true;
println("down is working");
}
}
void releaseKey(int key){
if(key == KeyEvent.VK_UP){
hU = false;
println("up released");
}
if(key == KeyEvent.VK_LEFT){
hL = false;
println("left released");
}
if(key == KeyEvent.VK_RIGHT){
hR = false;
println("right released");
}
if(key == KeyEvent.VK_DOWN){
hD = false;
println("down released");
}
}
}
class Player {
PVector pos, vel; //PVector contains two floats, x & y
Boolean facingRight;//used to keep track of which direction the player moved in last, flips player image
int animDelay;//timer between animations
int animFrame;//keeps track of which animation frame is currently shown
int coinsCollected;//a counter keeps track of how many coins have been collected
static final float RUN_SPEED = 3.0;//speed at which the player moves
static final float SLOWDOWN_PERC = 0.6;//friction from ground. multiplied by x speed each frame
static final int RUN_ANIMATION_DELAY = 3;//how many game cycles pass between animation updates
static final float TRIVIAL_SPEED = 1.0;//if under this, player seen as standing still
Player() {
facingRight = true;
pos = new PVector();
vel = new PVector();
reset();
}
void reset() { //by setting all to 0 it resets the game
coinsCollected = 0;
animDelay = 0;
animFrame = 0;
vel.x = 0;
vel.y = 0;
}
void inputCheck() {
float speedHere = RUN_SPEED;
float frictionHere = SLOWDOWN_PERC;
if (movement.hL) {
vel.x -= speedHere;
}
else if (movement.hR) {
vel.y += speedHere;
}
vel.x *= frictionHere; //causes player to continually lose speed
}
void checkCollision() {
int wallProbeDistance = int(23*0.4);
int ceilingProbeDistance = int(23*0.96);
//used as probes to detect running into walls, ceiling
PVector leftSideHigh, rightSideHigh, leftSideLow, rightSideLow, topSide;
leftSideHigh = new PVector();
rightSideHigh = new PVector();
leftSideLow= new PVector();
rightSideLow = new PVector();
topSide = new PVector();
//update wall probes
leftSideHigh.x = leftSideLow.x = pos.x - wallProbeDistance; // left edge of player
rightSideHigh.x = rightSideLow.x = pos.x + wallProbeDistance; // right edge of player
leftSideLow.y = rightSideLow.y = pos.y-0.2*23; // shin high
leftSideHigh.y = rightSideHigh.y = pos.y-0.8*23; // shoulder high
topSide.x = pos.x; // center of player
topSide.y = pos.y-ceilingProbeDistance; // top of guy
// if ( theWorld.worldSquareAt(topSide)==World.TILE_END ||
// theWorld.worldSquareAt(leftSideHigh)==World.TILE_END ||
// theWorld.worldSquareAt(leftSideLow)==World.TILE_END ||
// theWorld.worldSquareAt(rightSideHigh)==World.TILE_END ||
// theWorld.worldSquareAt(rightSideLow)==World.TILE_END ||
// theWorld.worldSquareAt(position)==World.TILE_END) {
// return;
if (game.worldSquareAt(topSide)==World.TILE_S) {
if (game.worldSquareAt(pos)==World.TILE_S) {
pos.sub(vel);
vel.x=0.0;
vel.y=0.0;
}
else {
pos.y = game.bottomOfSquare(topSide)+ceilingProbeDistance;
if (vel.y<0) {
vel.y = 0;
}
}
}
if (game.worldSquareAt(leftSideLow)==World.TILE_S) {
pos.x = game.rightOfSquare(leftSideLow)+wallProbeDistance;
if (vel.x<0) {
vel.x = 0.0;
}
}
if (game.worldSquareAt(leftSideHigh)==World.TILE_S) {
pos.x = game.rightOfSquare(leftSideHigh)+wallProbeDistance;
if (vel.x>0) {
vel.x=0.0;
}
}
if (game.worldSquareAt(rightSideLow)==World.TILE_S) {
pos.x = game.leftOfSquare(rightSideLow)-wallProbeDistance;
if (vel.x>0) {
vel.x = 0.0;
}
}
if (game.worldSquareAt(rightSideHigh)== World.TILE_S) {
pos.x= game.leftOfSquare(rightSideHigh)-wallProbeDistance;
if (vel.x >0) {
vel.x = 0.0;
}
}
}
void checkForGettingCoin() {
PVector centerOfPlayer;
centerOfPlayer = new PVector(pos.x, pos.y-23/2);
if (game.worldSquareAt(centerOfPlayer)== World.TILE_C) {
game.setSquareAtToThis(centerOfPlayer, World.TILE_E);
coinsCollected++;
}
}
void movement() {
pos.add(vel);
checkCollision();
checkForGettingCoin();
}
void drawStuff() {
if (vel.x<-TRIVIAL_SPEED) {
facingRight = false;
}
else if (vel.x>TRIVIAL_SPEED) {
facingRight = true;
}
translate(pos.x, pos.y);
if (facingRight == false) {
scale(-1, 1); //flips horizontally by scaling by -100% on x axis
}
translate(-23/2, -23);
if (animFrame == 0) {
stroke(255);
ellipse(pos.x, pos.y, 10, 10);
}
else {
stroke(255);
ellipse(pos.x, pos.y, 10, 10);
}
}
}
class World {
int coinsInStage;//counts how many coins are in a lvl when we start it up
int coinRotateTimer;//number cycles, rotates coins in level
static final int TILE_E = 0;
static final int TILE_S = 1;
static final int TILE_C = 2;
static final int TILE_START = 3;
static final int TILE_END = 4;
static final int GRID_UNIT_SIZE = 50; //size, in pixels, of each world unit square
//next two need to match the dimentions of the example level grid below
static final int GRID_UNITS_WIDE = 25;
static final int GRID_UNITS_TALL = 25;
int [][] worldGrid = new int[GRID_UNITS_TALL][GRID_UNITS_WIDE];//game checks this one during play
//the game copies this into worldGrid each reset, returning coins that have since been cleared
int[][]start_Grid = {/* 1 */{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 2 */{1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 3 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 4 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
/* 18*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1},
/* 19*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1},
/* 20*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1},
/* 21*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1},
/* 22*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1},
/* 23*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1},
/* 24*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 1},
/* 25*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1} };
//changing up numbers in the grid make the level different (TILE_ above is the key)
World() {//gets called when world is created
coinRotateTimer = 0;//initializing coinRotateTimer to a reasonable start value
}
//returns what type of tile
int worldSquareAt(PVector thisPos) {
float gridSpotX = thisPos.x/GRID_UNIT_SIZE;
float gridSpotY = thisPos.y/GRID_UNIT_SIZE;
//first a boundary check, to avoid looking outside the grid
//if check goes out of bounds, treat it as a solid tile
if (gridSpotX<0) {
return TILE_S;
}
if (gridSpotX>=GRID_UNITS_WIDE) {
return TILE_S;
}
if (gridSpotY>=GRID_UNITS_TALL) {
return TILE_S;
}
if (gridSpotY<0) {
return TILE_S;
}
return worldGrid[int(gridSpotY)][int(gridSpotX)];
}
//replaces coins with empty spots
void setSquareAtToThis(PVector thisPos, int newTile) {
int gridSpotX = int(thisPos.x/GRID_UNIT_SIZE);
int gridSpotY = int(thisPos.y/GRID_UNIT_SIZE);
if (gridSpotX<0 || gridSpotX>=GRID_UNITS_WIDE || gridSpotY<Y || gridSpotY>=GRID_UNITS_TALL) {
return;//cant change grid units outside the grid
}
worldGrid[gridSpotY][gridSpotX] = newTile;
}
//these helper functions help us correct for the player moving into a world tile
float topOfSquare(PVector thisPos) {
int thisY = int(thisPos.y);
thisY /= GRID_UNIT_SIZE;
return float(thisY*GRID_UNIT_SIZE);
}
float bottomOfSquare(PVector thisPos) {
if (thisPos.y<0) {
return 0;
}
return topOfSquare(thisPos)+GRID_UNIT_SIZE;
}
float leftOfSquare(PVector thisPos) {
int thisX = int(thisPos.x);
thisX /= GRID_UNIT_SIZE;
return float(thisX*GRID_UNIT_SIZE);
}
float rightOfSquare(PVector thisPos) {
if (thisPos.x<0) {
return 0;
}
return leftOfSquare(thisPos)+GRID_UNIT_SIZE;
}
void reload() {
coinsInStage = 0;
for (int i=0;i<GRID_UNITS_WIDE;i++) {
for (int ii=0;ii<GRID_UNITS_TALL;i++) {
if (start_Grid[ii][i] == TILE_START) { //where player starts
worldGrid[ii][i] = TILE_E;
//then update the player spot to the center of the tile
doodMan.pos.x = i*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2);
doodMan.pos.y = ii*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2);
}
else {
if (start_Grid[ii][i]==TILE_C) {
coinsInStage++;
}
worldGrid[ii][i] = start_Grid[ii][i];
}
}
}
}
void render() { //draws world
//these cycle the number to make the coins spin
coinRotateTimer--;
if (coinRotateTimer<-GRID_UNIT_SIZE/2) {
coinRotateTimer = GRID_UNIT_SIZE/2;
}
for (int i=0;i<GRID_UNITS_WIDE;i++) {//for each column
for (int ii=0;ii<GRID_UNITS_TALL;ii++) {//for each tile in that column
switch(worldGrid[ii][i]) {//checks the type of tile
case TILE_S:
noStroke();
fill(255);
break;
case TILE_START:
noStroke();
fill(39, 203, 40);
break;
case TILE_END:
noStroke();
fill(203, 39, 39);
break;
default:
noStroke();
noFill();
}
//then draw a rectangle
rect(i*GRID_UNIT_SIZE, ii*GRID_UNIT_SIZE, //x,y of top left corner to draw rectangle
GRID_UNIT_SIZE-1, GRID_UNIT_SIZE-1);//width, height of retangle
if (worldGrid[ii][i]==TILE_C) {//if it's a coin, draw the coin
stroke(48);//dark gray outline
fill(255, 196, 0);
ellipse(i*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2), ii*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2), //center of grid spot
abs(coinRotateTimer), GRID_UNIT_SIZE/2); //spin size wide, 1/2 height of box
}
if (gameWon()) {
if (worldGrid[ii][i]==TILE_END) {
noStroke();
fill(203, 39, 39);
rect(i*GRID_UNIT_SIZE, ii*GRID_UNIT_SIZE,
GRID_UNIT_SIZE-1, GRID_UNIT_SIZE-1);
}
}
}
}
}
}
(Oh random note, I get errors when i use multiple tabs for classes and such, any idea why? like expected EOF instead found 'this'.)
I've been experimenting with a way to correspond numbers set up in a grid to a certain square and have that create a maze like game where you can collect coins and such.
When I go to run it i get this error:
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 25
at Grid_Test$World.reload(Grid_Test.java:423)
at Grid_Test.resetGame(Grid_Test.java:41)
at Grid_Test.setup(Grid_Test.java:36)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
I have no idea as to what this may be, or how its coming up. Any help would be greatly appreciated!
Thanks again to all who read this,
Nerial
*Oops forgot the code x.x*
Keyboard movement = new Keyboard();
Player doodMan = new Player();
World game = new World();
PFont font;
PImage splash;
int gameStartTimeSec,gameCurrentTimeSec;//keeps track of time from start to where its at now
int slide = 1;
boolean move = true;
void setup(){
size(500,500);
smooth();
frameRate(60);
font = loadFont("font.vlw");
splash = loadImage("splash.jpg");
resetGame();
}
void resetGame(){
doodMan.reset();
game.reload();
}
Boolean gameWon(){
return (doodMan.coinsCollected >= game.coinsInStage);
}
void outlinedText(String sayThis, float atX, float atY){
textFont(font);
fill(0);
text(sayThis, atX-1,atY);
text(sayThis, atX+1,atY);
text(sayThis, atX, atY-1);
text(sayThis, atX, atY+1);
fill(255);
text(sayThis, atX, atY);
}
void draw(){
if(slide == 1){
image(splash,0,0);
if(mousePressed && move){
slide = 2;
move = false;
}
}
else if (slide == 2){
pushMatrix();
game.render();
doodMan.inputCheck();
doodMan.movement();
doodMan.drawStuff();
popMatrix();
if(focused == false){
textAlign(CENTER);
outlinedText("Click around here to continue play! \n\nUse Arrow Keys to move.",width/2, height/2);
}else{
textAlign(LEFT);
outlinedText("Coins:"+doodMan.coinsCollected + "/" + game.coinsInStage,8,height-10);
if(gameWon()){
textAlign(CENTER);
outlinedText("You've made it though the maze!" +"with:"+doodMan.coinsCollected+ "Of:" + game.coinsInStage,width/2,height/2);
}
}
}
}
void keyPressed(KeyEvent evt){
movement.pressKey(evt.getKeyCode());
}
void keyReleased(KeyEvent evt){
movement.releaseKey(evt.getKeyCode());
}
void mouseReleased(){
move = true;
}
class Keyboard{
boolean hU,hR,hL,hD;
Keyboard(){
hU=hR=hL=hD = false;
}
void pressKey(int key){
if (key == KeyEvent.VK_R){ //will never be held down so no boolean
if(gameWon()){ //if the game has been won
resetGame(); //then R key will reset it
}
}
if(key == KeyEvent.VK_UP){
hU = true;
println("up is working");
}
if(key == KeyEvent.VK_LEFT){
hL = true;
println("left is working");
}
if(key == KeyEvent.VK_RIGHT){
hR = true;
println("right is working");
}
if(key == KeyEvent.VK_DOWN){
hD = true;
println("down is working");
}
}
void releaseKey(int key){
if(key == KeyEvent.VK_UP){
hU = false;
println("up released");
}
if(key == KeyEvent.VK_LEFT){
hL = false;
println("left released");
}
if(key == KeyEvent.VK_RIGHT){
hR = false;
println("right released");
}
if(key == KeyEvent.VK_DOWN){
hD = false;
println("down released");
}
}
}
class Player {
PVector pos, vel; //PVector contains two floats, x & y
Boolean facingRight;//used to keep track of which direction the player moved in last, flips player image
int animDelay;//timer between animations
int animFrame;//keeps track of which animation frame is currently shown
int coinsCollected;//a counter keeps track of how many coins have been collected
static final float RUN_SPEED = 3.0;//speed at which the player moves
static final float SLOWDOWN_PERC = 0.6;//friction from ground. multiplied by x speed each frame
static final int RUN_ANIMATION_DELAY = 3;//how many game cycles pass between animation updates
static final float TRIVIAL_SPEED = 1.0;//if under this, player seen as standing still
Player() {
facingRight = true;
pos = new PVector();
vel = new PVector();
reset();
}
void reset() { //by setting all to 0 it resets the game
coinsCollected = 0;
animDelay = 0;
animFrame = 0;
vel.x = 0;
vel.y = 0;
}
void inputCheck() {
float speedHere = RUN_SPEED;
float frictionHere = SLOWDOWN_PERC;
if (movement.hL) {
vel.x -= speedHere;
}
else if (movement.hR) {
vel.y += speedHere;
}
vel.x *= frictionHere; //causes player to continually lose speed
}
void checkCollision() {
int wallProbeDistance = int(23*0.4);
int ceilingProbeDistance = int(23*0.96);
//used as probes to detect running into walls, ceiling
PVector leftSideHigh, rightSideHigh, leftSideLow, rightSideLow, topSide;
leftSideHigh = new PVector();
rightSideHigh = new PVector();
leftSideLow= new PVector();
rightSideLow = new PVector();
topSide = new PVector();
//update wall probes
leftSideHigh.x = leftSideLow.x = pos.x - wallProbeDistance; // left edge of player
rightSideHigh.x = rightSideLow.x = pos.x + wallProbeDistance; // right edge of player
leftSideLow.y = rightSideLow.y = pos.y-0.2*23; // shin high
leftSideHigh.y = rightSideHigh.y = pos.y-0.8*23; // shoulder high
topSide.x = pos.x; // center of player
topSide.y = pos.y-ceilingProbeDistance; // top of guy
// if ( theWorld.worldSquareAt(topSide)==World.TILE_END ||
// theWorld.worldSquareAt(leftSideHigh)==World.TILE_END ||
// theWorld.worldSquareAt(leftSideLow)==World.TILE_END ||
// theWorld.worldSquareAt(rightSideHigh)==World.TILE_END ||
// theWorld.worldSquareAt(rightSideLow)==World.TILE_END ||
// theWorld.worldSquareAt(position)==World.TILE_END) {
// return;
if (game.worldSquareAt(topSide)==World.TILE_S) {
if (game.worldSquareAt(pos)==World.TILE_S) {
pos.sub(vel);
vel.x=0.0;
vel.y=0.0;
}
else {
pos.y = game.bottomOfSquare(topSide)+ceilingProbeDistance;
if (vel.y<0) {
vel.y = 0;
}
}
}
if (game.worldSquareAt(leftSideLow)==World.TILE_S) {
pos.x = game.rightOfSquare(leftSideLow)+wallProbeDistance;
if (vel.x<0) {
vel.x = 0.0;
}
}
if (game.worldSquareAt(leftSideHigh)==World.TILE_S) {
pos.x = game.rightOfSquare(leftSideHigh)+wallProbeDistance;
if (vel.x>0) {
vel.x=0.0;
}
}
if (game.worldSquareAt(rightSideLow)==World.TILE_S) {
pos.x = game.leftOfSquare(rightSideLow)-wallProbeDistance;
if (vel.x>0) {
vel.x = 0.0;
}
}
if (game.worldSquareAt(rightSideHigh)== World.TILE_S) {
pos.x= game.leftOfSquare(rightSideHigh)-wallProbeDistance;
if (vel.x >0) {
vel.x = 0.0;
}
}
}
void checkForGettingCoin() {
PVector centerOfPlayer;
centerOfPlayer = new PVector(pos.x, pos.y-23/2);
if (game.worldSquareAt(centerOfPlayer)== World.TILE_C) {
game.setSquareAtToThis(centerOfPlayer, World.TILE_E);
coinsCollected++;
}
}
void movement() {
pos.add(vel);
checkCollision();
checkForGettingCoin();
}
void drawStuff() {
if (vel.x<-TRIVIAL_SPEED) {
facingRight = false;
}
else if (vel.x>TRIVIAL_SPEED) {
facingRight = true;
}
translate(pos.x, pos.y);
if (facingRight == false) {
scale(-1, 1); //flips horizontally by scaling by -100% on x axis
}
translate(-23/2, -23);
if (animFrame == 0) {
stroke(255);
ellipse(pos.x, pos.y, 10, 10);
}
else {
stroke(255);
ellipse(pos.x, pos.y, 10, 10);
}
}
}
class World {
int coinsInStage;//counts how many coins are in a lvl when we start it up
int coinRotateTimer;//number cycles, rotates coins in level
static final int TILE_E = 0;
static final int TILE_S = 1;
static final int TILE_C = 2;
static final int TILE_START = 3;
static final int TILE_END = 4;
static final int GRID_UNIT_SIZE = 50; //size, in pixels, of each world unit square
//next two need to match the dimentions of the example level grid below
static final int GRID_UNITS_WIDE = 25;
static final int GRID_UNITS_TALL = 25;
int [][] worldGrid = new int[GRID_UNITS_TALL][GRID_UNITS_WIDE];//game checks this one during play
//the game copies this into worldGrid each reset, returning coins that have since been cleared
int[][]start_Grid = {/* 1 */{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 2 */{1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 3 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 4 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 5 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 6 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 7 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 8 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 9 */{1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/* 10*/{1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1},
/* 24*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 1},
/* 25*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1} };
//changing up numbers in the grid make the level different (TILE_ above is the key)
World() {//gets called when world is created
coinRotateTimer = 0;//initializing coinRotateTimer to a reasonable start value
}
//returns what type of tile
int worldSquareAt(PVector thisPos) {
float gridSpotX = thisPos.x/GRID_UNIT_SIZE;
float gridSpotY = thisPos.y/GRID_UNIT_SIZE;
//first a boundary check, to avoid looking outside the grid
//if check goes out of bounds, treat it as a solid tile
if (gridSpotX<0) {
return TILE_S;
}
if (gridSpotX>=GRID_UNITS_WIDE) {
return TILE_S;
}
if (gridSpotY>=GRID_UNITS_TALL) {
return TILE_S;
}
if (gridSpotY<0) {
return TILE_S;
}
return worldGrid[int(gridSpotY)][int(gridSpotX)];
}
//replaces coins with empty spots
void setSquareAtToThis(PVector thisPos, int newTile) {
int gridSpotX = int(thisPos.x/GRID_UNIT_SIZE);
int gridSpotY = int(thisPos.y/GRID_UNIT_SIZE);
if (gridSpotX<0 || gridSpotX>=GRID_UNITS_WIDE || gridSpotY<Y || gridSpotY>=GRID_UNITS_TALL) {
return;//cant change grid units outside the grid
}
worldGrid[gridSpotY][gridSpotX] = newTile;
}
//these helper functions help us correct for the player moving into a world tile
float topOfSquare(PVector thisPos) {
int thisY = int(thisPos.y);
thisY /= GRID_UNIT_SIZE;
return float(thisY*GRID_UNIT_SIZE);
}
float bottomOfSquare(PVector thisPos) {
if (thisPos.y<0) {
return 0;
}
return topOfSquare(thisPos)+GRID_UNIT_SIZE;
}
float leftOfSquare(PVector thisPos) {
int thisX = int(thisPos.x);
thisX /= GRID_UNIT_SIZE;
return float(thisX*GRID_UNIT_SIZE);
}
float rightOfSquare(PVector thisPos) {
if (thisPos.x<0) {
return 0;
}
return leftOfSquare(thisPos)+GRID_UNIT_SIZE;
}
void reload() {
coinsInStage = 0;
for (int i=0;i<GRID_UNITS_WIDE;i++) {
for (int ii=0;ii<GRID_UNITS_TALL;i++) {
if (start_Grid[ii][i] == TILE_START) { //where player starts
worldGrid[ii][i] = TILE_E;
//then update the player spot to the center of the tile
doodMan.pos.x = i*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2);
doodMan.pos.y = ii*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2);
}
else {
if (start_Grid[ii][i]==TILE_C) {
coinsInStage++;
}
worldGrid[ii][i] = start_Grid[ii][i];
}
}
}
}
void render() { //draws world
//these cycle the number to make the coins spin
coinRotateTimer--;
if (coinRotateTimer<-GRID_UNIT_SIZE/2) {
coinRotateTimer = GRID_UNIT_SIZE/2;
}
for (int i=0;i<GRID_UNITS_WIDE;i++) {//for each column
for (int ii=0;ii<GRID_UNITS_TALL;ii++) {//for each tile in that column
switch(worldGrid[ii][i]) {//checks the type of tile
case TILE_S:
noStroke();
fill(255);
break;
case TILE_START:
noStroke();
fill(39, 203, 40);
break;
case TILE_END:
noStroke();
fill(203, 39, 39);
break;
default:
noStroke();
noFill();
}
//then draw a rectangle
rect(i*GRID_UNIT_SIZE, ii*GRID_UNIT_SIZE, //x,y of top left corner to draw rectangle
GRID_UNIT_SIZE-1, GRID_UNIT_SIZE-1);//width, height of retangle
if (worldGrid[ii][i]==TILE_C) {//if it's a coin, draw the coin
stroke(48);//dark gray outline
fill(255, 196, 0);
ellipse(i*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2), ii*GRID_UNIT_SIZE+(GRID_UNIT_SIZE/2), //center of grid spot
abs(coinRotateTimer), GRID_UNIT_SIZE/2); //spin size wide, 1/2 height of box
}
if (gameWon()) {
if (worldGrid[ii][i]==TILE_END) {
noStroke();
fill(203, 39, 39);
rect(i*GRID_UNIT_SIZE, ii*GRID_UNIT_SIZE,
GRID_UNIT_SIZE-1, GRID_UNIT_SIZE-1);
}
}
}
}
}
}
(Oh random note, I get errors when i use multiple tabs for classes and such, any idea why? like expected EOF instead found 'this'.)
1