首页

C# 坦克大战 小游戏源码

c#

2020-9-28

using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using Tank;
using System.Data.SqlClient;
//add

namespace 坦克
{
    public partial class Form1 : Form
    {
        //private Tank []eTanks=new Tank[11];
        private readonly int[,] Map = new int[10,10]; //砖块地图
        private readonly Tank MyTank = new Tank(6);
        private readonly ArrayList eTanks = new ArrayList();
        private int Score; //计分
        public int[,] TMap = new int[10,10]; //含坦克,砖的地图
        private int eCount; //敌方坦克数量
        private int eMaxCount = 10; //eMaxCount敌方坦克最大量
        private Tank eTank;
        private string path; //应用程序路径
        private int width = 32;

        public Form1()
        {
            InitializeComponent();
            label2.Text = User.mintime;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            pictureBox1.Width = 10*width;
            pictureBox1.Height = 10*width;
            path = Application.StartupPath;
            Random r = new Random();
            for (int x = 0; x < 10; x  = 2)
                for (int y = 0; y < 10; y  = 2)
                {
                    //产生0,1数其中0代表空地,1代表墙砖  
                    Map[x, y] = r.Next(0, 2);
                }
            Map[4, 9] = 0;
            MyTank.Top = 9;
            MyTank.Left = 4;
            MyTank.Direct = 0;
            lblX.Text = "X坐标:"   MyTank.Left   "  Y坐标:"   MyTank.Top;
        }

