Click here to Skip to main content
11,412,182 members (64,557 online)
Click here to Skip to main content

Puzzle: a 2D game using Visual Basic

, 23 Feb 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

Sample screenshot

=>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
        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)

Share

About the Author

Mohit Soam
Web Developer
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 Pinmemberheidar_sadeghi14-Jan-07 19:54 
GeneralFireFox PinsussAnonymous24-Feb-05 3:03 
GeneralToo many PinmemberYulianto AKA goodmast3r23-Feb-05 23:43 
GeneralThe Article PinmemberKen Mazaika23-Feb-05 18:22 
GeneralRe: The Article Pinmembernorm.net23-Feb-05 22:00 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 23 Feb 2005
Article Copyright 2005 by Mohit Soam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid