Source Code: Draw a Grid Demo
rating: 0+x

Description

This source code draws an interactive grid.

Instructions

  • WASD: Move grid.
  • IJKL: Resize grid.
  • Arrow Keys: Change number of cells in grid.

Source Code

SetFPS 60

LoadFont "Arial", 1, 20, 0

Constant VK_W = 17
Constant VK_A = 30
Constant VK_S = 31
Constant VK_D = 32

Constant VK_I = 23
Constant VK_K = 37
Constant VK_J = 36
Constant VK_L = 38

gridWidth# = 400
gridHeight# = 400
gridX# = 200
gridY# = 50
numbCellsX = 20
numbCellsY = 20

sw = GetScreenWidth()
sh = GetScreenHeight()

Do

    If LeftKey() Then numbCellsX = numbCellsX - 1
    If RightKey() Then numbCellsX = numbCellsX + 1
    If numbCellsX < 2 Then numbCellsX = 2
    If numbCellsX > 40 Then numbCellsX = 40

    If UpKey() Then numbCellsY = numbCellsY - 1
    If DownKey() Then numbCellsY = numbCellsY + 1
     If numbCellsY < 2 Then numbCellsY = 2
     If numbCellsY > 40 Then numbCellsY = 40

    If KeyState(VK_W) Then gridY# = gridY# - 1
    If KeyState(VK_S) Then gridY# = gridY# + 1
    If KeyState(VK_A) Then gridX# = gridX# - 1
    If KeyState(VK_D) Then gridX# = gridX# + 1

    If KeyState(VK_J) Then gridWidth# = gridWidth# - 1
    If KeyState(VK_L) Then gridWidth# = gridWidth# + 1
    If KeyState(VK_I) Then gridHeight# = gridHeight# - 1
    If KeyState(VK_K) Then gridHeight# = gridHeight# + 1

    Cls 0

        Print " gridX#: " + Str$( gridX# )
        Print " gridY#: " + Str$( gridY# )
        Print " gridWidth#: " + Str$( gridWidth# )
        Print " gridHeight#: " + Str$ ( gridHeight# )
        Print " numbCellsX: " + Str$( numbCellsX )
        Print " numbCellsY: " + Str$( numbCellsY )

        LockBuffer
        For i# = 0 To gridWidth# Step gridWidth# / numbCellsX
            Line gridX#+i#,gridY#, gridX#+i#, gridY# + gridHeight#
        Next
        If Mod(gridWidth#, numbCellsX)
                Line gridX#+gridWidth#, gridY#, gridX#+gridWidth#, gridY#+gridHeight#
        EndIf

        For i# = 0 To gridHeight# Step gridHeight# / numbCellsY
            Line gridX#, gridY# + i#, gridX# + gridWidth#, gridY# + i#
        Next
        If Mod(gridHeight#, numbCellsY)
                Line gridX#, gridY#+gridHeight#, gridX#+gridWidth#,gridY# + gridHeight#
        EndIf
        UnlockBuffer
    Sync
Loop

Related Pages

Backlinks

Categories: Graphics : Graphics Source Code : Source Code