Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Help with my tile slider

Author
Message
Thraxas
Retired Moderator
18
Years of Service
User Offline
Joined: 8th Feb 2006
Location: The Avenging Axe, Turai
Posted: 31st May 2007 08:39 Edited at: 31st May 2007 08:40
I am very new to DarkBasic Pro and I decided to make a tile slider game (like one I had made a while ago with gamemaker). Anyway so far I have got the game to display the pictures you can choose to play and then to break up the picture into individual tiles.

I am now stuck at trying to move the tiles around the screen. I want to click on a tile and if it is next to the 'blank' tile have them swap places. I have tried numerous methods for this but keep failing miserably.

I have attached the project and media in case someone is willing to help me out. I have removed most of the pictures and replaced them with placeholder images so that the download isn't massive. So it just contains one 'playable' picture, as it were. I'm thinking that instead of sprites I should have the tiles as types, but I'm not sure if that's the right thing to do.

Also this is the first thing I have tried to code by myself so the way I have done things probably isn't the best.

Thanks

Attachments

Login to view attachments
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 1st Jun 2007 04:58
If you use a numeric array dimensioned to the number of tiles across and down in the puzzle, you can fill the array with the numbers corresponding to the particular picture pieces at all the positions on the tile grid.

The blank space could be represented by a zero.

When the user clicks on a tile, you simply check the numbers in the array above, below, left and right of the tile clicked on. If any are zero then that's the one to swap with.

If none are zero then no swap is done.

The other advantage of this method is that you can very quickly loop through the array after very tile swap and if the numbers run consecutively from 1 to the number of tiles in the picture, then the puzzle has been solved.

TDK_Man

Thraxas
Retired Moderator
18
Years of Service
User Offline
Joined: 8th Feb 2006
Location: The Avenging Axe, Turai
Posted: 1st Jun 2007 06:09 Edited at: 1st Jun 2007 09:12
Thanks TDK... I'll have a go at that.

Edit-----

Ok so you made it sound so simple I thought I would be able to do it easily. Unfortunately I'm not as clever as I like to think I am and so am still having trouble here...

I made an array: dim grid(4,3) which I think gives me a 5 x 4 grid. I can place a number into each part of the array but I'm not sure how I can associate that number with a sprite. It's probably really easy and I'm missing something simple.
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 3rd Jun 2007 22:57 Edited at: 3rd Jun 2007 23:02
OK, you have your picture which you say chop into 16 pieces in a 4x4 grid. Each piece is turned into a sprite numbered 1 to 16.

If you place them on the grid in their correct positions, it would look like this:



So, you use Dim Grid(4,4) and the 1 would go into Grid(1,1), the 2 would go into Grid(2,1), the 3 into Grid(3,1) and so on.

But that's when the puzzle is completed.

What you would do is feed the 16 tile numbers into the array randomly using something like this:



After this, the array contains all 16 tiles, so we now have to choose one to be the empty slot. The easiest way is to set the array slot which currently has tile number 16 in it:



Next you use the contents of the array to create the sprites and place them on the puzzle:



In the game, we calculate the position on the grid that the mouse is over and get the sprite number when the mouse button is clicked.

We then Gosub CheckForGap which determines if there's a gap above, below, left or right of the tile clicked on. The variable OpenPosition is set to 0 if there's no gap or 1, 2, 3 or 4 if the gap is above, below, left or right.

The SwapPositions procedure moves the tile in the correct direction.

Here's all of it in a usable format, though I've left the CheckForWin procedure for you to do. I've put a few Text commands to make it a little easier to figure out what it's doing.



The image I used is also attached below.

TDK_Man

Attachments

Login to view attachments
Thraxas
Retired Moderator
18
Years of Service
User Offline
Joined: 8th Feb 2006
Location: The Avenging Axe, Turai
Posted: 4th Jun 2007 13:08
Thanks alot TDK you have been a great help. I've almost got everything working perfectly.

Login to post a reply

Server time is: 2024-09-26 22:55:29
Your offset time is: 2024-09-26 22:55:29