So for my class we are supposed to be making a game from someone elses template. Our group chose a bouncing game. We are trying to add in bouncing with the space key. We got the space key to work but sometimes when the space button is pressed the ball moves into mid jump already instead of starting a new jump.
Here is the code any help would be helpful.
// bounce
// bounce and roll the ball. avoid black squares.
// roll or bounce on multiple cyan and yellow squares to build up your scorw
float BALL_R=7; // ball radius
float BOUNCE_HEIGHT=0; // normal bounce height
float BOUNCE_STRIDE=10.0; // should be multiple of 20 (tile size)
float TILE_SIZE=20.0;
float hite=70.0; // camera height
int[][] tiles; // types of tiles
boolean [][] gotbonus; // whether or not tile has been touched. used to ensure a tile only gets counted once!
color[] palette; // tile colours
float xp,yp; // x and y positions
float yy, zz;
float xx=0;
int score;
int lives;
PFont font;
boolean gameover = false;
// used to build combos. certain tiles, when bounced on (but not rolled over) count towards a combo,
// which allows large scores to be built up if you hit several in a row. hitting any other type of tile
// is a combo breaker :)
float combo=1.0;
// used to flash screen red for a few frames after losing a life
boolean showFlash=false;
int FOR_FRAMES=10;
int flashCounter;
void setup() {
size(512,512,P3D);
reset();
palette=new color[]{
color(100), // black - avoid
color(255), // white - normal tile, breaks combo
color(255,255,0), // yellow - score, adds to combo
color(0,255,255), // cyan - double height bounce
color(255,0,0) // red - makes ball roll along ground
};
}
void reset() {
// reset game
tiles=new int[1000][6];
gotbonus =new boolean[1000][6];
for (int row=0;row<1000;row++) {
for (int col=0;col<6;col++) {
tiles[row][col]=1;
if (row>20 && random(0,10)>8) tiles[row][col]=0;
if (row>30 && random(0,100)>95) tiles[row][col]=2;
if (row>30 && random(0,100)>98) tiles[row][col]=3;
if (row>60 && random(0,100)>99) tiles[row][col]=4;
if (row>150 && random(0,25)>20) tiles [row][col]=0;
if (row>300 && random(0,50)>35) tiles [row][col]=0;
if (row>450 && random(0,50)>32) tiles [row][col]=0;
if (row>600 && random(0,75)>45) tiles [row][col]=0;
if (row>750 && random(0,100)>40) tiles [row][col]=0;
if (row>999 && random(0,100)>30) tiles [row][col]=0;
xx=0; //When the game starts this is the balls position
if (keyCode == LEFT )
{ xx+=4;}
if (keyCode == RIGHT)
{ xx-=4;}
if (keyCode == 65 )
{ xx+=4;}
if (keyCode == 68)
{ xx-=4;}
if (keyCode == 32)
{BOUNCE_HEIGHT=75.0f;
if (gameover) reset();}
if (keyCode == 78)
{lives++;}
}
//void mouseClicked() {
// if (gameover) reset();
//}
// background usually black, unless being 'flashed' briefly after losing a life
if (showFlash) {
// fade background red to black for a few frames
float flashamt=float(flashCounter)/float(FOR_FRAMES);
background(lerpColor(color(255,50,0),color(0),flashamt));
flashCounter--;
if (flashCounter==0) {
showFlash=false;
}
} else {
background(0);
}
// ball position
//float xx = 60-mouseX/4.25; //left-right
float yy = ypos+60+((300.0)/5.0); // front-back
float zz = BALL_R/2.0+(BOUNCE_HEIGHT*(float)Math.abs(Math.sin(frameCount/(BOUNCE_STRIDE*2)))); // up-down
// draw tiles, but only those in a visible subset
for (int col=0;col<6;col++) {
int x=-60+(20*col);
for (int row=0;row<1000;row++) {
int y=20*row;
// clip to visible region
if (y>ypos && y<ypos+500 && tiles[row][col]>0) {
fill(palette[tiles[row][col]],255-((y-ypos)/2));
rect(x,y,20,20);
}
}
}
fill(127);
// draw ball and shadow
if (!gameover) {
// ball shadow
// ellipse(xx,yy,20,20);
// draw player ball
pushMatrix();
translate(xx,yy,zz);
rotateX(-frameCount/20.0); // ball roll
sphereDetail(8);
fill(190,56,78);
sphere(BALL_R);
popMatrix();
// on the ground? if so, check for 'collisions'
if (zz <= BALL_R && !gameover) groundCheck(xx,yy);
// gameover?
if (gameover) {
pushMatrix();
translate(120,ypos+400,80);
rotateZ(PI);
rotateX((PI*3.0/2.2));
text("GAME OVER",50,-60);
text("Press space to restart",-40,-30);
popMatrix();
}
}
void groundCheck(float xx,float yy) {
// called whenever ball is on ground
//if (BOUNCE_HEIGHT>0.0) BOUNCE_HEIGHT=70.0; //reset bounce if not rolling along ground
// work out tile position underneath ball
int row=(int)((yy)/20.0);
int col=(int)((xx+60)/20.0);
int tiletype=0;
if (col<0 || col>=6) {
lives--;
if (lives==0) gameover=true;
} else {
tiletype=tiles[row][col];
if (!(gotbonus[row][col])) {
gotbonus[row][col]=true; // don't count a given tile more than once
switch (tiletype) {
case 0:
// fell down a hole
lives--;
if (lives==0) gameover=true;
combo=1.0;
BOUNCE_HEIGHT=0.0;
showFlash=true;
flashCounter=FOR_FRAMES;
break;
case 1:
// hit normal tile
score+=10;
combo=1.0;
BOUNCE_HEIGHT=0.0;
break;
case 2:
// hit gold tile
score+=25*combo;
tiles[row][col]=1;
BOUNCE_HEIGHT=0.0;
break;
case 3:
// cyan tile
score+=50*combo;
tiles[row][col]=1;
BOUNCE_HEIGHT=0.0;
break;
case 4:
// red tile
BOUNCE_HEIGHT=0.0;
combo=1.0;
lives++;
tiles[row][col]=1;
break;
default:
break;
}
}
}
}
this is my first post so sorry if I did something wrong