How to resume movement once my sprite has hit a wall?

Hi, I've finally got my sprite to stop at a wall. Now, I'm having trouble figuring out a way to resume it's movement once it hits black. My goal is to make a maze game and I'm doing it by pixel colour. So if colour == black then stop movement... Also, when I hold a direction, the sprite keeps moving whether I am pressing a key or not. Any thoughts? Help? Ideas?

PImage sprite;
int x = 580;
int y = 200;
int black = -16777216;
int speed = 3;
void setup(){
  size(800, 600);
  smooth();
  frameRate(60);
  sprite = loadImage("scaled_sprite.png");
}

void draw(){
  fill(0);
  rect(0,0, 400,2000);
  fill(255);
  rect(400,0, 400,2000);
  image(sprite,x,y);

  color c = get(x,y);
  if(c != black ){
  if ((keyPressed == true) && (key == 'w')) {
    y-=speed;
  }
  if ((keyPressed == true) && (key == 'a')) {
    x-=speed;
  }
   if ((keyPressed == true) && (key == 's')) {
    y+=speed;
  }
    if ((keyPressed == true) && (key == 'd')) {
    x+=speed;
  }
  }



println(c);
}

Answers

  • Your issue is that once the sprite hits a wall, it cannot get away from the wall because all movement was stopped. You need to change the code so that your sprite can still move in a direction that is not blocked by a wall.

Sign In or Register to comment.