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.
| Video Training Online Video Training, video training software, video editing training, total training video, microsoft training video, corporate training videos, ... |
![]() |
|
|||||||||||||||||||||||
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.
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 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 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 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
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 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 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 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 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 Form Final Enjoy playing! Project file is available! Code:
http://www.vbdotnetkingdom.com/downloads/tuts/BrickBreaker.rar Code:
www.vbdotnetkingdom.com
|
|||||||||||||||||||||||
![]() |
| Bookmarks |
| Tags |
| breaker, brick, game, tutorial |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |