Simple 2D pinball / brick game with Visual Basic 8.0 Drawing classes

VB.NET 2D drawing graphics classes provide various options for painting on WinForms. Following is a simple 2D pinball/brick game(e.g. like DX Ball) with unlimited random brick stages using these classes:

code:
Public Class UsrIntrfc
    Public HEROS As New ListBox
    Public SCORES As New ListBox
    Dim ball As New PictureBox
    Dim paddle As New PictureBox
    Dim score As Long = 0
    Dim chances As Integer = 5
    Dim Luck As Integer = 0
    Dim Scoreboard As New PictureBox
    Dim Chance As New PictureBox
    Dim ChanceSymbol As New PictureBox
    Dim BenefitOrLose As New PictureBox
    Dim brickx1(129) As Integer
    Dim bricky1(129) As Integer
    Dim brickx2(129) As Integer
    Dim bricky2(129) As Integer
    Dim BrickExists(129) As Boolean
    ' Flags
    Dim Cont As Boolean = False
    Dim Started As Boolean = False
    Dim returned As Boolean = False
    Dim Thrown As Boolean = False
    Dim SpeedUp As Boolean = False
    Dim PowerUp As Boolean = False
    Dim Paused As Boolean = False
    Dim LCSU As Boolean = False
    Dim ScoreShowed As Boolean = False
    Private bitmap As Bitmap
    Private ballPositionX As Integer
    Private ballPositionY As Integer
    Private ballRadiusX As Integer
    Private ballRadiusY As Integer
    Private ballMoveX As Integer
    Private ballMoveY As Integer
    Private ballBitmapWidth As Integer
    Private ballBitmapHeight As Integer
    Private bitmapWidthMargin As Integer
    Private bitmapHeightMargin As Integer
    Dim mcl As New MASUM

    Protected Overridable Function DrawBall() As Integer
        ball.Location = New Point(ballPositionX - ball.Width / 2,
 ballPositionY - ball.Height / 2)
    End Function
    Protected Overridable Function DrawPadel() As Integer
        If Control.MousePosition.X >= Me.ClientSize.Width - 
paddle.Width Then
            paddle.Location = New Point(Me.ClientSize.Width
 - paddle.Width, Me.ClientSize.Height - paddle.Height)
        Else
            paddle.Location = New Point(Control.MousePosition.X,
 Me.ClientSize.Height - paddle.Height)
        End If
    End Function
    Function hidelogo() As Integer
        Dim g As Graphics = Me.CreateGraphics
        g.Clear(Me.BackColor)
        Title.Visible = False
        TEXTQUIT.Visible = False
        TEXTRESUME.Visible = False
        Animation.Visible = False
        Instructions.Visible = False
        Tips.Visible = False
        LabelExit.Visible = False
        Button1.Visible = False
        Button2.Visible = False
        Button3.Visible = False
        Button4.Visible = False
        Button5.Visible = False
        Button6.Visible = False
        Button7.Visible = False
        Button8.Visible = False
        Button9.Visible = False
        Button10.Visible = False
        Button11.Visible = False
        Button12.Visible = False
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
        Button10.Enabled = False
        Button11.Enabled = False
        Button12.Enabled = False
        Me.Select()
    End Function
    Function Pause() As Integer
        Me.Cursor = Cursors.Default
        BallSpeed.Enabled = False
        LuckTimer.Enabled = False
        Do Until Paused = False
            Application.DoEvents()
            System.Threading.Thread.Sleep(15)
        Loop
        Me.Cursor = New Cursor("Cursor.rsdb")
        BallSpeed.Enabled = True
        TEXTQUIT.Visible = False
        TEXTRESUME.Visible = False
    End Function
    Function DrawScore() As Integer
        mcl.MakeMeString(Scoreboard, CStr(score), 
System.Drawing.FontFamily.GenericSansSerif, 1, 50, 30, 0)
    End Function
    Function DrawChances() As Integer
        mcl.MakeMeString(Chance, CStr(chances),
 System.Drawing.FontFamily.GenericSansSerif, 1, 50, 30, 0)
    End Function
    Function Died() As Integer
        My.Computer.Audio.Play(My.Resources.GONE,
 AudioPlayMode.Background)
        LCSU = False
        returned = False
        Thrown = False
        SpeedUp = False
        PowerUp = False
        ball.BackgroundImage = Image.FromFile("Ball.rsdb")
        LuckTimer.Enabled = False
        BallSpeed.Enabled = False
        Dim i As Integer
        ball.Visible = False
        SpeedUp = False
        Thrown = False
        System.Threading.Thread.Sleep(1000)
        If chances = 0 Then
            GameOver()
        Else
            chances -= 1
            DrawChances()
        End If
        ball.Width = 24
        ball.Height = 24
        ballRadiusX = ball.Width / 2
        ballRadiusY = ball.Height / 2
        ballPositionX = ball.Location.X - ball.Width / 2
        ballPositionY = ball.Location.Y - ball.Height / 2
        ballMoveX = 3
        ballMoveY = -3
        BenefitOrLose.Visible = False
        For i = 0 To 129
            If CBool(BrickExists(i)) Then
                If BenefitOrLose.Location.X > brickx1(i) And 
