Welcome to the Free PDF Ebooks Download.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Member Login:

Video Training Online Video Training, video training software, video editing training, total training video, microsoft training video, corporate training videos, ...

Download FREE EBOOK DOWNLAOD TOOLBAR

toolbar powered by free-ebook-download.net

Reply
  #1 (permalink)  
Old 04-17-2009, 12:17 PM
--:: VIP::--
 
Join Date: Sep 2007
Posts: 2,395
Points: 33,883, Level: 26
Points: 33,883, Level: 26 Points: 33,883, Level: 26 Points: 33,883, Level: 26
Level up: 56%, 1,167 Points needed
Level up: 56% Level up: 56% Level up: 56%
Activity: 99%
Activity: 99% Activity: 99% Activity: 99%
Default Tutorial - Brick Breaker Game

In this tutorial I'll show you how to create your very own Brick Breaker game in Visual Basic .NET covering new topics like timers and such. Project file is available as always. Read on for the full tutorial. First, as always start a new document in VB.NET - name it Brick Breaker for easy reference. Click on the form, change the Name property on the right. Now, edit the following to the corresponding change.
  • AutoScaleMode - Font
  • BackColour - Sky Blue
  • DoubleBuffered - True
  • FormBorderStyle - FixedDialog
  • StartPosition - CenterScreen
  • Text - Brick Breaker
