
-----------------------------------
nis
Fri Sep 26, 2003 10:38 pm

Gridding off the run window
-----------------------------------
I made this program where it moves a box around the grid. I thought it was pretty cool so i have decided to post the source for it. Feel free to use it or edit it however you'ld like.
It's well commented so it shouldn't be to hard to understand


%###########################################################################################################################
%####                                            Created by : n I $                                                     ####  
%####                                            September 26, 2003                                                     ####  
%###########################################################################################################################


%The Main Screen
const MWinX := 550
const MWinY := 528
var MWin : int := Window.Open ("graphics:" + intstr (MWinX) + ";" + intstr (MWinY) + ", position:top;left,title:BattleShip,offscreenonly")
%Keyboard
var ch : array char of boolean
%Player
var Column_Position, Row_Position : int := 1
%The Board
const Num_Of_Horizontal_Boxes : int := 100 %The smaller the number the bigger the boxes
const Num_Of_Verticle_Boxes : int := 100 %The smaller the number the bigger the boxes
const SpaceSize : int := 1 %Determains the amount of grid spaces between the boxes
%Calculates the coordinates of the verticle lines of the grid (Up and Down)
function Verticle_Line (num : int) : int
    result ((maxx div Num_Of_Verticle_Boxes) * num)
end Verticle_Line
%Calculates the coordinates of the horizontal lines of the grid (Left and right)
function Horizontal_Line (num : int) : int
    result ((maxy div Num_Of_Horizontal_Boxes) * num)
end Horizontal_Line
%Decides if the grid is shown or not
var Show_Grid : boolean := true

%Draws the Grid
for col : 1 .. Num_Of_Verticle_Boxes
    drawline (Verticle_Line (col), 0, Verticle_Line (col), maxy, 7)
end for
for row : 1 .. Num_Of_Horizontal_Boxes
    drawline (0, Horizontal_Line (row), maxx, Horizontal_Line (row), 7)
end for
%Draws the Box
drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, brightred)
View.Update
loop
    delay (30) %Slows Down the loop
    Input.KeyDown (ch) %Get all keys that are being pressed (Allows for multiple keyboard inputs
    if ch (KEY_UP_ARROW) then %If the up arrow key is pressed
        %Erases current box
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, 0)
        Row_Position += SpaceSize %Brings box up desired number of spaces on the grid
        if Row_Position > Num_Of_Verticle_Boxes - 1 then %If the box is past the top part of the grid bring to the top row
            Row_Position := Num_Of_Verticle_Boxes - 1
        end if
        %draws the box in its place
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, brightred)
        View.Update %redraws the screen
    end if
    if ch (KEY_DOWN_ARROW) then %If the down arrow key is pressed
        %Erases current box
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, 0)
        Row_Position -= SpaceSize %Brings box down desired number of spaces on the grid
        if Row_Position < 1 then
            Row_Position := 1
        end if
        %draws the box in its place
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, brightred)
        View.Update %redraws the screen
    end if
    if ch (KEY_RIGHT_ARROW) then %If the right arrow key is pressed
        %Erases current box
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, 0)
        Column_Position += SpaceSize %Brings box right desired number of spaces on the grid
        if Column_Position > Num_Of_Horizontal_Boxes - 1 then
            Column_Position := Num_Of_Horizontal_Boxes - 1
        end if
        %draws the box in its place
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, brightred)
        View.Update %redraws the screen
    end if
    if ch (KEY_LEFT_ARROW) then %If the left arrow key is pressed
        %Erases current box
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, 0)
        Column_Position -= SpaceSize %Brings box left desired number of spaces on the grid
        if Column_Position < 1 then
            Column_Position := 1
        end if
        %draws the box in its place
        drawfillbox (Verticle_Line (Column_Position) + 1, Horizontal_Line (Row_Position) + 1, Verticle_Line (Column_Position + 1) - 1, Horizontal_Line (Row_Position + 1) - 1, brightred)
        View.Update %redraws the screen
    end if
    if ch ('g') or ch ('G') then %If g is pressed
        if Show_Grid = false then %If grid is not being shown then show it
            Show_Grid := true
            for row : 1 .. Num_Of_Verticle_Boxes
                drawline (Verticle_Line (row), 0, Verticle_Line (row), maxy, 7)
            end for
            for col : 1 .. Num_Of_Horizontal_Boxes
                drawline (0, Horizontal_Line (col), maxx, Horizontal_Line (col), 7)
            end for
        else %If grid is being shown then hide
            Show_Grid := false
            for row : 1 .. Num_Of_Verticle_Boxes
                drawline (Verticle_Line (row), 0, Verticle_Line (row), maxy, 0)
            end for
            for col : 1 .. Num_Of_Horizontal_Boxes
                drawline (0, Horizontal_Line (col), maxx, Horizontal_Line (col), 0)
            end for
        end if
        View.Update %redraw the screen
        delay (500) %Stops the grid from switching between being shown and not being shown to quickly
    end if
end loop



-----------------------------------
Tony
Fri Sep 26, 2003 11:05 pm


-----------------------------------
thats a preaty good example of Input.KeyDown... I should link to this program from the tutorial :)

+15Bits

-----------------------------------
Chaoskiller
Sun Oct 25, 2009 12:04 pm

RE:Gridding off the run window
-----------------------------------
hey umm how do you know what the name of a key is such as space bar, what would i enter in the bracets for that? i want to do I   

if chars (Space bar) then

-----------------------------------
Zren
Sun Oct 25, 2009 12:21 pm

Re: RE:Gridding off the run window
-----------------------------------
hey umm how do you know what the name of a key is such as space bar, what would i enter in the bracets for that? i want to do I   

if chars (Space bar) then

KEY_SHIFT

Look up Keyboard in the Turing Help for more.

-----------------------------------
TheGuardian001
Sun Oct 25, 2009 10:02 pm

Re: Gridding off the run window
-----------------------------------
if chars(' ') then

You must use single quotes, not doubles.