BenefitOrLose.Location.X < brickx1(i) + brickx2(i) Or
 BenefitOrLose.Location.X + BenefitOrLose.Width > brickx1(i) And
 BenefitOrLose.Location.X + BenefitOrLose.Width < brickx1(i) + 
brickx2(i) Then
                    mcl.DrawGradientRectangleOnForm(Me, brickx1(i),
 bricky1(i), brickx2(i), bricky2(i), Me.BackColor, Me.BackColor, 90)
                    score += (brickx2(i) + bricky2(i))
                    DrawScore()
                    BrickExists(i) = False
                End If
            End If
        Next
        ball.Visible = True
        DrawChances()
        DrawScore()
        SetPadelandBall()
    End Function
    Function GameOver() As Integer
        Cont = False
        Estimate.ScoreLabel.Text = CStr(score)
        If score >= CLng(SCORES.Items.Item(SCORES.Items.Count - 1)) Then
            Estimate.OORLabel.Visible = False
            Estimate.HighScoreLabel.Visible = True
            Estimate.PlayerName.Visible = True
        End If
        Me.Hide()
        Estimate.Show()
        Estimate.Activate()
    End Function
    Function StageCompleted() As Integer
        My.Computer.Audio.Play(My.Resources.PUS, AudioPlayMode.Background)
        LCSU = False
        ball.Visible = False
        BenefitOrLose.Visible = False
        LuckTimer.Enabled = False
        BallSpeed.Enabled = False
        ball.Location = New Point(500, 2000)
        BenefitOrLose.Location = New Point(500, 2000)
        Dim mn As Graphics = Me.CreateGraphics
        mn.Clear(Me.BackColor)
        mn.Dispose()
        DrawScore()
        DrawChances()
        RandomStage()
        ballRadiusX = ball.Width / 2
        ballRadiusY = ball.Height / 2
        ballMoveX = 3
        ballMoveY = -3
        ballPositionX = ball.Location.X - ball.Width / 2
        ballPositionY = ball.Location.Y - ball.Height / 2
        ball.Visible = True

        SetPadelandBall()
    End Function
    Function AddBonus() As Integer
        If PowerUp = True Then
            My.Computer.Audio.Play(My.Resources.PUS,
 AudioPlayMode.Background)
            PowerUp = False
            ball.BackgroundImage = My.Resources.BB
            score += 500
            DrawScore()
            Application.DoEvents()
            System.Threading.Thread.Sleep(500)
        End If
        If ball.Width > 14 Then
            Do Until ball.Width <= 14
                My.Computer.Audio.Play(My.Resources.PUS,
 AudioPlayMode.Background)
                ball.Width -= 8
                ball.Height -= 8
                score += 400
                DrawScore()
                Application.DoEvents()
                System.Threading.Thread.Sleep(100)
            Loop
            System.Threading.Thread.Sleep(500)
        End If
        If paddle.Width > 50 Then
            Do Until paddle.Width <= 50
                My.Computer.Audio.Play(My.Resources.PUS,
 AudioPlayMode.Background)
                paddle.Width -= 10
                score += 100
                DrawScore()
                Application.DoEvents()
                System.Threading.Thread.Sleep(100)
            Loop
            System.Threading.Thread.Sleep(500)
        End If
        If chances > 0 Then
            Do Until chances <= 0
                My.Computer.Audio.Play(My.Resources.PUS,
 AudioPlayMode.Background)
                chances -= 1
                DrawChances()
                score += 300
                DrawScore()
                Application.DoEvents()
                System.Threading.Thread.Sleep(100)
            Loop
            System.Threading.Thread.Sleep(500)
        End If
    End Function
    Private Function ThrowBall() As Integer
        LuckTimer.Enabled = True
        BallSpeed.Enabled = True
        Dim i As Integer = 0
        Dim tbrX As Double
        Dim a(1) As Integer
        Dim AllBricksDistroyed As Integer = 0
        Do
            If Cont = True Then
                DrawBall()
                DrawPadel()
                ' Increment the ball position by the distance it has
                ' moved in both X and Y after being redrawn.
                ballPositionX += ballMoveX
                ballPositionY += ballMoveY
                'Check for the benefit/lose picturebox is visible
                If BenefitOrLose.Visible = True Then
                    BenefitOrLose.Location = New Point(
BenefitOrLose.Location.X, BenefitOrLose.Location.Y + 5)
                    For i = 0 To 129
                        If CBool(BrickExists(i)) Then
                            If (BenefitOrLose.Location.X > brickx1(i) And
 BenefitOrLose.Location.X < brickx1(i) + brickx2(i) Or
 BenefitOrLose.Location.X + BenefitOrLose.Width > brickx1(i) And
 BenefitOrLose.Location.X + BenefitOrLose.Width < brickx1(i) + 
brickx2(i)) And (BenefitOrLose.Location.Y > bricky1(i) And
 BenefitOrLose.Location.Y < bricky1(i) + bricky2(i) Or
 BenefitOrLose.Location.Y + BenefitOrLose.Height > bricky1(i) And
 BenefitOrLose.Location.Y + BenefitOrLose.Height < bricky1(i) + bricky2(i)) Then
                                BrickExists(i) = False
                                mcl.DrawGradientRectangleOnForm(Me,
 brickx1(i), bricky1(i), brickx2(i), bricky2(i), Me.BackColor, Me.BackColor, 90)
                                score += (brickx2(i) + bricky2(i))
                                DrawScore()
                                My.Computer.Audio.Play(
My.Resources.CLS, AudioPlayMode.Background)
                            End If
                        End If
                    Next
                    'Check if the padel got it... 
                    If BenefitOrLose.Location.Y >= Me.ClientSize.Height - 
