Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to make an object not go outside of the size of your screen?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sakibur001




PostPosted: 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


Turing:


<Add your code here>



Please specify what version of Turing you are using
<Answer Here>
Sponsor
Sponsor
Sponsor
sponsor
Gaming Lyfe




PostPosted: 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.
sakibur001




PostPosted: 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?
TWizard




PostPosted: 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.

code:

/*
you need to do is check if the player + radius(or minus depending where the player is), has gone outside your screen.

In this case, i'v created a procedure for this. you set the minimum and maximum values of your screen.

checking

top
if playerX + radius > max_boarder_MAXx then
set it to max_boarder minus radius %we add the radius and check if the player is greater because moving towards the top is moving positive in number
end it

bottom
if playerX - radius < max_boarder_MINx then
set it to max_boarder plus radius %we subtract the radius and check if the player is lesser because moving towards the bottom is moving negative in number
end it

left
if playerY - radius < max_boarder_MINy then
set it to max_boarder plus radius %we subtract the radius and check if the player is lesser because moving towards the left is moving negative in number
end it

right
if playerY + radius > max_boarder_MAXy then
set it to max_boarder minus radius %we add the radius and check if the player is greater because moving towards the right is moving positive in number
end it
*/


%firstly you'll need to have your players x and y
var player_x, player_y : int %required

%next you'll need your player radius
var player_rsum : int %required and must be the radius of the players circle

procedure boudary(boudary_min_x, boudary_min_y, boudary_max_x, boudary_max_y : int)
    if player_x > boudary_max_x - player_rsum then
        player_x := boudary_max_x - player_rsum;
    end if
    if player_x < boudary_min_x + player_rsum then
        player_x := boudary_min_x + player_rsum;
    end if
    if player_y > boudary_max_y - player_rsum then
        player_y := boudary_max_y - player_rsum;
    end if
    if player_y < boudary_min_y + player_rsum then
        player_y := boudary_min_y + player_rsum;
    end if
end boudary


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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: