Click here to Skip to main content

Puzzle: a 2D game using Visual Basic

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


Web03 | 2.8.160204.4 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service