paddle.Height - BenefitOrLose.Height Then
                        If BenefitOrLose.Location.X > paddle.Location.X And
 BenefitOrLose.Location.X < paddle.Location.X + paddle.Width Or
 BenefitOrLose.Location.X + BenefitOrLose.Width > paddle.Location.X And
 BenefitOrLose.Location.X + BenefitOrLose.Width < paddle.Location.X +
 paddle.Width Then
                            BenefitOrLose.Visible = False
                            If Luck = 1 Then
                                score += 200
                                DrawScore()
                                chances += 1
                                DrawChances()
                                My.Computer.Audio.Play(My.Resources.GPPS,
 AudioPlayMode.Background)
                            ElseIf Luck = 2 Then
                                score += 200
                                DrawScore()
                                Died()
                            ElseIf Luck = 3 Then
                                score += 200
                                DrawScore()
                                My.Computer.Audio.Play(My.Resources.GBS,
 AudioPlayMode.Background)

                                If paddle.Width < 600 Then
                                    paddle.Width += 20
                                End If
                            ElseIf Luck = 4 Then
                                score += 200
                                DrawScore()
                                My.Computer.Audio.Play(My.Resources.GSS,
 AudioPlayMode.Background)
                                If paddle.Width > 50 Then
                                    paddle.Width -= 20
                                End If
                            ElseIf Luck = 5 Then
                                score += 200
                                DrawScore()
                                My.Computer.Audio.Play(My.Resources.GPPS,
 AudioPlayMode.Background)
                                score += 1000
                                DrawScore()
                            ElseIf Luck = 6 Then
                                score += 200
                                DrawScore()
                                My.Computer.Audio.Play(My.Resources.GPS,
 AudioPlayMode.Background)
                                ball.BackgroundImage = My.Resources.PU
                                PowerUp = True
                            ElseIf Luck = 7 Then
                                score += 200
                                DrawScore()
                                PowerUp = False
                                ball.Width = 24
                                ball.Height = 24
                                ballRadiusX = ball.Width / 2
                                ballRadiusY = ball.Height / 2
                                ballPositionX = ball.Location.X - ball.Width / 2
                                ballPositionY = ball.Location.Y - ball.Height / 2
                                ball.BackgroundImage = Image.FromFile("Ball.rsdb")
                                paddle.Width = 150
                                My.Computer.Audio.Play(My.Resources.NMS,
 AudioPlayMode.Background)
                            ElseIf Luck = 8 Then
                                score += 200
                                DrawScore()
                                returned = False
                                Thrown = False
                                SpeedUp = False
                                StageCompleted()
                            ElseIf Luck = 9 Then
                                score += 200
                                DrawScore()
                                My.Computer.Audio.Play(My.Resources.DAMN,
 AudioPlayMode.Background)
                                LCSU = True
                                If ballMoveX > 0 Then
                                    ballMoveX += 5
                                ElseIf ballMoveX < 0 Then
                                    ballMoveX -= 5
                                End If
                                If ballMoveY > 0 Then
                                    ballMoveY += 5
                                ElseIf ballMoveY < 0 Then
                                    ballMoveY -= 5
                                End If
                            ElseIf Luck = 11 Then
                                score += 200
                                DrawScore()
                                If ball.Width < 160 Then
                                    ball.Width += 16
                                    ball.Height += 16
                                    ballRadiusX = ball.Width / 2
                                    ballRadiusY = ball.Height / 2
                                    ballPositionX = ball.Location.X -
 ball.Width / 2
                                    ballPositionY = ball.Location.Y - 
