Loading...
Logo
Processing Forum
Hey guys,

I'm pretty new to Processing and for my program that I am making I want to move a triangle with the directional arrows on the keyboard.I found a way to do it but the code is very long and it gots bugs because the triangle gets really weird shapes when i keep moving. 

This is my code for the moving triangle:

boolean up = true;
boolean down = false;
boolean right = false;
boolean left = false;
int X1 = 200;
int Y1 = 200;
int X2 = 220;
int Y2 = 160;
int X3 = 240;
int Y3 = 200;

void setup() {
  background(0);
  size(400, 400);
}

void draw() {
  background(0); 
  drawTriangle();
}
void drawTriangle() {
  fill(#FFFFFF);
  triangle(X1, Y1, X2, Y2, X3, Y3);
  if ( X1 == 1) {
    X1 = 0;
  }
}
void keyPressed() {
  if (keyCode == UP && up == true) {
    Y1= Y1 - 40;
    Y2 = Y2 - 40;
    Y3 = Y3 - 40;
  }
  if (keyCode == UP && down == true) {
    Y2 = Y2 - 40;
    Y1 = Y1 + 40;
    Y3 = Y3 + 40;
    up = true;
    down = false;
  }

  if (keyCode == UP && right == true) {
    Y1 = Y1 + 40;
    X2 = X2 - 20;
    Y2 = Y2 - 20;
    X3 = X3 + 40;
    up = true;
    right = false;
  }

  if (keyCode == UP && left == true) {
    X1 = X1 - 40;
    X2 = X2 + 20;
    Y2 = Y2 - 20;
    Y3 = Y3 + 40;
    left = false;
    up = true;
  }
  if (keyCode == DOWN && down == true) {
    Y1= Y1 + 40;
    Y2 = Y2 + 40;
    Y3 = Y3 + 40;
  }

  if (keyCode == DOWN && up == true) {
    Y2 = Y2 + 40;
    Y1 = Y1 - 40;
    Y3 = Y3 - 40;
    down = true;
    up = false;
  }

  if (keyCode == DOWN && right == true) {
    X1 = X1 + 40;  
    X2 = X2 - 20;
    Y2 = Y2 + 20;
    Y3 = Y3 - 40;
    down = true;
    right = false;
  }
  if (keyCode == DOWN && left == true) {
    Y1 = Y1 - 40;
    Y2 = Y2 + 20;
    X2 = X2 + 20;
    X3 = X3 - 40;
    left = false;
    down = true;
  }

  if (keyCode == RIGHT && right == true) {
    X1 = X1 + 40;
    X2 = X2 + 40;
    X3 = X3 + 40;
  }

  if (keyCode == RIGHT && up == true) {
    Y1 = Y1 - 40;
    X2 = X2 + 20;
    Y2 = Y2 + 20;
    X3 = X3 - 40;
    up = false;
    right = true;
  }

  if (keyCode == RIGHT && left == true) {
    X1 = X1 - 40;
    X2 = X2 + 40;
    X3 = X3 - 40;
    left = false;
    right = true;
  }

  if (keyCode == RIGHT && down == true) {
    Y1 = Y1 + 40;
    Y2 = Y2 - 20;
    X2 = X2 + 20;
    X3 = X3 - 40;
    down = false;
    right = true;
  }
  if (keyCode == LEFT && left == true) {
    X1 = X1 - 40;
    X2 = X2 - 40;
    X3 = X3 - 40;
  }  
  if (keyCode == LEFT && up == true) {
    X1 = X1 + 40;
    X2 = X2 - 20;
    Y2 = Y2 + 20;
    Y3 = Y3 - 40;
    left = true;
    up = false;
  }

  if (keyCode == LEFT && down == true) {
    Y1 = Y1 + 40;
    Y2 = Y2 - 20;
    X2 = X2 - 20;
    X3 = X3 + 40;
    left = true;
    down = false;
  }

  if (keyCode == LEFT && right == true) {
    X1 = X1 + 40;
    X2 = X2 - 40;
    X3 = X3 + 40;
    left = true;
    right = false;
  }
}


I hope that you guys can help me because i really want to finish this program.

Thank you very much!

Replies(4)



this...

Copy code
  1. int X1 = 200;
  2. int Y1 = 200;
  3. int X2 = 220;
  4. int Y2 = 160;
  5. int X3 = 240;
  6. int Y3 = 200;
  7. void setup() {
  8.   background(0);
  9.   size(400, 400);
  10. }
  11. //
  12. void draw() {
  13.   background(0);
  14.   drawTriangle();
  15. }
  16. //
  17. void drawTriangle() {
  18.   fill(#FFFFFF);
  19.   triangle(X1, Y1, X2, Y2, X3, Y3);
  20. }
  21. //
  22. void keyPressed() {
  23.   if (keyCode == UP) {
  24.     Y1 = Y1 - 40;
  25.     Y2 = Y2 - 40;
  26.     Y3 = Y3 - 40;
  27.   }
  28.   else if (keyCode == DOWN) {
  29.     Y1 = Y1 + 40;
  30.     Y2 = Y2 + 40;
  31.     Y3 = Y3 + 40;
  32.   }
  33.   else if (keyCode == RIGHT) {
  34.     X1 = X1 + 40;
  35.     X2 = X2 + 40;
  36.     X3 = X3 + 40;
  37.   }
  38.   else if (keyCode == LEFT) {
  39.     X1 = X1 - 40;
  40.     X2 = X2 - 40;
  41.     X3 = X3 - 40;
  42.   }
  43. }
  44. //

It works indeed. but i got one more problem i forgot to mention.
I want the triangle to point in the direction it's moving.
easiest solution woud be to create a class for the triangle, that stores the position and the rotation of the triangle.
Here is an example:
Copy code
  1. Tri t;      // the triangle

    void setup() {
      size(400, 400);
      t = new Tri(220, 220);
    }

    void draw() {
      background(0);
      // show the triangle
      t.display();
    }

    // triangle-class
    class Tri {
      float x, y;
      float rot;

      Tri(float x, float y) {
        this.x = x;
        this.y = y;
      }

      void display() {
        // color
        fill(#FFFFFF);
       
        pushMatrix();
       
        // go to the position of the triangle
        translate(x, y);
        // rotate
        rotate(rot);
        // draw the triangle
        triangle(-20, 20, 0, -20, 20, 20);
       
        popMatrix();
      }
    }

    // key input
    void keyPressed() {
      if (keyCode == UP) {
        t.y -= 40;
        t.rot= 0;
      }
      else if (keyCode == DOWN) {
        t.y += 40;
        t.rot= PI;
      }
      else if (keyCode == RIGHT) {
        t.x += 40;
        t.rot= PI*0.5;
      }
      else if (keyCode == LEFT) {
        t.x -= 40;
        t.rot= PI*1.5;
      }
    }

Thank you! 

You are my hero!
hero of the week Award goes out to benja for helping me out.
i dont understand the code yet and have to explain it on school
but i can try to figure it out! it works and thats important!
and is there a way to constrain it so it doesnt go out of the window?

Thanks again