I have a working game where the enemy moves towards a stationary target in the centre of the screen, I am struggling to put this code into classes I have attempted to put the code in classes but I go horribly wrong somewhere
<
float targetX, targetY; // coordinates of the targets
float enemyX, enemyY; // coordinates of the enemies
float speed; // speed at which the enemy moves
int radius = 25; // radius of the enemy
boolean enemyAlive; // true if the enemy is alive, false if it has been destroyed
boolean gameOver; // true if the game is over, false otherwise
int score; // current score
PImage backgroundImg; // image for the background
PImage enemyImg; // image for the enemies
PImage targetImg; // image for the target
void setup() {
size(400, 400);
targetX = 200;
targetY = 200;
enemyX = 50;
enemyY = 50;
speed = 1;
enemyAlive = true;
gameOver = false;
score = 0;
backgroundImg = loadImage(“space.jpg”);
enemyImg = loadImage(“enemy.png”);
targetImg = loadImage(“target.gif”);
}
void draw() {
image(backgroundImg, 0, 0);
if (!gameOver) {
if (enemyAlive) {
// move the enemy towards the target
float dx = targetX - enemyX;
float dy = targetY - enemyY;
float angle = atan2(dy, dx);
enemyX += cos(angle) * speed;
enemyY += sin(angle) * speed;
// check for collision between the enemy and the target
float distance = dist(targetX, targetY, enemyX, enemyY);
if (distance < radius) {
gameOver = true; // end the game
}
// draw the enemy
image(enemyImg, enemyX - radius, enemyY - radius, 50,50);
} else {
// enemy has been destroyed, spawn a new one
enemyX = random(0, width);
enemyY = random(0, height);
enemyAlive = true;
}
// draw the target
image(targetImg, targetX - 25, targetY - 25, 50,50);
// draw the score
fill(255);
textSize(32);
textAlign(LEFT);
text("SCORE: " + score, 10, 30);
} else {
// game is over si it will display “game over” message
fill(255);
textSize(32);
textAlign(CENTER);
text(“GAME OVER”, width/2, height/2);
}
}
void mousePressed() {
// checks if the mouse is on the enemy
float distance = dist(mouseX, mouseY, enemyX, enemyY);
if (distance < radius) {
enemyAlive = false; // destroy the enemy
score++; // increases the score the more user kills enemies
speed += 0.01; // increase the enemy’s speed
}
}
/>
please format code with </> button * homework policy * asking questions