# Help with crash detection

**URL:** <https://discourse.processing.org/t/help-with-crash-detection/17595>\
**Category:** Coding Questions\
**Created:** [February 6, 2020, 8:33pm UTC](https://discourse.processing.org/t/help-with-crash-detection/17595 "2020-02-06T20:33:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoobs](https://avatars.discourse-cdn.com/v4/letter/z/278dde/32.png) [@zoobs](https://discourse.processing.org/u/zoobs)\
**Post date:** [February 6, 2020, 8:33pm UTC](https://discourse.processing.org/t/help-with-crash-detection/17595/1 "2020-02-06T20:33:48Z")

</div>

So i have a game i have a defender and aliens who are moving towards the defender and the defender is the user who avoids the alien. I need to add a collision detection so when the alien collides with the defender the game can end. I know that i need some sort of for loop to repeat a block of actions a certain amount number of times and i can detect a collision using `color test = get(x,y);` and then `if (test==Alien1)` would someone be able to help me implement and explain to me how i can do this. Many thanks. here is my code below

```auto
class Defender {
    int x, y;

    Defender(int x, int y) {
        this.x = x;
        this.y = y;
    }

    void render() {
        //draw a defender
        fill(255, 0, 0);
        rect(x, y, 50, 20);
        triangle(x + 50, y, x + 50, y + 20, x + 60, y + 10);
        fill(0, 0, 100);
        rect(x, y - 10, 20, 10);
    }

    boolean crash() {
        return true;
    }
}

    PImage background;
    int x = 0; //global variable background location

    Alien alien1;
    Alien alien2;
    Alien alien3;

    Defender user1;

    void setup() {
        size(800, 400);
        background = loadImage("spaceBackground.jpg");
        background.resize(width, height);

        alien1 = new Alien(800, 100, 5);
        alien2 = new Alien(800, 200, 5);
        alien3 = new Alien(800, 300, 5);

        user1 = new Defender(10, height / 2);

    }

    void draw() {
        drawBackground();
        alien1.move();
        alien1.render();
        alien2.move();
        alien2.render();
        alien3.move();
        alien3.render();
        user1.render();
    }

    void drawBackground() {
        image(background, x, 0); //draw background twice adjacent
        image(background, x + background.width, 0);
        x -= 4;
        if (x == -background.width)
            x = 0; //wrap background
    }

    void keyPressed() {

        if (key == CODED) {
            if (keyCode == UP) {
                user1.y = user1.y - 5;
            } else if (keyCode == DOWN) {
                user1.y = user1.y + 5;
            }
        }

    }

```

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [February 6, 2020, 9:00pm UTC](https://discourse.processing.org/t/help-with-crash-detection/17595/2 "2020-02-06T21:00:54Z")

</div>

You could put it in void draw() for the void loop, and maybe do something like this

```auto
void draw() {
background(whatever colour you're using);
//put collision detection here
if(collided) { //or, if Alien1/whatever boolean you choose
endGame();
}

void endGame() {
//put code here
//for when the player has collided
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 15, 2020, 5:12am UTC](https://discourse.processing.org/t/help-with-crash-detection/17595/3 "2020-02-15T05:12:25Z")

</div>

For an overview of collision detection in Processing, see either the Happy Coding guide or Thompson’s [http://www.jeffreythompson.org/collision-detection/](http://www.jeffreythompson.org/collision-detection/)
