基本信息
源码名称:C# 坦克大战 小游戏源码
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2019-06-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

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();
        }
    }
}