Jumat, 17 Mei 2024

Game Tembak VB 6

 


GAME TEMBAK SOURCE CODE


Option Explicit


' The next two lines are used to setup the function for sound play.

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1         '  play asynchronously

Dim X, Y As Integer


'    What follows illustrates the most basic concept of animation in VB6.  There are much better

'    ways to do this that offer better transistions.  I simply used photoshop to create several quick

'    frames of each image and call them from the timers involved below.


'    This is not my usual line of programming.  I was asked how this could be accomplished so I thought

'    I thought I would share with who ever might be interested.


'    My element in programming generally revolves around port interfacing such USB, Serial and LPT for

'    for purposes of automation in the outside world.  Drop me a line if you have any questions or would

'    like to share.


'        **********************    stettlerbrad@gmail.com    ******************************



Private Sub Form_Click()

ImgShot.Tag = "1"  ' I like using the tag function of controls.  Why create new variables when evry object already has a "tag" that can hold a string value?

Call sndPlaySound(App.Path & "\media\gun.wav", SND_ASYNC) ' call the standard gunshot sound from the installation path of this project

Timer3.Enabled = True 'enable the timer that will quickly show the bullet hole and then fade it off the form

X = Text1.Text ' pick the x and x coordinates of where the mouse was clicked on the form for puposes of drawing the shot in that area

Y = Text2.Text

ImgShot.Top = Y - 200  ' we need to adjust the left and right positioning a little bit as the left and right properity don't refer to the center of the image we'll be drawing

ImgShot.Left = X - 160

ImgShot.Visible = True ' and of course show the shot where it belongs

End Sub



Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Text1.Text = X 'this is just an illustration to show the tracking of the mouse in its X and Y coordinates accross the form

Text2.Text = Y


End Sub


Private Sub ImgBottle_Click()

Call sndPlaySound(App.Path & "\media\gun_glass.wav", SND_ASYNC) ' play the unique sound of the gun shot mixed with the glass breaking

ImgBottle.Tag = "1" ' set the tag up for the bottle image so it is ready when it gets to timer2 to show the breaking bottle

Timer2.Enabled = True ' set the timer into action to show the bottle animation

End Sub


Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Text1.Text = X  ' an interesting little point - notice that the x and y coordinates in your text boxes are showing the X and Y of the bottle image. You could get really creative and change which animation plays in accordinace with where the target is struck.

Text2.Text = Y

End Sub


Private Sub ImgBottle_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Text1.Text = X

Text2.Text = Y

End Sub


Private Sub ImgCan_Click()

Call sndPlaySound(App.Path & "\media\gun_can.wav", SND_ASYNC) ' call the sound of the gun hitting the can

ImgCan.Tag = "1"

Timer1.Enabled = True

End Sub



Private Sub ImgCan_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Text1.Text = X

Text2.Text = Y

End Sub


Private Sub Timer1_Timer()  ' this is the 4 stage animation fo the can being shot off the page

' I set the interval pretty low to keep it as smooth as possible on the timer control

If ImgCan.Tag = "1" Then ImgCan.Tag = "2": ImgCan.Picture = LoadPicture(App.Path & "\media\can2.bmp"): Exit Sub

If ImgCan.Tag = "2" Then ImgCan.Tag = "3": ImgCan.Picture = LoadPicture(App.Path & "\media\can3.bmp"): Exit Sub

If ImgCan.Tag = "3" Then ImgCan.Tag = "4": ImgCan.Picture = LoadPicture(App.Path & "\media\can4.bmp"): Exit Sub

If ImgCan.Tag = "4" Then ImgCan.Tag = "": ImgCan.Visible = False ' take the can off the form as it is now gone

  

End Sub


Private Sub Timer2_Timer() ' bottle breaking animation

If ImgBottle.Tag = "1" Then ImgBottle.Tag = "2": ImgBottle.Picture = LoadPicture(App.Path & "\media\bottle2.bmp"): Exit Sub

If ImgBottle.Tag = "2" Then ImgBottle.Tag = "3": ImgBottle.Picture = LoadPicture(App.Path & "\media\bottle3.bmp"): Exit Sub

If ImgBottle.Tag = "3" Then ImgBottle.Tag = "4": ImgBottle.Picture = LoadPicture(App.Path & "\media\bottle4.bmp"): Exit Sub

If ImgBottle.Tag = "4" Then ImgBottle.Tag = "": ImgBottle.Visible = False


End Sub


Private Sub Timer3_Timer() ' gun shots fading quikly into the form background animation

If ImgShot.Tag = "1" Then ImgShot.Tag = "2": ImgShot.Picture = LoadPicture(App.Path & "\media\shot2.bmp"): Exit Sub

If ImgShot.Tag = "2" Then ImgShot.Tag = "3": ImgShot.Picture = LoadPicture(App.Path & "\media\shot3.bmp"): Exit Sub

If ImgShot.Tag = "3" Then ImgShot.Tag = "4": ImgShot.Picture = LoadPicture(App.Path & "\media\shot4.bmp"): Exit Sub

If ImgShot.Tag = "4" Then ImgShot.Tag = "": ImgShot.Visible = False

End Sub


Tidak ada komentar: