Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,085 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,791 people online right now. Registration is fast and FREE... Join Now!




How to keep track of time

 
Reply to this topicStart new topic

How to keep track of time, I am making a keep it up game and im not sure how to make a timer to k

ETP_RaYRaY
6 Jun, 2008 - 06:40 AM
Post #1

New D.I.C Head
*

Joined: 20 May, 2008
Posts: 5

Here is my code i know it somewhat messy but please help

vb

Public Class gameMain

#Region "Variables"
Private Const brickWidth As Integer = 160
Private Const brickHeight As Integer = 20
Private Const brickRows As Integer = 0
Private Const brickColumns As Integer = 0
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 = 10
Dim xVel As Single = Math.Cos(speed) * speed
Dim yVel As Single = Math.Sin(speed) * speed
Dim counter As Integer = 4
Dim walls As Integer
Dim score As Integer = 0

#End Region

#Region "Load Game"
Private Sub gameMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loadBricks()


End Sub
#End Region

#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.Black, gameBall)
e.Graphics.FillRectangle(Brushes.WhiteSmoke, gamePaddle)
End Sub
#End Region



#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
walls += 1
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)
counter -= 1
End If

' Check for left wall.
If gameBall.Location.X < 0 Then
gameBall.Location = New Point(0, gameBall.Location.Y)
xVel = -xVel
walls += 1
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
walls += 1
End If

' Check for paddle.
If gameBall.IntersectsWith(gamePaddle) Then
gameBall.Location = New Point(gameBall.X, gamePaddle.Y - gameBall.Height)
yVel = -yVel
End If
'hits box


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 counter = 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
walls = 0
isBallGlued = True
Windows.Forms.Cursor.Hide()
gameBall.Location = New Point(gamePaddle.X + 72 / 2 - (gameBall.Width / 2), _
432 - 16)
tmrgame.Start()
counter = 4
walls = 0
Else

Me.Close()
End If
End If
If walls = 0 Then
Me.lblbrick1.Visible = False
Me.lblbrick2.Visible = False
Me.lblbrick3.Visible = False
Me.lblbrick4.Visible = False
Me.lblbrick5.Visible = False
Me.lblbrick6.Visible = False
Me.lblbrick7.Visible = False

End If

Me.lblscore.Text = walls

If walls = 10 Then
Me.lblbrick1.Visible = True
End If
If walls = 15 Then
Me.lblbrick2.Visible = True
End If


If walls = 20 Then
Me.lblbrick3.Visible = True
End If

If walls = 25 Then
Me.lblbrick4.Visible = True
End If
If walls = 30 Then

Me.lblbrick5.Visible = True
End If
If walls = 35 Then
Me.lblbrick6.Visible = True
End If
If walls = 40 Then
Me.lblbrick7.Visible = True
End If

Me.lbllives.Text = "Bricks left " & counter - 1
Me.Refresh()
End Sub
#End Region

#Region "Set Up Bricks"
Sub loadBricks()
Dim xOffset As Integer = 250, yOffset As Integer = 200
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

#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

#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

#Region "Quit on Escape Key Press and Pause on P Key Press"
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 Me.Close()

' Toggle Paused
If e.KeyCode = Keys.P Then _
If tmrgame.Enabled Then tmrgame.Stop() _
Else tmrgame.Start()
End Sub
#End Region



End Class

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Keep Track Of Time
6 Jun, 2008 - 07:34 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Can you please, in the body of a post, post your question/problems and error's you may be encountering so we can better assist you. Also, I'm moving this to the VB.NET Forum as this is VB.Net code
User is offlineProfile CardPM
+Quote Post

robertelder
RE: How To Keep Track Of Time
6 Jun, 2008 - 07:53 AM
Post #3

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 30



Thanked: 3 times
My Contributions
ETP_RaYRaY,

This looks a lot like the code from the tutorial on how to make a break-out game.

What is your question?

-Rob
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month