anyway.
When making the snake game this is not the way to go.
Better have a data structure (lists of rect properties) and let each rect know its position and whether its snake or apple or wall…
here is a new version, again with better lerp / amt
float mousePosX=100, mousePosY=100,
mousePosXFinal=100, mousePosYFinal=100;
boolean startUp=false;
float amt;
float maxAmt = 0.066; // WAS 0.025
int i=0;
float initalDist1;
void setup() {
size(500, 500);
background(255);
frameRate(20);
}
void draw() {
background(255);
// show grid
for (int i = 0; i < width; i += 50) {
line(i, 0, i, height);
}
for (int i = 0; i < height; i += 50) {
line(0, i, width, i);
}
// move red rect
float dist1 = dist(mousePosX, mousePosY, mousePosXFinal, mousePosYFinal);
// Are we in the starting phase?
if (startUp &&
amt<maxAmt &&
dist1>initalDist1/2) { //
// Yes, slowly accelerate
amt += 0.0011;
} else { // OR else if (amt>=0.025)
// No, we slowly decelerate
if (startUp)
println("hit");
startUp=false;
amt = map (dist1, 0, 900, 0.06, maxAmt);
}
ellipse(i++, height-(float)amt*1900, 5, 5);
mousePosX = lerp(mousePosX, mousePosXFinal, amt);
mousePosY = lerp(mousePosY, mousePosYFinal, amt);
if (dist1<0.029) {
if (amt>0)
println("Thank you");
amt=0;
mousePosX = mousePosXFinal;
mousePosY = mousePosYFinal;
}
// show red rect
fill(255, 0, 0);
rect(mousePosX, mousePosY, 50, 50);
text(amt, 31, 31);
}
// ---------------------------------------------------------
void mousePressed() {
// receive new target position
// search mousePosXFinal
for (int i = 0; i < width; i += 50) {
if (mouseX<=i+50) {
mousePosXFinal = i;
break;
}
}
//
// search mousePosYFinal
for (int i = 0; i < height; i += 50) {
if (mouseY<=i+50) {
mousePosYFinal = i;
break;
}
}
// reset
startUp=true;
amt=0;
i=0;
float dist1 = dist(mousePosX, mousePosY, mousePosXFinal, mousePosYFinal);
println(dist1);
initalDist1=dist1;
}
//