Rotating a square using keyPressed
in
Programming Questions
•
1 month ago
I'm new to processing and I'm trying to make it so the square that is drawn will rotate when a button is pressed. I'm familiar with rotate() and translate(), I'm just unsure where to place the functions. I already have it so that when a button is pressed the square will either move horizontally or vertically or change shape or color. I even added mouse functions to drag the square and change the color randomly. The following is my code. Can anybody help?
int x = 300;
int y = 300;
int s = 50;
int boxcolor = color(0, 0, 255);
void setup()
{
size(600, 600);
background(255);
smooth(0);
rectMode(CENTER);
noStroke();
}
void draw()
{
noStroke();
background(255);
fill(boxcolor);
rect(x, y, s, s);
if (s <= 0)
{
stroke(255);
}
if (mousePressed && mouseButton == LEFT)
{
x = mouseX;
y = mouseY;
}
if (mousePressed && mouseButton == RIGHT)
{
boxcolor = color(random(0 , 256), random(0, 256), random(0 , 256));
}
}
void keyPressed()
{
if (key == 'e') //enlarge
{
s = s + 15;
}
if (key == 's') //smaller
{
s = s - 15;
}
if (key == 'r')
{
boxcolor = color(255, 0, 0);
}
if (key == 'g')
{
boxcolor = color(0, 255, 0);
}
if (key == 'b')
{
boxcolor = color(0, 0, 255);
}
if(key == CODED)
{
if(keyCode == UP)
{
y = y - 10;
}
if(keyCode == DOWN)
{
y = y + 10;
}
if(keyCode == LEFT)
{
x = x - 10;
}
if(keyCode == RIGHT)
{
x = x + 10;
}
}
if (s <= 0)
{
s = 0;
fill(255);
strokeWeight(0);
}
}
int x = 300;
int y = 300;
int s = 50;
int boxcolor = color(0, 0, 255);
void setup()
{
size(600, 600);
background(255);
smooth(0);
rectMode(CENTER);
noStroke();
}
void draw()
{
noStroke();
background(255);
fill(boxcolor);
rect(x, y, s, s);
if (s <= 0)
{
stroke(255);
}
if (mousePressed && mouseButton == LEFT)
{
x = mouseX;
y = mouseY;
}
if (mousePressed && mouseButton == RIGHT)
{
boxcolor = color(random(0 , 256), random(0, 256), random(0 , 256));
}
}
void keyPressed()
{
if (key == 'e') //enlarge
{
s = s + 15;
}
if (key == 's') //smaller
{
s = s - 15;
}
if (key == 'r')
{
boxcolor = color(255, 0, 0);
}
if (key == 'g')
{
boxcolor = color(0, 255, 0);
}
if (key == 'b')
{
boxcolor = color(0, 0, 255);
}
if(key == CODED)
{
if(keyCode == UP)
{
y = y - 10;
}
if(keyCode == DOWN)
{
y = y + 10;
}
if(keyCode == LEFT)
{
x = x - 10;
}
if(keyCode == RIGHT)
{
x = x + 10;
}
}
if (s <= 0)
{
s = 0;
fill(255);
strokeWeight(0);
}
}
1