I am try to make a sideways meteor dodging game where you are a spaceship that avoids asteroids that fly towards you from the left. My problem is that I have not been able to make a larger than 1 pixel hit box for the asteroids. Currently the player is 1 pixel and the asteroids are 1 pixel and my code only ends the game if they touch exactly. I would like help writing code so if the player is within 10 pixels of an asteroid on any side the game ends. Under here is my code, sorry if it’s a bit long.
void setup() {
size(800, 400);
noLoop(); // draw() will not loop
//start text
print ("press the mouse to start the game the up arrow moves your spaceship up and the down arrow moves it down");
}
// x for player
float x = 50 ;
// y for player
float y = 50;
// game end, triggered when you hit a meteor
float GameOver = 0;
//player score counter
float playerScore = 0;
// x’s for meteors
float A= 100;
float B= 100;
float C= 100;
float D= 100;
float E= 100;
float F = 100;
float G =100;
float H= 100;
float I=100;
float J = 100;
float K = 100;
float L= 100;
// Y’s for meteors
float AA= 50;
float BB = 60;
float CC = 90;
float DD =70;
float EE = 10;
float FF = 50;
float GG = 60;
float HH = 65;
float II = 80;
float JJ= 15;
float KK= 40;
float LL = 70;
void draw() {
background(200, 200, 200);
strokeWeight(1);
//score varaible
playerScore = playerScore + .6;
if (GameOver == 1) {
playerScore = playerScore - 0;
}
///code to move meteors across screen and reset them when they hit the left edge
A= A- .25;
if (A > width) {
A = 100;
}
B =B - .25;
if (B > width) {
B = 100;
}
C=C- .25;
if (C> width) {
C= 100;
}
D= D - .25;
if (D > width) {
D= 100;
}
E=E - .25;
if (E > width) {
E = 100;
}
F = F- .25;
if (F > width) {
F = 100;
}
G = G - .25;
if (G > width) {
G = 100;
}
H =H - .25;
if (H > width) {
H = 100;
}
I = I - .25;
if (I > width) {
I = 100;
}
J = J - .25;
if (J > width) {
J = 100;
}
K =K- .25;
if (K > width) {
K= 100;
}
L = L - .25;
if (L > width) {
L= 100;
}
// meteors
stroke (255);
// player
point(x, y);
stroke (0);
//meteors
point(A, AA);
point(B, BB);
point(C, CC);
point(D, DD);
point(E, EE);
point(F, FF );
point(G, GG);
point(H, HH);
point(I, II);
point(J, JJ);
point(K, KK);
point(L, LL);
// meteorcrash detector
while (X == A && Y == AA) {
print ("gameOver");
GameOver = 1;
}
while (X == B && Y == BB) {
print ("gameOver");
GameOver = 1;
}
while (X == C && Y == CC) {
print ("gameOver");
GameOver = 1;
}
while (X == D && Y == DD) {
print ("gameOver");
GameOver = 1;
}
while (X == E && Y == EE) {
print ("gameOver");
GameOver = 1;
}
while (X == F && Y == FF) {
print ("gameOver");
GameOver = 1;
}
while (X == G && Y == GG) {
print ("gameOver");
GameOver = 1;
}
while (X == H && Y == HH) {
print ("gameOver");
GameOver = 1;
}
while (X == I && Y == II ) {
print ("gameOver");
GameOver = 1;
}
while (X == J && Y == JJ ) {
print ("gameOver");
GameOver = 1;
}
while (X ==K && Y == KK ) {
print ("gameOver");
GameOver = 1;
}
while (X ==L && Y == LL) {
print ("gameOver");
GameOver = 1;
}
}
//player movement using arrow keys
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
y=y-1.5 ;
} else if (keyCode == DOWN) {
y=y+1.5;
} else if (keyCode == RIGHT) {
x=x+1.5 ;
} else if (keyCode == LEFT) {
x=x-1.5;
}
}
}
void mousePressed() {
loop();
}
void mouseReleased() {
loop();
}