Thursday, 20 August 2015

HOW TO CHECK FOR COLLISION BETWEEN TWO OBJECTS IN VB 6

Collision is always an event occur when two or more moving objects meets with each other.
To check for collision between two objects we must check for certain conditions to fullfil
The condition is as follows -

If Shape1.Left < Shape2.Left + Shape2.Width And Shape2.Left < Shape1.Left + Shape1.Width Then

If Shape1.Top < Shape2.Top + Shape2.Height And Shape2.Top < Shape1.Top + Shape1.Height Then
  
                 'Collision

End If
End If

Here, shape1 is one object and shape2 is another other object

There are some points that must be note for understanding the above condition

- The object as it is created in the form ocuppies some space in terms of width so when we
   move the object its left property increases but for form the total space occupied is
   object.left + object.width.

- In the first if clause both the objects are checked simultaneously i.e. whenever the shape1.Left
   value is less than the sum of occupied space of left and width, the objects had not crossed each
   so until that the collision is possible. Next the same applies to the other object so the condition is
   checked for other shape also

-Similiarly, as it occupies Left and Width it also occupies Top and Width. Therefore, the
 Top and Width must also be checked

Hence the above conditions are checked then the object's collision could be detected

Lets take an example by creating two rectangle boxes , and check for collision
And if they collide they must move away from them



Here we must take two variables that changes their signs the object moves

Following is the final code for the above design..

Dim p, q As Integer

Private Sub Form_Load()
Timer1.Enabled = True
p = 150
q = -150
Timer1.Interval = 50
End Sub

Private Sub Timer1_Timer()
Shape1.Left = Shape1.Left + p
Shape2.Left = Shape2.Left + q
If Shape2.Left <= -480 Then                        ' It is the left most position
q = 150
End If
If Shape1.Left >= Form1.Width Then
p = -150
End If

If Shape1.Left < Shape2.Left + Shape2.Width And Shape2.Left < Shape1.Left + Shape1.Width Then

If Shape1.Top < Shape2.Top + Shape2.Height And Shape2.Top < Shape1.Top + Shape1.Height Then
    p = 150                                  'Collision
    q = -150
End If
End If
End Sub

Here, as soon as the direction is reversed the value of p and q are swapped (or assigned swapped values)

OK, We are done here

Wednesday, 19 August 2015

HOW TO MAKE A MOVING ROAD IN VB 6

We always think that animation is some kind of
effect that is shown as a moving object.
But it is not worth it. It is just an illusion
that is shown as an object moving

So, to demonstrate this let us create a moving
road in which road blocks are moved and repeated
as soon as soon as the block reaches the bottom

Before we get started we must know the things
that are needed. Well in this we need
-3 rectangle shapes
-1 timer

STEP 1 - In the designer tool, design the foam by filling it with
black color , Now draw three rectangle which is like a road block



STEP 2 - Add a timer anywhere on the screen. Then enable the timer in foam_load event and
also set the interval  100  (calculated in ms) 

Private Sub Form_Load()
Timer1.Interval = 10
Timer1.Enabled = True
Shape1.FillColor = vbBlue
Shape2.FillColor = vbBlue
Shape3.FillColor = vbBlue

End Sub


Here, I have taken shape1, shape2 , shape3 as road blocks
 You can fill color at foam_load time

STEP 3- In timer1_timer() (Or whatever name you have assigned) 
increase the top property of every shape by 50 simultaneously.

Private Sub Timer1_Timer()

Shape1.Top = Shape1.Top + 50
Shape2.Top = Shape2.Top + 50
Shape3.Top = Shape3.Top + 50


End Sub

Step 4 - Running the above code will move every block to the bottom of the foam.
But the shapes will not come as they are gone.
To repeat this . We need to try a trick
Drag and drop the lowest shape to the  bottom of the foam (the most possible value 
of top where the shape will disappear) then note down its top value from right side.
For me it is 5040

After noting it drag and drop the shape to its previous position

Now this is the value which let us check whether the shape is disappeared or not.
And also we need to apply it on every shape.
So, to reduce the effort we will create a function with shape as an arguement.

Public Sub rewind(block As Shape)

If block.Top >= 5040 Then
block.Top = -240
End If

