I've been working on a grid array maze game and have run into a few issues yet again. After a while the ellipse being used as the "player" is glitching into and out of walls, and the collision for collecting the coins is on the left of the ellipse and I want it on the ellipse itself. Also the ellipse gets to a certain point then can only move left, no idea as to why yet either.
If you want to see the code that I used to make it this far it can be found here:
http://www.openprocessing.org/sketch/47845 I have modified it quite a bit so sorry for confusion if there is any :P
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 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
}
}
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;
}
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
//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'.)
So I'm definitely new to processing, as well as to programming in general. My professor for a class has us messing around with processing, and I myself find it fascinating. Anyway, I'm thoroughly confused as to how to get an ellipse to gain easing on all four sides of a constrain for starters. Another problem I'm having is that I would like lines to stop the ellipse and force you to traverse the maze to the end.
Below is the code I have so far. Any help is GREATLY appreciated.
float x;
float y;
int w=56;
int w2=56;
int w3=56;
int w4=56;
float easing = 0.03;
int ln;
int radius = 10;
int edge = 56;
int inner = edge + radius;