C# İLE İLERİ GERİ HAREKET EDEN TOP UYGULAMASI
C# uygulaması ile iki adet duvar ve bir adet top oluşturacağız ve topun bu duvarlar arasında gidip gelmesini sağlayacağız.
Bunun için formunuzun üzerine 3 adet PictureBox, bir adet timer ve iki adet button ekleyin. Form_Load eventine aşağıdaki kodları yazın. (form_load eventini formun boş bir yerine çift tıklayarak açabilirsiniz)
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Left = 10;
pictureBox1.Top = 80;
pictureBox1.Width = 50;
pictureBox1.Height = 200;
pictureBox2.Left = 850;
pictureBox2.Top = 80;
pictureBox2.Width = 50;
pictureBox2.Height = 200;
pictureBox3.Left = 60;
pictureBox3.Top = 120;
pictureBox3.Width = 60;
pictureBox3.Height = 60;
this.Width = 950;
this.Height = 650;
pictureBox1.ImageLocation = @"..\..\Resimler\duvar1.jpg";
pictureBox2.ImageLocation = @"..\..\Resimler\duvar1.jpg";
pictureBox3.ImageLocation = @"..\..\Resimler\Top.jpg";
}
Yukarıdaki kod bloku ile duvarları oluşturduk, topu oluşturduk, formun genişliği ve yüksekliğini ayarladık.
pictureBox1.ImageLocation = @"..\..\Resimler\duvar1.jpg";
Yukarıdaki kod satırını kullanmak için sağ tarafta Solution Explorer de uygulama adınıza sağ tıklayın Add >> New Folder adımları ile bir klasör oluşturup klasöre Resimler adını verin daha sonra bu klasöre resimleri kopyalayın.
Form üzerindeki Timer kontrolüne çift tıklayarak timer_tick eventini oluşturun bu evente aşağıdaki kodları yazın.
int left1 = 50;
int kontrol = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if(kontrol==0)
{
left1+=10;
if (left1 > 790)
{
kontrol = 1;
}
pictureBox3.Left += 10;
}
if(kontrol==1)
{
left1-=10;
if(left1<50)
{
kontrol = 0;
}
pictureBox3.Left -= 10;
}
}
Aynı şekilde 1. Buttona çift tıklayarak button eventine aşağıdaki kodları yazın.
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 60;
timer1.Start();
}
2. button üzerine çift tıklayarak click eventine aşağıdaki kodları yazın
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
Timer1.interval timer_tick eventinin içerisindeki kodların kaç milisaniyede bir çalıştırılacağını belirler.
Timer.Start() timeri çalıştırır; timer1.Stop(), timeri durdurur.
SANATSAL BİLGİ
23/08/2016