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

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