Computer Science Canada How to make an object not go outside of the size of your screen? |
Author: | sakibur001 [ Thu Dec 15, 2016 6:47 pm ] | ||
Post subject: | How to make an object not go outside of the size of your screen? | ||
What is it you are trying to achieve? I want my box in the program to not go outside of the screen. What is the problem you are having? It moves when you click arrow keys but it goes outside of the screen if you keep moving. I want the program to not make the box go outside of the screen. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) %Declaration section var startpointx, startpointy : int := 0 var endpointx, endpointy : int := 50 var key : string (1) %set screen mode and size setscreen ("graphics:400;400") setscreen ("noecho") setscreen ("nocursor") setscreen ("offscreenonly") procedure display drawfillbox (startpointx, startpointy, endpointx, endpointy, white) drawfillbox (startpointx + 1, startpointy + 1, endpointx - 1, endpointy - 1, blue) getch (key) if key = chr (200) then startpointy := startpointy + 1 endpointy := endpointy + 1 elsif key = chr (208) then startpointy := startpointy - 1 endpointy := endpointy - 1 elsif key = chr (205) then startpointx := startpointx + 1 endpointx := endpointx + 1 else if key = chr (203) then startpointx := startpointx - 1 endpointx := endpointx - 1 end if end if View.Update end display %Main Program loop display exit when key = chr (27) end loop
Please specify what version of Turing you are using <Answer Here> |
Author: | Gaming Lyfe [ Fri Dec 16, 2016 6:57 pm ] |
Post subject: | RE:How to make an object not go outside of the size of your screen? |
Here is a solution. Think about the code: if startpointx> maxx then startpointx:= maxx elsif startpointx < 0 then startpointx := 0 end if if startpointy> maxy then startpointy := maxy elsif startpointy < 0 then startpointy := 0 end if This code will allow you to find if the coordinate of the corner is greater than the maximum x or y value of the window, and if it is, it sets the coordinate at the max x or y value, not allowing it to go over. If you can do this for all the four corners, you can disallow the square to exit the window. |
Author: | sakibur001 [ Fri Dec 16, 2016 7:43 pm ] |
Post subject: | Re: RE:How to make an object not go outside of the size of your screen? |
Gaming Lyfe @ Fri Dec 16, 2016 6:57 pm wrote: Here is a solution. Think about the code:
if startpointx> maxx then startpointx:= maxx elsif startpointx < 0 then startpointx := 0 end if if startpointy> maxy then startpointy := maxy elsif startpointy < 0 then startpointy := 0 end if This code will allow you to find if the coordinate of the corner is greater than the maximum x or y value of the window, and if it is, it sets the coordinate at the max x or y value, not allowing it to go over. If you can do this for all the four corners, you can disallow the square to exit the window. so like this if startpointy > maxy then startpointy := maxy elsif startpointy < 0 then startpointy := 0 end if if endpointx > maxx then endpointx := maxx elsif endpointx < 0 then endpointx := 0 end if if endpointy > maxy then endpointy := maxx elsif endpointy < 0 then endpointy := 0 end if i tried that it doesnt work, what it does is it erases the part of the box that goes outside of the screen, maybe i put it in the wrong place? |
Author: | TWizard [ Sat Jan 07, 2017 12:09 pm ] | ||
Post subject: | Re: How to make an object not go outside of the size of your screen? | ||
You'll need to check each side one at a time, you'll need all the coordinates I'll give you a good basis on which you should be able to get past this issue.
I hope this helps You may want to figure out how to change this into a function and return a value if a player as hit a particular wall, and re-position him back to where he should be. This is beneficial because you then dont need to use global variables. You should also ways look into using functions instead of procedures. |