65.9K
CodeProject is changing. Read more.
Home

How to build a breakout style game with animation and sound.

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.31/5 (15 votes)

Jan 10, 2004

2 min read

viewsIcon

68794

downloadIcon

1280

Summery of the project.

Sample Image - breakout_picture.png

Introduction

Welcome to another installment of, "Boy this guy really has nothing better to do". :-) My latest creation is called Tiny Breakout. I 100% GDI+ code that runs really smooth after I stumbled across some new features. I am not going to give out the source on this for a few reasons. One I might want to release this as shareware and two because there are a few sloppy spots that I need to clean up. But, I will outline some of the new stuff I used in the creation of the animation effects and also show you how I incorporated sound without resorting to DirectX.

The Blocks

I used the basic structure I have been using now in my demos for the past few days. All the different elements (ex. ball, paddle and blocks) reside in there own class structure with the basic methods of New, Draw and Move. Some of them has special attributes such as isImpact and changeDirection.. But probably most importantly they all have a Public Location as rectangle in them to let me know where the objects are on the screen.

For instance say I needed a block for the board of the game. To create the block I simply call its new function.

'
' destPoint is a point structure of where you want to place the block
' 5 is the point value of the block
' Brushes.Blue is the color to make the block
'

Blocks(thisBlock) = New cBlocks(destpoint, 5, Brushes.Blue)

As you see I have an array of blocks and each one gets created sepritly.

The Paddle

The paddle is constructed much in the same way as the blocks (call the new procedure and it builds the paddle etc.). However, unlike the blocks the paddle needs to move.This was done with the basic mouse event.

    Private Sub game_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles game.MouseMove
        Paddle.Location.X = e.X - (Paddle.Location.Width / 2)
    End Sub

As you can see I don't redisplay the paddle, that's the job of the paint event. I tried something new when working with the paint event today, I started invalidating (repainting) regions of the screen instead of the whole screen like this.

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim PaddleInvalidate As Rectangle = New Rectangle(0, Paddle.Location.Y, game.ClientSize.Width, Paddle.Location.Y + 10)

I set a rectangle equal to the entire segment where the paddle could be.

        game.Invalidate(PaddleInvalidate)
        game.Update()
    End Sub

Then I tell it I want to redraw that segment of the screen and it needs to do it now. The paint event is then called and it executes paddle.draw(e.graphics) creating a seemless animation.

The sound

Here i a copy of the class I built based on a few usenet postings.

Imports System.Runtime.InteropServices

Public Class cSound
    Private Const SND_SYNC As Int32 = &H0
    Private Const SND_ASYNC As Int32 = &H1
    Private Const SND_FILENAME As Int32 = &H20000

    ' Import the Windows PlaySound() function.
    <DllImport("winmm.dll", EntryPoint:="PlaySound", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function WinPlaySound(ByVal pszSound As String, _
                                         ByVal hmod As Int32, _
                                         ByVal fdwSound As Int32) As Boolean
    End Function

    Public Sub playSound(ByVal filename As String)
        Dim path As String = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\"))
        If IO.File.Exists(path & "\" & filename) Then
            WinPlaySound(path & "\" & filename, 0, SND_FILENAME Or SND_ASYNC)
        End If
    End Sub

End Class

when I want to play a wav file all I have to do is:

Public sound As New cSound
sound.playSound("click.wav")

The status bar at the top of the screen

The status bar is actually a border less form. I created a 300x20 pixel form, set the opacity to 65% and positioned on the top of the game board for tracking Lives, Score and Level information.

The rest

The balls class i'm going to keep under wraps for a while but I will give you a hint on the animation side. When the timer ticks I just game.invalidate(ball.lastPos) and game.invalidate(bball.newPos).