Sizinde hoşuna gidebileceği bir programlama makalesi ile karşı karşıyayız. Çünkü bu sefer size visual basic dili ile resimde görmüş olduğunuz oyunu programlayacağız:
Eskiden oyunlar günümüze basit ve yaratıcı fikirleri öne sürerekten değilik varyasyonlar almış ve artık karmaşık matematik problemlerine dönüşmüştür.
Oyun yazmayı can sıkıcı hale getiren de budur aslında;
Matematik problemi...
Yukarıda görmüş oyunu yazmak içinde kısmi bir matematik problemini çözmüş olacağız, ayrıca da oyunun genelinden basit bir şekilde, bildiğimiz programlama temeli ile en basit ve seri bir şekilde programımızı bitirmiş olacağız.
Nedir Bu Matematik Problemi?
Oyunumuz için öncellikle aşılması gereken problem bir adet topumuz var ve bu top ekrandaki nesneler çarpıp sekiyor ve çarptığı şeyi anlıyor (yere çarpınca can kaybetmek gibi şeyler...). Peki biz bunu bilgisayara nasıl anlatacağız?
Öncellikle Forumuma bir Timer atıyorum ve genel olarak 2 adet ana değişkenimi belirtiyorum;
Dim dikey = 10
Dim yatay = 10
Bu değişkenler Label1 yani topumuzun X yönü ve Y yönleri arasındaki hareketini temsil edecek.
Timer1'in interval değerini 25 yaptıktan sonra tick kodlarına şunu yazıyoruz;
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Location.X + 30 > Me.Width Then
yatay = -11
ElseIf Label1.Location.X < 0 Then
yatay = 11
End If
Böyle Top yatay eksende bir ileri bir geri gidebilecek.Böyle yan kenarları anlamış olacak, matematik problemimizin birazını çözmüş olduk.Şimdi gelelim Dikey eksene;
Öncelikle aşağıdaki dizaynı yaratalım;
Sonra lives için oyunda canlarımızı kontrol edebileceğimiz bir kod ekleyelim;
Dim lives As Integer
Bu değişkeni global olarak atadıktan sonra aşağıdaki void'i yazıyoruz;
Private Sub canekle()
If lives = 0 Then
Label3.Hide()
Label4.Hide()
Label5.Hide()
ElseIf lives = 1 Then
Label3.Show()
Label4.Hide()
Label5.Hide()
ElseIf lives = 2 Then
Label3.Show()
Label4.Show()
Label5.Hide()
ElseIf lives = 3 Then
Label3.Show()
Label4.Show()
Label5.Show()
End If
End Sub
Bu şekilde canlarımızı ekleyip silebileceğiz...
Peki ya sonrası?Yatay olarak label1 ileri gidip geliyor, peki o zaman label1'imizin dikey olarak çarpmasını yazalım;
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Location.X + 30 > Me.Width Then
yatay = -11
ElseIf Label1.Location.X < 0 Then
yatay = 11
End If
If Label1.Location.Y + 40 > Me.Height Then
Timer1.Stop()
Label5.Hide()
lives -= 1
If lives = 1 Then
Label4.Hide()
Beep()
End If
If lives = 0 Then
Label3.Hide()
Beep()
ElseIf lives < 0 Then
MsgBox("Oyun Bitti")
End
End If
Label1.Left = Label2.Location.X + ((Label2.Width / 2))
Label1.Top = Label2.Location.Y - Label2.Height - Label1.Height
Timer1.Start()
ElseIf Label1.Location.Y < 0 Then
dikey = 10
End If
Bu şekilde label1 dikey ve yatay olarak hareket edecektir. Yere çarptığında ise Timer1 duracaktır.
E zaman hareketimiz tamam ise, Buttonlarımızı yerleştirelim ki oyunumuz oynansın
Bunu oluşturmak için kısa bir void yazıyorum;
Public Sub yarat(ByVal yatay As Integer, ByVal dikey As Integer, ByVal toplam As Integer)
Dim dikeyyeri As Integer
Dim yatayyeri As Integer
Dim ad As String
ad = "1"
Dim renksalla As New Random
dikeyyeri = 0
yatayyeri = 0
For i As Integer = 1 To toplam
Dim button As New Button
button.Size = New Size(20, 20)
button.Left = yatay + dikeyyeri
button.Top = dikey + yatayyeri
button.Name = "Button1"
button.Show()
button.Enabled = False
button.BackColor = Color.FromArgb(renksalla.Next(120, 250), renksalla.Next(120, 250), renksalla.Next(120, 250))
Me.Controls.Add(button)
dikeyyeri += 20
yatayyeri += 0
Next
End Sub
Böylelikle buttonları kendim istediğim yerde oluşturabilirim.
Şimdi yapmam gereken şey oyundaki seviyeyi yapmak.
Forma 2. bir Timer ekliyorum. Bu sayede kaçıncı bölümde olduğumu ekranda yazacak...
Dim flash As Integer = 0
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label6.Hide()
Label6.Show()
flash += 1
If flash = 100 Then
Timer2.Stop()
Label6.Hide()
flash = 0
End If
End Sub
Ve hemen ardından bölümleri oluşturuyorum;
Dim level As Integer = 1
Public Sub levels()
Timer1.Start()
If level = 2 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level3
End If
If level = 3 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level4
End If
If level = 4 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level5
End If
If level = 5 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level6
End If
If level = 6 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level7
End If
If level = 7 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level8
End If
If level = 8 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level9
End If
If level = 9 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level10
End If
level2:
yarat(50, 25, 10)
yarat(70, 45, 10)
yarat(20, 55, 15)
level -= 1
Timer2.Start()
Label6.Text = "Level 2"
Exit Sub
level3:
yarat(80, 80, 30)
yarat(60, 60, 5)
yarat(30, 40, 30)
level -= 1
Timer2.Start()
Label6.Text = "Level 3"
Exit Sub
level4:
yarat(60, 60, 15)
yarat(40, 20, 5)
yarat(20, 20, 5)
level -= 1
Timer2.Start()
Label6.Text = "Level 4"
Exit Sub
level5:
yarat(40, 60, 30)
yarat(10, 25, 15)
yarat(5, 5, 10)
level -= 1
Timer2.Start()
Label6.Text = "Level 5"
Exit Sub
level6:
yarat(15, 15, 20)
yarat(15, 15, 20)
yarat(15, 15, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 6"
Exit Sub
level7:
yarat(5, 5, 20)
yarat(15, 15, 20)
yarat(40, 40, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 7"
Exit Sub
level8:
yarat(60, 90, 25)
yarat(70, 70, 20)
yarat(10, 10, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 8"
Exit Sub
level9:
yarat(17, 95, 15)
yarat(37, 73, 13)
yarat(62, 10, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 9"
Exit Sub
level10:
yarat(17, 95, 15)
yarat(17, 95, 15)
yarat(17, 95, 15)
yarat(37, 73, 13)
yarat(37, 73, 13)
yarat(37, 73, 13)
yarat(62, 10, 20)
yarat(62, 10, 20)
yarat(62, 10, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 10"
Exit Sub
End Sub
E tabi bunları yaptık ne kaldı sırada tabii Form_Onload kısmına oyunun başlangıç kodlarını yazmalıyız. Böylece form açılır açılmaz oyun başlasın;
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer2.Start()
Label6.Text = "Level 1"
lives = 3
Label2.Top = Me.Height - Label2.Height - 25
Label1.Left = Label2.Location.X + ((Label2.Width / 2))
Label1.Top = Label2.Location.Y - Label2.Height - Label1.Height
GoTo Level1
Level1:
yarat(50, 50, 10)
yarat(70, 70, 10)
yarat(90, 90, 10)
End Sub
Bir sonraki adımda ise Label2'imizi yani çubuğumuzu hareketlendirmek olacak. Bunun içinde form_keydown eventine aşağıdaki gibi yazıyoruz;
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If Not Label2.Location.X - 15 < 0 Then
If e.KeyCode = Keys.A Then
Label2.Left -= 20
End If
End If
If Not Label2.Location.X + (Label2.Width) > Me.Width - 10 Then
If e.KeyCode = Keys.D Then
Label2.Left += 20
End If
End If
End Sub
Bu sayede çubuğumuz var, label1 hareket etmekte yere çarpınca canımız azalmakta, label2 ise klavyedeki tuşlara basarak hareket etmekte, tek sorun label1 buttonlara ve çubuğumuza çarpmıyor.
Oynumuzun en can alıcı kısmı bu, Matematik problemimizin 2. kısmı, bunu çözmek için label1'in label2 ile kesiştiği yerler dışında bulunursa canımız azalacak, aksi taktirde olursa label1 çubuğa(label2) çarpıp yoluna devam edecek.Bunu yapmak için Timer1'e kodlarımızı ekliyoruz;
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Location.X + 30 > Me.Width Then
yatay = -11
ElseIf Label1.Location.X < 0 Then
yatay = 11
End If
If Label1.Location.Y + 40 > Me.Height Then
Timer1.Stop()
Label5.Hide()
lives -= 1
If lives = 1 Then
Label4.Hide()
Beep()
End If
If lives = 0 Then
Label3.Hide()
Beep()
ElseIf lives < 0 Then
MsgBox("Oyun Bitti")
End
End If
Label1.Left = Label2.Location.X + ((Label2.Width / 2))
Label1.Top = Label2.Location.Y - Label2.Height - Label1.Height
Timer1.Start()
ElseIf Label1.Location.Y < 0 Then
dikey = 10
End If
'Buttonlara çarpma fonksiyonu:
For Each d As Control In Me.Controls
If d.GetType.Name = "Button" Then
Dim a As Button = d
If Label1.Location.X > a.Left AndAlso Label1.Location.Y = a.Location.Y Then
If Label1.Location.X < (a.Location.X + a.Width) Then
yatay = +10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
If Label1.Location.X < Me.Width - (a.Location.X + a.Width) AndAlso Label1.Location.Y = a.Location.Y Then
If Label1.Location.X > Label2.Left Then
yatay = -10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
If Label1.Location.Y < a.Top AndAlso Label1.Location.X > a.Left AndAlso Label1.Location.X < (a.Left + a.Width) Then
If Label1.Location.Y > a.Top - 25 Then
dikey = -10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
If Label1.Location.Y > (a.Top - 10) AndAlso Label1.Location.X > a.Left AndAlso Label1.Location.X < (a.Left + a.Width) Then
If Label1.Top < (a.Top + a.Height) Then
dikey = +10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
End If
Next
'topun kendi fonksiyonu:
If Label1.Location.X > Label2.Left AndAlso Label1.Location.Y = Label2.Location.Y Then
If Label1.Location.X < (Label2.Location.X + Label2.Width) Then
yatay = +10
End If
End If
If Label1.Location.X < Me.Width - (Label2.Location.X + Label2.Width) AndAlso Label1.Location.Y = Label2.Location.Y Then
If Label1.Location.X > Label2.Left Then
yatay = -10
End If
End If
If Label1.Location.Y < Label2.Top AndAlso Label1.Location.X > Label2.Left AndAlso Label1.Location.X < (Label2.Left + Label2.Width) Then
If Label1.Location.Y > Label2.Top - 25 Then
dikey = -10
End If
End If
If Label1.Location.Y > (Label2.Top - 10) AndAlso Label1.Location.X > Label2.Left AndAlso Label1.Location.X < (Label2.Left + Label2.Width) Then
If Label1.Top < (Label2.Top + Label2.Height) Then
dikey = +10
End If
End If
Label1.Location = New Point(Label1.Location.X + yatay, Label1.Location.Y + dikey)
End Sub
Yukarıdaki kodlara baktığımızda top(label1) buttonlara çarpıp geri göndebilecek, ayrıca çubuğumuza çarpıp kendi yoluna devam edebilecek...
Tüm sistemimiz bu genel olarak ise tüm kodları buraya yazmak kaldı;
Dim dikey = 10
Dim yatay = 10
Dim lives As Integer
Private Sub canekle()
If lives = 0 Then
Label3.Hide()
Label4.Hide()
Label5.Hide()
ElseIf lives = 1 Then
Label3.Show()
Label4.Hide()
Label5.Hide()
ElseIf lives = 2 Then
Label3.Show()
Label4.Show()
Label5.Hide()
ElseIf lives = 3 Then
Label3.Show()
Label4.Show()
Label5.Show()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Location.X + 30 > Me.Width Then
yatay = -11
ElseIf Label1.Location.X < 0 Then
yatay = 11
End If
If Label1.Location.Y + 40 > Me.Height Then
Timer1.Stop()
Label5.Hide()
lives -= 1
If lives = 1 Then
Label4.Hide()
Beep()
End If
If lives = 0 Then
Label3.Hide()
Beep()
ElseIf lives < 0 Then
MsgBox("Oyun Bitti")
End
End If
Label1.Left = Label2.Location.X + ((Label2.Width / 2))
Label1.Top = Label2.Location.Y - Label2.Height - Label1.Height
Timer1.Start()
ElseIf Label1.Location.Y < 0 Then
dikey = 10
End If
'Buttonlara çarpma fonksiyonu:
For Each d As Control In Me.Controls
If d.GetType.Name = "Button" Then
Dim a As Button = d
If Label1.Location.X > a.Left AndAlso Label1.Location.Y = a.Location.Y Then
If Label1.Location.X < (a.Location.X + a.Width) Then
yatay = +10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
If Label1.Location.X < Me.Width - (a.Location.X + a.Width) AndAlso Label1.Location.Y = a.Location.Y Then
If Label1.Location.X > Label2.Left Then
yatay = -10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
If Label1.Location.Y < a.Top AndAlso Label1.Location.X > a.Left AndAlso Label1.Location.X < (a.Left + a.Width) Then
If Label1.Location.Y > a.Top - 25 Then
dikey = -10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
If Label1.Location.Y > (a.Top - 10) AndAlso Label1.Location.X > a.Left AndAlso Label1.Location.X < (a.Left + a.Width) Then
If Label1.Top < (a.Top + a.Height) Then
dikey = +10
If Me.Controls.Count <= 5 Then
Timer1.Stop()
level += 1
Me.levels()
End If
If Not a.Name = "Button" Then Me.Controls.Remove(a) : Exit Sub
End If
End If
End If
Next
'topun kendi fonksiyonu:
If Label1.Location.X > Label2.Left AndAlso Label1.Location.Y = Label2.Location.Y Then
If Label1.Location.X < (Label2.Location.X + Label2.Width) Then
yatay = +10
End If
End If
If Label1.Location.X < Me.Width - (Label2.Location.X + Label2.Width) AndAlso Label1.Location.Y = Label2.Location.Y Then
If Label1.Location.X > Label2.Left Then
yatay = -10
End If
End If
If Label1.Location.Y < Label2.Top AndAlso Label1.Location.X > Label2.Left AndAlso Label1.Location.X < (Label2.Left + Label2.Width) Then
If Label1.Location.Y > Label2.Top - 25 Then
dikey = -10
End If
End If
If Label1.Location.Y > (Label2.Top - 10) AndAlso Label1.Location.X > Label2.Left AndAlso Label1.Location.X < (Label2.Left + Label2.Width) Then
If Label1.Top < (Label2.Top + Label2.Height) Then
dikey = +10
End If
End If
Label1.Location = New Point(Label1.Location.X + yatay, Label1.Location.Y + dikey)
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If Not Label2.Location.X - 15 < 0 Then
If e.KeyCode = Keys.A Then
Label2.Left -= 20
End If
End If
If Not Label2.Location.X + (Label2.Width) > Me.Width - 10 Then
If e.KeyCode = Keys.D Then
Label2.Left += 20
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer2.Start()
Label6.Text = "Level 1"
lives = 3
Label2.Top = Me.Height - Label2.Height - 25
Label1.Left = Label2.Location.X + ((Label2.Width / 2))
Label1.Top = Label2.Location.Y - Label2.Height - Label1.Height
GoTo Level1
Level1:
yarat(50, 50, 10)
yarat(70, 70, 10)
yarat(90, 90, 10)
End Sub
Public Sub yarat(ByVal yatay As Integer, ByVal dikey As Integer, ByVal toplam As Integer)
Dim dikeyyeri As Integer
Dim yatayyeri As Integer
Dim ad As String
ad = "1"
Dim renksalla As New Random
dikeyyeri = 0
yatayyeri = 0
For i As Integer = 1 To toplam
Dim button As New Button
button.Size = New Size(20, 20)
button.Left = yatay + dikeyyeri
button.Top = dikey + yatayyeri
button.Name = "Button1"
button.Show()
button.Enabled = False
button.BackColor = Color.FromArgb(renksalla.Next(120, 250), renksalla.Next(120, 250), renksalla.Next(120, 250))
Me.Controls.Add(button)
dikeyyeri += 20
yatayyeri += 0
Next
End Sub
Dim level As Integer = 1
Public Sub levels()
Timer1.Start()
If level = 2 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level3
End If
If level = 3 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level4
End If
If level = 4 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level5
End If
If level = 5 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level6
End If
If level = 6 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level7
End If
If level = 7 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level8
End If
If level = 8 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level9
End If
If level = 9 Then
If Not lives = 3 Then
lives += 1
canekle()
End If
GoTo level10
End If
level2:
yarat(50, 25, 10)
yarat(70, 45, 10)
yarat(20, 55, 15)
level -= 1
Timer2.Start()
Label6.Text = "Level 2"
Exit Sub
level3:
yarat(80, 80, 30)
yarat(60, 60, 5)
yarat(30, 40, 30)
level -= 1
Timer2.Start()
Label6.Text = "Level 3"
Exit Sub
level4:
yarat(60, 60, 15)
yarat(40, 20, 5)
yarat(20, 20, 5)
level -= 1
Timer2.Start()
Label6.Text = "Level 4"
Exit Sub
level5:
yarat(40, 60, 30)
yarat(10, 25, 15)
yarat(5, 5, 10)
level -= 1
Timer2.Start()
Label6.Text = "Level 5"
Exit Sub
level6:
yarat(15, 15, 20)
yarat(15, 15, 20)
yarat(15, 15, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 6"
Exit Sub
level7:
yarat(5, 5, 20)
yarat(15, 15, 20)
yarat(40, 40, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 7"
Exit Sub
level8:
yarat(60, 90, 25)
yarat(70, 70, 20)
yarat(10, 10, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 8"
Exit Sub
level9:
yarat(17, 95, 15)
yarat(37, 73, 13)
yarat(62, 10, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 9"
Exit Sub
level10:
yarat(17, 95, 15)
yarat(17, 95, 15)
yarat(17, 95, 15)
yarat(37, 73, 13)
yarat(37, 73, 13)
yarat(37, 73, 13)
yarat(62, 10, 20)
yarat(62, 10, 20)
yarat(62, 10, 20)
level -= 1
Timer2.Start()
Label6.Text = "Level 10"
Exit Sub
End Sub
Dim flash As Integer = 0
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label6.Hide()
Label6.Show()
flash += 1
If flash = 100 Then
Timer2.Stop()
Label6.Hide()
flash = 0
End If
End Sub
Evet bir makalenin sonuna daha geldik. Umarım okuyanlara bir faydası dokunmuştur. Sorunuz varsa sormaktan çekinmeyin. İyi eğlenceler diliyorum...
Başka makalerler görüşmek üzere,
Saygılarımla,
Buğra Çuhadaroğlu