This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
grand_mind1's profile picture

Boundary of screen

Started by grand_mind1, 06 April 2013 - 11:14 AM
grand_mind1 #1
Posted 06 April 2013 - 01:14 PM
I'm trying to make a game that uses the arrow keys to control the position of a character on the screen. I know how to detect the keys and move the player around the screen but if they are at the top (or any other side) and press the up arrow then it is going to go off the screen.(and probably produce an error because the position would be out of range, not sure though) I know I could just get the coordinate of every place on the side and not allow the player to use that button if it was at any of those coordinates but that would be a lot of variables and it would take up so much time. How would I get the boundary of the screen and not allow the player to go out of the screen?
Help is appreciated!
Thanks! :D/>
Smiley43210 #2
Posted 06 April 2013 - 04:10 PM
Use an if statement to check if moving a certain direction will cause the player position to exceed the width and/or height of the monitor/terminal size
Use an if statement to check if moving a certain direction will cause the player position to go below 1

Example:

local screenW, screenH = term.getSize() -- Or if you wrapped a monitor with peripheral.wrap(), use that

-- If moving player right
if playerX + 1 > screenW then
  -- Or shift the scenery and background left, if needed
  return false
else
  return true
end

-- If moving player up
if playerY + 1 < 1 then
  -- Or shift the scenery and background down, if needed
  return false
else
  return true
end