Help with creating classes for my code

I have created a simple game where enemies move towards a stationary target but I am struggling to separate the code and put them into classes. I want to have 2 classes called enemy and target. I have attempted to move them into classes but I don’t know where I go wrong

<
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
}
}
/>

Hello @EB1

Can you post your code attempt for the class(es)?
That way we can see the part you don’t currently understand and give specific guidance. :slightly_smiling_face:

:nerd_face:

sure

main class:
<
float speed; // speed at which the enemies move
boolean gameOver = false; // true if the game is over, false otherwise
int score; // current score
PImage backgroundImg; // image for the background

Enemy enemy; // enemy object
Target target; // target object

void setup() {
size(400, 400);

target = new Target(50, 50);
enemy = new Enemy(50, 50);
speed = 2;
gameOver = false;
score = 0;
backgroundImg = loadImage(“space.jpg”);
}

void draw() {
image(backgroundImg, 0, 0);

if (!gameOver) {
if (enemy.alive) {
enemy.update(target.x, target.y); // move the enemy towards the target
enemy.draw(); // draw the enemy
target.draw();
} else {
// enemy has been destroyed, spawn a new one
enemy = new Enemy(random(0, width), random(0, height));
score++;
}
}
}
/>

Enemy class:
<
class Enemy {
float x, y; // coordinates of the enemy
int radius = 25; // radius of the enemy
boolean alive; // true if the enemy is alive, false if it has been destroyed
PImage img; // image for the enemy

Enemy(float x, float y) {
this.x = x;
this.y = y;
alive = true;
img = loadImage(“enemy.png”);
}

void update(float targetX, float targetY) {
// move the enemy towards the target
float dx = targetX - x;
float dy = targetY - y;
float angle = atan2(dy, dx);
x += cos(angle) * speed;
y += sin(angle) * speed;

// check for collision between the enemy and the target
float distance = dist(targetX, targetY, x, y);
if (distance < radius) {
  gameOver = true;  // end the game
}

}

void draw() {
image(img, x - radius, y - radius);
}
}
/>

Target Class:
<
class Target {
float x, y; // coordinates of the target
int radius = 25; // radius of the target
PImage img; // image for the target

Target(float x, float y) {
this.x = x;
this.y = y;
img = loadImage(“target.gif”);
}

void draw() {
image(img, x - radius, y - radius);
}
}

/>

it works but the images are off-screen and doesn’t work like the first code I provided

Try replacing:
void draw()

with:
void display()

in both of your classes.

void draw() sh’d only be called once, and only in the main sketch.
See draw() entry in reference for more clarification:

I can’t test run your code bc I don’t have your png files… but start with addressing the use of draw to see if that clears up the issue. :slightly_smiling_face:

Also, I noticed in your set up with the background image, you may want to do:

background(backgroundImg); // size of image equal to size of sketch

Instead of:

image(backgroundImg, 0, 0);

I hope this helps!
:nerd_face:

it’s possible to name a method inside a class as draw(), because it doesn’t interfere with the draw in the main Sketch.

The function draw() in the class does run often, but it’s not calling the draw in the main Sketch.

1 Like

@debxyz @Chrisir ok so I was able to put the code into their classes but I am having a problem with the mousepressed() as I am not sure as to why it’s not working

here is all the code:

<
game class (main)

float targetX, targetY; // coordinates of the target
Enemy enemy;
boolean gameOver; // true if the game is over, false otherwise
int score; // current score
PImage backgroundImg; // image for the background
PImage targetImg; // image for the target

void setup() {
size(400, 400);
targetX = 200;
targetY = 200;
enemy = new Enemy(50, 50, 1, true, loadImage(“enemy.png”));
gameOver = false;
score = 0;
backgroundImg = loadImage(“space.jpg”);
backgroundImg.resize (width, height);
targetImg = loadImage(“target.gif”);
}

void draw() {
image(backgroundImg, 0, 0);

if (!gameOver) {
enemy.moveTowardsTarget(targetX, targetY);
if (enemy.checkCollisionWithTarget(targetX, targetY)) {
gameOver = true; // end the game
}
enemy.display();
new Target(targetX, targetY, targetImg).display();

// draw the score
fill(255);
textSize(32);
textAlign(LEFT);
text("SCORE: " + score, 10, 30);

} else {
// game is over so it will display “game over” message
fill(255);
textSize(32);
textAlign(CENTER);
text(“GAME OVER”, width/2, height/2);
}
}

enemy class:

class Enemy {
float x, y; // coordinates of the enemy
float speed; // speed at which the enemy moves
int radius = 25; // radius of the enemy
boolean alive; // true if the enemy is alive, false if it has been destroyed
PImage img; // image for the enemy

Enemy(float x, float y, float speed, boolean alive, PImage img) {
this.x = x;
this.y = y;
this.speed = speed;
this.alive = alive;
this.img = img;
}

// move the enemy towards the target
void moveTowardsTarget(float targetX, float targetY) {
float dx = targetX - x;
float dy = targetY - y;
float angle = atan2(dy, dx);
x += cos(angle) * speed;
y += sin(angle) * speed;
}

// check for collision between the enemy and the target
boolean checkCollisionWithTarget(float targetX, float targetY) {
float distance = dist(targetX, targetY, x, y);
return distance < radius;
}

// checks if the mouse is on the enemy
void checkMousePress(float mouseX, float mouseY) {
float distance = dist(mouseX, mouseY, x, y);
if (distance < radius) {
alive = false; // destroy the enemy
speed += 0.01; // increase the enemy’s speed
}
}

// draw the enemy
void display() {
image(img, x - radius, y - radius, 50,50);
}
}

target class:

class Target {
float x, y; // coordinates of the target
int radius = 25; // radius of the target
PImage img; // image for the target

Target(float x, float y, PImage img) {
this.x = x;
this.y = y;
this.img = img;
}

// draw the target
void display() {
image(img, x - radius, y - radius, 50,50);
}
}
/>

update

I think I fixed it but the enemy doesn’t get destroyed whenever I click on it but the score goes up

1 Like

We kindly ask you to please format your code. This will make it easier for other community members to help you. If you need help you can check the FAQ section on code formatting. Thanks!

ezgif.com-gif-maker (1)

1 Like