Solved

I hate VB!! ;)

Posted on 2000-03-22
12
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month6 days, 7 hours left to enroll

636 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