Mata Kuliah pemrograman Komputer with Bapak R. Hengki Rahmanto, ST.M.Eng

JAM CATUR ANALOG,


Buatkan program seperti dibawah ini :

Prinsip dasar pemrograman Jam Catur adalah bila pemain A menekan tombol A,

maka Jam A tidak jalan, tetapi Jam B bergerak, sampai pemain B menekan tombol B,

langsung B berhenti sedang A mulai bergerak lagi. Property diatas untuk Jam Catur

Digital anda modifikasi sendiri, berikut adalah source code untuk Jam Catur

Analog-Digital, tampilan dilayar adalah sebagai berikut (dapat dimodifikasi

sendiri), sedang nama-nama object anda sesuaikan.

(Form2)

 


Form 1

Source Code Form2

Option Explicit

Public StartTime, FinishTime As String

Dim JamStr, MinStr As String

Public TimeJam, TimeMin As Integer

Private Sub Command1_Click()

TimeMin = Val(TxtTime.Text)

If TimeMin > 60 Then

TimeJam = TimeJam + Int(TimeMin / 60)

TimeMin = TimeMin Mod 60

End If

If TimeJam > 12 Then TimeJam = TimeJam Mod 12

JamStr = Trim(Str(TimeJam))

MinStr = Trim(Str(TimeMin))

If TimeMin < 10 Then

MinStr = “0” & MinStr

End If

FinishTime = JamStr & “:” & MinStr & “:00”

Form2.Hide

Form1.Show

End Sub

Private Sub Form_Load()

Form_Activate

End Sub

Private Sub Form_Activate()

StartTime = “0:00:00”

TimeJam = 0

Form2.Top = (8000 – 2500) / 2 ‘2500

Form2.Left = (12000 – 5000) / 2 ‘4000

Form2.Width = 5000 ‘max 12000

Form2.Height = 2500 ‘max 8000

End Sub

Source Code Form1 dan Tampilan awal

Timer1    Line1(2)

Line1(1)

Timer2

LineMin(1)    LineMin(2)

Line3(1)

LineJam(2)

Line3(2)

LineJam(1)

Form1

Obyek (object) yang ada pada Form1

Line :

LineJam(1), LineJam(2) = untuk Jarum Jam

LineMin(1), LineMin(2) = untuk Jarum Menit

Line1(1), Line1(2) = untuk Jarum Detik

