9 Jan 2012

Latihan membuat game dengan VB

Kali ini kita kita akan belajar membuat game yang nantinya bisa kamu kembangkan sendiri.Penulis hanya membuat sample ini supaya kamu bisa menciptakan sendiri game yang lebih bagus.Game ini sangat simple dengan tampilan 2 dimensi menggunakan scipt kode di VB. Ada tiga option yang bisa dipilih yaitu :
1) Start
2) Options
3) The Game

Komponen yang digunakan
1) Timer Control
2) Picture Control
3) Label Control
4) Windows Media ocx

Untuk bermain game ini hanya menggunakan tombol arah serta tombol spasi. Silakan mencoba sendiri..
Dan berikut sample codenya

Option Explicit
Dim u, d, l, r, showm As Boolean
Dim x, y As Integer
Dim mx, my As Integer
Dim ex, ey As Integer
Dim score As Long
Dim fuel As Integer
Dim es As Integer

Private Sub Form_Load()'MediaPlayer1.playerApplication = App.Path & "\sfx\fire.wav"
'MediaPlayer2.FileName = App.Path & "\sfx\Explosion.wav"
'MediaPlayer3.FileName = App.Path & "\sfx\mainsound.mp3"lblScore.Caption = "0"x = 0
y = 0ex = -100
ex = -100es = 10fuel = 1
End Sub

Private Sub Form_Paint()
shooter.SetFocus
End Sub

Private Sub shooter_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 49 Then speed = speed - 1
If speed <= 0 Then speed = 0 If speed > 30 Then speed = 30

If KeyCode = 50 Then speed = speed + 1
If KeyCode = vbKeyLeft Then l = True
If KeyCode = vbKeyRight Then r = True
If KeyCode = vbKeyUp Then u = True
If KeyCode = vbKeyDown Then d = True
If KeyCode = vbKeySpace Then
If Not showm Then
fireit
End If
End If
If KeyCode = vbKeyEscape Then Unload Me: End
End Sub

Private Sub shooter_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft Then l = False
If KeyCode = vbKeyRight Then r = False
If KeyCode = vbKeyUp Then u = False
If KeyCode = vbKeyDown Then d = False
End Sub

Private Sub Timer1_Timer()Static ch As Boolean
ch = Not chIf ch Then
shooter.Picture = Picture2.Picture
Else
shooter.Picture = Picture3.Picture
End If
End Sub

Private Sub
Timer2_Timer()
If l Then
x = x - speed
If x < x =" 0" x =" x">= Me.ScaleWidth - 100 Then x = Me.ScaleWidth - 100

End If

If u Then
y = y - speed
If y < y =" 0" y =" y">= Me.ScaleHeight - 100 Then y = Me.ScaleHeight - 100
End If

Label5.Caption = "X = " & x
Label6.Caption = "Y = " & y

shooter.Left = x
shooter.Top = y

Label3.Caption = CStr(speed)

If showm Then

mx = mx + 20
If mx > Me.ScaleWidth Then
showm = False
fire.Visible = False

End If

fire.Left = mx
fire.Top = my

If (my > ey And my <> ex) Then
score = score + 10
showm = False
SetEn
End If

Else
fire.Visible = False

End If

ex = ex - es
en.Left = ex
If ex < -200 Then SetEn en.Top = ey End If lblScore = CStr(score) Label8.Caption = "EX = " & ex Label7.Caption = "EY = " & ey If (y > ey - 40 And y <> ex And x < fuel =" fuel"> 1 Then MediaPlayer2.Play
Picture1.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)

SetEn Select Case fuel

Case 2
Image1.Picture = LoadPicture(App.Path & "\data\fuel50.gif")

Case 3
Image1.Picture = LoadPicture(App.Path & "\data\fuel20.gif")

Case 4
Image1.Picture = LoadPicture(App.Path & "\data\game-over.gif")

End Select

If fuel = 4 Then

MsgBox "Game Over", vbCritical, "Shooter"
Unload Me
Form2.Show

End If

End If
End Sub

Private Sub fireit()
'MediaPlayer1.Play
showm = Truemx = shooter.Left + 100
my = shooter.Top + 50
fire.Visible = True
End Sub

Public Sub SetEn()
ey = Int(Rnd * Me.ScaleHeight) - 100

ex = Me.ScaleWidth
en.Left = ex
en.Top = ey

End Sub

Private Sub Timer3_Timer()
es = es + 5
End Sub

Berikut source code yang sudah jadi download

No comments:

Post a Comment

Garudayasa

Garudayasa