Need help with frogger

Hi guys,

I’m working on a processing game called Frogger. I created the world and the frog, but I could not move the frog. I am pretty sure there are some problems in my codes, but I just could not find the problems, could anyone check my codes, and tell me which part is wrong? Thank you sooooo much.

int level=1;
int bandHeight;
int bandWidth;
float x;
float y;
boolean up,down,left,right;
void setup()
{
fullScreen();
bandHeight=height/5;
bandWidth=width;
}

void draw()
{

drawWorld();
drawFrog(width/2,height/5*4.5,bandHeight/1.5);
}

void drawWorld()
{
fill(0);
rect(0,0,bandWidth,bandHeight);
for (int i=0;i<level+2;i++)
{
fill(0,0,100);
noStroke();
rect(0,bandHeight*(level+i),bandWidth,bandHeight);
}
fill(0);
rect(0,bandHeight*(level+3),bandWidth,bandHeight);
}

void drawFrog(float x, float y, float diam)
{
fill(0,100,0);
ellipse(x/1.08,y1.03,diam/2.5,diam/1.5);
ellipse(x
1.08,y1.03,diam/2.5,diam/1.5);
stroke(0);
ellipse(x,y,diam,diam);
fill(255);
ellipse(x/1.05,y/1.06,diam/3,diam/3);
ellipse(x
1.05,y/1.06,diam/3,diam/3);
fill(0);
ellipse(x/1.05,y/1.07,diam/5,diam/5);
ellipse(x*1.05,y/1.07,diam/5,diam/5);
}

void moveFrog(float xChange, float yChange)
{
y=y+yChangebandHeight;
x=x+xChange
bandHeight;
}

void keyPressed()
{
setkeys(keyCode,true);
println(keyCode);
}
void keyReleased()
{
setkeys(keyCode,false);
}
final void setkeys(int k, boolean decision)
{
if(k == ‘I’ | k == ‘W’)
{
moveFrog(0,-1);
up = decision;

}
else if (k == ‘S’| k ==‘K’)
{
moveFrog(0,1);
down = decision;
}
else if (k == ‘D’ | k == ‘L’)
{
moveFrog(1,0);
right = decision;
}
else if (k == ‘A’ | k==‘J’)
{
moveFrog(-1,0);
left = decision;
}
}

Step One is to format your code when you post it on the forums. Edit your post, select the code, and hit the MAGIC FORMAT LIKE CODE BUTTON, which looks like this: </>

After fixing a bunch of missing or broken symbols that got messed up because your code was not formatted like code when you posted it, I got your code to run.

It does, in fact, draw a frog.

So. WHERE does it draw the frog? Well, we know that the function that draws the frog - drawFrog() - is drawing it at some position that it takes as parameters.

So, if the frog doesn’t move, there are two things we can check:

  1. Make sure that the function that draws the frog actually uses the parameters it gets and draw the frog at that position.
  2. Make sure that the values passed to the function that draws the frog change somehow. I mean, if they don’t change, then the frog’s position will never change, and thus it will not move.

There really is no question what this issue is. If we look for the line in draw() that calls the function to draw the frog, we see that it is this line:

drawFrog(width/2, height/5*4.5, bandHeight/1.5);

Thus the frog will always be drawn at (width/2, height/5*4.5). And it will not move! Because it is always drawn in the same place!


So what really is the problem? Well, it’s a mix up. You have global variables called x and y that seem like they are where you want the frog to be. But inside the function that draws the frog, you don’t have access to these global variables, because you have LOCAL variables that have the same name. It’s a scope issue!

Let’s remove the local variables called x and y. Then we are always drawing the frog at (x,y) - and those are the GLOBAL variables called x and y.

Compare this code to yours!

int level=1;
int bandHeight;
int bandWidth;
float x;
float y;
boolean up, down, left, right;
void setup() {
  fullScreen();
  bandHeight=height/5;
  bandWidth=width;
  x = width/2;
  y = height/5*4.5;
}

void draw() {
  drawWorld();
  drawFrog( bandHeight/1.5);
}

void drawWorld() {
  fill(0);
  rect(0, 0, bandWidth, bandHeight);
  for (int i=0; i<level+2; i++) {
    fill(0, 0, 100);
    noStroke();
    rect(0, bandHeight*(level+i), bandWidth, bandHeight);
  }
  fill(0);
  rect(0, bandHeight*(level+3), bandWidth, bandHeight);
}

void drawFrog(float diam) {
  fill(0, 100, 0);
  ellipse(x/1.08, y*1.03, diam/2.5, diam/1.5);
  ellipse(x*1.08, y*1.03, diam/2.5, diam/1.5);
  stroke(0);
  ellipse(x, y, diam, diam);
  fill(255);
  ellipse(x/1.05, y/1.06, diam/3, diam/3);
  ellipse(x*1.05, y/1.06, diam/3, diam/3);
  fill(0);
  ellipse(x/1.05, y/1.07, diam/5, diam/5);
  ellipse(x*1.05, y/1.07, diam/5, diam/5);
}

void moveFrog(float xChange, float yChange) {
  y=y+yChange*bandHeight;
  x=x+xChange*bandHeight;
}

void keyPressed() {
  setkeys(keyCode, true);
  //println(keyCode);
}

void keyReleased() {
  setkeys(keyCode, false);
}

final void setkeys(int k, boolean decision) {
  if (k == 'I' | k == 'W') {
    moveFrog(0, -1);
    up = decision;
  } else if (k == 'S'| k =='K') {
    moveFrog(0, 1);
    down = decision;
  } else if (k == 'D' | k == 'L') {
    moveFrog(1, 0);
    right = decision;
  } else if (k == 'A' | k=='J') {
    moveFrog(-1, 0);
    left = decision;
  }
}

See how the drawFrog() function now only takes one parameter? It doesn’t need the position of the frog handed to it because that position is stored in the global variables x and y.


This code is not perfect, of course. Yes, the frog moves now. But look at what happens to him when he does…! I think you should take a better look at how it is being drawn, and how it being in a different position might effect where its component ellipses are being put… We’ll leave you to work on it. Post your code again if you need more help.

Thank u soooooooooooooooooooooo much!!

It was the first time post something in here. Sorry about the mess…
I managed to fix the problem by ur help!

Just wanna say thank u again, you really helped a lot! Thanks