home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 60 / supercd60_2.iso / BlitzBasic / inputtest / inputest.bb_bak1 < prev    next >
Encoding:
Text File  |  2001-10-12  |  7.8 KB  |  308 lines

  1. ; INPUTEST.BB
  2. ; ================================================================================
  3. ; First tutorial file, from Game Programming Masterclass, Issue #185
  4. ; Explains the basics of graphics and input in Blitz Basic.  In case you were
  5. ; wondering, next month we start work on something with some real gameplay value.
  6. ;
  7. ; INITIALISATION
  8. ; First, we need to tell Blitz how to display its images.  In this case we are going
  9. ; to use a 640x480 display, running in 16 bit colour.
  10.  
  11. Graphics 640, 480
  12. AppTitle "Basic Graphics/Input Test"
  13.  
  14. ; All graphics are to be written to the Back Buffer.  Think of this as a swinging
  15. ; blackboard - the audience can see one side while you draw on the other.  When it's
  16. ; time to move on, you flip it around and start again.  In this way we create fast,
  17. ; smooth animations
  18.  
  19. SetBuffer BackBuffer()
  20.  
  21. ; CONSTANTS
  22. ; There are two types of variables in this tutorial - variables themselves, which can
  23. ; be changed, and constants - such as ArrowFlash - which never change.  In this way
  24. ; we can edit values throughout the package by changing one line instead of having to
  25. ; search for every single example of that value - risking missing some of them out or
  26. ; mis-typing them.  We'll be using Constants a lot more in later editions of the
  27. ; Masterclass.  
  28.  
  29. Const c_ArrowFlash=4
  30.  
  31. ; OBJECTS 
  32. ; Here we create the different objects For our game.  These are all variables - anything
  33. ; here can either link to an image or other media file, or contain values that we alter to
  34. ; make our game work.  The names are a personal stylistic choice - g representing 'graphics',
  35. ; s representing 'sound' and v representing 'variable'.  More than one can be put on each
  36. ; line, and in this way we can keep all related variables together.  We can also assign them
  37. ; a starting value.
  38.  
  39. Global g_Dancer
  40. Global g_LeftArrow, g_RightArrow, g_UpArrow, g_DownArrow
  41. Global g_DanceFloor, v_DanceFloorStep, v_DanceCount, v_DanceFloorSpeed=15
  42.  
  43. Global v_LeftArrowPressed=0, v_RightArrowPressed=0, v_UpArrowPressed=0, v_DownArrowPressed=0
  44. Global v_BackTimer=0
  45. Global v_PoseTimer=0, v_NeutralTimer=0
  46. Global s_BackgroundMusic
  47.  
  48. ; ASSIGN IMAGES TO OBJECTS
  49. ; Here we tell Blitz which images to assign to each object.  This is not technically how it
  50. ; works, but for the time being it's a good description.  For the arrows, no animation is
  51. ; required, making the command extremely simple.
  52.  
  53. g_LeftArrow  = LoadImage("media\leftarrow.bmp")
  54. g_RightArrow = LoadImage("media\rightarrow.bmp")
  55. g_UpArrow = LoadImage("media\uparrow.bmp")
  56.  
  57. ; Animated images require a touch more work.  Each contains all of that image's necessary frames,
  58. ; making the command longer.  It reads:
  59. ;     LoadAnimImage(filename, width of frame, height of frame, first frame, number of frames)
  60. ; Effectively, Blitz takes the image file and slices it automatically according to these values.
  61. ; Be careful, if you get the information wrong your program will crash. 
  62.  
  63. g_Background = LoadAnimImage("media\back_flip.bmp", 536, 261, 0, 2)
  64. g_DanceFloor = LoadAnimImage("media\dancefloor.bmp", 299, 81, 0, 3)
  65.  
  66. ; Sprites are handled in exactly the same way, but we need to tell it to let us see past the magenta
  67. ; parts of the image.  This is done using the MaskImage command - which reads in red, green and blue
  68. ; values.  Magenta is 255, 0, 255 - you can check the exact value for your images in your painting
  69. ; program.  By default, Blitz punches out any black in the image, which is usually not required.
  70.  
  71. g_Dancer=LoadAnimImage("media\dancer.bmp", 220, 326, 0, 5)
  72. MaskImage g_Dancer, 255, 0, 255
  73.  
  74. ; And, more easily, the sound
  75.  
  76. s_BackGroundMusic = LoadSound ("media\bgm.wav")
  77. LoopSound s_BackgroundMusic
  78.  
  79. ; MAIN LOOP
  80. ; Every key on the keyboard has a unique code - ESC is '1'.  As such, the following commands will
  81. ; run whenever ESC is not hit.  Or, to put it another way - press ESC to quit.
  82.  
  83. While Not KeyHit(1)
  84.  
  85.     ; Background music.  One line
  86.     
  87.         ; Draw Background Layer
  88.     ; To actually paste an image onto the screen we use the DrawImage command - this reads like this:
  89.     ;       DrawImage filename, top-position, left-position, frame-number
  90.     ; 
  91.     ; The frame number is called "BackSetting", and will either be 1 or 0 according to whether we want
  92.     ; to see the blue or red lighting at the back of our scene.  The next bit of code checks to see
  93.     ; what this is, and alters it if a certain amount of time has passed
  94.  
  95.     DrawImage g_Background, 50, 50, BackSetting
  96.  
  97.     ; Simple half-way trigger manages the background lighting effect
  98.  
  99.     If Backtimer>50 Then
  100.         BackSetting=0
  101.             Else
  102.                 BackSetting=1
  103.                 EndIf
  104.                 
  105.         BackTimer=BackTimer+1
  106.  
  107.         If BackTimer=100 Then
  108.             BackTimer=0
  109.             EndIf 
  110.             
  111.     ; Draw DanceFloor Layer
  112.     ; A similar deal for the glowing dance floor
  113.  
  114.     DrawImage g_DanceFloor, 150,400, v_DanceFloorStep
  115.     
  116.     If v_DanceFloorCount=v_DanceFloorSpeed Then
  117.     
  118.         If v_DanceFloorStep=2 Then
  119.             v_DanceFloorStep=0
  120.                 Else
  121.                     v_DanceFloorStep=v_DanceFloorStep+1
  122.                     EndIf
  123.         
  124.         v_DanceFloorCount=0
  125.         
  126.         Else
  127.             v_DanceFloorCount=v_DanceFloorCount+1
  128.         EndIf
  129.         
  130.     ; Draw Dancer Layer
  131.     ; And yet again for our dancer.  The difference is that here the pose is selected
  132.     ; by the user pressing a key.  We check for this towards the end of the program.
  133.  
  134.     DrawImage g_Dancer, 220, 110, v_PoseSelect
  135.  
  136.     ; Draw Arrows
  137.     ; Likewise for the arrows.  The difference here is that instead of drawing a different
  138.     ; frame of the file we simply move the arrow
  139.     
  140.         ; Left Arrow
  141.         
  142.         If v_LeftArrowPressed>0
  143.             DrawImage g_LeftArrow, 135, 210
  144.             v_LeftArrowPressed=v_LeftArrowPressed-1
  145.             Else
  146.                 DrawImage g_LeftArrow, 140, 210, 0
  147.             EndIf
  148.  
  149.         ; ---------
  150.         ; Right Arrow
  151.         
  152.         If v_RightArrowPressed>0
  153.             DrawImage g_RightArrow, 420, 210
  154.             v_RightArrowPressed=v_RightArrowPressed-1
  155.             Else
  156.                 DrawImage g_RightArrow, 415, 210, 0
  157.             EndIf
  158.  
  159.         ; ---------
  160.         ; Up Arrow
  161.  
  162.         If v_UpArrowPressed>0
  163.             DrawImage g_UpArrow, 257, 50
  164.             v_UpArrowPressed=v_UpArrowPressed-1
  165.             Else
  166.                 DrawImage g_UpArrow, 257, 55, 0
  167.             EndIf
  168.  
  169.     ; READ INPUT
  170.     ; Finally, we turn things across to the player.  We read the key that they press
  171.     ; and select the dancer's pose accordingly.  We wrap this in another piece of code
  172.     ; that returns us to the original pose after a brief interval.
  173.         
  174.     If PoseTimer=0
  175.     
  176.         If KeyHit (75) And v_LeftArrowPressed=0
  177.             v_LeftArrowPressed=c_ArrowFlash
  178.             v_NeutralTimer=15
  179.             v_PoseSelect=2
  180.             EndIf
  181.         
  182.         If KeyHit (77) And v_RightArrowPressed=0
  183.             v_RightArrowPressed=c_ArrowFlash
  184.             v_NeutralTimer=15
  185.             v_PoseSelect=3
  186.             EndIf
  187.     
  188.         If KeyHit (72) And v_UpArrowPressed=0
  189.             v_UpArrowPressed=c_ArrowFlash
  190.             v_NeutralTimer=15
  191.             v_PoseSelect=4
  192.             EndIf
  193.         
  194.         Else
  195.         
  196.     EndIf        
  197.             
  198.     ; Each pose only lasts a certain amount of time before we return to the Neutral
  199.     ; position.
  200.             
  201.     If v_PoseSelect>1 And v_NeutralTimer=0 Then
  202.         v_PoseSelect=0
  203.             Else If v_PoseSelect>1
  204.                     v_NeutralTimer=v_NeutralTimer-1
  205.                     EndIf
  206.                                 
  207.     ; In addition, we have a couple of 'neutral' poses To keep things semi-animated when
  208.     ; the player isn't pressing a button.  Note that we use the same v_NeutralTimer
  209.     ; variable here as well - but we head the other way to check it.
  210.     
  211.     If v_PoseSelect=0 Or v_PoseSelect=1
  212.         v_NeutralTimer=v_NeutralTimer+1
  213.         EndIf
  214.                      
  215.     If v_PoseSelect=0 And v_NeutralTimer=30 Then
  216.               v_NeutralTimer=0
  217.             v_PoseSelect=1
  218.             EndIf
  219.             
  220.     If v_PoseSelect=1 And v_NeutralTimer=30 Then
  221.             v_NeutralTimer=0
  222.             v_PoseSelect=0
  223.             EndIf
  224.     
  225.     ; DISPLAY
  226.     ; And finally finally, we display everything on the screen and clear the buffer for the next image
  227.     
  228.     Flip
  229.     Cls
  230.     
  231.     ; ...and close the main loop of the file
  232.     
  233.     Wend
  234.  
  235.  
  236. ; ....and this really is it.
  237.  
  238. End
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. ; Really this time.