# Putting code into classes

**URL:** <https://discourse.processing.org/t/putting-code-into-classes/40474>\
**Category:** Coding Questions\
**Created:** [January 5, 2023, 8:26pm UTC](https://discourse.processing.org/t/putting-code-into-classes/40474 "2023-01-05T20:26:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JB1](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@JB1](https://discourse.processing.org/u/JB1)\
**Post date:** [January 5, 2023, 8:26pm UTC](https://discourse.processing.org/t/putting-code-into-classes/40474/1 "2023-01-05T20:26:28Z")

</div>

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](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 6, 2023, 1:13pm UTC](https://discourse.processing.org/t/putting-code-into-classes/40474/2 "2023-01-06T13:13:51Z")

</div>

Hi

> [@How to work with classes in Processing](https://discourse.processing.org/t/how-to-work-with-classes-in-processing/40395):
>
> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147) Hi processing community. I would like to ask you how you can work with classes I already learned a little bit how to work with classes and function: \</\> Text text = new Text(); public class Text{ int x; int y; int r; String text = “”; int variable; void add(String text, int x, int y, int r){ textSize(r); text(text, x,y); } public void date(int viriable, int x, int y, int r){ textSize(r); text(viriable, x,y…

---

<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 6, 2023, 1:20pm UTC](https://discourse.processing.org/t/putting-code-into-classes/40474/3 "2023-01-06T13:20:52Z")

</div>

Please format your code as a courtesy to our community:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

---

<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:29pm UTC](https://discourse.processing.org/t/putting-code-into-classes/40474/4 "2023-01-08T14:29:08Z")

</div>


