# Beginner needing help with coding sketch

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

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

1 Like

CrHallberg.com/CollisionDetection/Website/rect-rect.html

1 Like

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

Something like this

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

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

1 Like

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;
}
}``````
2 Likes

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

Thank you so much

2 Likes