How we add an alert if a shape hit on the edge of a canvas? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How we add an alert if a shape hit on the edge of a canvas?

I'm developing a coin collecting game.so I need to add an alert("GAME OVER") if the coin COLLECTER hits the edge of the canvas.The size of canvas is 600×400. So How I can do this. https://code.sololearn.com/Wml847ad4Z6w/?ref=app

17th May 2023, 11:20 AM
Seniru Himanjana Devapriya
Seniru Himanjana Devapriya - avatar
2 Answers
+ 2
whenever you move the collector rectangle, check if its x position is greater than 0 and smaller than the canvas width (minus the rectangle size), likewise processed with the rectangle's y position.
17th May 2023, 1:25 PM
Lisa
Lisa - avatar
0
You'll need to think of an end game method, but doing something like the below will give you your alert (you have to update the other directions in a similar way). if(dir == 1) { if(x+100 < 600) { x += (speed * timePassed); } else { x = 250; speed = 0; dir = 0; alert("Game Over"); } }
17th May 2023, 1:26 PM
Ausgrindtube
Ausgrindtube - avatar