效果图不到350行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication87
{
    /// <summary>
    /// Made by wartim 2009.10.12
    /// var 1.0.0.0
    /// </summary>
    public partial class Form1 : Form
    {
        static int HEIGHT = 300;
        static int WIDTH = 300;        Bitmap OrgBmp = new Bitmap(WIDTH, HEIGHT);
        List<Tank> Tanks = new List<Tank>();
        UserTank UTank = null;
        List<Bullet> Bullets = new List<Bullet>();
        PictureBox PB = new PictureBox();        public Form1()
        {
            InitializeComponent();            this.Size = new Size(WIDTH, HEIGHT);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(Form1_KeyDown);            using (Graphics G = Graphics.FromImage(OrgBmp))
                G.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);            PB.Parent = this;
            PB.Dock = DockStyle.Fill;
            PB.Image = OrgBmp;            for (int i = 0; i < 5; i++)
            {
                Tanks.Add(new Tank(Color.Blue, this.BackColor));
                Thread.Sleep(100);
            }            UTank = new UserTank(Color.Red, this.BackColor);            Thread T = new Thread(new ThreadStart(RunThread));
            T.IsBackground = true;
            T.Start();
        }        void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up: UTank.ChangeDirection(Direction.UP); UTank.Move(); break;
                case Keys.Down: UTank.ChangeDirection(Direction.DOWN); UTank.Move(); break;
                case Keys.Left: UTank.ChangeDirection(Direction.LEFT); UTank.Move(); break;
                case Keys.Right: UTank.ChangeDirection(Direction.RIGHT); UTank.Move(); break;
                case Keys.Space: Bullets.Add(new Bullet(Color.Black, UTank)); break;  // 发射子弹
            }
        }        void RunThread()
        {
            try
            {
                int Start = Environment.TickCount;
                Random R = new Random();
                int KillCount = 0, DeathCount = 0;                while (true)
                    if (Environment.TickCount - Start > 100)
                    {
                        Bitmap CacheBmp = new Bitmap(OrgBmp);                        for (int i = 0; i < Tanks.Count; i++)
                        {
                            Tanks[i].Move();
                            Tanks[i].Draw(ref CacheBmp);
                            if (R.Next(10) == 0) // 电脑发子弹是10分之一的可能
                                Bullets.Add(new Bullet(Color.Red, Tanks[i]));
                        }
                        UTank.Draw(ref CacheBmp);
                        List<Bullet> TempBullets = new List<Bullet>();
                        for (int i = 0; i < Bullets.Count; i++)
                        {
                            if (Bullets[i].ObjStep != -1)
                            {
                                Rectangle Test = new Rectangle(Bullets[i].Postion.X - 10, Bullets[i].Postion.Y - 10, 20, 20);
                                bool IsKilled = false;
                                for (int j = 0; j < Tanks.Count; j++)
                                    if (Test.Contains(Tanks[j].Postion))
                                    {
                                        if (Bullets[i].Owner == UTank)
                                        {
                                            KillCount++;
                                            IsKilled = true;
                                            Tanks[j] = new Tank(Color.Blue, this.BackColor);
                                        }
                                        break;
                                    }
                                if (!IsKilled)
                                    if (Test.Contains(UTank.Postion))
                                        if (Bullets[i].Owner != UTank)
                                        {
                                            DeathCount++;
                                            IsKilled = true;
                                            break;
                                        }
                                if (!IsKilled)
                                    TempBullets.Add(Bullets[i]);
                            }
                        }
                        Bullets = new List<Bullet>(TempBullets);
                        for (int i = 0; i < Bullets.Count; i++)
                        {
                            Bullets[i].Move();
                            Bullets[i].Draw(ref CacheBmp);
                        }
                        Monitor.Enter(CacheBmp);
                        using (Graphics G = Graphics.FromImage(CacheBmp))
                        {
                            G.DrawString("杀敌数: " + KillCount.ToString() + " 死亡数: " + DeathCount.ToString(), this.Font,
                                Brushes.Black, new PointF(0, 0));
                        }
                        Monitor.Exit(CacheBmp);
                        this.Invoke(new SetImage(DoSetImage), new Object[] { CacheBmp });
                        Start = Environment.TickCount;
                    }
            }
            catch
            {
                // 忽略程序退出异常
            }
        }
[/code]

