home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 60 / supercd60_2.iso / BlitzBasic / Blitz2DPCP / data1.cab / Support / help / samples / slide_block / Slideblock.bb < prev   
Encoding:
Text File  |  2001-11-21  |  13.4 KB  |  531 lines

  1. ;*********************************
  2. ;*                                 *
  3. ;*   SLIDING BLOCK PUZZLE GAME   *
  4. ;*                                  *                                    
  5. ;*  WRITTEN BY  DARRYL BARTLETT  *
  6. ;*                                 *
  7. ;*       USING  BLITZ BASIC      *
  8. ;*                                 *
  9. ;*********************************
  10.  
  11. ;Set up the game screen
  12. Const width=640, height=480
  13. Graphics width, height
  14. SetBuffer BackBuffer()
  15.  
  16. ;Load all the game graphics
  17. Global cursor = LoadImage("cursor.bmp")
  18. Global button = LoadImage("button.bmp")
  19. Global empty_button = LoadImage("emptybtn.bmp")
  20. Global background = LoadImage("backgrnd.bmp")
  21. Global banner = LoadImage("logo.bmp")
  22. Global btn1_1 = LoadImage("btn1_1.bmp")
  23. Global btn1_2 = LoadImage("btn1_2.bmp")
  24. Global btn1_3 = LoadImage("btn1_3.bmp")
  25. Global btn2_1 = LoadImage("btn2_1.bmp")
  26. Global btn2_2 = LoadImage("btn2_2.bmp")
  27. Global btn2_3 = LoadImage("btn2_3.bmp")
  28. Global screen_grab
  29.  
  30. ;Define the global variables
  31. Dim grid(6,6)
  32. Dim temp_grid(36)
  33. Global a$
  34. Global xm, ym, zone, x_zone, y_zone
  35. Global mouse_used, quit_game, moves
  36. Global up_move, down_move, left_move, right_move
  37.  
  38. ;Scrolling message variables
  39. Type HorizontalScroll
  40.     Field x
  41.     Field c$
  42. End Type
  43. Global chars_on_screen
  44. Global str_pos
  45. Global tt$
  46. Global scrolling_message$ = "WELCOME TO SLIDING BLOCK PUZZLE GAME WRITTEN BY DARRYL BARTLETT USING BLITZ BASIC.  THE AIM OF THE GAME IS TO PUT ALL THE BLOCKS IN ORDER (A..Z AND THEN 0..1)    "
  47. Global pause_counter
  48.  
  49. ;Set the in-game font
  50. Global font = LoadFont ("times", 32, 1, 0, 0)
  51. SetFont font
  52.  
  53. ;Set up the scrolling message font
  54. Global font2 = LoadFont("times", 16, 0, 0, 0)
  55.  
  56. ;Set up moves made font
  57. Global font3 = LoadFont("times", 24, 0, 0 ,0)
  58.  
  59. Gosub CreateGrid            ;Create the intial random grid layout
  60.  
  61. str_pos = 1
  62. chars_on_screen = 0
  63. pause_counter = 5
  64. quit_game = 0
  65.  
  66. ;Repeat until the quit game option is select
  67. While quit_game = 0
  68.     Cls
  69.     
  70.     Gosub DrawScreen        ;Draw the screen
  71.         
  72.     UpdateHorizontalScroll()    ;Update the scrolling message on the screen
  73.  
  74.     ;Work out if a new character in the scrolling message is to be 
  75.     ;added to the screen
  76.     If pause_counter = 0 Then    
  77.         If chars_on_screen < 41 
  78.             tt$ = Mid$(scrolling_message$, str_pos, 1)
  79.             CreateHorizontalScroll(tt$)
  80.             str_pos = str_pos + 1
  81.         
  82.             ;If end of scrolling message found, loop back to the beginning
  83.             If str_pos > Len(scrolling_message)
  84.                 str_pos = 1
  85.             End If
  86.         End If
  87.         
  88.         pause_counter = 5
  89.     End If
  90.     
  91.     If pause_counter > 0 Then
  92.         pause_counter = pause_counter - 1
  93.     End If
  94.     
  95.     ;Get the current x and y mouse co-ordinates
  96.     xm = MouseX()
  97.     ym = MouseY()
  98.         
  99.     Gosub CheckButtons        ;Handle user pressing on one of the two option buttons    
  100.     Gosub CheckZones        ;Handle the mouse being in a tile zone
  101.     Gosub CheckWin            ;Check if all the tiles are in the correct position
  102.     
  103.     ;Draw the mouse cursor
  104.     DrawImage cursor, xm, ym
  105.     
  106.     Flip
  107. Wend
  108.  
  109. ;End of game routine - grabs the whole screen and scrolls it down the screen
  110. screen_grab = CreateImage(640,480)
  111. GrabImage screen_grab, 0, 0
  112.  
  113. For counter = 0 To 480 Step 3
  114.     Cls    
  115.     DrawImage screen_grab, 0, counter
  116.     Flip
  117. Next
  118.  
  119.  
  120. ;------------------------------
  121. ;CREATE THE INITIAL GRID LAYOUT
  122. .CreateGrid
  123.  
  124. moves = 0
  125.  
  126. ;Create initial grid using a one-dimensional array
  127. For counter = 0 To 35
  128.     temp_grid(counter) = counter
  129. Next
  130.  
  131. ;Jumble up the grid
  132. SeedRnd MilliSecs()
  133. For counter = 1 To 500
  134.     a = Rnd(0,35)
  135.     b = Rnd(0,35)
  136.  
  137.     c = temp_grid(a)
  138.     d = temp_grid(b)
  139.  
  140.     temp_grid(a) = d
  141.     temp_grid(b) = c
  142. Next
  143.  
  144. ;Convert the temporary grid to a 6x6 two-dimensional array arrangement
  145. grid_ref = 0
  146. For counter1 = 0 To 5
  147.     For counter2 = 0 To 5
  148.         grid(counter2, counter1) = temp_grid(grid_ref)        
  149.         grid_ref = grid_ref + 1
  150.     Next
  151. Next
  152.  
  153. Return
  154.  
  155.  
  156. ;---------------
  157. ;DRAW THE SCREEN
  158. .DrawScreen
  159.  
  160. ;Draw the game background tile
  161. TileImage background
  162.  
  163. ;Draw game logo
  164. DrawImage banner, 450, 180
  165.     
  166. ;Draw the two option buttons
  167. DrawImage btn1_1, 470, 50
  168. DrawImage btn2_1, 470, 100
  169.  
  170. ;Display number of moves taken so far
  171. SetFont font3
  172. Text 450, 400, "MOVES MADE"
  173. Text 450, 420, moves
  174. SetFont font
  175.  
  176. ;Draw the game tiles
  177. x_pos = 50
  178. y_pos = 50
  179. For counter1 = 0 To 5
  180.     For counter2 = 0 To 5
  181.         If grid(counter2, counter1) = 35 Then
  182.             ;Draw the empty box tile
  183.             DrawImage empty_button, x_pos, y_pos
  184.         Else
  185.             ;Draw a filled-in tile
  186.             DrawImage button, x_pos, y_pos
  187.         End If
  188.         
  189.         ;Draw the letter on the tile
  190.         Select grid(counter2, counter1)
  191.             Case 0 : a$ = "A"
  192.             Case 1 : a$ = "B"
  193.             Case 2 : a$ = "C"
  194.             Case 3 : a$ = "D"
  195.             Case 4 : a$ = "E"            
  196.             Case 5 : a$ = "F"
  197.             Case 6 : a$ = "G"
  198.             Case 7 : a$ = "H"
  199.             Case 8 : a$ = "I"
  200.             Case 9 : a$ = "J"            
  201.             Case 10 : a$ = "K"
  202.             Case 11 : a$ = "L"
  203.             Case 12 : a$ = "M"
  204.             Case 13 : a$ = "N"
  205.             Case 14 : a$ = "O"        
  206.             Case 15 : a$ = "P"
  207.             Case 16 : a$ = "Q"
  208.             Case 17 : a$ = "R"
  209.             Case 18 : a$ = "S"
  210.             Case 19 : a$ = "T"        
  211.             Case 20 : a$ = "U"
  212.             Case 21 : a$ = "V"
  213.             Case 22 : a$ = "W"
  214.             Case 23 : a$ = "X"
  215.             Case 24 : a$ = "Y"            
  216.             Case 25 : a$ = "Z"
  217.             Case 26 : a$ = "1"
  218.             Case 27 : a$ = "2"
  219.             Case 28 : a$ = "3"
  220.             Case 29 : a$ = "4"
  221.             Case 30 : a$ = "5"
  222.             Case 31 : a$ = "6"
  223.             Case 32 : a$ = "7"
  224.             Case 33 : a$ = "8"
  225.             Case 34 : a$ = "9"
  226.             Case 35 : a$ = ""
  227.         End Select
  228.  
  229.         ;Make sure the letter is centred within the tile (a tile is 60x60 pixels)
  230.         w = StringWidth(a$)
  231.         h = StringHeight(a$)
  232.         xx = x_pos + ((60 - w) / 2)
  233.         yy = y_pos + ((60 - h) / 2)
  234.         Text xx, yy, a$
  235.         
  236.         x_pos = x_pos + 65
  237.     Next
  238.     x_pos = 50
  239.     y_pos = y_pos + 65
  240. Next
  241.  
  242. Return
  243.  
  244.  
  245.  
  246. ;--------------
  247. ;CHECK BUTTONS
  248. ;Check if the mouse is over and/or the mouse button is down over one of the two option buttons
  249. .CheckButtons
  250.  
  251. ;If the x mouse co-ordinate lies between 450 and 620
  252. If xm > 469 And xm < 621 
  253.  
  254.     ;If the cursor has a y co-ordinate between 50 and 100
  255.     If ym > 49 And ym < 100
  256.         ;If the left mouse button is not down
  257.         If mouse_used = 1 Or MouseDown(1) = 0
  258.             DrawImage btn1_2, 470, 50
  259.         Else
  260.             ;If the left mouse button is pressed down while over the option
  261.             If mouse_used = 0
  262.                 DrawImage btn1_3, 470, 50
  263.                 mouse_used = 1
  264.                 
  265.                 ;Make sure user really wants to start a new game
  266.                 Color 255, 255, 255
  267.                 Text 0, 0, "Are you sure you want to start a new game? (Y or N)"            
  268.                 ok_key = 0             
  269.                 Flip
  270.             
  271.                 ;Repeat until either Y or N is pressed
  272.                 Repeat
  273.                     ;If Y pressed
  274.                     If KeyDown(21) Then
  275.                         ok_key = 1
  276.                         Gosub CreateGrid
  277.                     End If
  278.                 
  279.                     ;If N pressed
  280.                     If KeyDown(49) Then
  281.                         ok_key = 1                        
  282.                     End If
  283.                 Until ok_key <> 0                
  284.             End If
  285.         End If
  286.     End If
  287.     
  288.     ;If the cursor has a y co-ordinate between 100 and 150
  289.     If ym > 99 And ym < 150
  290.         ;If the left mouse button is not down
  291.         If mouse_used = 1 Or MouseDown(1) = 0
  292.             DrawImage btn2_2, 470, 100
  293.         Else
  294.             ;If the left mouse button is pressed down while over the option
  295.             If mouse_used = 0 
  296.                 DrawImage btn2_3, 470, 100
  297.                 
  298.                 mouse_used = 1
  299.  
  300.                 ;Make sure user really wants to quit
  301.                 Color 255, 255, 255
  302.                 Text 0, 0, "Are you sure you want to quit? (Y or N)"
  303.             
  304.                 ok_key = 0 
  305.             
  306.                 Flip
  307.             
  308.                 ;Repeat until either Y or N is pressed
  309.                 Repeat
  310.                     ;If Y pressed
  311.                     If KeyDown(21) Then
  312.                         ok_key = 1
  313.                         quit_game = 1
  314.                     End If
  315.  
  316.                     ;If N pressed
  317.                     If KeyDown(49) Then
  318.                         ok_key = 1
  319.                         quit_game = 0
  320.                     End If
  321.                 Until ok_key <> 0
  322.             End If
  323.         End If
  324.     End If
  325.     
  326.     If MouseDown(1) = 0
  327.         mouse_used = 0 
  328.     End If
  329. End If
  330.  
  331. Return
  332.  
  333.  
  334. ;-----------
  335. ;CHECK ZONES
  336. ;Check if the mouse pointer is currently over one of the 36 tiles
  337. .CheckZones
  338.  
  339. found_zone = 0
  340. zone = 0
  341.  
  342. ;Loop through each zone to see if the mouse cursor is currently within it
  343. temp_x = 50
  344. temp_y = 50
  345. For counter1 = 0 To 5
  346.     For counter2 = 0 To 5
  347.         found_zone = found_zone + 1
  348.         
  349.         If xm >= temp_x And xm <= temp_x + 65
  350.             If ym >= temp_y And ym <= temp_y + 65
  351.                 zone = found_zone
  352.                 x_zone = counter2
  353.                 y_zone = counter1
  354.             End If
  355.         End If
  356.         
  357.         temp_x = temp_x + 65
  358.     Next
  359.     temp_x = 65
  360.     temp_y = temp_y + 65
  361. Next
  362.  
  363. ;Handle the mouse clicking the left mouse button when cursor is over a tile
  364. If MouseDown(1) = 1 Then
  365.     ;Work out the movement available for each individual tile
  366.     ;0 = cannot move in that direction    1 = can move in that direction
  367.     If zone > 0 Then
  368.         Select zone
  369.             Case 1 : up_move = 0 : down_move = 1 : left_move = 0 : right_move = 1
  370.             Case 2 : up_move = 0 : down_move = 1 : left_move = 1 : right_move = 1
  371.             Case 3 : up_move = 0 : down_move = 1 : left_move = 1 : right_move = 1
  372.             Case 4 : up_move = 0 : down_move = 1 : left_move = 1 : right_move = 1
  373.             Case 5 : up_move = 0 : down_move = 1 : left_move = 1 : right_move = 1
  374.             Case 6 : up_move = 0 : down_move = 1 : left_move = 1 : right_move = 0        
  375.             Case 7 : up_move = 1 : down_move = 1 : left_move = 0 : right_move = 1
  376.             Case 8 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  377.             Case 9 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  378.              Case 10 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  379.             Case 11 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  380.             Case 12 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 0        
  381.             Case 13 : up_move = 1 : down_move = 1 : left_move = 0 : right_move = 1
  382.             Case 14 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  383.             Case 15 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  384.             Case 16 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  385.             Case 17 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  386.             Case 18 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 0
  387.             Case 19 : up_move = 1 : down_move = 1 : left_move = 0 : right_move = 1
  388.              Case 20 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  389.             Case 21 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  390.             Case 22 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  391.             Case 23 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  392.             Case 24 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 0        
  393.             Case 25 : up_move = 1 : down_move = 1 : left_move = 0 : right_move = 1
  394.             Case 26 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  395.             Case 27 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  396.             Case 28 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  397.             Case 29 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 1
  398.             Case 30 : up_move = 1 : down_move = 1 : left_move = 1 : right_move = 0
  399.             Case 31 : up_move = 1 : down_move = 0 : left_move = 0 : right_move = 1
  400.             Case 32 : up_move = 1 : down_move = 0 : left_move = 1 : right_move = 1
  401.             Case 33 : up_move = 1 : down_move = 0 : left_move = 1 : right_move = 1
  402.             Case 34 : up_move = 1 : down_move = 0 : left_move = 1 : right_move = 1
  403.             Case 35 : up_move = 1 : down_move = 0 : left_move = 1 : right_move = 1
  404.             Case 36 : up_move = 1 : down_move = 0 : left_move = 1 : right_move = 0
  405.         End Select
  406.     
  407.         ;Work out if desired move is possible
  408.         If up_move = 1
  409.             If grid(x_zone, y_zone - 1) = 35
  410.                 temp_a = grid(x_zone, y_zone)
  411.                 grid(x_zone, y_zone) = 35
  412.                 grid(x_zone, y_zone - 1) = temp_a
  413.                 moves = moves + 1
  414.             End If
  415.         End If
  416.     
  417.         If down_move = 1
  418.             If grid(x_zone, y_zone + 1) = 35
  419.                 temp_a = grid(x_zone, y_zone)
  420.                 grid(x_zone, y_zone) = 35
  421.                 grid(x_zone, y_zone + 1) = temp_a
  422.                 moves = moves + 1
  423.             End If
  424.         End If
  425.     
  426.         If left_move = 1
  427.             If grid(x_zone - 1, y_zone) = 35
  428.                 temp_a = grid(x_zone, y_zone)
  429.                 grid(x_zone, y_zone) = 35
  430.                 grid(x_zone - 1, y_zone) = temp_a
  431.                 moves = moves + 1
  432.             End If
  433.         End If
  434.     
  435.         If right_move = 1
  436.             If grid(x_zone + 1, y_zone) = 35
  437.                 temp_a = grid(x_zone, y_zone)
  438.                 grid(x_zone, y_zone) = 35
  439.                 grid(x_zone + 1, y_zone) = temp_a
  440.                 moves = moves + 1
  441.             End If
  442.         End If    
  443.     End If
  444. End If
  445.  
  446. Return
  447.  
  448.  
  449.  
  450. ;------------------------------------------------------
  451. ;CHECKWIN - Check if all tiles are in the correct order
  452. .CheckWin
  453.     in_order = 1
  454.     counter = 0
  455.     
  456.     For counter1 = 0 To 5
  457.         For counter2 = 0 To 5
  458.             If grid(counter2, counter1) <> counter Then
  459.                 in_order = 0
  460.             End If
  461.             counter = counter + 1
  462.         Next
  463.     Next
  464.     
  465.     ;Congratulations routine
  466.     If in_order = 1 Then
  467.         ok_key = 0
  468.         
  469.         ;Repeat until either Y or N is pressed
  470.         Repeat
  471.             Cls
  472.             TileImage background
  473.             Text 320, 10,"GAME COMPLETED", 1, 0
  474.             Text 320, 70, "YOU COMPLETED THE GAME USING", 1, 0
  475.             Text 320, 100, moves, 1, 0
  476.             Text 320, 130, "MOVES", 1, 0            
  477.             Text 320, 200, "DO YOU WANT TO PLAY AGAIN?", 1, 0
  478.             Text 320, 230, "(Y or N)", 1, 0
  479.             
  480.             ;If Y pressed
  481.             If KeyDown(21) Then
  482.                 ok_key = 1
  483.                 quit_game = 0
  484.                 Gosub CreateGrid
  485.             End If
  486.  
  487.             ;If N pressed
  488.             If KeyDown(49) Then
  489.                 ok_key = 1
  490.                 quit_game = 1
  491.             End If
  492.             
  493.             Flip            
  494.         Until ok_key <> 0
  495.     End If
  496. Return
  497.  
  498.  
  499.  
  500.  
  501. ;-----------------------
  502. ;Text scroller functions
  503.  
  504. ;Adds a new character to the scrolling message
  505. Function CreateHorizontalScroll(tt$)
  506.     hs.HorizontalScroll = New HorizontalScroll
  507.     hs\x = 640
  508.     hs\c = tt$
  509. End Function
  510.  
  511. ;Updates the scrolling message letters on the screen
  512. Function UpdateHorizontalScroll()
  513.     SetFont font2
  514.     
  515.     ;Loop through each of the current scrolling characters
  516.     For hs.HorizontalScroll = Each HorizontalScroll
  517.         If pause_counter = 0 Then
  518.             hs\x = hs\x - 16
  519.         End If
  520.         
  521.         Text hs\x, 460, hs\c
  522.         
  523.         ;If the character has gone off the left hand side of the screen
  524.         If hs\x < 0
  525.             Delete hs
  526.             chars_on_screen = chars_on_screen - 1
  527.         End If
  528.     Next
  529.     
  530.     SetFont font
  531. End Function