how to move a triangle with keyCode?
in
Programming Questions
•
1 year ago
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!
1