Sub New
As a form control, the Tile objects have a few extra properties than the ones you defined, such as Location, Size, and Text. These will come in handy when moving the tiles around the puzzle board, especially the Location and Size properties. Also note that in the code to follow, CurrentLoc always represents the tile's current location and Text, an object property, represents the tile's original location. An object property was used for the latter value in order to increase flexibility in referencing it elsewhere in the app.
This also exemplifies one of the many differences between .NET CF and .NET Framework. In a desktop application, Name is one of the properties that can be set programmatically. In a PDA application, however, Name can be read, but not set. So here, Text is being used instead. This is another good reason to start small and scale up.
The key to chopping up the image into small, moveable pieces is to override the Paint routine for the object. Use it to paint the specific portion of the overall image that relates to this particular tile.
To keep things simple, code has not been added to actually stretch or resize the puzzle. The code above merely shows more of the image, if available. If you want to get a little fancier, you can add some algorithms to the tile creation that resize the image, as well. (In fact, the sample code that comes with VS.NET includes a slide puzzle written for the Pocket PC in C#. It includes a few more features than I'm showing here, as well as some different techniques for creating the puzzle, though it doesn't lend itself quite as easily to multi-platform development.)
The complete code for Tile.vb can be found here.
Building the grid class
Most of the real work happens in the Grid Class, which represents the puzzle itself. It's essentially a collection of Tile objects, along with all of the methods necessary for basic game mechanics. Add a new Class to the project and call it Grid.vb.
First, member declarations. These should be self-explanatory.
Next, instantiation, which assigns values to all these new variables.
You'll notice a Setup() routine there at the end. Here's the code for it. This actually creates the puzzle board, with all of its tiles. Notice that no constants appear here.
One thing to notice about the Setup() routine is the last line: It sets the "blank" tile's Visible property to "False". This could just as easily have been added to the form instead of the Grid class. And you may want to do just that, depending on what new features you add to the UI. The reason I add it here is to demonstrate that this is one of those little opportunities for pulling code out of the UI when it works on multiple platforms.
![]()
If you're interested in this topic, these articles may be helpful:
![]() | Programming visual basic .NET, 2nd edition by Jesse Liberty, O'Reilly O'Reilly's Programming Visual Basic .NET... |
![]() | ClickOnce - reduce the challenges of mobilized software deployment by 3 Leaf Solutions, Ltd. Intel Corp. This article introduces the n... |
![]() | Introduction to Eiffel.NET, part two: advanced language features by Daniele Pagano In Part One I introduced Eiffel and talked about ... |
![]() | Creating mobilized software solutions by Devu Pandit and Justin Huntsman. Intel Corp. The Mobilized Soft... |
![]() | ASP.NET tutorial: adding records to a database using ADO.NET, SQL Server 2000, and Visual Basic.NET (VB.NET) from EALabs Inc. - EAServe.com Adding records to a SQL Server 2000 da... |
![]()
Related Jobs:


