home *** CD-ROM | disk | FTP | other *** search
/ Game Hack 1 / GHM01.ZIP / TILEEX.BAS < prev    next >
BASIC Source File  |  1994-08-17  |  14KB  |  305 lines

  1. 'This is a small program using tile graphics. This source code and it's
  2. 'accompanying article are the property of James Tonn. You may modify this
  3. 'source code and use it in your own programs. You may use it and/or it's
  4. 'accompanying article in a written publication provided that neither item is
  5. 'altered, and that you give credit to the author (James Tonn).
  6. 'I hope this code will help you in your programming.
  7. '   -Jimmy Tonn 8/94
  8.  
  9.  
  10. DEFINT A-Z  'declare all variables starting with a through z as integers
  11. SCREEN 13   'set screen mode 13, 320x200x256
  12. CLS         'clear screen
  13.  
  14. RESTORE world   'restore data pointer so all data reading starts after label
  15.                 ' "world"
  16.  
  17. CONST ACROSS = 11 'this is the number of across tiles that will appear on
  18.                   'the screen at a time, the total number of across tiles
  19.                   'is stored in the variable tacross
  20. CONST DOWN = 11   'number of down tiles on screen... total down tiles are
  21.                   'stored in tdown
  22.  
  23. DIM blankimg(116) 'create an array for the blank tile
  24.                   'this tile will be totally black, all zero's
  25. DIM grassimg(116) 'create an array for the grass tile's image, 116 bytes
  26. DIM treeimg(116)  'create an array for the tree tile's image, 116 bytes
  27. DIM playerimg(116)'create as array for the player tile's image, 116 bytes
  28.  
  29. READ tacross, tdown  'get these values from the big data block "world"
  30.                      'at the end of this program
  31.  
  32. TYPE tiletype          'create a type called tiletype
  33.     x AS INTEGER       'the tile's x pixel coord
  34.     y AS INTEGER       'the tile's y pixel coord
  35.     style AS INTEGER   'the tile's style
  36. END TYPE
  37.  
  38. DIM tile(tacross, tdown) AS tiletype 'declare array "tile" as tiletype
  39.  
  40. TYPE playertype  'create a type called playertype
  41.     x AS INTEGER 'x tile where the player is
  42.     y AS INTEGER 'y tile where the player is
  43.     px AS INTEGER 'the players "permanant" x tile, the tile that he appears
  44.                   'in out of the tiles on the screen
  45.     py AS INTEGER 'the players "permanant" y tile
  46. END TYPE
  47.  
  48. DIM player AS playertype 'declare the variable player as a playertype
  49.  
  50. READ player.x, player.y  'read player x and y coords from data block "world"
  51. player.px = 6
  52. player.py = 6
  53.  
  54. DEF FNSetlocs    'function to set pixel locs of tiles that appear on screen
  55.    FOR a = 1 TO ACROSS
  56.       FOR d = 1 TO DOWN
  57.          tile(a, d).x = 15 * a  'set pixel x location of tile
  58.          tile(a, d).y = 15 * d  'set pixel y location of tile
  59.       NEXT d                    '(all tiles are 15x15 pixels)
  60.    NEXT a
  61. END DEF
  62.  
  63. DEF FNSetAtts   'function to read the styles of all tiles from data block
  64.    FOR d = 1 TO tdown
  65.       FOR a = 1 TO tacross
  66.          READ tile(a, d).style 'read the tile's style
  67.       NEXT a
  68.    NEXT d
  69. END DEF
  70.  
  71.  
  72. 'this next routine draws the tile on the screen
  73. 'the tx and ty variables are the tile numbers out of all the tiles
  74. 'the cx and cy variables are locations of the tile spaces where the tiles
  75. 'will be displayed on the screen
  76. DEF FNDrawTile (tx, ty, cx, cy)
  77.     SELECT CASE tile(tx, ty).style  'check the tile's style
  78.           CASE 0  'if it's 0, draw an array of zero's (a black tile)
  79.               PUT (tile(cx, cy).x, tile(cx, cy).y), blankimg, PSET
  80.           CASE 1  'if it's 1, draw the grass tile
  81.               PUT (tile(cx, cy).x, tile(cx, cy).y), grassimg, PSET
  82.           CASE 2  'if it's 2, draw the tree tile
  83.               PUT (tile(cx, cy).x, tile(cx, cy).y), treeimg, PSET
  84.     END SELECT
  85. END DEF
  86.  
  87. 'this draws all the visible tiles on the screen
  88. 'it will draw from 5 tiles to the left of the player to 5 tiles to his right
  89. 'and from 5 tiles up to 5 tiles down
  90. DEF FNDrawScreen
  91.    FOR rela = -5 TO 5
  92.       FOR reld = -5 TO 5
  93.            dummy = FNDrawTile(player.x + rela, player.y + reld, player.px + rela, player.py + reld)
  94.       NEXT reld
  95.    NEXT rela
  96. END DEF
  97.  
  98. 'this function draws the player on the screen in his "permanant" location
  99. DEF FNDrawPlayer
  100.    PUT (tile(player.px, player.py).x, tile(player.px, player.py).y), playerimg, PSET
  101. END DEF
  102.  
  103. DEF FNDisplayImg 'this function will read an images colors from a data block
  104.                  'and display the image on the screen. Before calling this
  105.                  'function, reset the data pointer to the beginning of the
  106.                  'data block for the image.
  107.    FOR dp = 1 TO 15
  108.       FOR ap = 1 TO 15
  109.          READ att                     'read the value at the point in the data block
  110.          PSET (ap + 10, dp + 10), att 'put the pixel on the screen with the
  111.                                       'color that was read from the data
  112.                                       'block
  113.       NEXT ap
  114.    NEXT dp
  115. END DEF
  116.  
  117. '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  118. ' * * * * * * * * * * * * End of Function Defs* * * * * * * * * * * * * *
  119.  
  120. dummy = FNSetlocs   'set the locations for the on-screen tiles
  121. dummy = FNSetAtts   'set the styles for all the tiles
  122.  
  123. GET (11, 11)-(25, 25), blankimg 'get an array that is all zero's, which will
  124.                                 'the black tile that we put around the edges
  125.                                 'of the map. No data reading is needed since
  126.                                 'the screen is already totally black. All
  127.                                 'we need to do is grab a block.
  128.  
  129. RESTORE grassdata   'restore the data pointer to the beginning of the data
  130.                     'block that stores the grass tile's image. If you're
  131.                     'reseting the data pointer to load the images, do it
  132.                     'after calling FNSetLocs and FNSetAtts, because they
  133.                     'need the data pointer in a certain place
  134. dummy = FNDisplayImg  'call routine to read and display the image
  135. GET (11, 11)-(25, 25), grassimg 'get the image into array "grassimg"
  136. PUT (11, 11), grassimg, XOR     'put image over existing one to clear it
  137.  
  138. RESTORE treedata 'restore data pointer to beginning of the tree tile's image
  139. dummy = FNDisplayImg 'read and display the image
  140. GET (11, 11)-(25, 25), treeimg  'get the image into array "treeimg"
  141. PUT (11, 11), treeimg, XOR  'put image on top of the old one to clear it
  142.  
  143. RESTORE playerdata 'restore pointer to beginning of player tile's image
  144. dummy = FNDisplayImg 'read and display the image
  145. GET (11, 11)-(25, 25), playerimg  'get the image into array "playerimg"
  146. PUT (11, 11), playerimg, XOR  'put image on top of the old one to clear it
  147.  
  148. dummy = FNDrawScreen  'draw the screen once
  149. dummy = FNDrawPlayer  'draw the player once
  150.  
  151.  
  152. DO
  153.   kbd$ = INKEY$            'get a "transparent" input
  154.   IF kbd$ <> "" THEN       'if there actually was a user input, then...
  155.     kbd$ = RIGHT$(kbd$, 1) 'get the first byte of the input (this is needed
  156.                            'if you want to look for input from the cursor
  157.                            'keys)
  158.     SELECT CASE kbd$
  159.           CASE CHR$(27)    'user pressed escape key
  160.               END          'end the program
  161.           CASE CHR$(72) 'user pressed up arrow
  162.               IF tile(player.x, player.y - 1).style <> 2 THEN
  163.               '^^^ check if the tile the player is about to move into is
  164.               'already occupied by a tree.
  165.               'if you check for a tree, you really don't need to check if
  166.               'the player is going to go past the line of trees at the
  167.               'edge with the other method, because if you check for a tree,
  168.               'it will stop you before you go past the line anyway I just
  169.               'added them both in case the reader wanted to use a certain
  170.               'method for some reason.
  171.                
  172.                 IF player.y - 1 > 5 THEN     'make sure he doesn't go past
  173.                                              'the line of trees. You will
  174.                                              'probably have to change this
  175.                                              'number around if you change the
  176.                                              'player's view screen size
  177.                   player.y = player.y - 1    'decrease player y tile by one
  178.                 END IF
  179.                 dummy = FNDrawScreen       'draw the screen
  180.                 dummy = FNDrawPlayer       'put the player on the screen
  181.               END IF
  182.           CASE CHR$(80) 'user pressed down arrow
  183.               IF tile(player.x, player.y + 1).style <> 2 THEN
  184.               '^^^ check if the tile the player is about to move into is
  185.               'already occupied by a tree.
  186.                 IF player.y + 1 < (tdown - 5) THEN 'keep player on screen
  187.                   player.y = player.y + 1    'increase player y tile by one
  188.                 END IF
  189.                 dummy = FNDrawScreen       'draw the screen
  190.                 dummy = FNDrawPlayer       'put the player on the screen
  191.               END IF
  192.           CASE CHR$(75) 'user pressed left arrow
  193.               IF tile(player.x - 1, player.y).style <> 2 THEN
  194.               '^^^ check if the tile the player is about to move into is
  195.               'already occupied by a tree.
  196.  
  197.                 IF player.x - 1 > 5 THEN   'keep player on screen
  198.                   player.x = player.x - 1    'decrease player x tile by one
  199.                 END IF
  200.                 dummy = FNDrawScreen       'draw the screen
  201.                 dummy = FNDrawPlayer       'put the player on the screen
  202.               END IF
  203.           CASE CHR$(77) 'user pressed right arrow
  204.               IF tile(player.x + 1, player.y).style <> 2 THEN
  205.               '^^^ check if the tile the player is about to move into is
  206.               'already occupied by a tree.
  207.  
  208.                 IF player.x < (tacross - 6) THEN  'keep player on screen
  209.                   player.x = player.x + 1    'increase player x tile by one
  210.                 END IF
  211.                 dummy = FNDrawScreen       'draw the screen
  212.                 dummy = FNDrawPlayer       'put the player on the screen
  213.               END IF
  214.     END SELECT
  215.   END IF
  216. LOOP        'restart the main loop
  217.  
  218.  
  219. world:
  220.  
  221. DATA 30,30
  222. DATA 15,15
  223. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  224. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  225. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  226. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  227. DATA 0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0
  228. DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0,0,0,0,0
  229. DATA 0,0,0,0,2,2,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,2,0,0,0,0,0
  230. DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0,0,0,0,0
  231. DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,2,0,0,0,0,0
  232. DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0,0,0,0,0
  233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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  249. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  250. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  251. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  252. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  253.  
  254. grassdata:
  255. DATA 0,0,0,0,2,0,0,0,148,0,0,2,0,0,0
  256. DATA 0,2,0,0,0,0,0,0,0,0,0,0,0,0,141
  257. DATA 0,0,0,0,142,0,0,2,0,0,0,0,0,0,0
  258. DATA 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0
  259. DATA 0,2,0,2,0,145,0,0,0,142,0,0,0,0,0
  260. DATA 0,0,0,0,0,0,0,0,2,0,0,0,0,0,2
  261. DATA 0,0,0,142,0,0,0,0,0,0,0,2,0,0,0
  262. DATA 0,2,0,0,0,142,0,0,0,0,0,0,0,0,0
  263. DATA 0,0,0,0,0,0,0,0,0,2,0,0,147,0,0
  264. DATA 0,142,0,0,0,0,0,0,0,0,0,0,0,0,0
  265. DATA 0,0,0,2,0,0,142,0,0,2,0,0,0,2,0
  266. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  267. DATA 2,0,0,0,0,145,0,2,0,0,0,0,2,0,0
  268. DATA 0,0,0,2,0,0,0,0,0,0,0,0,0,0,142
  269. DATA 145,0,0,0,147,0,0,0,2,0,0,2,0,0,0
  270.  
  271. treedata:
  272. DATA 0,0,0,2,2,2,2,2,2,2,2,0,0,0,0
  273. DATA 0,0,2,2,2,2,2,2,2,2,2,2,0,0,0
  274. DATA 0,6,2,2,2,2,2,2,2,2,2,6,0,0,0
  275. DATA 0,2,6,2,2,2,2,2,2,2,6,2,0,0,0
  276. DATA 0,2,2,6,2,2,2,2,2,6,2,2,0,0,0
  277. DATA 0,0,2,2,6,2,2,2,6,2,2,0,0,0,0
  278. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  279. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  280. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  281. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  282. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  283. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  284. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  285. DATA 0,0,0,0,6,6,6,6,6,0,0,0,0,0,0
  286. DATA 0,0,0,6,6,6,6,6,6,6,0,0,0,0,0
  287.  
  288. playerdata:
  289. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  290. DATA 0,0,0,0,0,0,7,7,7,0,0,0,1,0,0
  291. DATA 0,0,0,0,0,0,7,7,7,0,0,0,1,0,0
  292. DATA 0,0,0,0,0,0,7,7,7,0,0,0,1,0,0
  293. DATA 0,0,0,0,0,0,0,7,0,0,0,0,1,0,0
  294. DATA 0,0,0,0,0,0,0,7,0,0,0,0,1,0,0
  295. DATA 0,2,0,2,0,0,0,7,0,0,0,1,1,1,0
  296. DATA 0,2,2,7,7,7,7,7,7,7,7,7,7,0,0
  297. DATA 0,2,2,2,0,0,0,7,0,0,0,0,1,0,0
  298. DATA 0,0,2,0,0,0,0,7,0,0,0,0,1,0,0
  299. DATA 0,0,0,0,0,0,0,7,0,0,0,0,0,0,0
  300. DATA 0,0,0,0,0,0,7,0,7,0,0,0,0,0,0
  301. DATA 0,0,0,0,0,7,0,0,0,7,0,0,0,0,0
  302. DATA 0,0,0,0,7,0,0,0,0,0,7,0,0,0,0
  303. DATA 0,0,0,7,0,0,0,0,0,0,0,7,0,0,0
  304.  
  305.