transformations/keyPressed
in
Programming Questions
•
10 months ago
I need help on the void keyPressed() part, any suggestions;
float x;
float y;
float xSpeed;
float ySpeed;
float theta;
float rotationSpeed;
float gravityFactor;
boolean drawSquare = true;
void initialize()
{
color c;
x = 0;
y = 0;
xSpeed = 1.0;
ySpeed = 1.5;
c = generateRandomColor();
fill(c);
rotationSpeed = radians(2);
gravityFactor = 0.1;
// fill(generateRandomColor());
}
void setup()
{
size(300, 300);
initialize();
}
void mousePressed()
{
initialize();
}
void draw()
{
background(255);
if (drawSquare == true) {
pushMatrix();
translate(x, y);
rotate(theta);
rectMode(CENTER);
rect(0, 0, 20, 20);
popMatrix();
}
else
{
pushMatrix();
translate(x, y);
rotate (theta);
triangle(-20, -10, -20, 10, 20, 0);
popMatrix();// push, translate, rotate, draw triangle, pop
}
x = x + xSpeed;// Add the x speed to the x position
y = y + ySpeed;// Add the y speed to the y position
ySpeed = ySpeed + gravityFactor;// Add the gravity to the y speed
theta = theta + rotationSpeed;//Add the speed of rotation to θ
if (x < 0 || x > width)
{
xSpeed = -xSpeed;
}
if (y >= height)
{
ySpeed *= -0.9;
}
}
// If the x position is off-screen, reverse the x speed.
// If the y position is greater than the height of the
// sketch, set the y speed to -0.9 times its current value.
void keyPressed()
{
//If the user presses the s or S key, your program will set the drawSquare variable to true to indicate that the program should draw squares.
//If the user presses the t or T key, your program will set the drawSquare variable to false to indicate that the program should draw triangles.
//If the user presses the c or C key, your program will call the generateRandomColor() function to change the fill color for the current shape.
//This function will do nothing for any other keypresses.
}
color generateRandomColor()
{
color result;
result = color(random(0, 192), random(0, 192), random(0, 192));
return result;
}
float x;
float y;
float xSpeed;
float ySpeed;
float theta;
float rotationSpeed;
float gravityFactor;
boolean drawSquare = true;
void initialize()
{
color c;
x = 0;
y = 0;
xSpeed = 1.0;
ySpeed = 1.5;
c = generateRandomColor();
fill(c);
rotationSpeed = radians(2);
gravityFactor = 0.1;
// fill(generateRandomColor());
}
void setup()
{
size(300, 300);
initialize();
}
void mousePressed()
{
initialize();
}
void draw()
{
background(255);
if (drawSquare == true) {
pushMatrix();
translate(x, y);
rotate(theta);
rectMode(CENTER);
rect(0, 0, 20, 20);
popMatrix();
}
else
{
pushMatrix();
translate(x, y);
rotate (theta);
triangle(-20, -10, -20, 10, 20, 0);
popMatrix();// push, translate, rotate, draw triangle, pop
}
x = x + xSpeed;// Add the x speed to the x position
y = y + ySpeed;// Add the y speed to the y position
ySpeed = ySpeed + gravityFactor;// Add the gravity to the y speed
theta = theta + rotationSpeed;//Add the speed of rotation to θ
if (x < 0 || x > width)
{
xSpeed = -xSpeed;
}
if (y >= height)
{
ySpeed *= -0.9;
}
}
// If the x position is off-screen, reverse the x speed.
// If the y position is greater than the height of the
// sketch, set the y speed to -0.9 times its current value.
void keyPressed()
{
//If the user presses the s or S key, your program will set the drawSquare variable to true to indicate that the program should draw squares.
//If the user presses the t or T key, your program will set the drawSquare variable to false to indicate that the program should draw triangles.
//If the user presses the c or C key, your program will call the generateRandomColor() function to change the fill color for the current shape.
//This function will do nothing for any other keypresses.
}
color generateRandomColor()
{
color result;
result = color(random(0, 192), random(0, 192), random(0, 192));
return result;
}
1