So I am thinking of having the students complete the extra challenge that involves keeping the red square from leaving the screen. I get the general solution to that using an and boolean.
However, as an additional challenge I was going to have them see if they can figure out how to keep the whole rectangle on the screen. Best I can figure out is you should test for 25, 25, 375, 375 and I believe this works. But I am struggling to figure out how I would explain why that is to students. Has anyone considering giving the students this type of challenge. I feel like if this was a game you wouldn’t want any part of the character in this case the rectangle to leave the screen. So this could be a good key learning thing for the students. I have attached my code that accomplishes this, but I am really struggling with why it is 25, 25, 375, 375. I would think it would be 50,50,350,350. I hope this makes sense and isn’t confusing.
// Create a sprite
var sprite = createSprite(200, 100, 50, 50);
sprite.shapeColor = “red”;
function draw() {
background(“white”);
// Move the sprite in the direction of the arrow keys
// But make sure it doesn’t go off the screen
if (keyDown(“up”) && sprite.y > 25) {
sprite.y = sprite.y - 5;
}
if (keyDown(“down”) && sprite.y < 375) {
sprite.y = sprite.y + 5;
}
if (keyDown(“left”) && sprite.x > 25) {
sprite.x = sprite.x - 5;
}
if (keyDown(“right”) && sprite.x < 375) {
sprite.x = sprite.x + 5;
}
drawSprites();
}