[Utility] Grids
Author |
Message |
Cervantes
|
Posted: Sat Oct 08, 2005 2:52 pm Post subject: [Utility] Grids |
|
|
The following code will allow easy creation of a 2 dimensional grid. You can customize the number of rows/columns and the offset of the grid. (The offset determines where in the Run window the grid will appear.) Grid spaces are automatically calculated for size.
If this code does not work, let me know. I'm just going from memory that it works.
Turing: |
const screenResX := 600
const screenResY := 600
View.Set ("graphics:" + intstr (screenResX ) + ";" + intstr (screenResY ) + ",offscreenonly")
const gridX := 8 % Number of pieces the grid will be wide
const gridY := 8 % Number of pieces the grid will be high
const gridOffsetX1 := 100 % The grid will go from (gridOffsetX1, gridOffsetY1) to (screenResX - gridOffsetX2, screenResY - gridOffsetY2)
const gridOffsetX2 := 100
const gridOffsetY1 := 100
const gridOffsetY2 := 100
const gridX2 := screenResX - gridOffsetX2
const gridY2 := screenResY - gridOffsetY2
const gridWidth := (screenResX - gridOffsetX1 - gridOffsetX2 ) / gridX % Width of each grid piece, in pixels
const gridHeight := (screenResY - gridOffsetY1 - gridOffsetY2 ) / gridY % Height of each grid piece, in pixels
const gridEmptyClr := white
const gridFilledClr := black
var grid : array 1 .. gridX, 1 .. gridY of boolean % - 1 because the array starts from 0, not 1
for ix : 1 .. upper (grid, 1)
for iy : 1 .. upper (grid, 1)
grid (ix, iy ) := false
end for
end for
% Just to show the grid in action, make some squares "true". Later, "true" squares will be filled in.
for i : 1 .. 5
grid (Rand.Int (1, upper (grid, 1)), Rand.Int (1, upper (grid, 1))) := true
end for
proc input
var mx, my, btnnumber, btnupdown : int
var gridPosX, gridPosY : int
Mouse.ButtonWait ("down", mx, my, btnnumber, btnupdown )
gridPosX := floor ((mx - gridOffsetX1 ) / gridWidth ) + 1
gridPosY := floor ((my - gridOffsetY1 ) / gridHeight ) + 1
if gridPosX > 0 & gridPosX <= gridX & gridPosY > 0 & gridPosY <= gridY then
if grid (gridPosX, gridPosY ) = false then
grid (gridPosX, gridPosY ) := true
else
grid (gridPosX, gridPosY ) := false
end if
end if
end input
proc drawGrid
Draw.FillBox (gridOffsetX1, gridOffsetY1, gridX2, gridY2, gridEmptyClr )
% Colour the "true" squares black
for x : 1 .. upper (grid, 1)
for y : 1 .. upper (grid, 1)
if grid (x, y ) then
Draw.FillBox (round ((x - 1) * gridWidth + gridOffsetX1 ), round ((y - 1) * gridHeight + gridOffsetY1 ),
round ((x - 1) * gridWidth + gridWidth + gridOffsetX1 ), round ((y - 1) * gridHeight + gridHeight + gridOffsetY1 ),
gridFilledClr )
end if
end for
end for
% Draw the lines of the grid
for x : 0 .. upper (grid, 1)
Draw.Line (round (x * gridWidth + gridOffsetX1 ), gridOffsetY1, round (x * gridWidth + gridOffsetX1 ), maxy - gridOffsetY2, black)
end for
for y : 0 .. upper (grid, 2)
Draw.Line (gridOffsetX1, round (y * gridHeight + gridOffsetY1 ), maxx - gridOffsetX2, round (y * gridHeight + gridOffsetY1 ), black)
end for
end drawGrid
loop
cls
drawGrid
View.Update
input
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|