Imports System.Threading
Public Class Yilan
Public alanX(999) As Integer
Public alanY(999) As Integer
Public yilanX(999) As Integer
Public yilanY(999) As Integer
Public bmp As Drawing.Bitmap
Public gfx As Drawing.Graphics
Public rect As Drawing.Rectangle
Public yilangolgesi As Drawing.Brush
Public yilanrengi As Drawing.Brush
Public yemgolgesi As Drawing.Brush
Public yemrengi As Drawing.Brush
Public background As Drawing.Brush
Public uz, j As Integer
Public x, y As Integer
Public yem As New Drawing.Point
Public yemvar As Boolean
Public puan As Integer
Public rota As Integer
Public bekleint As Integer
Public Sub Draw_Yilan(ByVal x As Integer, ByVal y As Integer)
gfx.FillRectangle(yilanrengi, x * 10, y * 10, 10, 10)
gfx.FillRectangle(yilanrengi, x * 10, y * 10, 8, 8)
End Sub
Public Sub Bos_Draw_Yilan(ByVal x As Integer, ByVal y As Integer)
gfx.FillRectangle(background, x * 10, y * 10, 10, 10)
End Sub
Public Sub Load_Graphic()
bmp = New System.Drawing.Bitmap(400, 400)
gfx = System.Drawing.Graphics.FromImage(bmp)
rect = New System.Drawing.Rectangle(0, 0, 400, 400)
gfx.FillRectangle(System.Drawing.Brushes.Black, rect)
End Sub
Public Sub move_yilan_right()
x += 1
End Sub
Public Sub move_yilan_left()
x -= 1
End Sub
Public Sub move_yilan_up()
y -= 1
End Sub
Public Sub move_yilan_down()
y += 1
End Sub
Public Sub yem_koy(ByVal x As Integer, ByVal y As Integer)
gfx.FillRectangle(yemrengi, x * 10, y * 10, 10, 10)
gfx.FillRectangle(yemrengi, x * 10, y * 10, 8, 8)
End Sub
Public Function yilan_esit_yem() As Boolean
Dim b As Boolean = False
For i = 0 To (uz - 1)
If yem.X = alanX(j - i) And yem.Y = alanY(j - i) Then
b = True
Else
b = False
End If
Next
Return b
End Function
Public Sub yem_koord_yerlestir()
Dim rndX As New Random
yem.X = rndX.Next(38) + 1
Thread.Sleep(25)
Dim rndY As New Random
yem.Y = rndY.Next(38) + 1
yemvar = True
End Sub
Public Function yilan_kendisine_carptimi() As Boolean
Dim b As Boolean = False
Dim x1 As Integer = x * 10
Dim y1 As Integer = y * 10
Try
If rota = 1 Then
If (bmp.GetPixel(x1 + 5, y1 + 5) = bmp.GetPixel(x1 - 5, y1 + 5)) Then
b = True
Else
b = False
End If
End If
If rota = 2 Then
If (bmp.GetPixel(x1 + 5, y1 + 5) = bmp.GetPixel(x1 + 15, y1 + 5)) Then
b = True
Else
b = False
End If
End If
If rota = 3 Then
If (bmp.GetPixel(x1 + 5, y1 + 5) = bmp.GetPixel(x1 + 5, y1 - 5)) Then
b = True
Else
b = False
End If
End If
If rota = 4 Then
If (bmp.GetPixel(x1 + 5, y1 + 5) = bmp.GetPixel(x1 + 5, y1 + 15)) Then
b = True
Else
b = False
End If
End If
Catch ex As Exception
Dim str As String = ex.Message.ToString()
Return False
End Try
Return b
End Function
Public Sub game_reset()
ReDim alanX(999)
ReDim alanY(999)
puan = 0
End Sub
End Class
Yılan Classımıza bunları ekledikten sonra formumuza Menustrip, StatusStrip, Pictureboxve iki adette Timer ekliyoruz. MenuStripte Oyun sekmesi oluşturup altına Başlat ve Durdur seçeneklerini yazıyoruz. Timerın birini Oyun diğerini ise Bekle olarak adlandırıyoruz ve pictureboxımızın boyunu formla eşitliyoruz. Klavyeden yılanı hareket ettirebilmemiz için formun KeyPreview özelliğini True yapıyoruz ve formumuzun arkasında çalışacak kodlarımız ise;
Public Class Form1
Dim yilan As New Yilan
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
yilan.Load_Graphic()
yilan.uz = 4
yilan.j = 4
yilan.x = 10
yilan.y = 10
yilan.yem.X = -1
yilan.yem.Y = -1
If (yilan.bmp Is Nothing) = False Then
yilan.yilangolgesi = Brushes.DarkGreen
yilan.background = Brushes.Black
yilan.yilanrengi = Brushes.Green
PictureBox1.Image = yilan.bmp
PictureBox1.Refresh()
End If
yilan.rota = 2
yilan.yemvar = False
yilan.yemgolgesi = Brushes.DarkRed
yilan.yemrengi = Brushes.Red
yilan.puan = 0
yilan.bekleint = 5
End Sub
Private Sub Oyun_timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Oyun_timer.Tick
If (yilan.yilan_esit_yem() = True) Then
yilan.yem.X = -1
yilan.yem.Y = -1
yilan.yemvar = False
yilan.puan += 1
ToolStripStatusLabel1.Text = " PUAN : " + yilan.puan.ToString()
yilan.uz += 1
End If
If yilan.rota = 1 Then
yilan.move_yilan_left()
End If
If yilan.rota = 2 Then
yilan.move_yilan_right()
End If
If yilan.rota = 3 Then
yilan.move_yilan_up()
End If
If yilan.rota = 4 Then
yilan.move_yilan_down()
End If
yilan.Draw_Yilan(yilan.x, yilan.y)
yilan.alanX(yilan.j) = yilan.x
yilan.alanY(yilan.j) = yilan.y
yilan.j += 1
yilan.Bos_Draw_Yilan(yilan.alanX(yilan.j - yilan.uz), yilan.alanY(yilan.j - yilan.uz))
If (yilan.j > 899) Then
yilan.j = yilan.uz
For z = 0 To yilan.uz
yilan.alanX(yilan.uz - z) = yilan.alanX(900 - z)
yilan.alanY(yilan.uz - z) = yilan.alanY(900 - z)
Next
End If
If ((yilan.yilan_esit_yem() = False) And (yilan.yemvar = False)) Then
yilan.yem_koord_yerlestir()
yilan.yem_koy(yilan.yem.X, yilan.yem.Y)
End If
PictureBox1.Image = yilan.bmp
PictureBox1.Refresh()
If (yilan.yilan_kendisine_carptimi() = True) Then
Oyun_timer.Enabled = False
Bekle_timer.Enabled = True
If yilan.bekleint <= 0 Then
Else
yilan.bekleint = 20
End If
End If
If (yilan.x > 40) Then
yilan.x = -1
End If
If (yilan.x < -1) Then
yilan.x = 40
End If
If (yilan.y > 40) Then
yilan.y = -1
End If
If (yilan.y < -1) Then
yilan.y = 40
End If
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Left Then
yilan.rota = 1
End If
If e.KeyCode = Keys.Right Then
yilan.rota = 2
End If
If e.KeyCode = Keys.Up Then
yilan.rota = 3
End If
If e.KeyCode = Keys.Down Then
yilan.rota = 4
End If
If e.KeyCode = Keys.Escape Then
Application.Exit()
End If
yilan.bekleint = 20
Bekle_timer.Enabled = False
Oyun_timer.Enabled = True
End Sub
Private Sub BaşlatToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BaşlatToolStripMenuItem.Click
Oyun_timer.Enabled = True
End Sub
Private Sub DurdurToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DurdurToolStripMenuItem.Click
Oyun_timer.Enabled = False
End Sub
Private Sub Bekle_timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bekle_timer.Tick
yilan.bekleint += -1
If yilan.bekleint = 0 Then
Bekle_timer.Enabled = False
Game_Over()
End If
End Sub
Public Sub Game_Over()
Oyun_timer.Enabled = False
MessageBox.Show("Oyun Bitti. " + ToolStripStatusLabel1.Text)
yilan.game_reset()
yilan.Load_Graphic()
yilan.yemvar = False
Oyun_timer.Enabled = True
End Sub
End Class