Neredeyse tüm oyunların mantığını oluşturan bir uygulamayla karşınızdayız. Rastgele ve farklı konumlarda üretilen resimleri tıklayarak form sayfasından kaldırma uygulamasını yapacağız. Form sayfasına bir adet Label, bir adet Timer ekleyelim. Form sayfasına aşağıdaki kodları yazalım.
Random rand =new Random();
List<PictureBox> items = new List<PictureBox>();
private void MakePictureBox()
{
PictureBox newPic = new PictureBox();
newPic.Width = 50;
newPic.Height = 50;
newPic.BackColor = Color.Blue;
int x = rand.Next(10, this.ClientSize.Width - newPic.Width);
int y = rand.Next(10, this.ClientSize.Height - newPic.Height);
newPic.Location = new Point(x, y);
newPic.Click += NewPic_Click;
items.Add(newPic);
this.Controls.Add(newPic);
}
private void NewPic_Click(object sender, EventArgs e)
{
PictureBox temPic = sender as PictureBox;
items.Remove(temPic);
this.Controls.Remove(temPic);
label1.Text = "Resim :" + items.Count();
}
private void timer1_Tick(object sender, EventArgs e)
{
MakePictureBox();
label1.Text = "Resim :" + items.Count();
}
|