Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / VBScript
Article

Puzzle: a 2D game using Visual Basic

Rate me:
Please Sign up or sign in to vote.
2.30/5 (10 votes)
23 Feb 2005CPOL2 min read 124.8K   7.5K   16   5
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:

Image 1

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

Image 2

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.

Sample screenshot

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

Image 4

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

Image 5

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

Image 6

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

VB
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( )

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

If the Selected Block is Not in the First Row

VB
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

VB
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

VB
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

VB
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

VB
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

VB
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
        Load 
frmwin
        
frmwin.Show
    End If
Next x

Tools

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India 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

Comments and Discussions

 
GeneralCongratulation Pin
heidar_sadeghi14-Jan-07 18:54
heidar_sadeghi14-Jan-07 18:54 
GeneralFireFox Pin
Anonymous24-Feb-05 2:03
Anonymous24-Feb-05 2:03 
GeneralToo many Pin
Yulianto.23-Feb-05 22:43
Yulianto.23-Feb-05 22:43 
Image


<italic>Work hard and a bit of luck is the key to success.
You don`t need to be genius, to be rich.
GeneralThe Article Pin
Ken Mazaika23-Feb-05 17:22
Ken Mazaika23-Feb-05 17:22 
GeneralRe: The Article Pin
NormDroid23-Feb-05 21:00
professionalNormDroid23-Feb-05 21:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.