I need help with a code i want to increase the speed of a moving object when the key “P” is pressed i cant find the code. I need this for a project
Here is my code
float x = 100;
float y = 100;
float xSpeed = 0;
float ySpeed = 0;
boolean keyLeft, keyRight, keyUp, keyDown;
void setup(){
size(600,600);
}
void draw(){
background(255);
fill(255,0,0);
rect(0,0,600,250);
fill(0,255,0);
rect(x, y, 50, 50);
countSpeed(); //Change speed based on current keys pressed.
changePosition(); //Change position based on speed.
xSpeed *= 0.9; //Apply some drag so that the square wouldn’t fly off the screen indefinitely
ySpeed *= 0.9;
}
void countSpeed(){
if(keyLeft) xSpeed-= 0.5;
if(keyRight) xSpeed+= 0.5;
if(keyUp) ySpeed-= 0.5;
if(keyDown) ySpeed+= 0.5;
}
void changePosition(){
x+=xSpeed;
y+=ySpeed;
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
keyUp = true;
}
if (keyCode == DOWN) {
keyDown = true;
}
if (keyCode == LEFT) {
keyLeft = true;
}
if (keyCode == RIGHT) {
keyRight = true;
}
}
}
void keyReleased() {
if (key == CODED) {
if (keyCode == UP) {
keyUp = false;
}
if (keyCode == DOWN) {
keyDown = false;
}
if (keyCode == LEFT) {
keyLeft = false;
}
if (keyCode == RIGHT) {
keyRight = false;
}
}
}