C# ile yılan oyunu nasıl tam yapılır?

8keyd

Hectopat
Katılım
7 Eylül 2021
Mesajlar
457
Çözümler
2
Daha fazla  
Sistem Özellikleri
ASUS TUF F15 FX507ZV
Cinsiyet
Erkek
Ben C# ile yılan oyunu yapmaya çalışıyorum ve bu ilk oyun tarzı kodlamam fakat bir hata oluştu. Bu hatada yılanın boyutunu nasıl büyüteceğim?

Kod:
[
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace yılan_oyunu
{
    public partial class form2 : Form
    {
        //Public ile olusturulanlar başka formda kullanılıyor.
        public int buyukluk;
        public string dil = "";
        public string yukari;
        public string asagi;
        public string saga;
        public string sola;
        public string form1durdur;

        public form2()
        {
            InitializeComponent();
        }

        //Değişkenler
        Random random = new Random();
        private int _yilanParcaSayisi = 0;
        private int _yilanBoyutu = 20;
        private int _yemBoyutu = 20;
        private int timer_Sure_cs = 1;
        private Label _yilanKafasi;
        private Button _yem;
        private hareketYonu _yon;
        private int puan = 0;
        private bool durdur = false;

        private Label yilanOlustur(int locationX, int locationY)
        {
            _yilanParcaSayisi++;
            Label lbl = new Label()
            {
                Name = "yilanParca" + _yilanParcaSayisi,
                BackColor = Color.Blue,
                Width = _yilanBoyutu,
                Height = _yilanBoyutu,
                Location = new Point(locationX, locationY),
                Enabled = false,
            };
            this.Controls.Add(lbl);
            return lbl;
        }
        private void yilaniYerlestir()
        {
            _yilanKafasi = yilanOlustur(0, 0);
            _yilanKafasi.Text = ":";
            _yilanKafasi.TextAlign = ContentAlignment.MiddleCenter;
            _yilanKafasi.ForeColor = Color.White;
            var locationX = (this.Width / 2) - _yilanBoyutu;
            var locationY = (this.Height / 2) - _yilanBoyutu;
            _yilanKafasi.Location = new Point(locationX, locationY);
            _yilanKafasi.Enabled = false;
        }
        private void yemOlustur()
        {
            Button lbl = new Button()
            {
                Name = "yem",
                BackColor = Color.Red,
                Width = _yemBoyutu,
                Height = _yemBoyutu,
                FlatStyle = FlatStyle.Popup,
                Enabled = false,
            };
            _yem = lbl;
            yeminYeriniDegistir();
            this.Controls.Add(lbl);
        }
        private void yeminYeriniDegistir()
        {
            bool durum = false;
            var locationX = random.Next(0, this.Width - _yemBoyutu);
            var locationY = random.Next(0, this.Width - _yemBoyutu);
            do
            {
                var rect1 = new Rectangle(_yem.Location, _yem.Size);
                foreach (Control control in this.Controls)
                {
                    if (control is Label && control.Name.Contains("yilanParca"))
                    {
                        var rect2 = new Rectangle(control.Location, control.Size);
                        if (rect1.IntersectsWith(rect2))
                        {
                            durum = true;
                        }
                    }
                }
            } while (durum);
            _yem.Location = new Point(locationX, locationY);
        }
        private void Basla()
        {
            timerSure.Start();
            yemOlustur();
            timerHareket.Start();
            yilaniYerlestir();
        }
        private enum hareketYonu { _yukari, _asagi, _saga, _sola}
        private void form2_KeyDown(object sender, KeyEventArgs e)
        {
            var keycode = e.KeyCode;

            if (yukari == keycode.ToString())
            {
                _yon = hareketYonu._yukari;
            }
            if (asagi == keycode.ToString())
            {
                _yon = hareketYonu._asagi;
            }
            if (saga == keycode.ToString())
            {
                _yon = hareketYonu._saga;
            }
            if (sola == keycode.ToString())
            {
                _yon = hareketYonu._sola;
            }
            if (_yon == hareketYonu._yukari && asagi == keycode.ToString() ||
                _yon == hareketYonu._asagi && yukari == keycode.ToString() ||
                _yon == hareketYonu._sola && saga == keycode.ToString() ||
                _yon == hareketYonu._saga && sola == keycode.ToString()
                )
            {
                return;
            }
            if(form1durdur == keycode.ToString())
            {
                switch (durdur)
                {
                    case false:
                        timerHareket.Stop();
                        timerSure.Stop();
                        durdur = true;
                        break;
                    case true:
                        timerHareket.Start();
                        timerSure.Start();
                        durdur = false;
                        break;
                }
            }
        }
        private void yilaniHareketEttir()
        {
            var locationX = _yilanKafasi.Location.X;
            var locationY = _yilanKafasi.Location.Y;
            switch (_yon)
            {
                case hareketYonu._yukari:
                    _yilanKafasi.Location = new Point(locationX, locationY - (_yilanBoyutu + 2));
                    break;
                case hareketYonu._asagi:
                    _yilanKafasi.Location = new Point(locationX, locationY + (_yilanBoyutu + 2));
                    break;
                case hareketYonu._saga:
                    _yilanKafasi.Location = new Point(locationX + (_yilanBoyutu + 2), locationY);
                    break;
                case hareketYonu._sola:
                    _yilanKafasi.Location = new Point(locationX - (_yilanBoyutu + 2), locationY);
                    break;
            }
        }
        private void form2_Load(object sender, EventArgs e)
        {
            lbl_Puan.ForeColor = Color.White;
            lbl_Sure.ForeColor = Color.White;
            Basla();
        }

        private void timerSure_Tick(object sender, EventArgs e)
        {
            lbl_Sure.Text = (timer_Sure_cs++).ToString();
        }

        private void timerHareket_Tick(object sender, EventArgs e)
        {
            yilaniHareketEttir();
            yilanYedimi();
            if(_yilanParcaSayisi > 1)
            {

            }
        }

        private void yilanYedimi()
        {
            var rect1 = new Rectangle(_yem.Location, _yem.Size);
            var rect2 = new Rectangle(_yilanKafasi.Location, _yilanKafasi.Size);
            if (rect1.IntersectsWith(rect2))
            {
                var locationX = random.Next(0, this.Width - _yemBoyutu);
                var locationY = random.Next(0, this.Width - _yemBoyutu);
                _yem.Location = new Point(locationX, locationY);
                lbl_Puan.Text = (puan += 10).ToString();
                yilanOlustur(_yilanBoyutu+50,  _yilanBoyutu+50);
            }
        }
        private void yilanUzatma()
        {
            if (_yilanParcaSayisi <= 1) return;

            for (int i = _yilanParcaSayisi; i > 1; i--)
            {
               
            }

        }
    }
}
]
 
Son düzenleyen: Moderatör:

Technopat Haberler

Geri
Yukarı