No one knows the answer or wot?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: No one knows the answer or wot?

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    No one knows the answer or wot?

    [Originally posted by JoE]

    I am working on a game in which there is a user controlled space ship coming from the left side of the screen, and enemy ships are comin from the righ side, now when the user fires a missile the enemy ships are supposed to blow up, i am doing this using a long if-then construct, which is in a separte subroutine that is called by the timer responsible for moving the missile, the "if" statements check if the top of the ship is the same as that of the enemy ship or there is a margin of about "1500" it also checks if the enemy ship is inside the screen, then there is a counter that is responsible for makin the enemyships blow up only once, my code looks some how like this...


    If ((pic.Top = enemyship.Top) Or ((pic.Top > enemyship.Top) And (pic.Top < (enemyship.Top + 1900))) Or ((pic.Top < enemyship.Top) And (pic.Top > (enemyship.Top - 1200)))) And (enemyship.Left < Screen.Width - 500) Then
    If (((bullet.Left + bullet.Width) - enemyship.Left > 600) And ((bullet.Left + bullet.Width) - enemyship.Left < enemyship.Width)) And (exploded <> 1) Then
    ˙ enemyship.Visible = False
    ˙ enemyship.Enabled = False
    ˙ bullet.Visible = False
    ˙ explosion.Top = enemyship.Top
    ˙ explosion.Left = enemyship.Left
    ˙ explosion.Visible = True
    ˙ exp1.Play
    ˙ Timer4.Enabled = True
    ˙ Timer2.Enabled = False
    ˙ exploded = 1
    End If


    where pic is the name of the user controlled ship, enemy ship is the name of one of the enemy ships,
    exploded is the counter used to make the ships explode only one time, and explosion is a gif control, exp is a sound control, and timer 4 is used to make the explosion gif disappear after sometime, timer 2 is the timer that makes the missile move and checks if the missile is out of the screen then it terminates, else it leads the exeution to this timer.

    i know this coding of mine seems silly, and there must be a much better way of doin all this..

    so the problems i face with this code are..

    1 )some ships dont blow up even though the code is perfecly allright, and when i take them up in the if.elseif herichay they work just fine.

    example

    if....
    elseif
    elseif
    elseif
    elseif
    elseif 'the code for the respective enemy ship that makes it blow
    end if
    'this does not work

    if 'the same code used earlier
    elseif
    elseif
    elseif
    elseif
    end if
    'this code works fine

    2)making such a construct for each enemy ship can be a tedius task.

    and how can i make a moving background at the back?

    if anyone can provide me help of any sort, i will be utterly gratful.

    thanks in advance

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:No one knows the answer or wot?

    [Originally posted by Greg DeBacker]

    Maybe I can point you in the direction of one possible way.

    Windows has a huge set of built in functions called the API. One of these functions is called IntersectRect, and there is a Type Rect that goes with it. They are listed below.

    'Put these in a Module
    Public Type Rect
    ˙ ˙ ˙ ˙ Left As Long
    ˙ ˙ ˙ ˙ Top As Long
    ˙ ˙ ˙ ˙ right As Long
    ˙ ˙ ˙ ˙ bottom As Long
    End Type

    Public Declare Function IntersectRect Lib "user32" (lpDestRect As Rect, lpSrc1Rect As Rect, lpSrc2Rect As Rect) As Long


    What you would do is define your ships and missiles as rectangles. Say you have two ships and five missiles.

    'These need to be in a module
    Public Ship(1 To 2) As Rect
    Public Missile(1 To 5) As Rect

    'Define a ship as a rectangle
    'Every time you move a ship or missile you will need to update the appropriate Rect
    Ship(1).Left˙ = picShip(1).Left
    Ship(1).Top = picShip(1).Top
    Ship(1).right = picShip(1).Left + picShip(1).Width
    Ship(1).bottom = picShip(1).Top + picShip(1).Height

    Do your missiles in the same fashion.


    The IntersectRect function will tell you if two Rects (a ship and a missile, for instance) are occupying some or all of the same space on the screen. It will give you a new Rect which is the rectangle defined by the two overlapping areas of the first two rectangles. That sounds confusing, I know. And actually you don't have to worry about the New Rect. All you need to know is if two rectangles are occupying some or all of the same space. If they are then a ship hit a missile or another ship.

    Because the Ships and Missiles are arrays of Rect you can check this in a loop without having to use a long If...ElseIf...End If block

    'We won't use NewRect but the function expects it
    Dim NewRect As Rect
    Dim k As Integer
    Dim i As Integer

    'If IntersectRect returns anything greater than zero then we've hit something
    'Check the ships against the missiles
    For k = 1 To 2
    ˙ ˙ For i = 1 To 5
    ˙ ˙ ˙ ˙ If IntersectRect (NewRect , Ship(k) , Missile(i)) Then
    ˙ ˙ ˙ ˙ ˙ ˙ 'If we get here then Ship(k) has collided with Missile(i)
    ˙ ˙ ˙ ˙ ˙ ˙ 'Do you explosion and exit
    ˙ ˙ ˙ ˙ ˙ ˙ Exit For
    ˙ ˙ ˙ ˙ End If
    ˙ ˙ Next
    Next

    'Check if the two ships hit each other
    If IntersectRect (NewRect , Ship(1) , Ship(2)) Then
    'If we get here then Ship(1) has collided with Ship(2)
    End If


    Finally, from looking at your code it looks like you have the ScaleMode of your Form and/or PictureBox set to Twips. Type Rect and IntersectRect are expecting pixels. You should either set all ScaleModes to 3 - vbPixels or divide all X and Y coordinates by Screen.TwipsPerPixelX and Screen.TwipsPerPixelY. I would suggest using the 3 - vbPixels ScaleMode. If you do change to vbPixels it will make all of your other Left and Top numbers too high. You will need to play around with things to get it the way it was before you change the ScaleMode. A good start is to devide all of the numbers by 15.

    enemyship.Top + 1900

    Becomes

    enemyship.Top + 125

    Greg


  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:No one knows the answer or wot?

    [Originally posted by Makaveli]

    why dont u just do this
    If missile.Posistion = ship.position Then
    imgship.Visible = False
    imgexplosion.Visible = True
    End If
    Hope this structure makes sense to you.

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:No one knows the answer or wot?

    [Originally posted by Greg DeBacker]

    I'm not familiar with the .Position property. Is it part of some custom control?

    Greg

  5. #5
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:No one knows the answer or wot?

    [Originally posted by Jon]

    Umm I have a statement to state There is no .position

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center