# Beginner: help with classes and game

**URL:** <https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074>\
**Category:** Coding Questions\
**Created:** [February 24, 2020, 5:04pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074 "2020-02-24T17:04:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amprocessing](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@amprocessing](https://discourse.processing.org/u/amprocessing)\
**Post date:** [February 24, 2020, 5:04pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/1 "2020-02-24T17:04:40Z")

</div>

So I am making a small maze game. I want to add objects that when the ball touches it, they die. I have been practicing on another sketch. I’ve used classes for the aliens and the ball, as I thought this would be the best way to approach the situation, but I cant do it. Any ideas?

the is the main code

```auto
Alien a1;
Alien a2;
Ball ball;

void setup(){
  size(700,700);
  a1 = new Alien();
  a2 = new Alien();
  ball = new Ball();
}

void draw(){
  background(255);
  a1.type("alien1.png");
  a2.type("alien2.png");
  
  a1.display();
  a1.moveY();
  a2.display();
  a2.moveX();
  ball.display();
 
  }
  

```

this is the ball class

```auto
class Ball {

  void display() {
    fill(0,0,0);
    ellipse(mouseX,mouseY,30,30);
  }

}

```

this is the alien class

```auto
class Alien {
  String name;
  int positionX = 0;
  int positionY = 0;
  PImage alien;
  int dir = 1;
  
  
  
  Alien() {
  }
  
  void type(String type){
    alien = loadImage(type);
  }
  
  void display() {
    image(alien,positionX,positionY,35,35);
  }
  
  void moveX() {
    positionX = positionX + 1 * dir;
  }
  void moveY() {
    positionY = positionY + 1 * dir;
  }
  
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [February 24, 2020, 7:47pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/2 "2020-02-24T19:47:49Z")

</div>

I fixed a few problems with your code.

```auto
Alien[] aliens = new Alien[20];
Ball ball;
PImage[] imgs = new PImage[2];

void setup() {
  size(700, 700);
  imgs[0] = loadImage("alien1.png");
  imgs[1] = loadImage("alien2.png");
  
  for( int i = 0; i < aliens.length; i++){
    aliens[i] = new Alien(i%2);
  }
  ball = new Ball();
}

void draw() {
  background(255);
  for( int i = 0; i < aliens.length; i++){
    aliens[i].move();
    aliens[i].display();
  }  
  ball.display();
}

class Ball {
  void display() {
    fill(0);
    ellipse(mouseX, mouseY, 30, 30);
  }
}

class Alien {
  int kind;
  float positionX = random(width);
  float positionY = random(height);
  int dir = 1;
  Alien(int t) {
    kind = t;
  }
  void display() {
    image(imgs[kind%2], positionX, positionY, 35, 35);
  }
  void move() {
    if( kind == 0 ){ moveX(); }
    if( kind == 1 ){ moveY(); }
  }
  void moveX() {
    positionX = positionX + 1 * dir;
  }
  void moveY() {
    positionY = positionY + 1 * dir;
  }
}

```

Notice now that there is only one copy of each image. Also the aliens objects are now in an array. This lets me have more aliens very easily, because I can loop over the array to deal with all the aliens instead of having to call functions on each one individually.

---

<div class="post-metadata">

**Author:** ![amprocessing](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@amprocessing](https://discourse.processing.org/u/amprocessing)\
**Post date:** [February 25, 2020, 1:14pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/3 "2020-02-25T13:14:20Z")

</div>

Thanks. This is really helpful.  
How would I make it so when the ball and an alien touch, a message pops up? So I tried using an if statement, but I don’t know how to write the code. When I tried doing this, a error message would pop up saying that there was a type mismatch, but I couldn’t fix it.

---

<div class="post-metadata">

**Author:** ![amprocessing](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@amprocessing](https://discourse.processing.org/u/amprocessing)\
**Post date:** [February 25, 2020, 1:27pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/4 "2020-02-25T13:27:04Z")

</div>

Also, how would I write code to prevent the alien from leaving the screen by changing the direction. I kinda know how to do this but im not sure where in the code this would go.

---

<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:** [February 25, 2020, 2:52pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/5 "2020-02-25T14:52:27Z")

</div>

Here is an excellent tutorial by Abe Pazos on youtube on how to change directions when an object hits the edge of the screen.

And regarding where to place in your code. Go through your code and turn off each function one by one to see which function controls movement across the screen. That is where you’ll add the new code for changing direction.

[![]( " - YouTube") ](https://www.youtube.com/watch?v=-5M3Q6PGpsE)

---

<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:** [February 25, 2020, 3:02pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/6 "2020-02-25T15:02:41Z")

</div>

For when objects touch, in your case the ball and the alien:

You can **use the dist() function** outlined here in the processing reference:

> **[dist() / Reference](https://processing.org/reference/dist_.html)**
>
> Calculates the distance between two points.

And here’s another excellent youtube tutorial by Abe Pazos that **shows an example how to use the distance function:**

[![]( " - YouTube") ](https://www.youtube.com/watch?v=N5Ka4C3RVmw)

---

<div class="post-metadata">

**Author:** ![amprocessing](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@amprocessing](https://discourse.processing.org/u/amprocessing)\
**Post date:** [February 25, 2020, 10:58pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/7 "2020-02-25T22:58:19Z")

</div>

thanks this method worked perfectly.

---

<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:** [February 25, 2020, 11:01pm UTC](https://discourse.processing.org/t/beginner-help-with-classes-and-game/18074/8 "2020-02-25T23:01:58Z")

</div>

Glad to hear it worked out!!  
🙂
