How do I let the tiles change color if my goomba stands on them?

edited January 2016 in Questions about Code

Hello, I am making a little game. I want my goomba (that brown figure from Mario) stand on some tiles and if he stands on them that they will change color. I searched the internet for something look-a-like, but I cannot find anything on how I should handle the problem. For my goomba I used a spritesheet. My tiles I made in processing.

So my question is, how do I know the location of my goomba and let it interact with the tiles?

int x,y;
boolean [] keys;
Goomba goomba;
Tiles tiles;

void setup() {
  size(400,400); //this is the size(w*bs,h*bs)
  goomba = new Goomba(); //this is my goomba
  tiles = new Tiles();
  keys = new boolean[128]; //this are my keys
  frameRate(60); //60 frames per second(default)
  smooth(); //to smooth everything
}

void draw() {
  background(0,255,0); //the color of the background
  move(); //the move option for my goomba
  tiles.drawTiles(); //the draw option for the tiles in the class Tiles
  goomba.drawGoomba(); //the draw option for my goomba in the class boomba
}

void move() { //letting my goomba move
  int xDelta = 0;
  int yDelta = 0;

  if (keys['w'])
    yDelta--;  //moves up
  if (keys['s'])
    yDelta++;  //moves right
  if (keys['a'])
    xDelta--;   //moves left
  if (keys['d'])
    xDelta++; //moves down

  goomba.updateGoomba(xDelta, yDelta); //it updates so it changes position
}

void keyPressed() {
  keys[key] = true;
}

void keyReleased() {
  keys[key] = false;
}

  class Goomba {
  float x,y;
  PImage spriteSheet;
  PImage [][] movement; //2D array
  boolean inMotion;
  int currentDirection;
  float currentFrame;
  final int UP = 0, RIGHT = 2, DOWN = 4, LEFT = 6; //cannot be changed

  Goomba() {
    inMotion = false;
    currentDirection = 2; //character faces to the right
    currentFrame = 0;
    x = 300;
    y = 300;  
    setupSprites();
  }


  void setupSprites() {
    movement = new PImage [8][9]; //my spritesheet is 8 by 9
    spriteSheet = loadImage("Goomba sprite sheet.png");
    for (int i=0; i < 9; i++) {
      movement[0][i] = spriteSheet.get(0 +40*i,0,40,33);
      movement[1][i] = spriteSheet.get(0 +40*i,39,40,34);
      movement[2][i] = spriteSheet.get(0 +40*i,78,40,33);
      movement[3][i] = spriteSheet.get(0 +40*i,117,41,34);
      movement[4][i] = spriteSheet.get(0 +40*i,156,40,33);
      movement[5][i] = spriteSheet.get(0 +40*i,195,40,34);
      movement[6][i] = spriteSheet.get(0 +40*i,234,40,33);
      movement[7][i] = spriteSheet.get(0 +40*i,273,41,34); //I only used 8 in stead of 9
    }
  }

  void drawGoomba() {
    if(inMotion)
      image(movement[currentDirection][1 + int(currentFrame)], x, y);
     else if (inMotion == false)
      image(movement[currentDirection][0], x, y);

    }


 void updateGoomba(int xDelta, int yDelta) {
   currentFrame = (currentFrame + 0.2) %7; //the 7 is for the last number of the movement
   inMotion = true;

   if(xDelta == 0 && yDelta ==0)
     inMotion = false;
   else if(xDelta == -1)
     currentDirection = LEFT;
   else if (xDelta == 1)
     currentDirection = RIGHT;   
   else if (yDelta == -1)
     currentDirection = UP;
   else if (yDelta == 1)
     currentDirection = DOWN;

   x = x + xDelta;
   y = y + yDelta;

   if(isGoombaOffScreen(x,y)){
     x = x - xDelta;
     y = y - yDelta;
   }
 } 

 boolean isGoombaOffScreen(float x, float y){
   if(x < 0 || x > width-41||
     y < 0 || y > height-39)
       return true;
       return false;
 }
}

class Tiles{
  int x,y;
  int w=40, h=40;
  boolean goombaOn;

  Tiles(){

   goombaOn = false;
  }



void drawTiles(){


  if(goombaOn == true)
  fill(0,0,255); //blue color
  else if(goombaOn == false)
 for (int x=0; x<40*10; x+=40) {
    for (int y=0; y<40*10; y+=40) {
      fill (255,0,80);
      rect (x, y, 40, 40);
      rect (x+40, y+40, 40, 40);
      rect(x+40,y,40,40);
      rect(x,y+40,40,40);
    }
  }  
}
void checkGoomba(){
  goombaOn = true;
  if (goomba.x > this.x && goomba.x < this.x + this.w && goomba.y > this.y && goomba.y < this.y + this.h){
  }
}
}

The spritesheet I used: Goomba sprite sheet

Answers

  • I will change this immediatly. I don't know why it ended up like this

  • edited January 2016

    It would be helpful to have your Goomba class code to answer your question. To separately change tiles it is better to also have a class Tile - would be much easier to manipulate.

  • Thank you Ater, I made a class for my tiles. But still do not know how to let the tile change color if the Goomba "steps" on it. But I am a lot closer now, thank you.

  • edited January 2016

    In your Tile class then have a method to check if Goomba is there:

    // on top of Tile class
    boolean goombaOn = false;
    
    void checkGoomba(Goomba g){
      if (g.x > this.x && g.x < this.x + this.width && .......){
      goombaOn = true;
      }
    }
    

    Now in method where you draw tile check if goombaOn and if so, use different color!

    Look, i didn't finish conditional and leaving this for you, if you have problems with that, show your Tile class and we'll help you adjust everything. (Note, you can ommit this, I put it just for clearness).

    Edit: Oh, and i could use boolean instead of void, like in your isGoombaOffScreen it would be more correct.

  • Hee Ater, I updated my code above. Processing does not registrate my goomba and he does not recognize the this.width.

  • edited January 2016

    It should recognize Goomba (you need however to call this method, passing your goomba variable into it), but of course will not see this.width, as there's no such variable in Tile class! If you post your spritesheet, so your code would be runnable for others without headache, we will be able to show you how to implement it exactly.

  • I changed my code again, but the tiles do not change color when my goomba stands on them.

Sign In or Register to comment.