xxxxxxxxxx
float xPos, yPos, xVel, yVel;
boolean LEFT, DOWN, UP, RIGHT;
boolean isDraw;
void setup() {
size(500, 500);
background(0, 0, 0);
xPos = width / 2;
yPos = height / 2;
}
void draw() {
fill(200, 200, 200);
textAlign(CENTER, TOP);
textSize(64);
if (isDraw) {
text('DRAW MODE', width / 2, 0);
} else {
fill(0, 0, 0, 10);
rect(0, 0, width, height);
fill(200, 200, 200);
text('MOVE MODE', width / 2, 0);
}
fill(255, 0, 0);
stroke(200, 0, 0);
strokeWeight(5);
ellipse(xPos, yPos, 20, 20);
if (LEFT) {
xVel -= 0.5;
}
if (RIGHT) {
xVel += 0.5;
}
if (UP) {
yVel -= 0.5;
}
if (DOWN) {
yVel += 0.5;
}
xVel = constrain(xVel / 1.08, -10, 10);
yVel = constrain(yVel / 1.08, -10, 10);;
xPos += xVel;
yPos += yVel;
if (xPos + 10 >= width || xPos - 10 <= 0) {
if (xPos + 10 >= width) {
xVel = 0 - xVel - 10;
} else {
xVel = 0 - xVel + 10;
}
}
if (yPos + 10 >= height || yPos - 10 <= 0) {
if (yPos + 10 >= height) {
yVel = 0 - yVel - 10;
} else {
yVel = 0 - yVel + 10;
}
}
}
void keyPressed() {
if (key == 'w') {
UP = true;
}
if (key == 's') {
DOWN = true;
}
if (key == 'a') {
LEFT = true;
}
if (key == 'd') {
RIGHT = true;
}
if (key == ' ') {
fill(0, 0, 0);
rect(0, 0, width, 75);
isDraw = !isDraw;
}
}
void keyReleased() {
if (key == 'w') {
UP = false;
}
if (key == 's') {
DOWN = false;
}
if (key == 'a') {
LEFT = false;
}
if (key == 'd') {
RIGHT = false;
}
}