天天看點

C# 坦克遊戲大戰中學習相關類(Rectangle)Rectangle 構造函數

C# 坦克遊戲大戰,老王類Boss代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using TankCar.Properties;

namespace TankCar
{
    class Boss:Coordination
    {
        //構造函數
        public Boss(int x,int y)
            :base(x,y)
        {

        }

        //畫牆函數
        public void Draw(Graphics g)
        {
            g.DrawImage(Resources.Boss, X, Y, 30, 30);
        }

        //擷取圖檔的矩形
        public Rectangle GetRectangle()
        {
            return new Rectangle(this.X, this.Y, 30, 30);
        }

        public void DrawGameOver(Graphics g)
        {
            g.DrawImage(Resources.GameOver, 0, 0, 390, 390);
        }
    }
}
           

Rectangle 構造函數

用指定的位置和大小初始化 Rectangle 類的新執行個體。

重載

Rectangle(Point, Size) 用指定的位置和大小初始化 Rectangle 類的新執行個體。
Rectangle(Int32, Int32, Int32, Int32) 用指定的位置和大小初始化 Rectangle 類的新執行個體。

Rectangle(Point, Size)

用指定的位置和大小初始化 Rectangle 類的新執行個體。

public Rectangle (System.Drawing.Point location, System.Drawing.Size size);
           

參數:location Point  Point,它表示矩形區域的左上角。size Size  Size,它表示矩形區域的寬度和高度。

Rectangle(Int32, Int32, Int32, Int32)

用指定的位置和大小初始化 Rectangle 類的新執行個體。

public Rectangle (int x, int y, int width, int height);
           

參數:x Int32 矩形左上角的 x 坐标。y Int32 矩形左上角的 y 坐标。width Int32 矩形的寬度。height Int32 矩形的高度。

示例

下面的代碼示例示範了 Rectangle 、 Intersect 、 IsEmpty 和 IntersectsWith 成員。 此示例應與 Windows 窗體一起使用。 将此代碼粘貼到窗體中,并在處理窗體事件時調用此方法,并将 Paint 

e

 作為傳遞 PaintEventArgs 。

private void InstanceRectangleIntersection(PaintEventArgs e)
{

    Rectangle rectangle1 = new Rectangle(50, 50, 200, 100);
    Rectangle rectangle2 = new Rectangle(70, 20, 100, 200);

    e.Graphics.DrawRectangle(Pens.Black, rectangle1);
    e.Graphics.DrawRectangle(Pens.Red, rectangle2);

    if (rectangle1.IntersectsWith(rectangle2))
    {
        rectangle1.Intersect(rectangle2);
        if (!rectangle1.IsEmpty)
        {
            e.Graphics.FillRectangle(Brushes.Green, rectangle1);
        }
    }
}
           

參考:

1、c#坦克大戰,有牆體有爆炸等效果