home *** CD-ROM | disk | FTP | other *** search
- ; INPUTEST.BB
- ; ================================================================================
- ;
- ; First tutorial file, from Game Programming Masterclass, Issue #185
- ; Explains the basics of graphics and input in Blitz Basic. In case you were
- ; wondering, next month we start work on something with some real gameplay value.
- ;
- ; INITIALISATION
- ; First, we need to tell Blitz how to display its images. In this case we are going
- ; to use a 640x480 display, running in 16 bit colour.
-
- Graphics 640, 480
- AppTitle "Basic Graphics/Input Test"
-
- ; All graphics are to be written to the Back Buffer. Think of this as a swinging
- ; blackboard - the audience can see one side while you draw on the other. When it's
- ; time to move on, you flip it around and start again. In this way we create fast,
- ; smooth animations
-
- SetBuffer BackBuffer()
-
- ; CONSTANTS
- ; There are two types of variables in this tutorial - variables themselves, which can
- ; be changed, and constants - such as ArrowFlash - which never change. In this way
- ; we can edit values throughout the package by changing one line instead of having to
- ; search for every single example of that value - risking missing some of them out or
- ; mis-typing them. We'll be using Constants a lot more in later editions of the
- ; Masterclass.
-
- Const c_ArrowFlash=4
-
- ; OBJECTS
- ; Here we create the different objects For our game. These are all variables - anything
- ; here can either link to an image or other media file, or contain values that we alter to
- ; make our game work. The names are a personal stylistic choice - g representing 'graphics',
- ; s representing 'sound' and v representing 'variable'. More than one can be put on each
- ; line, and in this way we can keep all related variables together. We can also assign them
- ; a starting value.
-
- Global g_Dancer
- Global g_LeftArrow, g_RightArrow, g_UpArrow, g_DownArrow
- Global g_DanceFloor, v_DanceFloorStep, v_DanceCount, v_DanceFloorSpeed=15
-
- Global v_LeftArrowPressed=0, v_RightArrowPressed=0, v_UpArrowPressed=0, v_DownArrowPressed=0
- Global v_BackTimer=0
- Global v_PoseTimer=0, v_NeutralTimer=0
- Global s_BackgroundMusic
-
- ; ASSIGN IMAGES TO OBJECTS
- ; Here we tell Blitz which images to assign to each object. This is not technically how it
- ; works, but for the time being it's a good description. For the arrows, no animation is
- ; required, making the command extremely simple.
-
- g_LeftArrow = LoadImage("media\leftarrow.bmp")
- g_RightArrow = LoadImage("media\rightarrow.bmp")
- g_UpArrow = LoadImage("media\uparrow.bmp")
-
- ; Animated images require a touch more work. Each contains all of that image's necessary frames,
- ; making the command longer. It reads:
- ; LoadAnimImage(filename, width of frame, height of frame, first frame, number of frames)
- ;
- ; Effectively, Blitz takes the image file and slices it automatically according to these values.
- ; Be careful, if you get the information wrong your program will crash.
-
- g_Background = LoadAnimImage("media\back_flip.bmp", 536, 261, 0, 2)
- g_DanceFloor = LoadAnimImage("media\dancefloor.bmp", 299, 81, 0, 3)
-
- ; Sprites are handled in exactly the same way, but we need to tell it to let us see past the magenta
- ; parts of the image. This is done using the MaskImage command - which reads in red, green and blue
- ; values. Magenta is 255, 0, 255 - you can check the exact value for your images in your painting
- ; program. By default, Blitz punches out any black in the image, which is usually not required.
-
- g_Dancer=LoadAnimImage("media\dancer.bmp", 220, 326, 0, 5)
- MaskImage g_Dancer, 255, 0, 255
-
- ; And, more easily, the sound
-
- s_BackGroundMusic = LoadSound ("media\bgm.wav")
- LoopSound s_BackgroundMusic
-
- ; MAIN LOOP
- ; Every key on the keyboard has a unique code - ESC is '1'. As such, the following commands will
- ; run whenever ESC is not hit. Or, to put it another way - press ESC to quit.
-
- While Not KeyHit(1)
-
- ; Background music. One line
-
- ; Draw Background Layer
- ; To actually paste an image onto the screen we use the DrawImage command - this reads like this:
- ; DrawImage filename, top-position, left-position, frame-number
- ;
- ; The frame number is called "BackSetting", and will either be 1 or 0 according to whether we want
- ; to see the blue or red lighting at the back of our scene. The next bit of code checks to see
- ; what this is, and alters it if a certain amount of time has passed
-
- DrawImage g_Background, 50, 50, BackSetting
-
- ; Simple half-way trigger manages the background lighting effect
-
- If Backtimer>50 Then
- BackSetting=0
- Else
- BackSetting=1
- EndIf
-
- BackTimer=BackTimer+1
-
- If BackTimer=100 Then
- BackTimer=0
- EndIf
-
- ; Draw DanceFloor Layer
- ; A similar deal for the glowing dance floor
-
- DrawImage g_DanceFloor, 150,400, v_DanceFloorStep
-
- If v_DanceFloorCount=v_DanceFloorSpeed Then
-
- If v_DanceFloorStep=2 Then
- v_DanceFloorStep=0
- Else
- v_DanceFloorStep=v_DanceFloorStep+1
- EndIf
-
- v_DanceFloorCount=0
-
- Else
- v_DanceFloorCount=v_DanceFloorCount+1
- EndIf
-
- ; Draw Dancer Layer
- ; And yet again for our dancer. The difference is that here the pose is selected
- ; by the user pressing a key. We check for this towards the end of the program.
-
- DrawImage g_Dancer, 220, 110, v_PoseSelect
-
- ; Draw Arrows
- ; Likewise for the arrows. The difference here is that instead of drawing a different
- ; frame of the file we simply move the arrow
-
- ; Left Arrow
-
- If v_LeftArrowPressed>0
- DrawImage g_LeftArrow, 135, 210
- v_LeftArrowPressed=v_LeftArrowPressed-1
- Else
- DrawImage g_LeftArrow, 140, 210, 0
- EndIf
-
- ; ---------
- ; Right Arrow
-
- If v_RightArrowPressed>0
- DrawImage g_RightArrow, 420, 210
- v_RightArrowPressed=v_RightArrowPressed-1
- Else
- DrawImage g_RightArrow, 415, 210, 0
- EndIf
-
- ; ---------
- ; Up Arrow
-
- If v_UpArrowPressed>0
- DrawImage g_UpArrow, 257, 50
- v_UpArrowPressed=v_UpArrowPressed-1
- Else
- DrawImage g_UpArrow, 257, 55, 0
- EndIf
-
- ; READ INPUT
- ; Finally, we turn things across to the player. We read the key that they press
- ; and select the dancer's pose accordingly. We wrap this in another piece of code
- ; that returns us to the original pose after a brief interval.
-
- If PoseTimer=0
-
- If KeyHit (75) And v_LeftArrowPressed=0
- v_LeftArrowPressed=c_ArrowFlash
- v_NeutralTimer=15
- v_PoseSelect=2
- EndIf
-
- If KeyHit (77) And v_RightArrowPressed=0
- v_RightArrowPressed=c_ArrowFlash
- v_NeutralTimer=15
- v_PoseSelect=3
- EndIf
-
- If KeyHit (72) And v_UpArrowPressed=0
- v_UpArrowPressed=c_ArrowFlash
- v_NeutralTimer=15
- v_PoseSelect=4
- EndIf
-
- Else
-
- EndIf
-
- ; Each pose only lasts a certain amount of time before we return to the Neutral
- ; position.
-
- If v_PoseSelect>1 And v_NeutralTimer=0 Then
- v_PoseSelect=0
- Else If v_PoseSelect>1
- v_NeutralTimer=v_NeutralTimer-1
- EndIf
-
- ; In addition, we have a couple of 'neutral' poses To keep things semi-animated when
- ; the player isn't pressing a button. Note that we use the same v_NeutralTimer
- ; variable here as well - but we head the other way to check it.
-
- If v_PoseSelect=0 Or v_PoseSelect=1
- v_NeutralTimer=v_NeutralTimer+1
- EndIf
-
- If v_PoseSelect=0 And v_NeutralTimer=30 Then
- v_NeutralTimer=0
- v_PoseSelect=1
- EndIf
-
- If v_PoseSelect=1 And v_NeutralTimer=30 Then
- v_NeutralTimer=0
- v_PoseSelect=0
- EndIf
-
- ; DISPLAY
- ; And finally finally, we display everything on the screen and clear the buffer for the next image
-
- Flip
- Cls
-
- ; ...and close the main loop of the file
-
- Wend
-
-
- ; ....and this really is it.
-
- End
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ; Really this time.