Rotating an image on Keyboard Input.
in
Programming Questions
•
24 days ago
Hello, I am absolutely brand new to Processing. Brand new.
I am just playing around and Im wondering if someone can help me get the code correct so that my image rotates on key input.
/**
* Easing.
*
* Move the mouse across the screen and the symbol will follow.
* Between drawing each frame of the animation, the program
* calculates the difference between the position of the
* symbol and the cursor. If the distance is larger than
* 1 pixel, the symbol moves part of the distance (0.05) from its
* current position toward the cursor.
*/
int angle;
int rectWidth;
PImage marioj;
PImage mario;
PImage img;
PImage bg;
PImage goomba;
PImage luigi;
PImage luigij;
float x;
float y;
float x1 = -20;
float easing = 0.05;
void setup() {
size(1000, 938);
noStroke();
imageMode(CENTER);
bg = loadImage("bg.png");
mario = loadImage("mario.png");
marioj = loadImage("mariojump.png");
img = loadImage("nothing.png");
goomba = loadImage("goomba.png");
angle = 0;
}
void draw() {
background(bg);
rotate(radians(angle));
image(goomba, x1+= 1,790);
if (mousePressed) {
image(marioj,mouseX,mouseY);
} else {
image(mario, mouseX,mouseY);
}
float targetX = mouseX;
float dx = targetX - x;
if(abs(dx) > 1) {
x += dx * easing;
}
float targetY = mouseY;
float dy = targetY - y;
if(abs(dy) > 1) {
y += dy * easing;
}
//ellipse(x, y, 66, 66);
imageMode(CENTER);
image(img, x, y);
}
void keyPressed() {
// int keyIndex = -1;
// if (key >= 'A' && key <= 'Z') {
// keyIndex = key - 'A';
// } else if (key >= 'a' && key <= 'z') {
// keyIndex = key - 'a';
// }
// if (keyIndex == -1) {
// // If it's not a letter key, clear the screen
// background(0);
// } else {
// // It's a letter key, fill a rectangle
// fill(millis() % 255);
// float x = map(keyIndex, 0, 25, 0, width - rectWidth);
// rect(x, 0, rectWidth, height);
// }
//my try============
if(keyCode == 37){ //arrows are coded so you can't call "key"
rotate(mouseX*PI); //left arrow rotates one way
} else if(keyCode == 39){
rotate(mouseY/PI); //right arrow rotates the other way
}
}
1