Hi guys,
I am creating a game called frogger, and I’m confused about why my codes not working. Hope get some help here.
I created the hazards, but I can’t move them, tried many ways to fix but still failed :< (move to the right for bottom line of hazards, move opposite direction for line above, there were 3 lines of hazards for level 1. Number of lines changed when level increase.)
Here are my codes without movement, I deleted my movement codes just to make my codes looks more clear.
float level = 1;
float bandHeight;
float BANDWIDTH;
float middleBand_y;
final int BAND_X = 0;
final int TOPBAND_Y = 0;
float bottomBand_y;
float frog_X;
float frog_Y;
float frogSize;
float edge;
float radius;
float inner;
float x0;
float x1;
float x2;
void setup()
{
fullScreen();
bandHeight = height/(level+4);
BANDWIDTH = width;
frog_X = width/2;
frog_Y = height-bandHeight/2;
frogSize = bandHeight/2;
radius = frogSize/100;
edge = frogSize;
inner = edge+radius;
}
void draw()
{
drawWorld();
drawFrog(frog_X,frog_Y,frogSize);
drawHazards();
}
void drawWorld()
{
for (int i=1;i<level+3;i++)
{
fill(0,0,255);
stroke(0,0,255);
rect(BAND_X,middleBand_y,BANDWIDTH,bandHeight);
middleBand_y = bandHeight*i;
}
fill(0);
stroke(0);
rect(BAND_X,TOPBAND_Y,BANDWIDTH,bandHeight);
rect(BAND_X,bottomBand_y,BANDWIDTH,bandHeight);
bottomBand_y = height-bandHeight;
}
void drawFrog(float x, float y, float diam)
{
fill(0,100,0);
ellipse(x,y,diam,diam);
}
void moveFrog(float xChange, float yChange)
{
if (objectInCanvas(frog_X, frog_Y, frogSize))
{
frog_X = frog_X+xChange;
frog_Y = frog_Y+yChange;
frog_X = constrain(frog_X,inner,width-inner);
frog_Y = constrain(frog_Y,inner,height-inner);
}
}
void keyPressed()
{
if ( key == 'w' || key == 'i')
moveFrog(0,-bandHeight);
if ( key == 's' || key == 'k')
moveFrog(0,bandHeight);
if ( key == 'a' || key == 'j')
moveFrog(-bandHeight,0);
if ( key == 'd' || key == 'l')
moveFrog(bandHeight,0);
}
boolean objectInCanvas(float x, float y, float diam)
{
boolean InCanvas = true;
if ( x < 0 || x > width || y < 0 || y > height || diam < 0)
InCanvas = false;
return InCanvas;
}
void drawHazard(int type, float x, float y, float size)
{
if (type == 0)//draw type 0 hazard when 0 is called
{
fill(255,0,0);
ellipse(x,y,size,size);
}
if (type == 1)
{
fill(0);
rect(x,y,size,size);
}
if (type == 2)
{
fill(255);
ellipse(x,y,size,size);
}
}
void drawHazards()
{
for (int j=0;j<width;j++)//
{
for (float i=0;i<level+2;i++)
{
float y0 = (height-bandHeight*1.5)-i*(bandHeight*3);
if (y0 < bandHeight)
{
y0 = height-bandHeight*1.5;
}
float y1 = (height-bandHeight*2.8)-i*(bandHeight*3);
if (y1 < bandHeight)
{
y1 = height-bandHeight*2.8;
}
float y2 = (height-bandHeight*3.5)-i*(bandHeight*3);
if (y2 < bandHeight)
{
y2 = height-bandHeight*3.5;
}
x0 = j*(bandHeight*3);
x1 = j*(bandHeight*4)-((bandHeight-50)/2);
x2 = j*(bandHeight*5);
drawHazard(0,x0,y0,bandHeight/1.5);
drawHazard(1,x1,y1,bandHeight/1.5);
drawHazard(2,x2,y2,bandHeight/1.5);
}
}
}