解决方案 »

  1.   

     delegate void SetImage(Bitmap CacheBmp);        void DoSetImage(Bitmap CacheBmp)
            {
                PB.Image = CacheBmp;
            }        public enum Direction
            {
                UP, DOWN, LEFT, RIGHT
            };        public class RunObject
            {
                protected int X = 0;
                protected int Y = 0;
                public Point Postion
                {
                    get
                    {
                        return new Point(X + 10, Y + 10);
                    }
                }
                protected int Step = 0;
                public int ObjStep
                {
                    get
                    {
                        return Step;
                    }
                }
                protected int SumStep = 0;
                Random R = new Random();
                protected Direction Dir;
                public Direction ObjDirection
                {
                    get
                    {
                        return Dir;
                    }
                }
                public Bitmap ObjBmp = null;
                protected Color ForeColor;
                protected Color BackColor;
                protected int Rate = 1;            public RunObject(Color ForeColor, Color BackColor)
                {
                    this.ForeColor = ForeColor;
                    this.BackColor = BackColor;
                    X = R.Next(WIDTH - 20);
                    Y = R.Next(HEIGHT - 20);
                }            protected void HitCheck(Direction NewDir)
                {
                    int XStep = 0;
                    int YStep = 0;                switch (NewDir)
                    {
                        case Direction.UP: YStep = -3 * Rate; break;
                        case Direction.DOWN: YStep = 3 * Rate; break;
                        case Direction.LEFT: XStep = -3 * Rate; break;
                        case Direction.RIGHT: XStep = 3 * Rate; break;
                    }
                    X += XStep;
                    Y += YStep;
                    if (X < 0)
                        X = WIDTH - ObjBmp.Width;
                    if (X > WIDTH - ObjBmp.Width)
                        X = 0;
                    if (Y < 0)
                        Y = HEIGHT - ObjBmp.Height;
                    if (Y > HEIGHT - ObjBmp.Height)
                        Y = 0;
                    // 简单起见,就不检查坦克间的碰撞了
                }            public virtual void Move()
                {
                    if (Step > SumStep)
                    {
                        Dir = (Direction)R.Next(4);
                        Step = 0;
                        SumStep = R.Next(10);
                    }
                    HitCheck(Dir);
                    Step++;
                }            public virtual void Draw(ref Bitmap Bmp)
                {
                    Monitor.Enter(Bmp);
                    using (Graphics G = Graphics.FromImage(Bmp))
                    {
                        G.DrawImage(ObjBmp, X, Y);
                    }
                    Monitor.Exit(Bmp);
                }
            }
      

  2.   

    class Tank : RunObject
            {
                public Tank(Color ForeColor, Color BackColor)
                    : base(ForeColor, BackColor)
                {
                    ObjBmp = new Bitmap(20, 20);
                }            public override void Draw(ref Bitmap Bmp)
                {
                    using (Graphics G = Graphics.FromImage(ObjBmp))
                    {
                        Color FootColor1 = Step % 2 == 0 ? Color.Teal : Color.Black;
                        Color FootColor2 = Step % 2 != 0 ? Color.Teal : Color.Black;                    G.FillRectangle(new SolidBrush(BackColor), 0, 0, ObjBmp.Width, ObjBmp.Height);
                        for (int i = 4; i < ObjBmp.Width - 4; i++)
                            switch (Dir)
                            {
                                case Direction.UP:
                                case Direction.DOWN: G.DrawLine(new Pen(i % 2 == 0 ? FootColor1 : FootColor2), 0, i, ObjBmp.Width, i); break;
                                case Direction.LEFT:
                                case Direction.RIGHT: G.DrawLine(new Pen(i % 2 == 0 ? FootColor1 : FootColor2), i, 0, i, ObjBmp.Height); break;
                            }
                        G.FillRectangle(new SolidBrush(ForeColor), 4, 4, ObjBmp.Width - 8, ObjBmp.Height - 8);
                        switch (Dir)
                        {
                            case Direction.UP: G.DrawLine(new Pen(new SolidBrush(ForeColor)),
                                ObjBmp.Width / 2, ObjBmp.Height / 2, ObjBmp.Width / 2, 0); break;
                            case Direction.DOWN: G.DrawLine(new Pen(new SolidBrush(ForeColor)),
                                ObjBmp.Width / 2, ObjBmp.Height / 2, ObjBmp.Width / 2, ObjBmp.Height); break;
                            case Direction.LEFT: G.DrawLine(new Pen(new SolidBrush(ForeColor)),
                                ObjBmp.Width / 2, ObjBmp.Height / 2, 0, ObjBmp.Height / 2); break;
                            case Direction.RIGHT: G.DrawLine(new Pen(new SolidBrush(ForeColor)),
                                ObjBmp.Width / 2, ObjBmp.Height / 2, ObjBmp.Width, ObjBmp.Height / 2); break;
                        }
                    }
                    base.Draw(ref Bmp);
                }
            }        class UserTank : Tank
            {
                public UserTank(Color ForeColor, Color BackColor)
                    : base(ForeColor, BackColor)
                {
                }            public void ChangeDirection(Direction Dir)
                {
                    this.Dir = Dir;
                }            public override void Move()
                {
                    HitCheck(Dir);
                    Step = 1 - Step;
                }
            }        class Bullet : RunObject
            {
                public Tank Owner = null;            public Bullet(Color BackColor, Tank Owner)
                    : base(BackColor, BackColor)
                {
                    ObjBmp = new Bitmap(2, 2);
                    this.X = Owner.Postion.X;
                    this.Y = Owner.Postion.Y;
                    this.Dir = Owner.ObjDirection;
                    this.Owner = Owner;
                    this.SumStep = 20;
                    this.Rate = 2;
                }            public override void Draw(ref Bitmap Bmp)
                {
                    using (Graphics G = Graphics.FromImage(ObjBmp))
                    {
                        G.FillRectangle(new SolidBrush(BackColor), 0, 0, ObjBmp.Width, ObjBmp.Height);
                    }
                    base.Draw(ref Bmp);
                }            public override void Move()
                {
                    if (Step > SumStep)
                    {
                        Step = -1;
                        return;
                    }
                    HitCheck(Dir);
                    Step++;
                }
            }
        }
    }
      

  3.   

    对了,bullet子弹的draw最好也有个moniter.enter...moniter.exit
      

  4.   

    http://download.csdn.net/source/1735780
      

  5.   

    发现这游戏真是高难度- =
    玩了不到1分钟死了10+次,杀敌1
    然后出了个错误
    ************** 异常文本 **************
    System.InvalidOperationException: 对象当前正在其他地方使用。学习下代码
      

  6.   

    为什么vs2003里编译报错:
    List <Tank> Tanks = new List <Tank>(); 
    UserTank UTank = null; 
    List <Bullet> Bullets = new List <Bullet>(); 这段报错:类、结构或接口成员声明中的 标记"<"无效