ball.Height / 2
                                End If
                                My.Computer.Audio.Play(My.Resources.GPS,
 AudioPlayMode.Background)
                            ElseIf Luck = 12 Then
                                score += 200
                                DrawScore()
                                If ball.Width > 14 Then
                                    ball.Width -= 8
                                    ball.Height -= 8
                                    ballRadiusX = ball.Width / 2
                                    ballRadiusY = ball.Height / 2
                                    ballPositionX = ball.Location.X -
 ball.Width / 2
                                    ballPositionY = ball.Location.Y - 
ball.Height / 2
                                End If
                                My.Computer.Audio.Play(My.Resources.GSS,
 AudioPlayMode.Background)
                            ElseIf Luck = 10 Then
                                score += 200
                                DrawScore()
                                My.Computer.Audio.Play(My.Resources.GPSBS,
 AudioPlayMode.Background)
                                If LCSU = True Then
                                    If ballMoveX > 0 Then
                                        ballMoveX -= 5
                                    ElseIf ballMoveX < 0 Then
                                        ballMoveX += 5
                                    End If
                                    If ballMoveY > 0 Then
                                        ballMoveY -= 5
                                    ElseIf ballMoveY < 0 Then
                                        ballMoveY += 5
                                    End If
                                Else
                                    If ballMoveX > 0 Then
                                        ballMoveX -= 1
                                    ElseIf ballMoveX < 0 Then
                                        ballMoveX += 1
                                    End If
                                    If ballMoveY > 0 Then
                                        ballMoveY -= 1
                                    ElseIf ballMoveY < 0 Then
                                        ballMoveY += 1
                                    End If
                                End If
                                LCSU = False
                            End If
                            LuckTimer.Enabled = True
                        End If
                    End If
                    'Escaped
                    If BenefitOrLose.Location.Y > Me.ClientSize.Height Then
                        BenefitOrLose.Visible = False
                        LuckTimer.Enabled = True
                    End If
                End If
                'Check that the padel got the ball or else die !
                If ballPositionY + ballRadiusY >= paddle.Location.Y Then
                    If ballPositionX + ballRadiusX >= paddle.Location.X And
 ballPositionX - ballRadiusX <= paddle.Location.X + paddle.Width Then
                    ... ... ... ... ...
                    ... ... ... ... ...
                    ... ... ... ... ...
Download full project code: random-bricks-game-source-code.zip

No comments:

Post a Comment