Solved

I hate VB!! ;)

Posted on 2000-03-22
12
171 Views
Last Modified: 2010-05-02
Okay, I am working on this program right and its not working right.  Its an asteroids like thing but i can not get the asteroids to blow up when you hit them with your laser, nor can i get you to die when they hit you.

I have a copy at
http://www.gamepl.com/space.zip
(250k)
0
Comment
Question by:shiny
  • 8
  • 3
12 Comments
 
LVL 27

Expert Comment

by:Ark
ID: 2647617
Hi
You forgot Asteroid and laser size
Take a look - I change:
'I'm not a very good shooter, so I increase IF statement to 10 pixels
'       IF HIT THEN DIE CODE (not even close to remotely trying to work)
'       =======
       
        If Abs(LaserY + Laser.Height - 8 - RockYPos) < 10 And LaserY > 0 Then
           If Abs(LaserX + Laser.Width / 2 - RockXPos - Asteroid(RockNum).Height / 2) < 10 Then
       
            frmGame.Show   'Temp until this works right
            End If
        End If
'       (I tryed (Derek))

'More changes:

Public Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    ShootLaser = True
    LaserX = XDest + Ship.Width / 2 - Laser.Width / 2
    LaserY = YDest + Ship.Height - 20
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    XDest = x - Ship.Width / 2
End Sub

' From Key-Down Event
     Case vbKeySpace
            'Allows the space bar to shoot too
            ShootLaser = True
            LaserX = XDest + Ship.Width / 2 - Laser.Width / 2
            LaserY = YDest + Ship.Height - 20
0
 
LVL 27

Expert Comment

by:Ark
ID: 2647635
Sorry, change this string
           If Abs(LaserX + Laser.Width / 2 - RockXPos - Asteroid(RockNum).Width / 2) < 15 Then

(was Asteroid(RockNum).Height)
0
 
LVL 27

Expert Comment

by:Ark
ID: 2647644
Place this just before "Hit to die Code"
        If Abs(YDest + Ship.Height - RockYPos - 20) < 5 Then
           If Abs(XDest + Ship.Width / 2 - RockXPos - Asteroid(RockNum).Width / 2) < 15 Then
              MsgBox "Booooommmm !!!", vbCritical
       
            frmGame.Show   'Temp until this works right
            End If
        End If
0
 
LVL 27

Expert Comment

by:Ark
ID: 2647669
Ok, now all together:
Change Mouse_Move,Mouse_Down and Key_Down events as I wrote before.
'Change this part:  IF HIT THEN DIE CODE (not even close to remotely trying to work)
'       =================================================
        If Abs(YDest + Ship.Height - RockYPos - 20) < 5 Then
           If Abs(XDest + Ship.Width / 2 - RockXPos - Asteroid(RockNum).Width / 2) < 15 Then
              MsgBox "Booooommmm !!!", vbCritical
           End If
        End If
       
        If Abs(LaserY + Laser.Height - 12 - RockYPos) < 5 And LaserY > 0 Then
           If Abs(LaserX + Laser.Width / 2 - RockXPos - Asteroid(RockNum).Width / 2) < 10 Then
              MsgBox "Nice shoot!!!", vbExclamation
           End If
        End If
0
 

Expert Comment

by:FONATURE
ID: 2648065
I do Collision checks in all of my games
Now I tried to download your zip file but i cant get it so this probally wont work
But it works really well for me.
in the form

Public Function Collision(spr1 As _ Sprite, spr2 As Sprite) As Boolean
Collision = True        
' Sets the collision as True
'Now we see if the collision impossible:
'one line
If spr1.YCoord > spr2.YCoord + _ spr2.CollisionHeight Then Collision _ =False
'one line
If spr1.YCoord + spr1.CollisionHeight _ < spr2.YCoord Then Collision = False
'one line
If spr1.XCoord + spr1.CollisionWidth _
< spr2.XCoord Then Collision = False
'one line
If spr1.XCoord > spr2.XCoord + _ spr2.CollisionWidth Then Collision = _ False
End Function


Now In your timer

   If Collision(Laser, Rock) Then
     'What ever you want it to do
     'Hit
    Else
      'Not a damb thing
    End If


'If your useing sprites this will work
'If not this is no help
'The collision width and height
'are the sprites width and height
0
 

Author Comment

by:shiny
ID: 2648833
Ark, thank you a lot for your help, when the asteroid hits you, 'boom' ;) but nothing happens when the laser hits the asteroid.  Is there a way that I can give you partial points for all your help so far?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:Ark
ID: 2651308
Hi
I don't know about partial points. But it's strange that there is no effect when laser hits asteroid.
Here is form code (without declarations)

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyEscape
            'Returns to other form, stops sound
            j = sndPlaySound(vbNullString, SND_ASYNC)
            frmStart.Show
            frmGame.Hide
        Case vbKeySpace
            'Allows the space bar to shoot too
            ShootLaser = True
            LaserX = XDest + Ship.Width / 2 - Laser.Width / 2
            LaserY = YDest + Ship.Height - 20
    End Select
