int ellipseRadius = 40;
int r = 0;
int b = 0;
int g = 0;
int rectPowerShowSize = 1;
float xVel = 1;
float yVel = 1;
//images
pImage img;
void setup() {
size(800, 400);
background(0);
frameRate(60);
img = loadImage('Golf_grass');
}
void draw() {
int rectRightEdge = ellipseXPos + ellipseRadius/2;
int rectLeftEdge = ellipseXPos - ellipseRadius/2;
if (gameStage == 0) {
rectMode(CENTER);
fill(200, 200);
rect(width/2, 160, 120, 55);
textSize(30);
textAlign(CENTER);
fill(255, 255, 0);
text("START", width/2, 175);
if (mousePressed) {
gameStage = 1;
}
}
if (gameStage == 1) { // the game
imageMode(CENTER);
image(img, width/2, height/2);
img.resize(800, 400);
//background(0)
fill(255);
ellipseXPos = ellipseXPos + xVel;
ellipseYPos = ellipseYPos + yVel;
rect(ellipseXPos, ellipseYPos, ellipseRadius, ellipseRadius);
rect(width/2, height/2, 50, 800);
xVel = xVel*0.94;
yVel = yVel*0.94;
if (dist(ellipseXPos, ellipseYPos, width/2, height/2) < ellipseRadius/2 +50/2) {
// background(255);
}
//when touch left and right edge, changes speed accordingly
if(ellipseXPos > width - ellipseRadius/2) {
xVel = xVel*-1;
}
if (ellipseXPos < ellipseRadius/2) {
xVel = xVel*-1;
}
//when touch top and bottom, changes speed accordingly
if(ellipseYPos > height - ellipseRadius/2) {
yVel = yVel*-1;
}
if (ellipseYPos < ellipseRadius/2) {
yVel = yVel*-1;
}
if (rectRightEdge > width/2 - 25 && rectRightEdge < width/2+25) {
if (xVel > 0) {
xVel *=-1;
}
}
if (rectLeftEdge < width/2 + 25 && rectLeftEdge > width/2-25) {
if (xVel < 0) {
xVel *=-1;
}
}
rectMode(CENTER);
fill(r, g, b);
r = abs(xVel*5+yVel*5);
g = abs(xVel*5+yVel*5);
b = abs(xVel*5+yVel*5);
rect (0, 0,80, 80);
if (mousePressed) {
if (xVel < 0.3 && yVel <0.3) {
xVel = mouseX/10-ellipseXPos/10;
yVel = mouseY/10-ellipseYPos/10;
// xVel *= -1;
//yVel *= -1;
// print(xVel);
}
}
} //if size == 1
}
I know that my code also checks for areas that are unnecessary to check but I can’t figure out how i can do it without that line.
Sometimes the collision is of and mty rect can travel through it.
my code:
if (rectRightEdge > width/2 - 25 && rectRightEdge < width/2+25) {
if (xVel > 0) {
xVel *=-1;
}
}
if (rectLeftEdge < width/2 + 25 && rectLeftEdge > width/2-25) {
if (xVel < 0) {
xVel *=-1;
}
}