Add timer to the form - rename it to tmrGame. Now that's done, double click anywhere in the form and punch in the following code just below Public Class gameMain.
Code:
#Region "Variables"      Private Const brickWidth As Integer = 75      Private Const brickHeight As Integer = 23      Private Const brickRows As Integer = 6 - 1      Private Const brickColumns As Integer = 6 - 1      Private brickArray(brickRows, brickColumns) As Rectangle      Private isBrickEnabled(brickRows, brickColumns) As Boolean      Private gamePaddle As Rectangle = New Rectangle(300, 434, 72, 10)      Private gameBall As Rectangle = New Rectangle(gamePaddle.X + 72 / 2 _     - (16 / 2), 432 - 16, 16, 16)      Private isBallGlued As Boolean = True      Dim speed As Single = 5      Dim xVel As Single = Math.Cos(speed) * speed      Dim yVel As Single = Math.Sin(speed) * speed  #End Region
Your code when the above is typed should look like this. Now, below that type the following code.
Code:
#Region "Load Game"      Private Sub gameMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load          loadBricks()          Windows.Forms.Cursor.Hide()      End Sub  #End Region
Should end up looking like this along with the previous code minimized. Now, below that comes even more code.
Code:
#Region "Paint Event"     Private Sub gameMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint         ' Loop through all enabled bricks and display them.         For row As Integer = 0 To brickRows             For column As Integer = 0 To brickColumns                 If isBrickEnabled(row, column) Then _                     e.Graphics.FillRectangle(Brushes.Red, brickArray(row, column))             Next         Next          ' Show the ball and the paddle.         e.Graphics.FillRectangle(Brushes.Yellow, gameBall)         e.Graphics.FillRectangle(Brushes.DarkGreen, gamePaddle)     End Sub #End Region
Ending up with this result with the others minimized. Once more, add more code below, since it's all organised into Regions you shoudn't have any probs.
Code:
#Region "Game Timer"     Private Sub tmrGame_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGame.Tick         If Not isBallGlued Then _         gameBall.Location = New Point(gameBall.X + xVel, gameBall.Y + yVel)          ' Check for top wall.         If gameBall.Location.Y < 0 Then             gameBall.Location = New Point(gameBall.Location.X, 0)             yVel = -yVel         End If          ' Check for bottom wall (restart)         If gameBall.Location.Y - gameBall.Height > Me.Height Then             isBallGlued = True             gameBall.Location = New Point(gamePaddle.X + 72 / 2 _             - (gameBall.Width / 2), 432 - 16)         End If          ' Check for left wall.         If gameBall.Location.X < 0 Then             gameBall.Location = New Point(0, gameBall.Location.Y)             xVel = -xVel         End If          ' Check for right wall.         If gameBall.Location.X + gameBall.Width > Me.Width Then             gameBall.Location = New Point(Me.Width - gameBall.Width, _             gameBall.Location.Y)             xVel = -xVel         End If          ' Check for paddle.         If gameBall.IntersectsWith(gamePaddle) Then             gameBall.Location = New Point(gameBall.X, gamePaddle.Y - gameBall.Height)             yVel = -yVel         End If          ' Check for blocks          For rows As Integer = 0 To brickRows             For columns As Integer = 0 To brickColumns                 If Not isBrickEnabled(rows, columns) Then Continue For                 If gameBall.IntersectsWith(brickArray(rows, columns)) Then                     isBrickEnabled(rows, columns) = False                     If gameBall.X + 10 < brickArray(rows, columns).X Or _                     gameBall.X > brickArray(rows, columns).X + brickArray(rows, columns).Width _                      Then                         xVel = -xVel                     Else                         yVel = -yVel                     End If                 End If             Next         Next          ' Check for end of game.         If getBrickCount() = 0 Then             tmrGame.Stop()             Windows.Forms.Cursor.Show()             If MessageBox.Show("Would you like to play again?", "Play Again?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then                 loadBricks()                 isBallGlued = True                 Windows.Forms.Cursor.Hide()                 gameBall.Location = New Point(gamePaddle.X + 72 / 2 - (gameBall.Width / 2), _                 432 - 16)                 tmrGame.Start()             Else                 Application.Exit()             End If         End If          Me.Refresh()     End Sub #End Region
Phew, add more code underneath that.
Code:
#Region "Set Up Bricks"     Sub loadBricks()         Dim xOffset As Integer = 75, yOffset As Integer = 100         For row As Integer = 0 To brickRows             For column As Integer = 0 To brickColumns                  brickArray(row, column) = New Rectangle( _                 xOffset, yOffset, brickWidth, brickHeight)                  xOffset += brickWidth + 10                 isBrickEnabled(row, column) = True             Next             yOffset += brickHeight + 10             xOffset = 75         Next     End Sub #End Region
We're nearly finished, a few more to go. Result is awesome so don't worry.
Code:
#Region "Get Amount of Bricks"      Function getBrickCount() As Integer          Dim Count As Integer = 0          For Each brick As Boolean In isBrickEnabled              If brick = True Then Count += 1          Next          Return Count      End Function  #End Region
Yet, more code.
Code:
#Region "Move Paddle According to Mouse Position"     Private Sub gameMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove         If e.X > 0 And e.X < Me.Width - 72 Then _         gamePaddle.Location = New Point(e.X, gamePaddle.Y)          If isBallGlued Then             gameBall.Location = New Point(gamePaddle.X + 72 / 2 - (gameBall.Width / 2), _             432 - 16)         End If     End Sub #End Region
Just two more, promise.
Code:
#Region "Launch Ball"      Private Sub gameMain_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick         If e.Button = Windows.Forms.MouseButtons.Left Then             ' Launch the ball.             If isBallGlued Then                 isBallGlued = False             End If         End If     End Sub #End Region
Last one.
Code:
#Region "Quit on Esc / Pause on P"     Private Sub gameMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown         ' Exit         If e.KeyCode = Keys.Escape Then Application.Exit()          ' Toggle Paused         If e.KeyCode = Keys.P Then _             If tmrGame.Enabled Then tmrGame.Stop() _             Else tmrGame.Start()     End Sub #End Region
That's it! The coding is done, well done if you've achieved it, it was quite confusing. What now? Run your Brick Breaker - F5. Click the the left mouse button and start playing - when all bricks are gone, it'll ask you whether you want to play again or not. Final view of code and form. Code Form Final Enjoy playing! Project file is available!
Code:
http://www.vbdotnetkingdom.com/downloads/tuts/BrickBreaker.rar
Code:
www.vbdotnetkingdom.com
Enjoy! More tutorial to come later
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!
Reply With Quote

Reply

Bookmarks

Tags
breaker, brick, game, tutorial


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding Emerging Markets: Building Business Bric by Brick free-ebook Business Book 0 09-01-2009 11:38 AM
[Megaupload.com] Banging Your Head Against a Brick Wall topso Multimedia Book 0 08-25-2009 08:08 PM
Special Effects Game Programming with DirectX w/CD (The Premier Press Game Development Series) FED Programing Book 1 04-25-2009 04:11 PM
The Complete Guide to Game Audio: For Composers, Musicians, Sound Designers, and Game Developers shooter Programing Book 0 03-29-2009 03:45 PM
Brick Watching FED Multimedia Book 0 03-28-2009 06:24 AM

All times are GMT. The time now is 07:10 AM.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227