1

こんにちは、私はこの仕事に不慣れです!Rectangle と Circle の作成に成功しました。フォーム内の円と矢印キー (左右) で四角形を動かしています! 問題は、印刷できるように、これら 2 つの間の衝突を検出する方法を知る必要があることです。

  Console.WriteLine("COLLISION OCCURS!");

円と長方形の検出のみが必要です。円が Y 軸の範囲外になると、メッセージ (CIRCLE WENT OUT OF BOUNDS) が表示されます。私が試したことは次のとおりです。

  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 Paddle_Test
{
    public partial class Form1 : Form
    {
        Rectangle rec;
        Rectangle circle;
        int wLoc=0;
            int hLoc=0;
            int eWL;
            int eHL;
            int dx = 4;
            int dy = 4;

        public Form1()
        {
            InitializeComponent();
            wLoc=(this.Width) - 100;
            hLoc=(this.Height) - 100;
            eWL = 10;
            eHL = 10;
            rec = new Rectangle(wLoc,hLoc , 60, 10);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.FillRectangle(new SolidBrush(Color.Blue), rec);
            g.FillEllipse(new SolidBrush(Color.Red), eWL, eHL, 40, 40);

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Left)
            {
                rec.X -= 30;
                this.Refresh();
            }

            if (e.KeyCode==Keys.Right)
            {
                rec.X += 30;
                this.Refresh();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            eWL = eWL + dx;
            eHL = eHL + dy;

            if (eWL >= (this.Width) - 100)
            {
                dx = dx * (-1);
            }
            else if (eHL >= (this.Height) - 100)
            {
                dy = dy * (-1);
                //timer.Enabled = false;
                //MessageBox.Show("Game Over!");
            }
            else if (eWL<=0)
            {
                 dx = dx * (-1);
            }
            else if (eHL <= 0)
            {
                dy = dy * (-1);
            }
            else if (eWL == rec.X || eHL == rec.Y)   //here I'm trying to detect the collision!
            {
                dx = dx * (-1);//invert the direction x-axis
                dy = dy * (-1);//invert the direction y-axis


            }
            this.Refresh();
        }
    }
}

誰か助けてください!前もって感謝します

4

1 に答える 1