Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Atari: BreakOut Game] Ball Exits Playing Field, Does Not End Game!

block buster visual basic teens harbour ball game wont end miss

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts

Posted 28 May 2014 - 06:52 PM

I have all my forms and icons done properly. Here is my code, please tell me why the game does not properly use UpdateLives() when Ball misses the paddle and goes outside the playing field.. The game just keeps running with no ball and no way to keep play. Or if the ball gets stuck under the paddle it bounces in-between the paddle and program boarder until the paddle moves out of the way. When the paddle moves out of the way, the ball breaks a block, and the ball comes back down. If the paddle is not in the way and the ball doesn't get stuck under the paddle, the ball goes outside the play field and the game just sits without a ball. I do not understand. I have no errors on compile, every other aspect of the game works except for when the ball exits the playing field :(
 
 
Public Class Form1
    Dim intSpeedX As Integer = 2
    Dim intSpeedY As Integer = -2
    Dim intScore As Integer
    Dim intLives As Integer = 3
    Dim intAllGone As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 20
        Timer1.Enabled = True
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        AllGone = 0
        CheckCollisions()
        If AllGone = 1 Then
            Timer1.Enabled = False
            MsgBox("You finished the game!", , "CONGRATULATIONS!")
        End If
        BallX += SpeedX
        If BallX < 3 Or BallX + Ball.Width > Me.Width - 5 Then
            SpeedX = -SpeedX
        End If
 
        BallY += SpeedY
        If BallY < 3 Then
            SpeedY = -SpeedY
        End If
        If BallY = Ball.Height > Me.Height - 5 Then
            Timer1.Enabled = False
            UpdateLives()
            BallX = 232
            BallY = 376
            SpeedX = 2
            SpeedY = -2
            If Lives < 1 Then
                MsgBox("You have lost the game.", , "OH NO!")
            Else
                MsgBox("You Missed!", , "OH NO!")
                Timer1.Enabled = True
            End If
        End If
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Paddle.Left = e.X - Paddle.Width \ 2
    End Sub
    Public Sub CheckCollisions()
        CheckCollision(Paddle, False)
        CheckCollision(Block1, True)
        CheckCollision(Block2, True)
        CheckCollision(Block3, True)
        CheckCollision(Block4, True)
        CheckCollision(Block5, True)
        CheckCollision(Block6, True)
        CheckCollision(Block7, True)
        CheckCollision(Block8, True)
        CheckCollision(Block9, True)
        CheckCollision(Block10, True)
        CheckCollision(Block11, True)
        CheckCollision(Block12, True)
        CheckCollision(Block13, True)
        CheckCollision(Block14, True)
        CheckCollision(Block15, True)
        CheckCollision(Block16, True)
        CheckCollision(Block17, True)
        CheckCollision(Block18, True)
        CheckCollision(Block19, True)
        CheckCollision(Block20, True)
    End Sub
    Public Sub CheckCollision(ByRef src As PictureBox, ByVal Hide As Boolean)
        If src.Visible = True Then
            If BallX > src.Location.X And BallX < src.Location.X + src.Size.Width And Ball.Location.Y > src.Location.Y And Ball.Location.Y < src.Location.Y + src.Size.Height Then
                SpeedY = -SpeedY
                UpdateScore()
                If Hide Then
                    src.Visible = False
                End If
            End If
            AllGone += 1
        End If
    End Sub
    REM declare the overloaded version of CheckCollision
    Public Sub CheckCollison(ByVal src As PictureBox)
        REM call the original version
        CheckCollision(src, True)
    End Sub
    Public Sub UpdateScore()
        Score += 10
        Label2.Text = "SCORE: " & Score
    End Sub
    Public Sub UpdateLives()
        Lives -= 1
        Label1.Text = "LIVES: " & Lives
    End Sub
    Public Property BallX() As Integer
        Get
            Return Ball.Left
        End Get
        Set(ByVal Value As Integer)
            Ball.Left = Value
        End Set
    End Property
    Public Property BallY() As Integer
        Get
            Return Ball.Top
        End Get
        Set(ByVal Value As Integer)
            Ball.Top = Value
        End Set
    End Property
    Public Property Lives() As Integer
        Get
            Return intLives
        End Get
        Set(ByVal Value As Integer)
            intLives = Value
        End Set
    End Property
    Public Property SpeedX() As Integer
        Get
            Return intSpeedX
        End Get
        Set(ByVal Value As Integer)
            intSpeedX = Value
        End Set
    End Property
    Public Property SpeedY() As Integer
        Get
            Return intSpeedY
        End Get
        Set(ByVal Value As Integer)
            intSpeedY = Value
        End Set
    End Property
    Public Property Score() As Integer
        Get
            Return intScore
        End Get
        Set(ByVal Value As Integer)
            intScore = Value
        End Set
    End Property
    Public Property AllGone() As Integer
        Get
            Return intAllGone
        End Get
        Set(ByVal Value As Integer)
            intAllGone = Value
        End Set
    End Property
