home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter08 / demo08-05.bb < prev    next >
Encoding:
Text File  |  2003-04-06  |  3.0 KB  |  102 lines

  1.  
  2. ;demo08-05.bb - Demonstrates sprite movement with a tiled background
  3.  
  4. Graphics 800,600
  5. ;Set up backbuffer and automidhandle
  6. SetBuffer BackBuffer()
  7. AutoMidHandle True
  8.  
  9. ;CONSTANTS
  10. ;These constants define the direction that is begin faced
  11. Const DIRECTIONLEFT = 1     ;When direction is left
  12. Const DIRECTIONUP = 2     ;When direction is up
  13. Const DIRECTIONRIGHT = 3     ;When direction is right
  14. Const DIRECTIONDOWN = 4     ;When direction is down
  15.  
  16. ;These constants define how many pixels are moved per frame
  17. Const MOVEX = 5     ;How many pixels moved left/right per frame?
  18. Const MOVEY = 5     ;How many pixels moved up.down per frame?
  19.  
  20.  
  21. ;These are key code constants
  22. Const LEFTKEY = 203, UPKEY = 200, RIGHTKEY = 205, DOWNKEY = 208
  23.  
  24.  
  25. ;TYPES
  26. ;The player type is used for the character on the screen
  27. Type player
  28.     Field x,y    ;The coordinate position
  29.     Field direction    ;The direction that is being faced (one of the DIRECTIONXXX constants)
  30.     Field frame    ;The frame that should be drawn
  31.     Field image    ;The image that should be drawn
  32. End Type
  33.  
  34. ;Create the player
  35. player.player = New player 
  36.  
  37. ;Give the player starting variables
  38. player\x = 400 
  39. player\y = 300 
  40. player\direction = DIRECTIONLEFT 
  41. player\frame = 0 
  42. ;Load the player's image
  43. player\image = LoadAnimImage("monkeyanim.bmp",48,40,0,8) 
  44. ;Load the grass background
  45. grassimage = LoadImage("grassimage.bmp") ;Load stone
  46.  
  47.  
  48.  
  49.  
  50.  
  51. ;MAIN LOOP
  52. While Not KeyDown(1)
  53.  
  54. ;Tile the grass background
  55.  
  56. TileBlock grassimage 
  57.  
  58.  
  59. ;Place the text in top left hand corner
  60. Locate 0,0 
  61.  
  62. ;Print player info
  63. Print "Player X: " + player\x
  64. Print "Player Y: " + player\y
  65. Print "Player Direction: " + player\direction
  66. Print "Frame: " + player\frame
  67.  
  68.  
  69. ;If player hits left, move him left, and find the correct direction and frame
  70. If KeyDown(LEFTKEY) 
  71.     player\x = player\x - MOVEX     ;Move him left 
  72.     player\direction = DIRECTIONLEFT     ;face him left 
  73.     player\frame = (player\frame + 1 )Mod (2) + (2 * (player\direction)-2)     ;find frame
  74.  
  75. ;If player hits up, move him up, and find the correct direction and frame
  76. ElseIf KeyDown(UPKEY) 
  77.     player\y = player\y - MOVEY     ;Move him up
  78.     player\direction = DIRECTIONUP     ;face him up
  79.     player\frame = (player\frame + 1 )Mod (2) + (2 * (player\direction)-2)     ;find frame
  80.     
  81. ;If player hits right, move him right, and find the correct direction and frame
  82. ElseIf KeyDown(RIGHTKEY) 
  83.     player\x = player\x + MOVEX     ;move him right
  84.     player\direction = DIRECTIONRIGHT     ;face him right
  85.     player\frame = (player\frame + 1 )Mod (2) + (2 * (player\direction)-2)     ;find frame
  86.  
  87. ;If player hits down, move him down, and find the correct direction and frame
  88. ElseIf KeyDown(DOWNKEY)
  89.     player\y = player\y + MOVEY     ;Move him down
  90.     player\direction = DIRECTIONDOWN     ;face him down
  91.     player\frame = (player\frame + 1 )Mod (2) + (2 * (player\direction)-2)     ;find frame
  92. EndIf
  93.  
  94. ;Draw the player at correct position and frame
  95. DrawImage player\image,player\x,player\y, player\frame 
  96.  
  97. ;wait a (fraction of a) sec
  98. Delay 50 
  99.  
  100. Flip
  101. Wend
  102. ;END OF MAIN LOOP