Sliding Tile Puzzle |
Click and drag the tiles. Using the one open space, arrange the tiles next to the spheres according to color. Two types of collision are used. DBC commands make the sliding collision happen. The cursor, however, 'collides' with the tiles by using a variation of the box_collision function explained by Emperor Baal |
` Sliding Blocks Final ` Author: Hop A Long ` Uses DBC Sliding Collision and a variation of ` Emperor Baal's Box Collision ` August 6, 2004 `*****SET UP***** Sync On : Sync Rate 40 Autocam Off : Hide Mouse ` *****THE BOXES************ ` An Array of Coordinates Dim HotSpots#(4) hot# = 15.0 For h = 1 To 4 HotSpots#(h) = hot# Inc hot#,10.0 Next h ` The Game Tiles For tiles = 5 To 19 Make Object Box tiles, 10,10,2 If tiles >= 5 And tiles <= 8 Color Object tiles, RGB(0,200,0) Position Object tiles, HotSpots#(tiles-4),15,0 EndIf If tiles >= 9 And tiles <= 12 Color Object tiles, RGB(200,0,0) Position Object tiles, HotSpots#(tiles-8),25,0 EndIf If tiles >= 13 And tiles <= 14 Color Object tiles, RGB(250,150,0) Position Object tiles, HotSpots#(tiles-12),35,0 EndIf If tiles >= 15 And tiles <= 16 Color Object tiles, RGB(200,0,200) Position Object tiles, HotSpots#(tiles-12),35,0 EndIf If tiles >= 17 And tiles <= 18 Color Object tiles, RGB(250,250,0) Position Object tiles, HotSpots#(tiles-16),45,0 EndIf If tiles = 19 Color Object tiles, RGB(100,100,250) Position Object tiles, HotSpots#(tiles-16),45,0 EndIf Make Object Collision Box tiles,-5,-5,-1,5,5,1,0 Next tiles ` *****THE ENCLOSURE******** `Top Make Object Box 1,60,10,2 Position Object 1,30,55,0 Make Object Collision Box 1,-30,-5,-1,30,5,1,0 Make Object Sphere 30,5 Position Object 30,5,55,-1 Color Object 30,RGB(0,200,0) Make Object Sphere 31,5 Position Object 31,25,55,-1 Color Object 31,RGB(250,150,0) Make Object Sphere 32,5 Position Object 32,35,55,-1 Color Object 32,RGB(250,150,0) Make Object Sphere 33,5 Position Object 33,55,55,-1 Color Object 33,RGB(200,0,0) `Right Make Object Box 2,10,60,2 Position Object 2,55,30,0 Make Object Collision Box 2,-5,-30,-1,5,30,1,0 Make Object Sphere 34,5 Position Object 34,55,35,-1 Color Object 34,RGB(250,250,0) Make Object Sphere 35,5 Position Object 35,55,25,-1 Color Object 35,RGB(250,250,0) `Bottom Make Object Box 3,60,10,2 Position Object 3,30,5,0 Make Object Collision Box 3,-30,-5,-1,30,5,1,0 Make Object Sphere 36,5 Position Object 36,55,5,-1 Color Object 36,RGB(0,200,0) Make Object Sphere 37,5 Position Object 37,35,5,-1 Color Object 37,RGB(100,100,250) Make Object Sphere 38,5 Position Object 38,5,5,-1 Color Object 38,RGB(200,0,0) `Left Make Object Box 4,10,60,2 Position Object 4,5,30,0 Make Object Collision Box 4,-5,-30,-1,5,30,1,0 Make Object Sphere 39,5 Position Object 39,5,25,-1 Color Object 39,RGB(200,0,200) Make Object Sphere 40,5 Position Object 40,5,35,-1 Color Object 40,RGB(200,0,200) ` *****CURSOR*************** Make Object Sphere 20,2 Position Object 20,30,30,-3 Color Object 20, RGB(240,0,240) `***************************** MAIN LOOP ******************************** Do ` *****CURSOR ROUTINE**** ` Find the 'Cursor' : 20 old_cx# = Object Position X(20) old_cy# = Object Position Y(20) old_cz# = Object Position Z(20) ` Control it with the Mouse mx# = MouseMoveX()* 0.05 my# = MouseMoveY()* 0.05 new_cx# = old_cx# + mx# new_cy# = old_cy# - my# ` Stay in the frame If new_cx# > 49.0 Then new_cx# = 49.0 If new_cx# < 11.0 Then new_cx# = 11.0 If new_cy# > 49.0 Then new_cy# = 49.0 If new_cy# < 11.0 Then new_cy# = 11.0 ` Move it to the New Location Position Object 20,new_cx#,new_cy#,old_cz# ` *****LOCATE A TILE, MOVE IT AND********* ` ********CHECK FOR COLLISION************* For tile = 5 To 19 ` Consult the Box Collision Function to find ` the Tile Behind the Cursor returns = Box_Collision(20,tile) If returns = 1 and MouseClick() = 1 x# = Object Position X(tile) y# = Object Position Y(tile) Text 10,160,"tile : "+str$(tile) Text 10,180,"collides with : "+str$(obj_id) temp_x# = x# + mx# temp_y# = y# - my# ` Move the Selected Tile Position Object tile,temp_x#,temp_y#,0 obj_id = 0 ` Make Sliding Collision for the ` Selected Tile as It Moves If Object Collision(tile,0)>0 obj_id = Object Collision(tile,0) Dec temp_x#,get object collision x() Dec temp_y#,get object collision y() EndIf ` Make Sure the Cursor Stays With a Selected Tile When It Stops Position Object tile,temp_x#,temp_y#,0 ` Save the value for the correction routine below test_tile = tile If temp_x# = x# And temp_y# = y# Then Position Object 20,old_cx#,old_cy#,old_cz# If temp_x# = x# And temp_y# <> y# Then Position Object 20,old_cx#,new_cy#,old_cz# If temp_x# <> x# And temp_y# = y# Then Position Object 20,new_cx#,old_cy#,old_cz# EndIf Next tile ` If a tile is not placed correctly, the tile movement locks up. ` This routine checks for a misplacement and corrects it. If test_tile <> 0 And MouseClick() <> 1 test_x# = Object Position X(test_tile) test_y# = Object Position Y(test_tile) For test = 1 To 4 If ABS(test_x# - HotSpots#(test)) < 2.0 Then test_x# = HotSpots#(test) If ABS(test_y# - HotSpots#(test)) < 2.0 Then test_y# = HotSpots#(test) Next test Position Object test_tile,test_x#,test_y#,0 EndIf ` *****CAMERA ROUTINE**** Position Camera 30,30,-60 Point Camera 30,30,0 ` *****DISPLAY*********** Center Text 320,15,"SLIDING TILE PUZZLE" Center Text 320,455,"Place the Tiles Next To the Spheres of the Same Color" `*****REFRESH***** Sync Loop `******************END OF LOOP******************************************** Function Box_Collision(nr1,nr2) If Object Position X(nr1) > Object Position X(nr2) - Object Size X(nr2)/2 If Object Position X(nr1) < Object Position X(nr2) + Object Size X(nr2)/2 If Object Position Y(nr1) > Object Position Y(nr2) - Object Size Y(nr2)/2 If Object Position Y(nr1) < Object Position Y(nr2) + Object Size Y(nr2)/2 ExitFunction 1 EndIf EndIF EndIf EndIf EndFunction 0 |
For an example using only two objects colliding try: Bare Bones Collision |