# Beginner needing help with coding sketch

**URL:** <https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533>\
**Category:** Coding Questions\
**Created:** [May 24, 2019, 6:59am UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533 "2019-05-24T06:59:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelsierose94](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@kelsierose94](https://discourse.processing.org/u/kelsierose94)\
**Post date:** [May 24, 2019, 6:59am UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533/1 "2019-05-24T06:59:23Z")

</div>

Hi everyone!

I’m a beginner coder (as in, learnt a few months ago) and a project I’m working on requires me to make a mini game. Here is what I have:

[https://editor.p5js.org/kelsierose94/sketches/ha9-e5FAw](https://editor.p5js.org/kelsierose94/sketches/ha9-e5FAw)

the yellow ellipse following the cursor is the player, and the red ellipses are monsters (will add images later).

What I need to do but can’t for the life of me figure out, is have the player “die” when within a certain distance of the monsters. I separated each scene into states, with state 0 being the play screen, state 1 being the death screen, and state 2 being the win screen. I know that I need a line of code that uses the dist() function, and if the monsters is a certain distance to the cursor, it goes to state 1. I just can’t seem to figure out the code!

Any help would be hugely appreciated!!

Kels

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 24, 2019, 7:18am UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533/2 "2019-05-24T07:18:27Z")

</div>

> [@kelsierose94](#):
>
> I know that I need a line of code that uses the **dist()** function,

[CrHallberg.com/CollisionDetection/Website/rect-rect.html](http://CrHallberg.com/CollisionDetection/Website/rect-rect.html)

---

<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:** [May 24, 2019, 7:30am UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533/3 "2019-05-24T07:30:20Z")

</div>

if(dist(obj1x, obj1y, obj2x,obj2y) \< 44) state = 1;

Something like this

---

<div class="post-metadata">

**Author:** ![kelsierose94](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@kelsierose94](https://discourse.processing.org/u/kelsierose94)\
**Post date:** [May 24, 2019, 7:53am UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533/4 "2019-05-24T07:53:00Z")

</div>

I’ve tried this with

if (dist(mouseX, mouseY, monsters.x, monsters.y) \< 5) {  
state = 1;  
}

I have a class for the monsters with a this.x and this.y positions so I’m not sure why this isn’t working

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [May 24, 2019, 2:02pm UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533/5 "2019-05-24T14:02:34Z")

</div>

Your a brave soul for all that hard coding !

But besides my awful joke here’s a little class showcasing collision and one which can be used for instances of objects where each can have their colors set. You can use this to more expand your project. [Play sound example on collision](https://editor.p5js.org/InferNova/sketches/F4rtELZ_K)

---

<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:** [May 24, 2019, 4:45pm UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533/6 "2019-05-24T16:45:01Z")

</div>

> I have a class for the monsters with a this.x and this.y positions so I’m not sure why this isn’t working

I see

I think it’s better with && instead of ||

```
for (let i = 0; i < 3; i++) {
  if ((abs(mouseX-myMonsters[i].x) < 12) && (abs(mouseY-myMonsters[i].y) < 12)) {
    state = 1;
  }
}

```

---

<div class="post-metadata">

**Author:** ![kelsierose94](https://avatars.discourse-cdn.com/v4/letter/k/c89c15/32.png) [@kelsierose94](https://discourse.processing.org/u/kelsierose94)\
**Post date:** [May 26, 2019, 4:19am UTC](https://discourse.processing.org/t/beginner-needing-help-with-coding-sketch/11533/7 "2019-05-26T04:19:41Z")

</div>

Yes!! This worked!!! I knew it would have been something very simple!

Thank you so much 🙂
