C# İki Kişilik Oyun - Bilişim Konuları

C# İki Kişilik Oyun

C# programında yapılmış ve iki kişi birlikte oynayabileceği bir oyun programı yaptık. Oyunumuzda iki kişi karşılıklı klavye kullanarak oynayabilir. İki oyuncuyu temsil eden iki tane button nensesi ve hedefi temsil eden bir tane button nesnesi bulunmaktadır. Hedef ekranda rastgele herhangi bir yerde 3 er saniye aralıklarla çıkmaktadır. Oyuncular ise klavye tuşlarını kullanarak bu hedefi yakalamakla görevlidir. Hedefi ilk yakalayan oyuncu oyunu kazanıyor ve oyun bitiyor. Bu oyun için iki tanede timer nesnesi kullanıldı. Timer nesnesinden bir tanesi oyuncuları hareket ettirmek için, diğer timer nesnesi de hedefin rastgele bir yerden çıkmasını sağlamak için kullanıldı. Ayrıca hedefin ve oyuncuların koordinatlarını gösteren bir tabloda ekranın sol alt kısmına yerleştirildi. Programın çalışan bir exe dosyası ektedir.

oyun

Programın C# Görüntüleri:

csharp-oyun1

csharp-oyun1

csharp-oyun2

csharp-oyun2

Programın C# Kodları:

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 WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Random s=new Random();
        int x = 440, y = 240;

        protected override bool ProcessDialogKey(Keys keyData)
        {
 
            if (keyData == Keys.Left && button1.Left > 0)
                button1.Location = new Point(button1.Left - 10, button1.Top);
            else if (keyData == Keys.Right && button1.Right < this.Size.Width)
                button1.Location = new Point(button1.Left + 10, button1.Top);    
            else if (keyData == Keys.Up && button1.Top > 0)
                button1.Location = new Point(button1.Left, button1.Top - 10);    
            else if (keyData == Keys.Down && button1.Bottom < this.Size.Height - 50)
                button1.Location = new Point(button1.Left, button1.Top + 10);
       
            else if (keyData == Keys.A && button1.Left > 0)
                button2.Location = new Point(button2.Left - 10, button2.Top);
  
            else if (keyData == Keys.D && button2.Right < this.Size.Width)
                button2.Location = new Point(button2.Left + 10, button2.Top);
       
            else if (keyData == Keys.W && button2.Top > 0)
                button2.Location = new Point(button2.Left, button2.Top - 10);
       
            else if (keyData == Keys.S && button2.Bottom < this.Size.Height - 50)
                button2.Location = new Point(button2.Left, button2.Top + 10); 

            return base.ProcessDialogKey(keyData);
        }

        
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            timer1.Interval = 5000;

            timer2.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            x = s.Next(0, 880);
            y = s.Next(0, 500);
            button3.Location = new Point(x, y);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            int o1y = button1.Top;
            int o1x = button1.Left;

            int o2y = button2.Top;
            int o2x = button2.Left;

            int hedefy = button3.Top;
            int hedefx = button3.Left;





      /*    label2.Text = o1x.ToString();
            label3.Text = o1y.ToString();

            label5.Text = o2x.ToString();
            label4.Text = o2y.ToString();

            label8.Text = hedefx.ToString();
            label7.Text = hedefy.ToString();  */

            int a1x1 = o1x + 15;
            int a1x2 = o1x - 15;

            int a1y1 = o1y + 15;
            int a1y2 = o1y - 15;

            int a2x1 = o2x + 15;
            int a2x2 = o2x - 15;

            int a2y1 = o2y + 15;
            int a2y2 = o2y - 15;



            if ((a1x1 > hedefx && a1x2 < hedefx) && (a1y1 > hedefy && a1y2 < hedefy))
            {
                timer1.Stop();
                timer2.Stop();
                MessageBox.Show("1. Oyuncu kazandı");
            }
            if ((a2x1 > hedefx && a2x2 < hedefx) && (a2y1 > hedefy && a2y2 < hedefy))
            {
                timer1.Stop();
                timer2.Stop();
                MessageBox.Show("2. Oyuncu kazandı");
            }
        }
        
    }
}
Bu Yazıya Tepkin Nedir?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Yorum Yap

2 Yorum