so, now i have this:
int footstep = 254;
int footstep1 = 0;
int footstep2 = 255;
float accelX, accelY;
//soft-body dynamics
float organicConstant = 1;
float circleRadius = 10;
//buttons
int s1x = 0;
int s1y = 0;
int s1w = 680;
int s1h = 320;
int s2x = 480;
int s2y = 270;
int s2w = 20;
int s2h = 20;
//center point
float centerX = 0, centerY = 0;
float radius = 1, rotAngle = -45;
//obstacle1 properties
float rectX = 600;
float rectY = int(random(0,250));
float rectWidth = 10;
float rectHeight = 50;
float z = 100;
float speed = 5;
//obstacle2 properties
float rectX2 = -100;
float rectY2 = int(random(0,250));
float rectWidth2 = 10;
float rectHeight2 = 50;
float z2 = 100;
float speed2 = 6;
//obstacle3 properties
float rectX3 = -100;
float rectY3 = int(random(0,250));
float rectWidth3 = 10;
float rectHeight3 = 50;
float z3 = 100;
float speed3 = 7;
//obstacle4 properties
float rectX4 = -100;
float rectY4 = int(random(0,250));
float rectWidth4 = 10;
float rectHeight4 = 50;
float z4 = 100;
float speed4 = 8;
//cursor easing
float x;
float y;
float targetX, targetY;
float easing = 0.05;
//miscellaneous
int score = 0;
int level = 1;
int s = 50;
float springing = 0.0006, damping = 0.98;
int slide = 1;
int centX = 0;
int centY = 0;
//corner nodes
int nodes =4;
float nodeStartX[] = new float[nodes];
float nodeStartY[] = new float[nodes];
float[]nodeX = new float[nodes];
float[]nodeY = new float[nodes];
float[]angle = new float[nodes];
float[]frequency = new float[nodes];
//void setup
void setup() {
size(600, 300);
loop();
noStroke();
smooth();
int score = 0;
}
//void draw
void draw() {
redraw();
background(51);
fill(0);
textSize(12);
loop();
if(slide == 1){
gameintro();
}
else if(slide == 2) {
drawGame();
if((overButton(s1x, s1y, s1w, s1h))) {
slide = 2;
if (rectCircleIntersect(rectX, rectY, rectWidth, rectHeight, x, y, circleRadius) == true) {
slide = 3;
}
if(slide == 1){
gameintro();
}
}
}
else if(slide == 2) {
drawGame();
if((overButton(s1x, s1y, s1w, s1h))) {
slide = 2;
if (rectCircleIntersect(rectX2, rectY2, rectWidth2, rectHeight2, x, y, circleRadius) == true) {
slide = 3;
}
if(slide == 1){
gameintro();
}
}
}
else if(slide == 2) {
drawGame();
if((overButton(s1x, s1y, s1w, s1h))) {
slide = 2;
if (rectCircleIntersect(rectX3, rectY3, rectWidth3, rectHeight3, x, y, circleRadius) == true) {
slide = 3;
}
if(slide == 1){
gameintro();
}
}
}
else if(slide == 2) {
drawGame();
if((overButton(s1x, s1y, s1w, s1h))) {
slide = 2;
if (rectCircleIntersect(rectX4, rectY4, rectWidth4, rectHeight4, x, y, circleRadius) == true) {
slide = 3;
if(slide ==1){
gameintro();
}
}
}
}
if(slide == 3){
gameOver();
if((overButton(s1x, s1y, s1w, s1h))) {
slide = 3;
}
if((overButton(s2x, s2y, s2w, s2h))) {
slide = 2;
}
}
}
boolean overButton(int x, int y, int w, int h) {
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
return true;
}
else {
return false;
}
}
void mousePressed() {
if ((slide == 1)) {
if ((overButton(s1x, s1y, s1w, s1h)) == true) {
slide = 2;
loop();
}
else if ((slide == 3)) {
if ((overButton(s1x, s1y, s1w, s1h)) == true) {
slide = 1;
}
}
else if ((slide == 3)) {
if ((overButton(s2x, s2y, s2w, s2h)) == true) {
slide = 2;
}
}
}
}
void drawShape() {
// calculate node starting locations
for (int i=0; i<nodes; i++){
nodeStartX[i] = centerX+cos(radians(rotAngle))*radius;
nodeStartY[i] = -210 + centerY+sin(radians(rotAngle))*radius *300;
rotAngle += 360.0/nodes;
}
// draw polygon
curveTightness(organicConstant);
fill(0, footstep);
beginShape();
for (int i=0; i<nodes; i++){
curveVertex(nodeX[i], nodeY[i]);
}
for (int i=0; i<nodes-1; i++){
curveVertex(nodeX[i], nodeY[i]);
}
endShape(CLOSE);
}
void drawShape2() {
// calculate node starting locations
for (int i=0; i<nodes; i++){
nodeStartX[i] = centerX+cos(radians(rotAngle))*radius;
nodeStartY[i] = -210 + centerY+sin(radians(rotAngle))*radius *300;
rotAngle += 360.0/nodes;
}
// draw polygon
curveTightness(organicConstant);
fill(0, footstep1);
beginShape();
for (int i=0; i<nodes; i++){
curveVertex(nodeX[i], nodeY[i]);
}
//println("nodeX = " + nodeX[0] + " nodeStartY = " + nodeStartY[3] + "mouseX = " + mouseX + "mouseY" + mouseY);
for (int i=0; i<nodes-1; i++){
curveVertex(nodeX[i], nodeY[i]);
}
endShape(CLOSE);
}
void moveShape() {
//move center point
float deltaX = mouseX-centerX;
float deltaY = mouseY-centerY;
// create springing effect
deltaX *= springing;
deltaY *= springing;
accelX += deltaX;
accelY += deltaY;
// move predator's center
centerX += accelX;
centerY += accelY;
// slow down springing
accelX *= damping;
accelY *= damping;
// change curve tightness
organicConstant = 1-((abs(accelX)+abs(accelY))*.1);
//move nodes
for (int i=0; i<nodes; i++){
nodeX[i] = nodeStartX[i]+sin(radians(angle[i]))*(accelX *.5);
nodeY[i] = nodeStartY[i]+sin(radians(angle[i]))*(accelY *.5);
angle[i]+=frequency[i];
}
centX = (int) centerX;
centY = (int) centerY;
//println("deltaX =" + (centX) + "mouseX" + (mouseX-10));
// if((mouseX == centX) && (mouseY < centY)
// || (mouseX - 1 == centX) && (mouseY < centY) ||
// (mouseX -2 == centX) && (mouseY < centY)
// || (mouseX - 3 == centX) && (mouseY < centY) ||
// (mouseX -4 == centX) && (mouseY < centY)
// || (mouseX - 5 == centX) && (mouseY < centY) ||
// (mouseX -6 == centX) && (mouseY < centY)
// || (mouseX - 7 == centX) && (mouseY < centY) ||
// (mouseX -8 == centX) && (mouseY < centY)) {
// gameOver();
// mouseX = centX;
//
//
//
//
// }
}
void drawGame() {
redraw();
background(200);
fill(0);
textSize(12);
text( "LEVEL: " + level, 10, 20);
textSize(10);
fill(0);
text( "SCORE: " + score, 10, 35);
//fill(255, 100);
//rect(0,0,width, height);
score += 1;
if(score >= 200) {
s = 45;
level = 2;
rectX2 -= speed2;
if(rectX2 < -100) {
rectX2 = 600;
rectY2 = int(random(0,250));
}
if( score >= 400) {
s = 40;
level = 3;
rectX3 -= speed3;
if(rectX3 < -50) {
rectX3 = 650;
rectY3 = int(random(0,250));
}
}
if( score >= 600) {
s = 35;
level = 4;
rectX4 -= speed4;
if(rectX4 < -75) {
rectX4 = 650;
rectY4 = int(random(0,250));
}
}
if( score >= 700) {
s = 30;
level = 5;
springing = .0010;
}
if( score >= 1100) {
s = 25;
level = 6;
springing = .00105;
}
if( score >= 1500) {
s = 20;
level = 7;
springing = .0011;
}
if( score >= 2000) {
s = 15;
level = 8;
springing = .00115;
}
if( score >= 2600) {
s = 10;
level = 9;
springing = .0012;
}
if( score >= 3250) {
s = 5;
level = 10;
springing = .00125;
}
}
//cursor easing properties in void draw
targetX = mouseX;
float dx = targetX - x;
if(abs(dx) > 1) {
x += dx * easing;
}
targetY = mouseY;
float dy = targetY - y;
if(abs(dy) > 1) {
y += dy * easing;
}
//decrease value to move obstacles from right to left and appear again ramdomly on the right
rectX -= speed;
if(rectX < 0) {
rectX = 600;
rectY = int(random(0,250));
}
//collision between player(circle) and obstacle1
if (rectCircleIntersect(rectX, rectY, rectWidth, rectHeight, x, y, circleRadius) == true) {
gameOver();
}
else {
fill(255);
}
rect(rectX, rectY, rectWidth, rectHeight);
ellipse(x, y, 33, 33);
//collision between player(circle) and obstacle2
if (rectCircleIntersect(rectX2, rectY2, rectWidth2, rectHeight2, x, y, circleRadius) == true) {
gameOver();
}
else {
fill(255);
}
rect(rectX2, rectY2, rectWidth2, rectHeight2);
ellipse(x, y, 33, 33);
//collision between player(circle) and obstacle3
if (rectCircleIntersect(rectX3, rectY3, rectWidth3, rectHeight3, x, y, circleRadius) == true) {
gameOver();
}
else {
fill(255);
}
rect(rectX3, rectY3, rectWidth3, rectHeight3);
ellipse(x, y, 33, 33);
//collision between player(circle) and obstacle4
if (rectCircleIntersect(rectX4, rectY4, rectWidth4, rectHeight4, x, y, circleRadius) == true) {
gameOver();
}
else {
fill(255);
}
rect(rectX4, rectY4, rectWidth4, rectHeight4);
ellipse(x, y, 33, 33);
}
boolean rectCircleIntersect(float rx, float ry, float rw, float rh, float cx, float cy, float cr) {
float circleDistanceX = abs(cx - rx - rw/2);
float circleDistanceY = abs(cy - ry - rh/2);
if (circleDistanceX > (rw/2 + cr)) { return false; }
if (circleDistanceY > (rh/2 + cr)) { return false; }
if (circleDistanceX <= rw/2) { return true; }
if (circleDistanceY <= rh/2) { return true; }
float cornerDistance = pow(circleDistanceX - rw/2, 2) + pow(circleDistanceY - rh/2, 2);
return cornerDistance <= pow(cr, 2);
}
void gameintro() {
background(200);
fill(255);
stroke(0);
fill(0);
strokeWeight(1);
textSize(16);
text( " Circlavoider by Rui Rela ", 50,100);
text( " Avoid the obstacles ", 50, 200);
text( " Try to survive the longest you can ", 80, 230);
text( " Click anywhere to start :) ", 400, 275);
line( 400, 0, 400, 210);
stroke(0, 50);
line( 410, 0, 410, 210);
fill(0,0);
rect(s1x, s1y, s1w, s1h);
}
void gameOver() {
redraw();
background(200);
fill(255);
stroke(0);
fill(255,0,0);
rect(480,270,20,20);
//ellipse(270, 195, 25, 25);
fill(0);
strokeWeight(1);
textSize(16);
text( "GAME OVER", 50, height/2);
text( ":'(", 50, 200);
// text( "SCORE: " + score, 50, 200);
// text( "LEVEL: " + level, 50, 220);
text( "MOUSE ROLLOVER HERE TO RESTART ", 300, 250);
line( 400, 0, 400, 210);
stroke(0, 50);
line( 410, 0, 410, 210);
fill(0,0);
//stroke(0,0);
stroke(0, 0);
rect(s2x, s2y, s2w, s2h);
score = 0;
rectX2 = -100;
rectX3 = -100;
rectX4 = -100;
level = 1;
s = 50;
springing = .0006;
}
//delay(500000);
and i have another problem: only obstacle1 goes to gameover, other rectangles go to gameover for a sec, because is only when they touch the circle...whats missing?
(and I prefered not to create a class, I prefer doing like this)