        private void DragWall(Graphics g) //画游戏地图
        {
            Image WallImage = Image.FromFile("BMP/TQ.BMP");
            for (int x = 0; x < 10; x  )
                for (int y = 0; y < 10; y  )
                {
                    if (Map[x, y] == 1)
                    {
                        //得到绘制这个墙砖块的在游戏面板中的矩形区域
                        Rectangle Rect = new Rectangle(x*width, y*width, width, width);
                        g.DrawImage(WallImage, Rect);
                    }
                }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up: //上 
                    if (MyTank.Top == 0 || Map[MyTank.Left, MyTank.Top - 1] == 1
                        || Meet_Tank(MyTank.Left, MyTank.Top - 1)) //遇到墙砖或坦克
                        ; //不动
                    else if (MyTank.Direct == 0) MyTank.Top--;
                    MyTank.Direct = 0;
                    break;
                case Keys.Down: //下 
                    if (MyTank.Top == 9 || Map[MyTank.Left, MyTank.Top   1] == 1
                        || Meet_Tank(MyTank.Left, MyTank.Top   1)) //遇到墙砖或坦克
                        ; //不动
                    else if (MyTank.Direct == 1) MyTank.Top  ;
                    MyTank.Direct = 1;
                    break;

                case Keys.Left: //左 
                    if (MyTank.Left == 0 || Map[MyTank.Left - 1, MyTank.Top] == 1
                        || Meet_Tank(MyTank.Left - 1, MyTank.Top)) //遇到墙砖或坦克
                        ; //不动
                    else if (MyTank.Direct == 2) MyTank.Left--;
                    MyTank.Direct = 2;
                    break;
                case Keys.Right: //右 
                    if (MyTank.Left == 9 || Map[MyTank.Left   1, MyTank.Top] == 1
                        || Meet_Tank(MyTank.Left   1, MyTank.Top)) //遇到墙砖或坦克
                        ; //不动
                    else if (MyTank.Direct == 3) MyTank.Left  ;
                    MyTank.Direct = 3;
                    break;
                case Keys.Space: //空格发射子弹
                    MyTank.fire();
                    break;
            }
            pictureBox1.Invalidate(); //重画游戏面板区域
            lblX.Text = "X坐标:"   MyTank.Left   "  Y坐标:"   MyTank.Top;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Tank t in eTanks)
            {
                switch (t.Direct) //0--上,1--下,2--左,3--右
                {
                    case 0: //向上
                        if (t.Top == 0 || Map[t.Left, t.Top - 1] == 1
                            || Meet_Tank(t.Left, t.Top - 1)) //遇到墙砖或坦克
                            t.newDirect(); //坦克转向
                        else
                            t.Top--;
                        break;
                    case 1: //向下
                        if (t.Top == 9 || Map[t.Left, t.Top   1] == 1
                            || Meet_Tank(t.Left, t.Top   1)) //遇到墙砖或坦克
                            t.newDirect(); //坦克转向
                        else
                            t.Top  ;
                        break;
                    case 2: //向左
                        if (t.Left == 0 || Map[t.Left - 1, t.Top] == 1
                            || Meet_Tank(t.Left - 1, t.Top)) //遇到墙砖或坦克
                            t.newDirect(); //坦克转向
                        else
                            t.Left--;
                        break;
                    case 3: //向右
                        if (t.Left == 9 || Map[t.Left   1, t.Top] == 1
                            || Meet_Tank(t.Left   1, t.Top)) //遇到墙砖或坦克
                            t.newDirect(); //坦克转向
                        else
                            t.Left  ;
                        break;
                }
                Random r = new Random();
                int fire_bool = r.Next(0, 8); //产生0—7的数
                if (fire_bool == t.Direct) t.fire();
            }
            pictureBox1.Invalidate(); //重画游戏面板区域            
        }

        private bool Meet_Tank(int left, int top) //比较坦克位置
        {
            foreach (Tank t in eTanks) //遍历地方
            {
                if (left == t.Left && top == t.Top) //遇到坦克
                    return true;
            }
            if (left == MyTank.Left && top == MyTank.Top) //遇到游戏方坦克
                return true;
            return false;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //修改含坦克信息的地图
            for (int x = 0; x < 10; x  )
                for (int y = 0; y < 10; y  )
                {
                    if (Map[x, y] == 1) TMap[x, y] = 1; //砖块
                    else TMap[x, y] = 0; //0空地
                }
            for (int i = 0; i < eTanks.Count; i  )
                if (eTanks[i] != null)
                {
                    int x = ((Tank) eTanks[i]).Left;
                    int y = ((Tank) eTanks[i]).Top;
                    TMap[x, y] = ((Tank) eTanks[i]).Type; //此处为敌方坦克
                }
            TMap[MyTank.Left, MyTank.Top] = MyTank.Type; //此处为己方坦克(6)

            //*******************重画游戏界面
            DragWall(e.Graphics); //画墙砖
            for (int i = 0; i < eTanks.Count; i  ) //画敌方坦克及子弹
                if (eTanks[i] != null)
                {
                    Tank t = (Tank) eTanks[i];
                    t.Draw(e.Graphics, t.Type);
                    t.DrawBullet(e.Graphics, TMap);
                }
            MyTank.Draw(e.Graphics, MyTank.Type); //画己方坦克Type=6
            MyTank.DrawBullet(e.Graphics, TMap); //画己方子弹
            //处理爆破
            for (int i = 0; i < eTanks.Count; i  ) //画敌方坦克爆破
                if (eTanks[i] != null)
                {
                    Tank t = (Tank) eTanks[i];
                    if (TMap[t.Left, t.Top] == -1)
                    {
                        t.Explore(e.Graphics);
                        eTanks.RemoveAt(i);
                        i--; //注意此处                        
                        TMap[t.Left, t.Top] = 0;
                        lblX.Text = "("   t.Left   ","   t.Top   ")坦克被击中";
                        Score  = 100;
                        //PlaySound.Play("Sound/Score.WAV"); 
                        lblScore.Text = Score.ToString();
                    }
                }
            if (TMap[MyTank.Left, MyTank.Top] == -1) //画己方坦克爆破
            {   
                MyTank.Explore(e.Graphics);
                TMap[MyTank.Left, MyTank.Top] = 0;
                lblX.Text = "游戏者你被击中,游戏结束";
                timer1.Enabled = false; //游戏结束
            }
            CheckWin(); //检查是否胜利
        }

        private void CheckWin() //检查是否胜利
        {
            if (eTanks.Count == 0 && eCount == eMaxCount) //胜利
            {
                lblX.Text = " 过关! , 恭喜";
                PlaySound.Play("Sound/WIN.WAV"); //过关后播放相应音乐

                timer1.Enabled = false;
               String  time = label1.Text;
                timer3.Enabled = false;
                string strconn = "Server=.;Database=tank;user id=sa;pwd=123";
                SqlConnection sqlconn = new SqlConnection(strconn);
                try
                {
                    sqlconn.Open();
                    //MessageBox.Show("连接数据库成功");
                    string sqladd = "update  user2  set time = '" time "' where username = '"   User.username "'";
                    SqlCommand sqlcmd = new SqlCommand(sqladd, sqlconn);
                    sqlcmd.ExecuteNonQuery();
                   
                  
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据库打开失败,详细信息:"   ex.ToString());
                }
                finally
                {
                    sqlconn.Close();
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e) //定时产生新敌方坦克
        {
            if (eCount < eMaxCount) //eMaxCount敌方坦克最大量
            {
                //敌方坦克类型为3,改变此数可以产生不同图案的地方坦克
                eTank = new Tank(3);
                eTanks.Add(eTank); // eTanks[eCount] = eTank;
                eCount  ;
            }
            else
                timer2.Enabled = false; //不再产生新的敌方坦克
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
         public int t = 0;
        string hh, mm, ss, ms;
        public string outformat(int t)//自定义类用来提供给我们自己想要的字符串格式,以及时间的代还运算
        {
            
            int temp = t / 100;
            int mms = t % 100;
            int h = temp/ 3600;
            int m = temp / 60 % 60;
            int s = temp % 60;

            if (h < 10) hh = "0"   h.ToString(); else hh=h.ToString();
            if (m < 10) mm = "0"   m.ToString(); else mm = m.ToString();
            if (s < 10) ss = "0"   s.ToString(); else ss = s.ToString();
            if (mms < 10) ms = "0"   mms.ToString(); else ms = mms.ToString();

            return hh   ":"   mm   ":"   ss   "."   ms;
        }
        private void timer3_Tick(object sender, EventArgs e)
        {
            t  ;

            label1.Text = outformat(t);
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
.vs/╠╣┐╦┤≤╒╜/v15/.suo , 36352
.vs/╠╣┐╦┤≤╒╜/v15/Server/sqlite3/db.lock , 0
.vs/╠╣┐╦┤≤╒╜/v15/Server/sqlite3/storage.ide , 479232
.vs/╠╣┐╦┤≤╒╜/v16/.suo , 41472
.vs/╠╣┐╦┤≤╒╜/v16/Server/sqlite3/db.lock , 0
.vs/╠╣┐╦┤≤╒╜/v16/Server/sqlite3/storage.ide , 503808
ClassDiagram2.cd , 887
Form1.Designer.cs , 6715
Form1.cs , 12133
Form1.resx , 6571
Form2.Designer.cs , 4725
Form2.cs , 2499
Form2.resx , 5817
Form3.Designer.cs , 4018
Form3.cs , 1439
Form3.resx , 5817
PlaySound.cs , 602
Program.cs , 449
Properties/AssemblyInfo.cs , 1080
Properties/Resources.Designer.cs , 2840
Properties/Resources.resx , 5957
Properties/Settings.Designer.cs , 1089
Properties/Settings.settings , 249
Tank.cs , 6647
Tank.ico , 22486
TankMap.cs , 207
User.cs , 209
bin/Debug/MySQLDriverCS.dll , 69632
bin/Debug/MySQLDriverCS.pdb , 192000
bin/Debug/MySQLDriverCS.xml , 99480
bin/Debug/bmp/DX.BMP , 822
bin/Debug/bmp/ETANK1.BMP , 9270
bin/Debug/bmp/ETANK2.BMP , 9270
bin/Debug/bmp/ETANK3.BMP , 9270
bin/Debug/bmp/ETANK4.BMP , 9270
bin/Debug/bmp/MYTANK.BMP , 9270
bin/Debug/bmp/TQ.BMP , 822
bin/Debug/bmp/explode1.bmp , 566
bin/Debug/bmp/explode2.bmp , 5174
bin/Debug/bmp/missile1.bmp , 2906
bin/Debug/bmp/missile2.bmp , 2906
bin/Debug/bmp/tankD.bmp , 13970
bin/Debug/bmp/tankL.bmp , 14646
bin/Debug/bmp/tankR.bmp , 14526
bin/Debug/bmp/tankU.bmp , 14654
bin/Debug/sound/DropCell.wav , 1977
bin/Debug/sound/Explode.wav , 85420
bin/Debug/sound/MOVE.WAV , 13318
bin/Debug/sound/Music1.mid , 9851
bin/Debug/sound/Music8.MID , 10389
bin/Debug/sound/Shoot.wav , 48274
bin/Debug/sound/Win.wav , 41328
bin/Debug/sound/bob.wav , 11414
bin/Debug/sound/explode2.WAV , 38400
bin/Debug/sound/score.wav , 11504
bin/Debug/sound/tank.wav , 972
bin/Debug/sound/tie.wav , 7170
bin/Debug/sound/tongguan.wav , 4194418
bin/Debug/sound/wall.wav , 4034
bin/Debug/╠╣┐╦.exe , 46080
bin/Debug/╠╣┐╦.pdb , 58880
bin/Debug/╠╣┐╦.vshost.exe , 11608
bin/Debug/╠╣┐╦.vshost.exe.manifest , 490
bullet.cs , 2620
obj/Debug/DesignTimeResolveAssemblyReferences.cache , 745
obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6540
obj/Debug/TempPE/Properties.Resources.Designer.cs.dll , 4608
obj/Debug/╠╣┐╦.Form1.resources , 180
obj/Debug/╠╣┐╦.Form2.resources , 180
obj/Debug/╠╣┐╦.Form3.resources , 180
obj/Debug/╠╣┐╦.Properties.Resources.resources , 180
obj/Debug/╠╣┐╦.csproj.FileListAbsolute.txt , 1033
obj/Debug/╠╣┐╦.csproj.GenerateResource.Cache , 847
obj/Debug/╠╣┐╦.exe , 46080
obj/Debug/╠╣┐╦.pdb , 58880
obj/Debug/╠╣┐╦┤≤╒╜.csproj.CopyComplete , 0
obj/Debug/╠╣┐╦┤≤╒╜.csproj.CoreCompileInputs.cache , 42
obj/Debug/╠╣┐╦┤≤╒╜.csproj.FileListAbsolute.txt , 4119
obj/Debug/╠╣┐╦┤≤╒╜.csproj.GenerateResource.cache , 963
obj/Debug/╠╣┐╦┤≤╒╜.csprojAssemblyReference.cache , 424
obj/Debug/╠╣┐╦┤≤╒╜.csprojResolveAssemblyReference.cache , 5903
obj/╠╣┐╦.csproj.FileList.txt , 261
╠╣┐╦┤≤╒╜.csproj , 4809
╠╣┐╦┤≤╒╜.sln , 913
╠╣┐╦┤≤╒╜.suo , 32768
没有账号? 忘记密码?

社交账号快速登录