C# form objelerin birbirine değdiğini nasıl kontrol edebiliriz?

FireHawk

Hectopat
Katılım
3 Ekim 2020
Mesajlar
339
Çözümler
5
Daha fazla  
Cinsiyet
Erkek
Visual Studio C# form da oyun yapıyorum. Oyunumun resmini yükledim.

sas.png
Amacım bu lazerlerin karşıda ki düşman gemisine çarpıp değdiğini anlamam.
Eskiden bunu başarmıştım ama nedense şu anda olmuyor.

Sizden bunun nedeni nedir öğrenmek isterim. Cevap verenlere teşekkür ederim.
Resim 3,4 lazer, resim 2 gemimiz, resim 5 düşman gemi.
Lazerin boyutu 30 40, düşman geminin boyutu 175 200.
C#:
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 WinFormsApp1
{
    public partial class Form1 : Form
    {
       // basılan tuşların değerleri
        public bool w = false;
        public bool s = false;
        public bool a = false;
        public bool d = false;
        public bool space = false;
        //kontrol sayacı
        int sayac=5;
        //düşman gemisinin ve lazerlerin y ve x koordinatlarının değerleri
        int x3, x4, x5, y3, y4, y5;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //tuşları kontrol etme
            switch (e.KeyCode)
            {
                case Keys.W: w = true; break;
                case Keys.S: s = true; break;
                case Keys.A: a = true; break;
                case Keys.D: d = true; break;
                case Keys.Space: space = true; break;
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            //tuşları kontrol etme
            switch (e.KeyCode)
            {
                case Keys.W: w = false; break;
                case Keys.S: s = false; break;
                case Keys.A: a = false; break;
                case Keys.D: d = false; break;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            //lazerlerin görünürlüğünü kapatma
            pictureBox3.Visible = false;
            pictureBox4.Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //gemimizin hareketi
            if (w == true && pictureBox2.Location.Y > 0)
            {
                pictureBox2.Top -= 20;
                //gemimizle beraber hareket eden lazerler
                if (space==false)
                {
                    pictureBox3.Top -= 20;
                    pictureBox4.Top -= 20;
                }
            }
            if (s == true && pictureBox2.Location.Y < 1400)
            {
                pictureBox2.Top += 20;
                if (space==false)
                {
                    pictureBox3.Top += 20;
                    pictureBox4.Top += 20;
                }
            }
            if (a == true &&pictureBox2.Location.X > 0)
            {
                pictureBox2.Left -= 20;
                if (space==false)
                {
                    pictureBox3.Left -= 20;
                    pictureBox4.Left -= 20;
                }
            }
            if (d == true && pictureBox2.Location.X < 1262)
            {
                pictureBox2.Left += 20;
                if (space == false)
                {
                    pictureBox3.Left += 20;
                    pictureBox4.Left += 20;
                }
            }
            //lazer ateşlenmesi
            if (space==true)
            {
                pictureBox3.Visible = true;
                pictureBox4.Visible = true;
                pictureBox3.Top -= 120;
                pictureBox4.Top -= 120;
                label2.Text = Convert.ToString(pictureBox3.Location.Y);
                label3.Text = Convert.ToString(pictureBox3.Location.X);
                if (pictureBox3.Location.Y<0 && pictureBox4.Location.Y<0)
                {
                    pictureBox3.Visible = false;
                    pictureBox4.Visible = false;
                    pictureBox3.Top = pictureBox2.Location.Y;
                    pictureBox4.Top = pictureBox2.Location.Y;
                    pictureBox3.Left = pictureBox2.Location.X+117;
                    pictureBox4.Left = pictureBox2.Location.X+27;
                    space = false;
                }
            }
            //düşman geminin hareketi
            pictureBox5.Top += 20;
            sayac++;
            label1.Text = Convert.ToString(sayac);
            x3 = pictureBox3.Left;
            x4 = pictureBox4.Left;
            x5 = pictureBox5.Left;
            y3 = pictureBox3.Top;
            y4 = pictureBox4.Top;
            y5 = pictureBox5.Top;

            //değmeyi kontrol etme
            if (x3 + 20 >= x5 - 20 && x3 - 20 <= x5 + 20 && y3 + 20 >= y5 - 20 && y3 - 20 <= y5 + 20)
            {
                label4.Text = "sağdaki lazer değdi";
            }
            if (x4 + 20 >= x5 - 20 && x4 - 20 <= x5 + 20 && y4 + 20 >= y5 - 20 && y4 - 20 <= y5 + 20)
            {
                label5.Text = "soldaki lazer değdi";
            }

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

Technopat Haberler

Geri
Yukarı