Maze Game Simple Question

Here is the code that we have. We need to know how to make the dot go faster because when we start it moves slow. We just need to know how to increase the speed of the ellipse. We are beginners!

PImage GreenMaze;
int x = 162;
int y = 162;

void setup(){
GreenMaze = loadImage("GreenMaze.gif"); 
size(290, 290);
}

void draw()
{
background(255);
image(GreenMaze,0,0);
if((x > 155) && (x < 180) && (y < 10)){
textSize(48);
textAlign(CENTER);
fill(255,0,0);
text("YOU WIN!",width/2,height/2);
println(mouseX + "," + mouseY);
}

fill(255, 0, 0);
noStroke();
float touch = red(get(x,y));
ellipse(x,y, 10, 10);
if(touch <= 200){
x = 162;
y = 162;
println(mouseX + "," + mouseY);
}
}

void keyPressed(){
if ((key == CODED) && (keyCode == UP)){
y--;
} 
if ((key == CODED) && (keyCode == DOWN)){
y++;
} 
if ((key == CODED) && (keyCode == RIGHT)){
x++;
} 

if ((key == CODED) && (keyCode == LEFT)){
x--;
}
}
1 Like

Right now, when you press a key, your code may increase or decrease x or y (by 1). This is a good start.

What you could do instead is keep track yourself of which of the four movement keys are being pressed, and depeniding on which keys are pressed, move the player every frame, instead of every key press. This means you will need to use the keyReleased() function too!

For example:

boolean move_right, move_down;
float x, y;
float speed = 1.3;

void setup(){
  size(200,200);
}

void draw(){
  if( move_down ){
    y+=speed;
  }
  if( move_right ){
    x+=speed;
  }
  background(0);
  fill(0,200,0);
  rect(x,y,20,20);
}

void keyPressed(){
  if( keyCode == RIGHT ){
    move_right = true;
  }
  if( keyCode == DOWN ){
    move_down = true;
  }
}

void keyReleased(){
  if( keyCode == RIGHT ){
    move_right = false;
  }
  if( keyCode == DOWN ){
    move_down = false;
  }
}

You can add code for UP and LEFT yourself, and adjust the speed variable as you like! Notice that this provides a much smoother movement!

2 Likes