End Class
 
If you need any more information, let me know! I am coding this in Visual Basic 2010. I know  the book is for VB 2008 but I am using what I have.


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 29 May 2014 - 07:55 AM

Wow!

 

I tried to follow the code. What I would do if I were you, is to debug it, stop it right when the error comes and see the var values.

 

you could even have then printed on the game screen as many arcades used to do for debugging



#3 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts

Posted 29 May 2014 - 08:36 AM

Oh, good idea. That totally slipped my mind. I have been on and off VB for years. This project is from a book, but I am modifying and customizing the interface and code in my own ways to make it work smoother, look better, etc.

Thank you for your reply. I will check that out!



#4 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts

Posted 29 May 2014 - 10:40 AM

I am having some issues with debugging. I am using Visual Basic 2010 Express because I had already downloaded it. But, I got the Retail 2010 in a box that I am about to load up. I figure maybe if I use the full version of the compiler, I may get better results.

I will update this soon with my findings.

Thanks,
Donovan


Edited by Donovan, 29 May 2014 - 02:25 PM.


#5 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts

Posted 29 May 2014 - 02:25 PM

AHA! I found it!  ;D   :o
 
If BallY = Ball.Height > Me.Height - 5 Then

Line 28/150 of the total project code. Never thought I would let a mistype like that happen.   :irritated: 

The line should read...:

If BallY + Ball.Height > Me.Height - 5 Then
 
Here is my new code. Still a few problems here! My main issue now, is that ball has a possibility of getting stuck under paddle and bouncing off the bottom control.    However, the ball can leave the screen if the paddle is nowhere near the ball. You would have to slide the paddle just right, over the ball, and then the game would glitch allowing the ball to bouncing back and forth between the underside of the battle and the bottom of the control form of Form1.
 
