Click here to Skip to main content
15,886,060 members
Articles / Programming Languages / Visual Basic
Article

A Clock Game

Rate me:
Please Sign up or sign in to vote.
2.33/5 (7 votes)
10 May 20052 min read 40.1K   1.9K   17   2
Create an analogue clock with a game !!

Sample Image - clockgame.jpg

Introduction

Frustrated and bored with everything digital around me... :) (I m not joking). I wanted something "Analogue"... what's better than an Analogue watch. The game came afterwarrds when I finally got bored at staring my creation.

Here is the code..

First let us start with creating a clock. Starting with the design part, I Created a smart frame for my clock in Photoshop. I then added it as the background image of the Form, wrote the numbers on the dial and created three small lines in the middle (the hands of the clock).

Design part finished !! Moving on to the coding part. Let us start with creating a clock
!!!BAM!!!

VB
Private Sub Timer1_Timer()
Form1.Caption = Time

sdeg = Second(Time) * 6
mdeg = Minute(Time) * 6
hdeg = Hour(Time) * 6

' Second Hand
srad = (3.14 / 180) * sdeg
Line2.X1 = Line2.X2 + (90 * Sin(srad))
Line2.Y1 = Line2.Y2 - (90 * Cos(srad))

' Minute Hand
mrad = (3.14 / 180) * mdeg
Line1.X1 = Line1.X2 + (80 * Sin(mrad))
Line1.Y1 = Line1.Y2 - (80 * Cos(mrad))

' Hour Hand
hdeg = Hour(Time) * 30 + (0.5 * Minute(Time))
hrad = (3.14 / 180) * hdeg
Line3.X1 = Line3.X2 + (65 * Sin(hrad))
Line3.Y1 = Line3.Y2 - (65 * Cos(hrad))
End Sub

That's all the code neede to created a running clock !! Easy Isn't it?
It's all mathematics. First we calculate the angle of the hands in degrees (i.e multiply by 6) since

1 circle = 360 degrees , we have 60 divisions on the clock so difference between 2 consequtive divisions is 6 degrees

Now we convert the angle into radians (pie/180) * angle in degrees (remember primary math lectures :) )

Finally we move the hands by changing there X and Y co-ordinates...If we interchange the Cos with a Sin and vice versa the clock will move anti-clockwise (he he).

That's it our clock is now finished.....It did not do much so I thought of creating a game in it. The game is simple. All you have to do is select any number from 1 to 12 (the numbers on the dial) and then spin the wheel. The wheel will keep spinning on the numbers reversing it's direction after a specified interval unless you click on it to stop it. If the wheel stops on the number which you had selected then you win!!.

This is the concept of our simple Time Pass game... Now for the code

VB
Private Sub Timer2_Timer()
Label1(12).ForeColor = vbGreen

If (i <= 13) Then
    Label1(i).ForeColor = vbBlue
    Label1(i - 1).ForeColor = vbGreen
    i = i + 1
End If

If i = 13 Then
    i = 1
End If

If Timer2.Interval >= 10 And Timer2.Interval < 90 Then
    Timer2.Interval = Timer2.Interval + 1
End If
If Timer2.Interval = 90 Then
    Timer2.Interval = 10
    Timer3.Enabled = True
    Timer2.Enabled = False
End If
End Sub

' To display the spinning effect anti clockwise

Private Sub Timer3_Timer()
Label1(1).ForeColor = vbGreen

If (j >= 1) Then
    Label1(j).ForeColor = vbBlue
    Label1(j + 1).ForeColor = vbGreen
    j = j - 1
End If

If j = 0 Then
    j = 12
End If

If Timer3.Interval <= 90 And Timer3.Interval > 10 Then
    Timer3.Interval = Timer3.Interval - 1
End If
If Timer3.Interval = 10 Then
    Timer3.Interval = 90
    Timer3.Enabled = False
    Timer2.Enabled = True
End If
End Sub

Above we have 2 timers for spinning the wheel. The only difference is that one spins the wheel clockwise and the other anti-clockwise after a specified interval and so on(just to add some un to the spin). The spinning accelerates and deaccelerates as it changes direction.

Now to give the spinning effect all we do is change the color of the current number with blue(all are default green), move onto the next number change it to blue and change the previous back to green. in this we it seems as the wheel is moving. The code is fairly easy to understand..

That's all I had to say.. Don't forget to give your feedback

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I love coding, gaming and .......... http://www.gauravcreations.com

Comments and Discussions

 
GeneralMy vote of 3 Pin
hansoctantan2-Dec-11 3:58
professionalhansoctantan2-Dec-11 3:58 
GeneralWrong place! Pin
mav.northwind11-May-05 3:07
mav.northwind11-May-05 3:07 
CodeProject isn't a place for VB6 projects and code (although the april's fool joke stated otherwise). Sigh | :sigh:

mav

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.