+ 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?
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
+ 8
Thank you
+ 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
+ 8
But I really like learning
+ 5
function moveup() {
if (condition) {
//move
}
}
//there
+ 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)