Public Class Form1
    Dim intSpeedX As Integer = 5
    Dim intSpeedY As Integer = -5
    Dim intScore As Integer
    Dim intLives As Integer = 3
    Dim intAllGone As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 20
        Timer1.Enabled = True
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        AllGone = 0
        CheckCollisions()
        If AllGone = 1 Then
            Timer1.Enabled = False
            MsgBox("You finished the game!", , "CONGRATULATIONS!")
        End If
        BallX += SpeedX
        If BallX < 3 Or BallX + Ball.Width > Me.Width - 5 Then
            SpeedX = -SpeedX
        End If
        BallY += SpeedY
        If BallY < 3 Then
            SpeedY = -SpeedY
        End If
        If BallY + Ball.Height > Me.Height - 5 Then
            Timer1.Enabled = False
            UpdateLives()
            BallX = 206
            BallY = 498
            SpeedX = 2
            SpeedY = -2
            If Lives < 1 Then
                MsgBox("You have lost the game.", , "OH NO!")
            Else
                MsgBox("You Missed!", , "OH NO!")
                Timer1.Enabled = True
            End If
        End If
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Paddle.Left = e.X - Paddle.Width \ 2
    End Sub
    Public Sub CheckCollisions()
        CheckCollision(Paddle, False)
        CheckCollision(Block1, True)
        CheckCollision(Block2, True)
        CheckCollision(Block3, True)
        CheckCollision(Block4, True)
        CheckCollision(Block5, True)
        CheckCollision(Block6, True)
        CheckCollision(Block7, True)
        CheckCollision(Block8, True)
        CheckCollision(Block9, True)
        CheckCollision(Block10, True)
        CheckCollision(Block11, True)
        CheckCollision(Block12, True)
        CheckCollision(Block13, True)
        CheckCollision(Block14, True)
        CheckCollision(Block15, True)
        CheckCollision(Block16, True)
        CheckCollision(Block17, True)
        CheckCollision(Block18, True)
        CheckCollision(Block19, True)
        CheckCollision(Block20, True)
    End Sub
    Public Sub CheckCollision(ByRef src As PictureBox, ByVal Hide As Boolean)
        If src.Visible = True Then
            If BallX > src.Location.X And BallX < src.Location.X + src.Size.Width And Ball.Location.Y > src.Location.Y And Ball.Location.Y < src.Location.Y + src.Size.Height Then
                SpeedY = -SpeedY
                UpdateScore()
                If Hide Then
                    src.Visible = False
                End If
            End If
            AllGone += 1
        End If
    End Sub
    REM declare the overloaded version of CheckCollision
    Public Sub CheckCollison(ByVal src As PictureBox)
        REM call the original version
        CheckCollision(src, True)
    End Sub
    Public Sub UpdateScore()
        Score += 10
        Label2.Text = "SCORE: " & Score
    End Sub
    Public Sub UpdateLives()
        Lives -= 1
        Label1.Text = "LIVES: " & Lives
    End Sub
    Public Property BallX() As Integer
        Get
            Return Ball.Left
        End Get
        Set(ByVal Value As Integer)
            Ball.Left = Value
        End Set
    End Property
    Public Property BallY() As Integer
        Get
            Return Ball.Top
        End Get
        Set(ByVal Value As Integer)
            Ball.Top = Value
        End Set
    End Property
    Public Property Lives() As Integer
        Get
            Return intLives
        End Get
        Set(ByVal Value As Integer)
            intLives = Value
        End Set
    End Property
    Public Property SpeedX() As Integer
        Get
            Return intSpeedX
        End Get
        Set(ByVal Value As Integer)
            intSpeedX = Value
        End Set
    End Property
    Public Property SpeedY() As Integer
        Get
            Return intSpeedY
        End Get
        Set(ByVal Value As Integer)
            intSpeedY = Value
        End Set
    End Property
    Public Property Score() As Integer
        Get
            Return intScore
        End Get
        Set(ByVal Value As Integer)
            intScore = Value
        End Set
    End Property
    Public Property AllGone() As Integer
        Get
            Return intAllGone
        End Get
        Set(ByVal Value As Integer)
            intAllGone = Value
        End Set
    End Property
End Class
 
 
CONCLUSION
 
Pros> Life Action Play! Ball moves smoothly, hits blocks and demolishes correctly, banks in-between blocks correctly. Paddle moves back and forth smoothly. The program runs through sequence pretty smoothly so far with mention of minor bugs in the Cons section.
 
