+ 7

How could I make a barrier on the top and the bottom of the canvas so that the game piece can't go above the the green walls?

https://code.sololearn.com/WO84hylfGcl0/?ref=app

2nd Dec 2018, 5:52 PM
Abraham
Abraham - avatar
6 Answers
+ 4
in the functions moveup and movedown, add a condition to check if the object is at borders. for moveup, check if object's center y-coordinate WILL be MORE than the cube's height divided by 2 after subtracting 1 from it (cubeY - 1 > cubeH / 2), if it is, then allow moving with myGamePiece.speedY -= 1; for movedown, check if object's center y-coordinate WILL be LESS than the canvas' height minus object's height divided by 2 after subtracting 1 from it (cubeY + 1 < canvH - cubeH / 2), if it is, then allow moving with myGamePiece.speedY += 1; Same maths apply for all directions
2nd Dec 2018, 6:30 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
Thank you
3rd Dec 2018, 12:23 AM
Abraham
Abraham - avatar
+ 8
I'm still pretty new to coding, I understand what your saying but I'm not sure where to put the code you gave me in my code
3rd Dec 2018, 3:56 AM
Abraham
Abraham - avatar
+ 8
But I really like learning
3rd Dec 2018, 3:57 AM
Abraham
Abraham - avatar
+ 5
function moveup() { if (condition) { //move } } //there
9th Dec 2018, 12:36 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
(I also checked that obstacles spawn outside the canvas, you might want to fix that, and perhaps add a speedup so that game gets harder on higher scores)
2nd Dec 2018, 6:31 PM
Valen.H. ~
Valen.H. ~ - avatar