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

Username:   Password: 
 RegisterRegister   
 preventing a box from leaving the screen
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Solar_Projectile




PostPosted: Sat Jan 07, 2006 3:40 pm   Post subject: preventing a box from leaving the screen

code:
%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


my question is this: how do i modify the code so that the box doesn't go outside the size of the screen or distort?

if anybody could help, id be very thankful.
Sponsor
Sponsor
Sponsor
sponsor
Solar_Projectile




PostPosted: Sat Jan 07, 2006 3:54 pm   Post subject: (No subject)

I think i may have found a solution on my own, but i dont know if this is the proper/best way to do it

code:
%Declaration section
var startpointx, startpointy : int := 0
var endpointx, endpointy : int := 50
var key : string (1)
var reply : 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 startpointx = 0 then
        startpointx := startpointx + 1
        endpointx := endpointx + 1
    elsif startpointy = 0 then
        startpointy := startpointy + 1
        endpointy := endpointy + 1
    elsif endpointx = 400 then
        startpointx := startpointx - 1
        endpointx := endpointx - 1
    elsif endpointy = 400 then
        startpointy := startpointy - 1
        endpointy := endpointy - 1
    elsif 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
Cervantes




PostPosted: Sat Jan 07, 2006 4:32 pm   Post subject: (No subject)

Pretty much, though there's no need to keep two variables (start and end) when you could just have end depend on start. endx is always = startx + width, and endy is always = starty + height. width and height are constants.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: