C# ile zar oyunu

Rast gele sayı tutturarak bir oyun yapmak istedim ve aklıma biz zar oyunu yapmak geldi. Bunu için gerekli form tasarımı aşağıdaki gibidir.

form

Ortada iki adet picturebox kullandım. Böylece atılan zarları kullanıcılara sayı olarak değilde resim olarak gösterebilirim. İsteyenler oraya label kullanarak sayıları yazdırabilirler.

Ayrıca burada gösterilecek olan zar resimlerini de

Belgelerim\Visual Studio 2010\Projects\zar oyunu\zar oyunu\bin\Debug\gorseller

klasörüne tek tek kopyaladım. Her sayı için bir resim yani.

Tasarımı yaptıktan sonra kodlamaya geçtim. Kodlar şu şekilde;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace zar_oyunu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //global değişken tanımlanıyor
        Random rastgele = new Random() ;
        int Oyuncu1Toplam, Oyuncu2Toplam = 0;
        int a, b;
        private void zar_at()
        {
            //işlem başlıyor....
            a = rastgele.Next(1, 7);
            b = rastgele.Next(1, 7);
            //birinci zarı göster
            if (a == 1)
                pictureBox1.ImageLocation = "gorseller\\1.png";
            if (a == 2)
                pictureBox1.ImageLocation = "gorseller\\2.png";
            if (a == 3)
                pictureBox1.ImageLocation = "gorseller\\3.png";
            if (a == 4)
                pictureBox1.ImageLocation = "gorseller\\4.png";
            if (a == 5)
                pictureBox1.ImageLocation = "gorseller\\5.png";
            if (a == 6)
                pictureBox1.ImageLocation = "gorseller\\6.png";
                //İkinci zarı göster
            if (b == 1)
                pictureBox2.ImageLocation = "gorseller\\1.png";
            if (b == 2)
                pictureBox2.ImageLocation = "gorseller\\2.png";
            if (b == 3)
                pictureBox2.ImageLocation = "gorseller\\3.png";
            if (b == 4)
                pictureBox2.ImageLocation = "gorseller\\4.png";
            if (b == 5)
                pictureBox2.ImageLocation = "gorseller\\5.png";
            if (b == 6)
                pictureBox2.ImageLocation = "gorseller\\6.png";
        }
        private void skor_kontrol() //hangi oyuncu kazandıysa onu belirleyen fonksiyon
        {
            if (Oyuncu1Toplam >= Convert.ToInt16(textBox1.Text))
            {
                label7.Visible = true;
                label7.Text = "1. Oyuncu Kazandı...";
                button3.Visible = true;
                button2.Enabled = false;
            }
            if (Oyuncu2Toplam >= Convert.ToInt16(textBox1.Text))
            {
                label7.Visible = true;
                label7.Text = "2. Oyuncu Kazandı...";
                button3.Visible = true;
                button1.Enabled = false;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
            pictureBox1.Visible = true;
            pictureBox2.Visible = true; 
            button1.Enabled = false;
            button2.Enabled = true;
            zar_at();
            Oyuncu1Toplam = Oyuncu1Toplam + a + b;
            label4.Text = Oyuncu1Toplam.ToString();
            skor_kontrol();
         }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = false; 
            pictureBox1.Visible = true;
            pictureBox2.Visible = true;
            button2.Enabled = false;
            button1.Enabled = true;
            //işlem başlıyor....
            zar_at();
            Oyuncu2Toplam = Oyuncu2Toplam + a + b;
            label5.Text = Oyuncu2Toplam.ToString();
            skor_kontrol();            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = true;
            textBox1.Text = "100";
            button1.Enabled = true;
            button2.Enabled = true;
            label7.Visible = false;
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            Oyuncu1Toplam = 0;
            Oyuncu2Toplam = 0;
            label4.Text = "0";
            label5.Text = "0";
            button3.Visible = false;
        }
    }
}

Projeyi buradan indirebilirsiniz.

Dosyayı indirip açınca karşınıza form yada kodlar gelmezse sağ tarafta bulunan Solution Explorer sekmesinde form1.cs’yi tıklayınız.

7 yorum

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir