Solved

I hate VB!! ;)

Posted on 2000-03-22
12
174 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 28

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 28

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 28

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

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
 
LVL 28

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 28

Expert Comment

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

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 28

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

828 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