home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter09 / demo09-03.bb < prev    next >
Encoding:
Text File  |  2003-04-06  |  1.6 KB  |  85 lines

  1. ;demo09-03.bb - Draws a bounding box
  2.  
  3. Graphics 800,600
  4.  
  5. ;Set default backbuffer and automidhandle to true
  6. SetBuffer BackBuffer()
  7. AutoMidHandle True
  8.  
  9. ;IMAGES
  10. ;Load the ship image
  11. Global shipimage = LoadImage("ship.bmp")
  12.  
  13. ;Give the ship default parameters
  14. Global x = 400
  15. Global y = 300
  16.  
  17.  
  18.  
  19. ;CONSTANTS
  20. ;The key code constants
  21. Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205 
  22.  
  23. ;These constants define how many pixels are moved per frame
  24. Const MOVEX = 5
  25. Const MOVEY = 5
  26.  
  27. ;MAIN LOOP
  28. While Not KeyDown(1)
  29. ;Clear the screen
  30. Cls
  31.  
  32. ;Find out if any important keys on the keyboard have been pressed
  33. TestKeys() 
  34.  
  35. ;Draw the bounding box around the player
  36. DrawPlayerRect() 
  37.  
  38. ;Draw the image of the ship
  39. DrawImage shipimage,x,y 
  40.  
  41. Flip
  42. Wend
  43. ;END OF MAIN LOOP
  44.  
  45.  
  46. ;FUNCTION DrawPlayerRect() - Draws a bounding rectangle
  47. Function DrawPlayerRect()
  48.  
  49. ;find the width of the image
  50. iw = ImageWidth(shipimage)
  51.  
  52. ;Find the upper left hand coordinates
  53. x1# = ((-ImageWidth(shipimage)/2) +x)
  54. y1# = ((-ImageHeight(shipimage)/2) + y)
  55.  
  56. ;Draw the entire bounding box
  57. Rect x1#,y1#,ImageWidth(shipimage),ImageHeight(shipimage), 0
  58.  
  59. End Function
  60.  
  61.  
  62. ;FUNCTION TestKeys() - Tests all of the keys to see if they were hit
  63. Function TestKeys()
  64.  
  65. ;If up is hit, move player up
  66. If KeyDown(UPKEY) 
  67.     y = y - MOVEY 
  68. EndIf
  69.  
  70. ;If down is hit, move player down
  71. If KeyDown(DOWNKEY) ;If down was hit
  72.     y = y + MOVEY 
  73. EndIf
  74.  
  75. ;If left is hit, move player left
  76. If KeyDown(LEFTKEY) 
  77.     x = x - MOVEX 
  78. EndIf
  79.  
  80. ;If right is hit, move player right
  81. If KeyDown(RIGHTKEY) 
  82.     x = x + MOVEX 
  83. EndIf
  84.  
  85. End Function