TGC Codebase Backup



Grid Base by Cr4t3r

20th May 2004 14:02
Summary

FREE Grid Base for learning purposes or use in a full game.



Description

This is a grid base that can be used to make a variety of different games and game styles. It's free to use by anyone who wants to. Feel free to use the included artwork but you may get laughed at if it's in your final product. It's a little on the ugly side.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` Initialize the program
CLS
SYNC ON
SYNC RATE 30
DISABLE ESCAPEKEY

`Declare some variables
`New and Old type of the current tile
DIM TileTypeA(20, 15)
DIM TileTypeB(20, 15)

`New and Old Coords of player
DIM PlayerXA(1)
DIM PlayerYA(1)
DIM PlayerXB(1)
DIM PlayerYB(1)

`How far the player can move
DIM PlayerRange(1)

`New and Old Glow level
DIM RangeGlowA(1)
DIM RangeGlowB(1)

`When this gets high it increases RangeGlowA(1)
DIM RangeGlowCounter(1)

`Whether the space is within moveable range or not
DIM Range(20, 15)

`Setup the program
Load_Media()
Display_Grid()
Initialize_Variables()

`Main loop
WHILE ESCAPEKEY() <> 1
  Check_Cursor()
  Check_Player()
  Calculate_Range()
  Adjust_Glow()
  Update_Grid()
  SYNC
ENDWHILE

`Shutdown the program
Shutdown()

`Wait for key press before ending
WAIT KEY
END

`------------------------------------------------
`------------------------------------------------


`Load all media used during game
FUNCTION Load_Media()
  LOAD BITMAP "ImagesStrips.bmp", 1
ENDFUNCTION

`-----------------------

`Draw the initial grid to the screen
FUNCTION Display_Grid()
  FOR y = 0 TO 14
    FOR x = 0 TO 19
      COPY BITMAP 1, 0, 0, 31, 31, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
    NEXT y
  NEXT x
ENDFUNCTION

`-----------------------

`Get the location of the cursor...
`...so that space can be highlighted
FUNCTION Check_Cursor()
  Clicked = MOUSECLICK()
  MouseX = MOUSEX()
  MouseY = MOUSEY()
  FOR y = 0 TO 14
    FOR x = 0 TO 19
      IF MouseX > x * 32 AND MouseX < 31 + (x * 32)
        IF MouseY > y * 32 AND MouseY < 31 + (y * 32)
          TileTypeA(x, y) = 2
          IF Clicked = 1
            IF x < PlayerXA(1)
              SpacesX = PlayerXA(1) - x
            ENDIF
            IF x > PlayerXA(1)
              SpacesX = x - PlayerXA(1)
            ENDIF
            IF x = PlayerXA(1)
              SpacesX = 0
            ENDIF
            IF y < PlayerYA(1)
              SpacesY = PlayerYA(1) - y
            ENDIF
            IF y > PlayerYA(1)
              SpacesY = y - PlayerYA(1)
            ENDIF
            IF y = PlayerYA(1)
              SpacesY = 0
            ENDIF
            IF SpacesX + SpacesY <= PlayerRange(1)
              PlayerXA(1) = x
              PlayerYA(1) = y
              RangeGlowA(1) = 0
              RangeGlowCounter(1) = 0
            ENDIF
          ENDIF
        ELSE
          TileTypeA(x, y) = 0
        ENDIF
      ELSE
        TileTypeA(x, y) = 0
      ENDIF
    NEXT y
  NEXT x
ENDFUNCTION

`-----------------------

`Get the location of the player...
`...so that space can be marked
FUNCTION Check_Player()
  FOR y = 0 TO 14
    FOR x = 0 TO 19
      IF TileTypeA(x, y) = 1 THEN TileTypeA(x, y) = 0
      IF TileTypeA(x, y) = 3 THEN TileTypeA(x, y) = 2
    NEXT x
  NEXT y
  IF TileTypeA(PlayerXA(1), PlayerYA(1)) = 0
    TileTypeA(PlayerXA(1), PlayerYA(1)) = 1
  ELSE
    IF TileTypeA(PlayerXA(1), PlayerYA(1)) = 2 THEN TileTypeA(PlayerXA(1), PlayerYA(1)) = 3
  ENDIF
ENDFUNCTION

`-----------------------

`Calculate the range for movement...
`...and the glow tiles
FUNCTION Calculate_Range()
  FOR y = 0 TO 14
    FOR x = 0 TO 19
      IF x < PlayerXA(1)
        SpacesX = PlayerXA(1) - x
      ENDIF
      IF x > PlayerXA(1)
        SpacesX = x - PlayerXA(1)
      ENDIF
      IF x = PlayerXA(1)
        SpacesX = 0
      ENDIF
      IF y < PlayerYA(1)
        SpacesY = PlayerYA(1) - y
      ENDIF
      IF y > PlayerYA(1)
        SpacesY = y - PlayerYA(1)
      ENDIF
      IF y = PlayerYA(1)
        SpacesY = 0
      ENDIF
      IF SpacesX + SpacesY <= PlayerRange(1)
        Range(x, y) = 1
      ELSE
        Range(x, y) = 0
      ENDIF
    NEXT x
  NEXT y
ENDFUNCTION

`-----------------------

