I am Making Breakout, trouble with the brick collision, need array for high score


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: I am Making Breakout, trouble with the brick collision, need array for high score

  1. #1
    Join Date
    May 2005
    Posts
    6

    Exclamation I am Making Breakout, trouble with the brick collision, need array for high score

    Hello

    I have a project that I am doing on bricks. I have most everything done but I am running into the same problems over and over. the first one being my brick collision, this is the code I use to get rid of ONE brick :

    If shpBall.Top <= (shpBrick(1).Top + shpBrick(1).Height) And _
    shpBall.Left <= (shpBrick(1).Left + shpBrick(1).Width) And _
    shpBall.Left <= (shpBrick(1).Left) Then
    shpBrick(1).Visible = False
    End If

    you have to hit it right on, or it won't dissapear, can this be done more efficiently? another problem I'm haveing is how to get the score to work properly when I hit a brick, does anyone have any idea on how I can set up a scoring system? and third I have been playing with the Highscore table and I can't think of how to make all the arrays work so that I can have the top 7 scores and have them saved on to the table so it stays there until replaced by a higher score.

    please, anything will help.

  2. #2
    Join Date
    Nov 2004
    Location
    Huddinge, Sweden
    Posts
    283
    Quote Originally Posted by TwoBit
    Hello

    I have a project that I am doing on bricks. I have most everything done but I am running into the same problems over and over. the first one being my brick collision, this is the code I use to get rid of ONE brick :

    If shpBall.Top <= (shpBrick(1).Top + shpBrick(1).Height) And _
    shpBall.Left <= (shpBrick(1).Left + shpBrick(1).Width) And _
    shpBall.Left <= (shpBrick(1).Left) Then
    shpBrick(1).Visible = False
    End If

    you have to hit it right on, or it won't dissapear, can this be done more efficiently?
    Why, are you having performance problems, or do you have one test per brick?
    If it's the second issue, just put the code in a loop:
    Code:
    For brickNumber = LBount(shpBrick) To UBound(shpBrick)
        If shpBall.Top <= (shpBrick(brickNumber).Top + shpBrick(brickNumber).Height) And _
           shpBall.Left <= (shpBrick(brickNumber).Left + shpBrick(brickNumber).Width) And _
           shpBall.Left <= (shpBrick(brickNumber).Left) Then
           shpBrick(brickNumber).Visible = False
        End If
    Next brickNumber
    another problem I'm haveing is how to get the score to work properly when I hit a brick, does anyone have any idea on how I can set up a scoring system? and third I have been playing with the Highscore table and I can't think of how to make all the arrays work so that I can have the top 7 scores and have them saved on to the table so it stays there until replaced by a higher score.

    please, anything will help.
    You need ONE array, and a User Defined Type:
    Code:
    Option Explicit
    
    Private Type HiScoreEntry
        Name As String
        Score As Long
    End Type
    
    Private m_Hiscores(1 To 7) As HiScoreEntry
    
    Private Sub AddToHiScoreList(ByVal Name As String, ByVal Score As Long)
    
        Dim index As Integer
        
        If Score > m_Hiscores(7).Score Then
            ' It belongs on the list, so the lowest entry will leave the list
            index = 6
            Do While Score > m_Hiscores(index).Score
                'Bump the next lowest down one spot
                m_Hiscores(index + 1) = m_Hiscores(index)
                index = index - 1
                If index < 1 Then ' It's a high-score!
                    Exit Do
                End If
            Loop
            m_Hiscores(index + 1).Name = Name
            m_Hiscores(index + 1).Score = Score
        End If
    
                
    End Sub
    
    Private Sub cmdAdd_Click()
    
        AddToHiScoreList txtName.Text, CLng(txtScore.Text)
        FillHighScoreList
        
    End Sub
    
    Private Sub FillHighScoreList()
    
        Dim i As Integer
        
        lstHighScores.Clear
        
        For i = 1 To 7
            If m_Hiscores(i).Score > 0 Then
                lstHighScores.AddItem m_Hiscores(i).Name + vbTab + CStr(m_Hiscores(i).Score)
            End If
        Next i
    End Sub
    Rune
    If you hit a brick wall, you didn't jump high enough!

  3. #3
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    you hit the wall....
    your collision test is wrong. Lines intersection is one of the first things to be tought in CS101, and altough easy can be tricky. Take for example your case. The ball moves from position A to position B. Now, both A and position B can be outside a brick, that there was indeed a collision is any point between A and B intersects any point of the brick. And because the ball is not a point, this line must be "thick". You can make the problem easier creating two lines between A and B, as far as the ball diameter, and check both lines. And check the collision only with the bottom line of the brick. So this is the trick. Create the two lines between A and B (or, if have a point as the ball, one line is fine). Compute the X point of the A-B line(s) at the coordinate of the bottom part of the brick. If X is between the left and the right of the brick, you got a hit
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  4. #4
    Join Date
    May 2005
    Posts
    6
    I don't really understand that though( the A and B stuff ) do you think you could simplify or elaborate on the matter, I'm only a Freshman in High School. do you have any examples? that would be a better way for me to understand.
    Thank you

  5. #5
    Join Date
    Nov 2004
    Location
    Huddinge, Sweden
    Posts
    283
    I think what he's trying to say is that if the ball moves so quickly that before movement it is on one side of the brick, and after movement it has moved through the brick but is still outside, your test won't catch that. It's a non-issue if movement is slow enough. However, there's also the issue of the actual ball being round, whereas your test works with a rectangular shape. If I were you, I'd leave that 'til later...

    Rune
    If you hit a brick wall, you didn't jump high enough!

  6. #6
    Join Date
    May 2005
    Posts
    6
    this is like the last thing I have to do now. is just the bircks and the score when you hit a brick.

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