12,449,919 members (47,615 online)
alternative version

91.6K views
13 bookmarked
Posted

# Puzzle: a 2D game using Visual Basic

, 23 Feb 2005 CPOL
 Rate this:
This is a complete Puzzle implementation.

## Introduction

This is my first upload. This is just a two hours project and my first submission to The Code Project, so be indulgent ~('.')~

I write this articles because I wish to share something I just learned. I remember that I am a beginner I can make a mistake. I have developed a Simple 2D game. I wrote this game program in my school days out of my interest in learning Visual Basic. I used the Visual Studio 6 for coding this.

## The Game

I realize that a number of people will just want to try out the sample and see if it is worth spending the time looking at the source code. So I will first present the game, the controls, then I will explain how it works in later sections. This intermediate tutorial describes how to play Puzzle and how to Re arrange the numbers.

First, the user starts the game by clicking on OK:

Then a Main window appears. Here numbers are arranged randomly you have to arrange numbers in ordered way.

How is Puzzle played?

=> To swap a number with its adjacent empty block, just click the number. Continue this process until all numbers are arranged in a ordered way.

=>To end the game, click the icon in the upper right corner.

=>To view about the game, click the icon in the upper right corner.

=> To rearrange numbers, click button in the upper middle of screen.

## Design

The code is easy to read: this is not a bunch of unreadable code lines and the game is fully functional. I think it can be interesting for beginners.

### Global Variable Declaration

```Dim x, Y, c As Integer
Dim bug As Integer
Dim flag As Integer
Dim
TEMP As Integer```

The game is built up from a number of smaller sub-Modules. Each Module provides a distinct purpose. I have identified the Modules below.

• `CMD_Click( )`
• `SWAP( )`
• `COMPARE()`

### CMD_Click( )

```.
.
.
If CMD(Index).Caption <> "" Then 'To check that block is not empty```

### If the Selected Block is Not in the First Row

```If Index > 3 Then
If CMD(Index - 4).Caption = "" Then
'To check that empty block is in first row or not
Call SWAP(Index, -4) 'if Ok Swap
Call COMPARE
'To check all number are arranged or not
Exit Sub
End If
End If```

### If the Selected Block is Not in the Last Row

```If Index < 12 Then
If CMD(Index + 4).Caption = "" Then
'To check that empty block is in first row or not
Call SWAP(Index, 4) 'if Ok Swap
Call COMPARE
'To check all number are arranged or not
Exit Sub
End If
End If```

### If Selected Block is Not in Last Column

```If (Index + 1) Mod 4 <> 0 Then
If CMD(Index + 1).Caption = "" Then
'To check that adjacent right block is empty
Call SWAP(Index, 1) 'if Ok Swap
Call COMPARE
'To check all number are arranged or not
Exit Sub
End If
End If```

### If the Selected Block is Not in the First Column

```If Index Mod 4 <> 0 Then
If CMD(Index - 1).Caption = "" Then
'To check that adjacent left block is empty
Call SWAP(Index, -1) 'if Ok Swap
Call COMPARE
'To check all number are arranged or not
Exit Sub
End If
End If
End If
.
.
. ```

### SWAP ( )

#### Module to Swap Selected Number with Empty Block

```TEMP = CMD(A).Caption

CMD(A).Caption = CMD(A + B).Caption

CMD(A + B).Caption = TEMP```

### COMPARE()

#### Module to Check that All Numbers are Arranged in Order or Not If so, Show Win Screen

```For x = 0 To 14
If CMD(x).Caption = x + 1 Then
flag = flag + 1
ElseIf CMD(x).Caption <> x + 1
Then
flag = 0
Exit Sub

End If
If flag = 14
Then
frmwin

frmwin.Show
End If
Next x```

## Conclusion

I would like to give credit and thanks to my brother Rohit Soam for constructive criticisms and editing. Thanks, brother.

## History

30-Jan-2001 - Initial Release of Article

## Share

 Web Developer India
FD 100
RT 135
FD 50
LT 90
FD 50
RT 45
FD 100

output : M

that's my first code in LOGO, when i was in VI standard.

it's me... Mohit.

from Holy City of India.... Rishikesh.
I love programming, not much else to say

"Those who dreams the most do the the most"

RSM 4 U

## You may also be interested in...

 Pro Pro

 First Prev Next
 FireFox Anonymous24-Feb-05 2:03 Anonymous 24-Feb-05 2:03
 Too many Yulianto AKA goodmast3r23-Feb-05 22:43 Yulianto AKA goodmast3r 23-Feb-05 22:43
 The Article Ken Mazaika23-Feb-05 17:22 Ken Mazaika 23-Feb-05 17:22
 Re: The Article norm.net23-Feb-05 21:00 norm.net 23-Feb-05 21:00
 Last Visit: 31-Dec-99 18:00     Last Update: 26-Aug-16 14:58 Refresh 1