`Add to the glowcounter(s) and...
`...increase or decrease glow
FUNCTION Adjust_Glow()
  RangeGlowCounter(1) = RangeGlowCounter(1) + 1
  IF RangeGlowCounter(1) = 4
    RangeGlowA(1) = RangeGlowA(1) + 1
    RangeGlowCounter(1) = 0
  ENDIF
  IF RangeGlowA(1) > 5 THEN RangeGlowA(1) = 0
ENDFUNCTION

`-----------------------

`Redraw the grid with character,...
`...cursor, and range markings
FUNCTION Update_Grid()
  g = RangeGlowA(1)
  FOR y = 0 TO 14
    FOR x = 0 TO 19
      IF RangeGlowA(1) <> RangeGlowB(1)
        IF Range(x, y) = 1
          IF TileTypeA(x, y) = 0 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 0, 31 + (RangeGlowA(1) * 32), 31, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
          IF TileTypeA(x, y) = 1 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 32, 31 + (RangeGlowA(1) * 32), 63, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
          IF TileTypeA(x, y) = 2 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 64, 31 + (RangeGlowA(1) * 32), 95, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
          IF TileTypeA(x, y) = 3 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 96, 31 + (RangeGlowA(1) * 32), 127, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
        ELSE
`          IF TileTypeA(x, y) <> TileTypeB(x, y)
            IF TileTypeA(x, y) = 0 THEN COPY BITMAP 1, 0, 0, 31, 31, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 1 THEN COPY BITMAP 1, 0, 32, 31, 63, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 2 THEN COPY BITMAP 1, 0, 64, 31, 95, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 3 THEN COPY BITMAP 1, 0, 96, 31, 127, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
`          ENDIF
        ENDIF
      ELSE
        IF Range(x, y) = 1
`          IF TileTypeA(x, y) <> TileTypeB(x, y)
            IF TileTypeA(x, y) = 0 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 0, 31 + (RangeGlowA(1) * 32), 31, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 1 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 32, 31 + (RangeGlowA(1) * 32), 63, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 2 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 64, 31 + (RangeGlowA(1) * 32), 95, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 3 THEN COPY BITMAP 1, RangeGlowA(1) * 32, 96, 31 + (RangeGlowA(1) * 32), 127, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
`          ENDIF
        ELSE
`          IF TileTypeA(x, y) <> TileTypeB(x, y)
            IF TileTypeA(x, y) = 0 THEN COPY BITMAP 1, 0, 0, 31, 31, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 1 THEN COPY BITMAP 1, 0, 32, 31, 63, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 2 THEN COPY BITMAP 1, 0, 64, 31, 95, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
            IF TileTypeA(x, y) = 3 THEN COPY BITMAP 1, 0, 96, 31, 127, 0, x * 32, y * 32, 31 + (x * 32), 31 + (y * 32)
`          ENDIF
        ENDIF
      ENDIF
      TileTypeB(x, y) = TileTypeA(x, y)
    NEXT x
  NEXT y
  RangeGlowB(1) = RangeGlowA(1)
ENDFUNCTION

`-----------------------

`Set variables such as PlayerX with a...
`...random integer or set them to zero
FUNCTION Initialize_Variables()
  FOR y = 0 TO 14
    FOR x = 0 TO 19
      TileTypeA(x, y) = 0
      TileTypeB(x, y) = 0
      Range(x, y) = 0
    NEXT x
  NEXT y
  RANDOMIZE TIMER()
  PlayerXA(1) = RND(19)
  PlayerYA(1) = RND(14)
  PlayerXB(1) = 0
  PlayerYB(1) = 0
  PlayerRange(1) = 3
  RangeGlowA(1) = 0
  RangeGlowB(1) = 0
  RangeGlowCounter(1) = 0
ENDFUNCTION

`-----------------------

`Delete media and reset variables
FUNCTION Shutdown()
  DELETE BITMAP 1
  FOR y = 0 TO 14
    FOR x = 0 TO 19
      TileTypeA(x, y) = 0
      TileTypeB(x, y) = 0
      Range(x, y) = 0
    NEXT x
  NEXT y
  PlayerXA(1) = 0
  PlayerYA(1) = 0
  PlayerXB(1) = 0
  PlayerYB(1) = 0
  PlayerRange(1) = 0
  RangeGlowA(1) = 0
  RangeGlowB(1) = 0
  RangeGlowCounter(1) = 0
ENDFUNCTION