End Sub

Private Sub Form_Load()
    Show
    RockYPos = 425
    For XLoader = 1 To x
        Randomize Timer
        RandX = Int(Rnd * x + 1)
        StarXPos(XLoader) = RandX
    Next XLoader
    For YLoader = 1 To x
        StarYPos(YLoader) = YLoader
    Next YLoader
    'Play sound
    'j = sndPlaySound(App.Path + "\Needle.wav", SND_ASYNC Or SND_LOOP)
    Do
        DoEvents
        Cls
        For i = 1 To (x - 1)
            StarXPos(i) = StarXPos(i + 1)
        Next i
        For i = 1 To x
            PSet (StarXPos(i), StarYPos(i)), vbWhite
        Next i
        RandX = Int(Rnd * x + 1)
        StarXPos(x) = RandX
        'Draw Ship
        j = BitBlt(Me.hDC, XDest, YDest, 150, 112, ShipMask.hDC, 0, 0, vbSrcAnd)
        j = BitBlt(Me.hDC, XDest, YDest, 150, 112, Ship.hDC, 0, 0, vbSrcPaint)
        If ShootLaser = True Then 'If the laser has been activated then:
            j = BitBlt(Me.hDC, LaserX, LaserY, 150, 112, LaserMask.hDC, 0, 0, vbSrcAnd)
            j = BitBlt(Me.hDC, LaserX, LaserY, 150, 112, Laser.hDC, 0, 0, vbSrcPaint)
            LaserY = LaserY + 10
                If LaserY >= Me.ScaleHeight Then
                    ShootLaser = False
                    LaserY = 0
                End If
        End If
'       ASTEROID STUFF
'       =================================================
        RockYPos = RockYPos - 5
        If RockYPos + 100 <= 0 Then
            RockYPos = 425
            RockXPos = Int(Rnd * Me.ScaleWidth)
            RockNum = Int(Rnd * 4)
        End If
        j = BitBlt(Me.hDC, RockXPos, RockYPos, 150, 112, AsteroidMask(RockNum).hDC, 0, 0, vbSrcAnd)
        j = BitBlt(Me.hDC, RockXPos, RockYPos, 150, 112, Asteroid(RockNum).hDC, 0, 0, vbSrcPaint)
'       =================================================
'       IF HIT THEN DIE CODE (not even close to remotely trying to work)
'       =================================================
        If Abs(YDest + Ship.Height - RockYPos - 20) < 5 Then
           If Abs(XDest + Ship.Width / 2 - RockXPos - Asteroid(RockNum).Width / 2) < 15 Then
              MsgBox "Booooommmm !!!", vbCritical
           End If
        End If
'You cahnge asteroid pos by 5 and laser by 10, so every loop distance change by 15 - you need check abs < 15/2 = 8
        If Abs(LaserY + Laser.Height - 12 - RockYPos) < 8 And LaserY > 0 Then
' Check if laser find asteroid
           If Abs(LaserX + Laser.Width / 2 - RockXPos - Asteroid(RockNum).Width / 2) < Asteroid(RockNum).Width / 2 Then
              MsgBox "Nice shoot!!!", vbExclamation
           End If
        End If
'       (I tryed (Derek))
'       =================================================
    Loop
End Sub

Public Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    ShootLaser = True
    LaserX = XDest + Ship.Width / 2 - Laser.Width / 2
    LaserY = YDest + Ship.Height - 20
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    XDest = x - Ship.Width / 2
End Sub

Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 2651316
PS. First code hits asteroid only in centre, last one - in any part
0
 
LVL 27

Accepted Solution

by:
Ark earned 730 total points
ID: 2651406
One more - improved "Boom" staff
'Calculate distance between asteroid and ship centeres and
'let asteroid fly some more pixels until collision
        Dim iTemp As Integer
        iTemp = Abs(XDest + Ship.Width / 2 - RockXPos - Asteroid(RockNum).Width / 2)
        If Abs(YDest + Ship.Height - RockYPos - 20 - iTemp) < 5 Then
           If iTemp < Ship.Width / 2 Then
              MsgBox "Booooommmm !!!", vbCritical
           End If
        End If
0
 

Author Comment

by:shiny
ID: 2651590
Adjusted points from 729 to 730
0
 

Author Comment

by:shiny
ID: 2651591
Thank you so much, I have spent many hours working with this.

-Shiny (shiny@gamepl.com)
0
 
LVL 27

Expert Comment

by:Ark
ID: 2651695
Glad to help you!
BTW, if change 20 to 25:
If Abs(YDest + Ship.Height - RockYPos - 25 - iTemp) < 5 Then
Collision will be more closely
Cheers
           
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 56
VBA: Select SQL query based on a config Sheet v2 11 38
How to compare ms sql hashbytes results within vb6 5 54
Hide vba in gp 7 82
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now