Computer Science Canada

preventing a box from leaving the screen

Author:  Solar_Projectile [ 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.

Author:  Solar_Projectile [ Sat Jan 07, 2006 3:54 pm ]
Post 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

Author:  Cervantes [ Sat Jan 07, 2006 4:32 pm ]
Post 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.


: