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);
}
}
}
参考: