 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace TankWar{  public partial class FormMain : Form  {    #region 私有字段    //游戏状态    private GameState _gameState = GameState.Close;    //我方坦克    private Tank _myTank = new Tank(Side.Me);    //敌方坦克集合    private List<Tank> _listEnemyTank = new List<Tank>();    //子弹的集合    private List<Bullet> _listBullet = new List<Bullet>();    #endregion    //导入动态链接库    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]    public static extern short GetAsyncKeyState(int keyCode);    public FormMain()    {      InitializeComponent();    }    //开始游戏    private void BeginToolStripMenuItem_Click(object sender, EventArgs e)    {      _gameState = GameState.Open;      //启用timer      timer1.Enabled = true;      timer2.Enabled = true;      timer3.Enabled = true;      timer4.Enabled = true;    }    //结束游戏    private void EndToolStripMenuItem_Click(object sender, EventArgs e)    {      _gameState = GameState.Close;      //禁用timer      timer1.Enabled = false;      timer2.Enabled = false;    }    private void pictureBox1_Paint(object sender, PaintEventArgs e)    {      //绘制我方      _myTank.DrawMe(e.Graphics);      //绘制敌方      for (int i = 0; i <= _listEnemyTank.Count - 1; i )        _listEnemyTank[i].DrawMe(e.Graphics);      foreach (Bullet myBullet in _listBullet)      {        myBullet.DrawMe(e.Graphics);      }    }    private void FormMain_KeyDown(object sender, KeyEventArgs e)    {      //如果开始游戏      if (_gameState == GameState.Open)      { //开火        if (e.KeyCode == Keys.Space)        {          //我方坦克发射          Bullet myBullet = _myTank.Fire();          _listBullet.Add(myBullet);        }        //强制刷新        pictureBox1.Invalidate();      }    }    private void timer1_Tick(object sender, EventArgs e)    {      //如果开始游戏      if (_gameState == GameState.Open)      {        //先产生一辆敌方坦克        Tank enemyTank = new Tank(Side.Enemy);        //把他加入列表        _listEnemyTank.Add(enemyTank);        //强制刷新        pictureBox1.Invalidate();      }    }    private void timer2_Tick(object sender, EventArgs e)    {      //如果开始游戏      if (_gameState == GameState.Open)      {        //定义一个随机类        Random myRand = new Random(DateTime.Now.Second);        //随机控制每一辆的运动方向        for (int i = 0; i <= _listEnemyTank.Count - 1; i )        {          //产生一个随机数          int direction = myRand.Next(1, 50);          //让坦克移动一个步长     if (direction >= 5 && direction <= 10)          {            if (_listEnemyTank[i]._Position.X >= _myTank._Position.X)              _listEnemyTank[i].Move(Direction.Left);            else              _listEnemyTank[i].Move(Direction.Right);          }          else if (direction >= 11 && direction <= 15)          {            if (_listEnemyTank[i]._Position.Y >= _myTank._Position.Y)              _listEnemyTank[i].Move(Direction.Up);            else              _listEnemyTank[i].Move(Direction.Down);          }          else if (direction >= 16 && direction <= 49)          {            _listEnemyTank[i].Move(_listEnemyTank[i]._Direction);          }          else          {            _listEnemyTank[i]._Direction = (Direction)direction;            _listEnemyTank[i].Move(_listEnemyTank[i]._Direction);          }        }         foreach (Bullet myBullet in _listBullet)        {          myBullet.Move();        }        //强制刷新        pictureBox1.Invalidate();      }    }    private void timer3_Tick(object sender, EventArgs e)    {      //如果游戏开始了      if (_gameState == GameState.Open)      {        //定义一个随机类        Random myRand = new Random(DateTime.Now.Second);        //让子弹飞        foreach (Tank enemyTank in _listEnemyTank)        {          //产生一个随机数          int fireFlag = myRand.Next(1, 10);          //判断是否发射          if (fireFlag <= 6)          {            Bullet enemyBullet = enemyTank.Fire();            _listBullet.Add(enemyBullet);          }        }      }    }    private void timer4_Tick(object sender, EventArgs e)    {      if (_gameState == GameState.Open)      {        bool keyDown = (((ushort)GetAsyncKeyState((int)Keys.Down)) & 0xffff) != 0;        if (keyDown == true)          _myTank.Move(Direction.Down);        bool keyUp = (((ushort)GetAsyncKeyState((int)Keys.Up)) & 0xffff) != 0;        if (keyUp == true)          _myTank.Move(Direction.Up);        bool keyLeft = (((ushort)GetAsyncKeyState((int)Keys.Left)) & 0xffff) != 0;        if (keyLeft == true)          _myTank.Move(Direction.Left);        bool keyRight = (((ushort)GetAsyncKeyState((int)Keys.Right)) & 0xffff) != 0;        if (keyRight == true)          _myTank.Move(Direction.Right);      }      //强制刷新      pictureBox1.Invalidate();    }  }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace TankWar{  public partial class FormMain : Form  {    #region 私有字段    //游戏状态    private GameState _gameState = GameState.Close;    //我方坦克    private Tank _myTank = new Tank(Side.Me);    //敌方坦克集合    private List<Tank> _listEnemyTank = new List<Tank>();    //子弹的集合    private List<Bullet> _listBullet = new List<Bullet>();    #endregion    //导入动态链接库    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]    public static extern short GetAsyncKeyState(int keyCode);    public FormMain()    {      InitializeComponent();    }    //开始游戏    private void BeginToolStripMenuItem_Click(object sender, EventArgs e)    {      _gameState = GameState.Open;      //启用timer      timer1.Enabled = true;      timer2.Enabled = true;      timer3.Enabled = true;      timer4.Enabled = true;    }    //结束游戏    private void EndToolStripMenuItem_Click(object sender, EventArgs e)    {      _gameState = GameState.Close;      //禁用timer      timer1.Enabled = false;      timer2.Enabled = false;    }    private void pictureBox1_Paint(object sender, PaintEventArgs e)    {      //绘制我方      _myTank.DrawMe(e.Graphics);      //绘制敌方      for (int i = 0; i <= _listEnemyTank.Count - 1; i )        _listEnemyTank[i].DrawMe(e.Graphics);      foreach (Bullet myBullet in _listBullet)      {        myBullet.DrawMe(e.Graphics);      }    }    private void FormMain_KeyDown(object sender, KeyEventArgs e)    {      //如果开始游戏      if (_gameState == GameState.Open)      { //开火        if (e.KeyCode == Keys.Space)        {          //我方坦克发射          Bullet myBullet = _myTank.Fire();          _listBullet.Add(myBullet);        }        //强制刷新        pictureBox1.Invalidate();      }    }    private void timer1_Tick(object sender, EventArgs e)    {      //如果开始游戏      if (_gameState == GameState.Open)      {        //先产生一辆敌方坦克        Tank enemyTank = new Tank(Side.Enemy);        //把他加入列表        _listEnemyTank.Add(enemyTank);        //强制刷新        pictureBox1.Invalidate();      }    }    private void timer2_Tick(object sender, EventArgs e)    {      //如果开始游戏      if (_gameState == GameState.Open)      {        //定义一个随机类        Random myRand = new Random(DateTime.Now.Second);        //随机控制每一辆的运动方向        for (int i = 0; i <= _listEnemyTank.Count - 1; i )        {          //产生一个随机数          int direction = myRand.Next(1, 50);          //让坦克移动一个步长     if (direction >= 5 && direction <= 10)          {            if (_listEnemyTank[i]._Position.X >= _myTank._Position.X)              _listEnemyTank[i].Move(Direction.Left);            else              _listEnemyTank[i].Move(Direction.Right);          }          else if (direction >= 11 && direction <= 15)          {            if (_listEnemyTank[i]._Position.Y >= _myTank._Position.Y)              _listEnemyTank[i].Move(Direction.Up);            else              _listEnemyTank[i].Move(Direction.Down);          }          else if (direction >= 16 && direction <= 49)          {            _listEnemyTank[i].Move(_listEnemyTank[i]._Direction);          }          else          {            _listEnemyTank[i]._Direction = (Direction)direction;            _listEnemyTank[i].Move(_listEnemyTank[i]._Direction);          }        }         foreach (Bullet myBullet in _listBullet)        {          myBullet.Move();        }        //强制刷新        pictureBox1.Invalidate();      }    }    private void timer3_Tick(object sender, EventArgs e)    {      //如果游戏开始了      if (_gameState == GameState.Open)      {        //定义一个随机类        Random myRand = new Random(DateTime.Now.Second);        //让子弹飞        foreach (Tank enemyTank in _listEnemyTank)        {          //产生一个随机数          int fireFlag = myRand.Next(1, 10);          //判断是否发射          if (fireFlag <= 6)          {            Bullet enemyBullet = enemyTank.Fire();            _listBullet.Add(enemyBullet);          }        }      }    }    private void timer4_Tick(object sender, EventArgs e)    {      if (_gameState == GameState.Open)      {        bool keyDown = (((ushort)GetAsyncKeyState((int)Keys.Down)) & 0xffff) != 0;        if (keyDown == true)          _myTank.Move(Direction.Down);        bool keyUp = (((ushort)GetAsyncKeyState((int)Keys.Up)) & 0xffff) != 0;        if (keyUp == true)          _myTank.Move(Direction.Up);        bool keyLeft = (((ushort)GetAsyncKeyState((int)Keys.Left)) & 0xffff) != 0;        if (keyLeft == true)          _myTank.Move(Direction.Left);        bool keyRight = (((ushort)GetAsyncKeyState((int)Keys.Right)) & 0xffff) != 0;        if (keyRight == true)          _myTank.Move(Direction.Right);      }      //强制刷新      pictureBox1.Invalidate();    }  }}

 
  
					
				
评论