# Help with creating classes for my code

**URL:** <https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471>\
**Category:** Coding Questions\
**Created:** [January 5, 2023, 5:55pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471 "2023-01-05T17:55:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![EB1](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@EB1](https://discourse.processing.org/u/EB1)\
**Post date:** [January 5, 2023, 5:55pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/1 "2023-01-05T17:55:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [January 5, 2023, 6:00pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/2 "2023-01-05T18:00:50Z")

</div>

Hello @EB1

> [@EB1](#):
>
> I have attempted to move them into classes but I don’t know where I go wrong

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. 🙂

🤓

---

<div class="post-metadata">

**Author:** ![EB1](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@EB1](https://discourse.processing.org/u/EB1)\
**Post date:** [January 5, 2023, 6:08pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/3 "2023-01-05T18:08:16Z")

</div>

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);  
}  
}

/\>

---

<div class="post-metadata">

**Author:** ![EB1](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@EB1](https://discourse.processing.org/u/EB1)\
**Post date:** [January 5, 2023, 6:09pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/4 "2023-01-05T18:09:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [January 5, 2023, 8:33pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/5 "2023-01-05T20:33:01Z")

</div>

> [@EB1](#):
>
> void draw() {  
> image(img, x - radius, y - radius);  
> }

> [@EB1](#):
>
> void draw() {  
> image(img, x - radius, y - radius);  
> }

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:

> **[draw() / Reference](https://processing.org/reference/draw_.html)**
>
> Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called. draw()…

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. 🙂

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);`

> [@EB1](#):
>
> image(backgroundImg, 0, 0);

I hope this helps!  
🤓

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2023, 10:17pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/6 "2023-01-05T22:17:31Z")

</div>

> [@debxyz](#):
>
> void draw() sh’d only be called once, and only in the main sketch.

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.

---

<div class="post-metadata">

**Author:** ![EB1](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@EB1](https://discourse.processing.org/u/EB1)\
**Post date:** [January 6, 2023, 3:12pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/7 "2023-01-06T15:12:49Z")

</div>

@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);  
}  
}  
/\>

---

<div class="post-metadata">

**Author:** ![EB1](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@EB1](https://discourse.processing.org/u/EB1)\
**Post date:** [January 6, 2023, 3:23pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/8 "2023-01-06T15:23:41Z")

</div>

**update**

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

---

<div class="post-metadata">

**Author:** ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)\
**Post date:** [January 8, 2023, 2:43pm UTC](https://discourse.processing.org/t/help-with-creating-classes-for-my-code/40471/9 "2023-01-08T14:43:29Z")

</div>

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](https://discourse.processing.org/faq#format-your-code). Thanks!

![ezgif.com-gif-maker (1)](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/4/34521338de6902479873291389bf53a11d12237d.gif)