End Sub


In the above we have made the top value as -240 as soon as the shape gets to the bottom .
Therefore the shape repeats its motion on every run 

STEP 5 - Finally, call the function by passing every shape because this is just a user defined 
function and without calling it is useless. Call the function in the timer1_time()

Private Sub Timer1_Timer()

Shape1.Top = Shape1.Top + 50
Shape2.Top = Shape2.Top + 50
Shape3.Top = Shape3.Top + 50

rewind Shape3
rewind Shape2
rewind Shape1

End Sub 



So, we are done here. Now you can run the application

The final code will go like this

Private Sub Form_Load()
Timer1.Interval = 10
Timer1.Enabled = True
Shape1.FillColor = vbBlue
Shape2.FillColor = vbBlue
Shape3.FillColor = vbBlue

End Sub
Public Sub rewind(block As Shape)

If block.Top >= 5040 Then
block.Top = -240
End If

End Sub

Private Sub Timer1_Timer()

Shape1.Top = Shape1.Top + 50
Shape2.Top = Shape2.Top + 50
Shape3.Top = Shape3.Top + 50

rewind Shape3
rewind Shape2
rewind Shape1

End Sub


 

Tuesday, 18 August 2015

HOW TO EXPORT TEXT TO TEXT FILE IN VB6

In Visual Basic 6 with little effort you can save your data to
a .TXT FILE

The following is the syntax that is used to create and export
data to a .txt File

Open "FILE LOCATION" for Output as #1
Print #1,text
close #1

This is the general syntax for opening and printing text to
text file

LET US NOW USE A BUTTON THAT PRINTS THE DATA
TO A TEXT FILE ON BUTTON CLICK
NOTE - WE ALSO CAN PASS THE FILENAME TO TEXTBOX



Now, in the button_Click event write the following code

Open "C:\textFile.txt" For Output As #1
Print #1, Text1.Text
Close #1
MsgBox "Your File Has Been Saved"

So, your file has been saved

Now, lets expand it a little More.
We are going to change the file name at run time
For this we need to declare a String( That stores filename" and a TextBox

Your Layout Goes like this...


Declare a String

dim filename as String

Next, assign the filename as,
filename=text1.text

Finally, replace the value of directory in the open command

Open "C:\" + filename + ".txt" For Output As #2
Print #2, Text1.Text
Close #2
MsgBox "FILE " + filename + ".txt IS SUCCESSFULLY CREATED AT C:\ " + filename + ".txt"

Hence the final code will be...



SO, IF YOU HAVE ANY DOUBTS FEEL FREE TO CLEAR OUT YOUR DOUBTS IN COMMENT

HOW TO PLAY SOUND IN VISUAL BASIC 6

Playing sound in VB6 is quite easier than any other application platform.
In this we need to use the SYSTEM DLL File WINMM.dll
This dll file is responsible in playing audios

Step 1 - First declare the following function...

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hmodule As Long, ByVal dwFlag As Long) As Long


Step 2 - Now declare the following variables

Private Const SND_APPLICATION As Long = &H80
Private Const SND_ALIAS As Long = &H10000
Private Const SND_ALIAS_ID As Long = &H110000
Private Const SND_ASYNC As Long = &H1
Private Const SND_FILENAME As Long = &H20000
Private Const SND_LOOP As Long = &H8
Private Const SND_MEMORY As Long = &H4
Private Const SND_NODEFAULT As Long = &H2
Private Const SND_NOSTOP As Long = &H10
Private Const SND_NOWAIT As Long = &H2000
Private Const SND_PURGE As Long = &H40
Private Const SND_RESOURCE As Long = &H40004
Private Const SND_SYNC As Long = &H0


Step 3 - Finally, where you wanted to play Sound
call the Function PlaySound with three parameters

eg.
PlaySound "E:\%FOLDERNAME%\FILENAME.wav", 0, SND_ASYNC


Here you have passed three arguements
- First you have passed the file location
- Second 0 is passed to ensure that you are using full File Location
- Third playing sound mode as ASYNCHRONOUS eg. SND_ASYNC ( You can also use other modes like SND_FILENAME,SND_LOOP etc)
  ASYNC means your application can produce sound in background