首页 > 代码库 > 绘图部分

绘图部分

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Test01
{
public partial class MainForm : Form
{

public MainForm()
{

InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
FrameTimer.Interval = Common.UPDATE_INTERVAL;

buttonStateList.Capacity = Keys.D9 - Keys.D0;
for (int i = 0; i < buttonStateList.Capacity; ++i )
{
buttonStateList.Add(0);
}

gs = Graphics.FromImage(bmp);
ps = new Point(10000,10000);
Matrix trans = new Matrix(1f,0,0,1f,0,0);
gs.MultiplyTransform(trans);
gs.SmoothingMode = SmoothingMode.AntiAlias;
}
static int tx = 0, ty =-600;
static Graphics gs;
static Point ps;
Bitmap bmp = new Bitmap(3000,3000);

private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
if (Keys.D1 <= e.KeyCode && e.KeyCode <= Keys.D9)
{
buttonStateList[e.KeyCode - Keys.D1] = 1;
}
}
private void FrameTimer_Tick(object sender, EventArgs e)
{
this.OnFrame();
List<Unit> unitlist = BattleField.Instance.unitList;
gs.Clear(Color.White);
sx = -1;
ps.X = 10000; ps.Y = 10000;
foreach (Unit unit in unitlist)
{
sx++;
if (!unit.IsAlive()) continue;
Vec2i points = unit.GetPosProp().getPos();
drs(unit, 800 + points.x, points.y - 440);

}
this.CreateGraphics().DrawImage(bmp, 0, 0);
}

void OnFrame()
{
for (int i = 0; i < buttonStateList.Count; ++i)
{
RenderLayerCmd cmd = new RenderLayerCmd();
if (buttonStateList[i] > 0)
{
if (i < 5)
{
cmd.cmd = CmdType.SKILL;
cmd.unitId = BattleField.Instance.unitList[i].guid;
AppEntrance.PushCommand(cmd);
}
else
{
switch (i + 1)
{
case 7:
cmd.cmd = CmdType.PAUSE;
AppEntrance.PushCommand(cmd);
break;
case 8:
cmd.cmd = CmdType.RESUME;
AppEntrance.PushCommand(cmd);
break;
case 9:
cmd.cmd = CmdType.NEXT_REGION;
AppEntrance.PushCommand(cmd);
break;
}
}

buttonStateList[i] = 0;
}
}

AppEntrance.Update();
}

List<int> buttonStateList = new List<int>();
public delegate void drawPos(Unit unit,int x,int y);
private static int sx=0;
private static int buffi = 0;
public static drawPos drs = new drawPos(delegate(Unit unit,int x, int y)
{

string id = unit.unitConfig.name;
int a = unit.GetTeamId() % 2;
int hp = unit.fightingProperty.getValue(PropertyType.HP), mp = unit.fightingProperty.getValue(PropertyType.MP);
Brush s=System.Drawing.Brushes.Red;
Pen p = new Pen(s);
if (a == 0) { s = System.Drawing.Brushes.Blue; p = new Pen(s); a = -3; }
if (unit.enemy != null&&unit.enemy.IsAlive())
{

p.EndCap = LineCap.ArrowAnchor;
Point p1 = new Point(tx + unit.GetPosProp().getPos().x / 4 + 20 + a, ty + unit.GetPosProp().getPos().y / 4 - 70 + a);
Point p2 = new Point(tx + unit.enemy.GetPosProp().getPos().x / 4 + 20 + a, ty + unit.enemy.GetPosProp().getPos().y / 4 - 70 + a);
if (unit.ai.aiState == AIState.SKILL_CASTING)
{
gs.DrawLine(p, p1, p2);
}
}
foreach(Buff buff in unit.buffList.dataList)
{
Random ran =new Random((int)(buff.type)*171271);
Color color = Color.FromArgb(ran.Next(0, 255), ran.Next(0, 255), ran.Next(0, 255));
Brush sd = new SolidBrush(color);
gs.DrawString("★", new Font("黑体", 6), sd, new PointF(tx+x / 4 - 200+buffi*6,ty+ y / 4 + 74));
buffi++;
//gs.TranslateTransform(-x, -y);
gs.DrawString(buff.type.ToString(), new Font("宋体", 8), sd, new PointF( 700 + sx * 160 - sx / 5 * 800, 92 - buffi * 10 + sx / 5 * 400));
//gs.TranslateTransform(x, y);
}
buffi = 0;
//if ( ps.X > x / 4 - 200) ps.X = x / 4 - 200;
//if ( ps.Y > y / 4 + 30) ps.X = y / 4 + 30;
//tx = -ps.X + 100; ty = -ps.Y + 100;
gs.DrawString("?", new Font("黑体", 16), s, new PointF(tx + x / 4 - 200, ty + y / 4 + 30));
gs.DrawString(unit.fightingProperty.getValue(PropertyType.DAMAGE).ToString(), new Font("黑体", 8), s, new PointF(tx + x / 4 - 180, ty + y / 4 + 32));
gs.DrawString(hp.ToString() + "," + mp.ToString(), new Font("宋体", 8), s, new PointF(tx + x / 4 - 200, ty + y / 4 + 65));
gs.DrawString(id, new Font("宋体", 8), s, new PointF(tx + x / 4 - 180, ty + y / 4 + 46));
gs.DrawString(x.ToString() + "," + y.ToString(), new Font("宋体", 8), s, new PointF(tx + x / 4 - 200, ty + y / 4 + 54));
foreach (Vec2i v in RevengeArrow.list)
{
gs.DrawString("*", new Font("黑体", 16), s, new PointF(tx + v.x / 4, ty + v.y / 4 - 80));
}
bool t = true;

for (int j = 0; j < (int)PropertyType.END; j++)
{
if (unit.fightingProperty.getValue((PropertyType)j) != zu[sx,j])
{
if ((PropertyType)j==PropertyType.HP)
cha[sx,j] = unit.fightingProperty.getValue((PropertyType)j) - zu[sx,j];
zu[sx,j] = unit.fightingProperty.getValue((PropertyType)j);
}
if (t) { t = false; gs.DrawString(id, new Font("宋体", 8), s, new PointF(700 + sx * 160 - sx / 5 * 800,92 + j * 10 + sx / 5 * 400)); }
gs.DrawString(((PropertyType)j).ToString() + ":" + zu[sx,j]+"("+cha[sx,j]+")",
new Font("宋体", 8),s, new PointF(700+sx*160-sx/5*800, 100 + j * 10+sx/5*400));
}

});
public static int[,] zu = new int[20,(int)PropertyType.END];
public static int[,] cha = new int[20,(int)PropertyType.END];
public Dictionary<int, int> notifyProperty = new Dictionary<int, int>();
private void MainForm_Load(object sender, EventArgs e)
{

}

private void MainForm_Paint(object sender, PaintEventArgs e)
{

}

private void MainForm_MouseDoubleClick(object sender, MouseEventArgs e)
{
Unit curunit;
Vec2i vec2i = new Vec2i(e.X,e.Y);
int mindist = int.MaxValue;

for (int i=0;i<BattleField.Instance.unitList.Count;i++)
{
int dist =vec2i.dist2(BattleField.Instance.unitList[i].GetPosProp().getPos());
if (dist<mindist)
{
mindist = dist;
curunit = BattleField.Instance.unitList[i];
}
}

}
}
}

绘图部分