Line2(

Line3(1), Line3(2) = untuk petunjuk akhir dari permainan Catur

Label:

Label1 = untuk tampilan jam digital (putih)

Label2 = untuk tampilan jam digital (hitam)

Label3 = untuk tampilan jam digital (Batas Akhir Waktu Permainan)

Label4 = untuk tampilan pernyataan kalah/menang

Label5 = “Finish Time”

Timer:

Timer1 = untuk timernya Putih

Timer2 = untuk timernya Hitam

Source Code Form1

‘Program Jam Catur

Option Explicit

Const Pi = 3.14159265358979

Dim Yy, RTit, Rout, RAng, RDet, RMen, RJam As Integer

Dim Xx(2) As Integer

Dim Putih, GameOver As Boolean

Dim Waktu1, Waktu2, Akhir As String

Dim i As Integer

‘Warna Violet = &H00FF00FF&

Private Sub CmdB_Click()

Putih = False

Time = Waktu1

Timer2.Enabled = False

If GameOver Then

Timer1.Enabled = False

Else

Timer1.Enabled = True

CmdW.SetFocus

End If

End Sub

Private Sub CmdS_Click()

Form1.Hide

Form2.Show

End Sub

Private Sub CmdW_Click()

Time = Waktu2

Timer1.Enabled = False

If GameOver Then

Timer2.Enabled = False

Else

Timer2.Enabled = True

CmdB.SetFocus

End If

End Sub

Private Sub Form_Load()

Form_Activate

End Sub

Private Sub Form_Activate()

Inisialisasi

Putih = True

RTit = 1900

Rout = 2000

RAng = RTit – 200

RDet = RTit – 175

RMen = Int(0.85 * RDet)

RJam = Int(0.6 * RDet)

Lingkaran (1)

Lingkaran (2)

End Sub

Sub Inisialisasi()

Dim Lebar, Tinggi, Pusat, PusatH As Integer

Hilang (True)

Pusat = 4500

PusatH = 3000

Form1.Top = (8000 – 2 * PusatH) / 2

Form1.Left = (12000 – 2 * Pusat) / 2

Form1.Width = 2 * Pusat

Form1.Height = 2 * PusatH

Lebar = 2000

Tinggi = 500

Label3.Height = Tinggi

Label3.Width = Lebar

Label3.Left = Pusat – Lebar / 2

Label5.Height = Tinggi

Label5.Width = Lebar

Label5.Left = Pusat – Lebar / 2

Label1.Width = Lebar

Label1.Height = Tinggi

Label1.Left = Pusat / 2 – Lebar / 2 ‘750

Label2.Width = Lebar

Label2.Height = Tinggi

Label2.Left = Pusat * 3 / 2 – Lebar / 2 ‘5250

Label1.Top = 200

Label2.Top = 200

Lebar = 3 / 4 * Lebar

CmdS.Width = Lebar

CmdS.Height = Tinggi

CmdW.Width = Lebar

CmdW.Height = Tinggi

CmdB.Width = Lebar

CmdB.Height = Tinggi

CmdW.Left = Pusat / 2 – Lebar / 2 ‘1500

CmdB.Left = Pusat / 2 * 3 – Lebar / 2

CmdS.Left = Pusat – Lebar / 2

Lebar = 4 * Lebar ‘6000

Tinggi = 2 * Tinggi

Label4.Width = Lebar ‘6000

Label4.Height = Tinggi

Label4.Top = PusatH – 100 – Tinggi / 2 ‘2400

Label4.Left = Pusat – Lebar / 2 ‘1500

Waktu1 = Form2.StartTime

Waktu2 = Form2.StartTime

Time = Form2.StartTime

Akhir = Form2.FinishTime

Label1.Caption = Form2.StartTime

Label2.Caption = Waktu2

Label3.Caption = Akhir

End Sub

Sub Lingkaran(x)

Dim k, L, Lm, Angka As Byte

Dim Xt, Yt, Xa, Ya As Integer

Dim Batas As Integer

If x = 1 Then

k = 0

L = 0

Lm = 0

Xx(1) = 2250

Timer1.Interval = 300 ‘ Set Timer interval.

Else

k = 240

L = 60

Lm = 12

Xx(2) = 3 * 2250

Timer2.Interval = 300 ‘ Set Timer interval.

End If

Yy = 3000 – 100

Line1(x).X1 = Xx(x)

Line1(x).Y1 = Yy

LineMenit(x).X1 = Xx(x)

LineMenit(x).Y1 = Yy

LineJam(x).X1 = Xx(x)

LineJam(x).Y1 = Yy

‘BUAT LINGKARAN

Line2(k + 0).X1 = Xx(x) + Int(Rout * Cos(Pi / 2 – 239 * Pi /

120))

Line2(k + 0).Y1 = Yy + Int(Rout * Sin(Pi / 2 – 239 * Pi / 120))

Line2(k + 0).X2 = Xx(x) + Int(Rout * Cos(Pi / 2))

Line2(k + 0).Y2 = Yy + Int(Rout * Sin(Pi / 2))

For i = (1 + k) To (239 + k)

Xt = Int(Rout * Cos(Pi / 2 – i * Pi / 120))

Yt = Int(Rout * Sin(Pi / 2 – i * Pi / 120))

Line2(i).X1 = Line2(i – 1).X2

Line2(i).X2 = Xx(x) + Xt

Line2(i).Y1 = Line2(i – 1).Y2

Line2(i).Y2 = Yy + Yt

Next i

‘Jarum Max

Batas = Form2.TimeMin

Xt = Int(RTit * Cos(Pi / 2 – Batas * Pi / 30))

Xa = Int((RTit – 500) * Cos(Pi / 2 – Batas * Pi / 30))

Yt = Int(RTit * Sin(Pi / 2 – Batas * Pi / 30))

Ya = Int((RTit – 500) * Sin(Pi / 2 – Batas * Pi / 30))

Line3(x).X1 = Xx(x) + Xt

Line3(x).X2 = Xx(x) + Xa

Line3(x).Y1 = Yy – Yt

Line3(x).Y2 = Yy – Ya

‘MENIT dalam CLOCK

For i = 0 To 59

Xt = Int(RTit * Cos(Pi / 2 – i * Pi / 30)) ‘-

Yt = Int(RTit * Sin(Pi / 2 – i * Pi / 30)) ‘-

Xa = Int(RAng * Cos(Pi / 2 – i * Pi / 30)) ‘-

Ya = Int(RAng * Sin(Pi / 2 – i * Pi / 30)) ‘-

LDetik(i + L).BorderStyle = 6

If i Mod 5 = 0 Then

LDetik(i + L).BorderWidth = 5

LDetik(i + L).BorderColor = &HFF0000

LMen((i + L) / 5).Top = Yy – (Ya + 150)

LMen((i + L) / 5).Left = Xx(x) + (Xa – 150)

Else

LDetik(i + L).BorderWidth = 3

LDetik(i + L).BorderColor = &HFFFF00

End If

LDetik(i + L).X1 = Xx(x) + Xt

LDetik(i + L).Y1 = Yy – Yt

LDetik(i + L).X2 = Xx(x) + Xt

LDetik(i + L).Y2 = Yy – Yt

Next i

‘Angka dalam CLOCK

For i = 0 To 11

If i = 0 Then Angka = 12 Else Angka = i

LMen(i + Lm).Caption = Angka

Next i

End Sub

Private Sub Timer1_Timer()

Label1.Caption = Time ‘ Update waktu display.

TimerAuto (1)

If Waktu1 = Akhir Then Kalah (“PUTIH”)

End Sub

Private Sub Timer2_Timer()

‘If Putih Then Waktu1 = Time

Label2.Caption = Time ‘ Update waktu display.

TimerAuto (2)

If Putih Then CmdB_Click

If Waktu2 = Akhir Then Kalah (“HITAM”)

End Sub

Sub Kalah(x)

Beep

Label4.Caption = x & ” KALAH”

Hilang (False)

End Sub

Sub TimerAuto(x)

Dim Jam, Menit, Detik, Panj As Integer

Dim Xd, Yd, Xm, Ym, xj, yj As Integer

Panj = Len(Time)

If Panj < 10 Then Panj = Panj + 3

Detik = Mid(Time, Panj – 4, 2)

Menit = Mid(Time, Panj – 7, 2)

Jam = Left(Time, Panj – 9)

Xd = Int(RDet * Cos(Pi / 2 – Detik * Pi / 30)) ‘-

Yd = Int(RDet * Sin(Pi / 2 – Detik * Pi / 30)) ‘-

Xm = Int(RMen * Cos(Pi / 2 – (Menit + Detik / 60) * Pi / 30)) ‘-

Ym = Int(RMen * Sin(Pi / 2 – (Menit + Detik / 60) * Pi / 30)) ‘-

xj = Int(RJam * Cos(Pi / 2 – (Jam + Menit / 60) * Pi / 6)) ‘-

yj = Int(RJam * Sin(Pi / 2 – (Jam + Menit / 60) * Pi / 6)) ‘-

LineJam(x).X2 = Xx(x) + xj

LineJam(x).Y2 = Yy – yj

LineMenit(x).X2 = Xx(x) + Xm

LineMenit(x).Y2 = Yy – Ym

Line1(x).X2 = Xx(x) + Xd

Line1(x).Y2 = Yy – Yd

Waktu1 = Label1.Caption

Waktu2 = Label2.Caption

End Sub

Sub Hilang(x)

Timer1.Enabled = x

Timer2.Enabled = x

CmdW.Visible = x

CmdB.Visible = x

Label3.Visible = x

Label5.Visible = x

CmdS.Visible = Not x

Label4.Visible = Not x

GameOver = Not x

End Sub

Semoga bermanfaat.


Iklan