Cons> 1) Ball gets stuck under paddle. 2) Paddle does not stop within Form1 dimensions properly (Paddle runs off screen half way.) 3) You get points by banking off of the paddle (I would prefer only blocks give points). 4) I can't properly bank the ball. Maybe it is just me?
 
 
Some of these could be a matter of opinion. I know it worked the way TwoBits originally planned. I am just spending additional time to enhance my knowledge on each bar of code. Thank you for your views and responses!
 
 
!UPDATE / BUG FIXES!
Public Class Form1
    Dim intSpeedX As Integer = 5
    Dim intSpeedY As Integer = -5
    Dim intScore As Integer
    Dim intLives As Integer = 3
    Dim intAllGone As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 20
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        AllGone = 0
        CheckCollisions()
        If AllGone = 1 Then
            Timer1.Enabled = False
            MsgBox("You finished the game!", , "CONGRATULATIONS!")
        End If
        BallX += SpeedX
        If BallX < 3 Or BallX + Ball.Width > Me.Width - 5 Then
            SpeedX = -SpeedX
        End If
        BallY += SpeedY
        If BallY < 3 Then
            SpeedY = -SpeedY
        End If
        If BallY + Ball.Height > Me.Height - 5 Then
            Timer1.Enabled = False
            UpdateLives()
            BallX = 206
            BallY = 498
            SpeedX = 5
            SpeedY = -5
            MsgBox("You Missed!", , "OH NO!")
            Timer1.Enabled = True
            If Lives < 1 Then
                MsgBox("You have lost the game.", , "OH NO!")
                End
            End If
        End If
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Paddle.Left = e.X - Paddle.Width \ 2
    End Sub
    Public Sub CheckCollisions()
        CheckCollision(Paddle, False)
        CheckCollision(Block1, True)
        CheckCollision(Block2, True)
        CheckCollision(Block3, True)
        CheckCollision(Block4, True)
        CheckCollision(Block5, True)
        CheckCollision(Block6, True)
        CheckCollision(Block7, True)
        CheckCollision(Block8, True)
        CheckCollision(Block9, True)
        CheckCollision(Block10, True)
        CheckCollision(Block11, True)
        CheckCollision(Block12, True)
        CheckCollision(Block13, True)
        CheckCollision(Block14, True)
        CheckCollision(Block15, True)
        CheckCollision(Block16, True)
        CheckCollision(Block17, True)
        CheckCollision(Block18, True)
        CheckCollision(Block19, True)
        CheckCollision(Block20, True)
    End Sub
    Public Sub CheckCollision(ByRef src As PictureBox, ByVal Hide As Boolean)
        If src.Visible = True Then
            If BallX > src.Location.X And BallX < src.Location.X + src.Size.Width And Ball.Location.Y > src.Location.Y And Ball.Location.Y < src.Location.Y + src.Size.Height Then
                SpeedY = -SpeedY
                If src.Name = "Paddle" Then

                Else
                    UpdateScore()
                End If
                If Hide Then
                    src.Visible = False
                End If
            End If
            AllGone += 1
        End If
    End Sub
    REM declare the overloaded version of CheckCollision
    Public Sub CheckCollison(ByVal src As PictureBox)
        REM call the original version
        CheckCollision(src, True)
    End Sub
    Public Sub UpdateScore()
        Score += 10
        Label2.Text = "SCORE: " & Score
    End Sub
    Public Sub UpdateLives()
        Lives -= 1
        Label1.Text = "LIVES: " & Lives
    End Sub
    Public Property BallX() As Integer
        Get
            Return Ball.Left
        End Get
        Set(ByVal Value As Integer)
            Ball.Left = Value
        End Set
    End Property
    Public Property BallY() As Integer
        Get
            Return Ball.Top
        End Get
        Set(ByVal Value As Integer)
            Ball.Top = Value
        End Set
    End Property
    Public Property Lives() As Integer
        Get
            Return intLives
        End Get
        Set(ByVal Value As Integer)
            intLives = Value
        End Set
    End Property
    Public Property SpeedX() As Integer
        Get
            Return intSpeedX
        End Get
        Set(ByVal Value As Integer)
            intSpeedX = Value
        End Set
    End Property
    Public Property SpeedY() As Integer
        Get
            Return intSpeedY
        End Get
        Set(ByVal Value As Integer)
            intSpeedY = Value
        End Set
    End Property
    Public Property Score() As Integer
        Get
            Return intScore
        End Get
        Set(ByVal Value As Integer)
            intScore = Value
        End Set
    End Property
    Public Property AllGone() As Integer
        Get
            Return intAllGone
        End Get
        Set(ByVal Value As Integer)
            intAllGone = Value
        End Set
    End Property
    Private Sub Start(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MouseEnter, MyBase.MouseLeave
        Paddle.Location = Label3.Location
    End Sub
End Class

 

 

.::FIXES::.

  1. Ball hitting paddle no longer rewards you with 10 score points.
  2. The paddle will now stay within the boarders of the container when the mouse exits the control form.
  3. When Ball passes the paddle and exits the playfield, "Oh No! You Missed!" pops up, and UpdateLives is initiated properly.
  4. Ball properly banks off of paddle.
  5. Ball colliding under paddle no longer imbalances the game.

‚Äč

.::BUGS::.

  1. Part of the paddle still moves outside of the playing field. The ball does not collide with the paddle perfectly and accurately. Part of the ball either goes into the paddle and banks off the center of the image, instead of colliding at the edges and rickashaying off.  (This may only be on my end, due to my custom images, sizes, etc)
  2. STILL BUG TESTING

Edited by Donovan, 30 May 2014 - 03:50 PM.


#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 31 May 2014 - 09:39 AM

Good job!

 

I know how bad can a simple typo like that can do to our code. And about that, maybe you could make constants of these hard-wired values ( 3 and 5 ) you are using, that would help you spot the code better next time.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download