r/p5js • • Nov 07 '22

Please help with my code!!

https://editor.p5js.org/jast6661/sketches/KvEfn7DX9

Here's my code, I want to make it so that when one of the rectangles hits the circle the game like pauses, like a game over kind of thing, need this ASAP!!!

0 Upvotes

11 comments sorted by

2

u/forgotmyusernamedamm Nov 07 '22

This is a great site for all your collision detection needs.
https://www.jeffreythompson.org/collision-detection/

1

u/Still-Opening-2792 Nov 07 '22

ok sick ! and what about making the screen turn green when the yellow ball gets to the red ball?

1

u/youdonthaveafuture Nov 07 '22

What have you tried? There are lots of good articles on collision detection, ex: https://developer.mozilla.org/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript/Collision_detection

1

u/Still-Opening-2792 Nov 07 '22

i haven't tried really a lot, i am just sort of lost in this class and just need to make it so that if the ball is hit by the square, then the game ends

1

u/Kinrany Nov 07 '22

You have two separate things to solve:

  • only run the game logic while the game is still running. When it's over, show a different thing instead. You can have a button/key for ending the game while you're debugging

  • detect a collision

1

u/Still-Opening-2792 Nov 07 '22

could you help me with that ? i’m just so lost as it is

1

u/Kinrany Nov 07 '22

What have you tried? What's one thing you don't know how to do?

1

u/Still-Opening-2792 Nov 07 '22

i’ve tried function collisionDetection but don’t know how to word it right, this was a partner project so I just need to do my part which is collision and winning condition

3

u/Kinrany Nov 07 '22

There is no such function in p5js. There are some libraries, but doing the math yourself will be easier than including a library in the p5 editor.

2

u/[deleted] Nov 07 '22

You could work with coordinates. If the coordinates of the circle and a bus overlap, it means they collided. This is probably the easiest way to do collisions with p5.

Once you detect a collision, you can just put a fail screen above everything. Then, make it so when fail = true, the user can press enter (Or any key you like) to trigger location.reload. Not the cleanest way to do this, but it works :P

1

u/lavaboosted Nov 07 '22

Here's an example of circle - rectangle collision https://editor.p5js.org/rjgilmour/sketches/AXT20XUp5