c - operation 5: tank battle (unfinished)

Keywords: Windows Attribute Mobile

c - operation 5: tank battle (unfinished)

General view & bullshit area

Why don't you finish it? Explain to yourself. It's not because you're lazy. It's not because you don't have time... Reason 1, no courseware, reason 2, I haven't played (I have childhood but childhood is not this. So I don't know how to do it. I don't have rules or impressions. Do I develop at will? I choose to do my big homework. I love Tetris

Therefore, I will make sure that the teacher has courseware (and more than class hours, which is the impression, or do it faster than the teacher's class process? Right. I was doing my homework when the teacher was talking about tank war in class

Code (all)

FormMain.cs

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 MyTankWar
{
    public partial class FormMian : Form
    {
        #region Private field
        
        //Game state
        private GameState _gameState = GameState.Close;

        //Our tanks
        private Tank _myTank = new Tank(Side.Me);

        //Enemy tank List set
        private List<Tank> _listEnemyTank = new List<Tank>();

        //List set of bullets
        private List<Bullet> _listBullet = new List<Bullet>();

        #endregion

        //Import dynamic link library (System.Runtime.InteropServices)
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
        public static extern short GetAsyncKeyState(int keyCode);

        public FormMian()
        {
            InitializeComponent();
        }

        private void BeginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Start the game
            _gameState = GameState.Open;

            timer1.Enabled = true;
            timer2.Enabled = true;
            timer3.Enabled = true;
            timer4.Enabled = true;
        }

        private void EndToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //End the game
            _gameState = GameState.Close;

            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
            timer4.Enabled = false;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //Draw our tank
            _myTank.DrawMe(e.Graphics);

            //Draw enemy tanks
            for (int i = 0;i <= _listEnemyTank.Count - 1 ;i++)
                _listEnemyTank[i].DrawMe(e.Graphics);

            //Draw bullets one by one
            foreach (Bullet myBullet in _listBullet)
            {
                myBullet.DrawMe(e.Graphics);
            }
        }

        private void FormMian_KeyDown(object sender, KeyEventArgs e)
        {
            if(_gameState == GameState.Open)
            {
                //If you press the Space key, our tank will fire bullets
                if (e.KeyCode == Keys.Space)
                {
                    //Our tank fired bullets and added them to the "listBullet"
                    Bullet myBullet = _myTank.Fire();
                    _listBullet.Add(myBullet);
                }

                //Force refresh of pictureBox1 control
                pictureBox1.Invalidate();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_gameState == GameState.Open)
            {
                //Make a tank
                Tank enemyTank = new Tank(Side.Enemy);

                //Add tanks to the list
                _listEnemyTank.Add(enemyTank);

                //force refresh
                pictureBox1.Invalidate();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (_gameState == GameState.Open)
            {
                //Define a random class
                Random myRand = new Random(DateTime.Now.Second);

                //Randomly control the movement direction of each enemy tank
                for (int i = 0; i <= _listEnemyTank.Count - 1; i++)
                {
                    int newDircetion = myRand.Next(1, 20);

                    if(newDircetion <= 4)
                        _listEnemyTank[i].Move((Direction)newDircetion);
                    else 
                        _listEnemyTank[i].Move(_listEnemyTank[i]._Dircetion);
                }

                //Let the bullets fly
                foreach (Bullet myBullet in _listBullet)
                {
                    myBullet.Move();
                }

                //force refresh
                pictureBox1.Invalidate();
            }
        }

        //Timer realizes the function of enemy tank firing bullets
        private void timer3_Tick(object sender, EventArgs e)
        {
            if (_gameState == GameState.Open)
            {
                //Define a random class
                Random myRand = new Random(DateTime.Now.Second);

                //Let the bullets fly
                foreach (Tank enemyTank in _listEnemyTank)
                {
                    int fireFlag = myRand.Next(1, 10);

                    if (fireFlag <= 4)
                    {
                        Bullet enemyBullet = enemyTank.Fire();
                        _listBullet.Add(enemyBullet);
                    }
                }

                //force refresh
                pictureBox1.Invalidate();
            }
        }

        //Measure the state of pressing the direction key at a fixed time to solve the problem of firing bullets while our tank is moving
        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();

            }
        }

        
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MyTankWar
{
    static class Program
    {
        /// <summary>
        ///The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormMian());
        }
    }
}

Bullet.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace MyTankWar
{
    class Bullet
    {
        #region
        //Bullet coordinate position
        private Point _position = new Point(200, 200);

        //Firing direction of bullet
        private Direction _dircetion = Direction.Up;

        //Bullet movement step
        private int _step = 10;

        //Mark of enemy and our bullets
        Side _side;

        //Bullet bitmap
        private Bitmap _bulletBmp = new Bitmap(16, 16);

        #endregion

        #region

        public Point _Position
        {
            get { return _position; }
            set { _position = value; }
        }


        public Direction _Dircetion
        {
            get { return _dircetion; }
            set { _dircetion = value; }
        }

        public int _Step
        {
            get { return _step; }
            set { _step = value; }
        }

        public Side _Side
        {
            get { return _side; }
            set { _side = value; }
        }

        #endregion

        //Construction method
        public Bullet(Side side, Direction direction)
        {
            //Keep our logo
            _side = side;

            //Save the direction of the bullet
            _dircetion = direction;

            //According to the enemy and our side and the direction of the bullets, load the corresponding bullets bitmap
            if (side == Side.Me)
            {
                if (direction == Direction.Up)
                    _bulletBmp = new Bitmap("Images\\MyBulletUp.gif");
                else if (_dircetion == Direction.Down)
                    _bulletBmp = new Bitmap("Images\\MyBulletDown.gif");
                else if (_dircetion == Direction.Left)
                    _bulletBmp = new Bitmap("Images\\MyBulletLeft.gif");
                else if (_dircetion == Direction.Right)
                    _bulletBmp = new Bitmap("Images\\MyBulletRight.gif");
            }
            else
            {
                if (direction == Direction.Up)
                    _bulletBmp = new Bitmap("Images\\EnemyBulletUp.gif");
                else if (_dircetion == Direction.Down)
                    _bulletBmp = new Bitmap("Images\\EnemyBulletDown.gif");
                else if (_dircetion == Direction.Left)
                    _bulletBmp = new Bitmap("Images\\EnemyBulletLeft.gif");
                else if (_dircetion == Direction.Right)
                    _bulletBmp = new Bitmap("Images\\EnemyBulletRight.gif");
            }

            //Set transparent color of tank bitmap
            _bulletBmp.MakeTransparent(Color.Black);
        }

        //Bullet movement
        public void Move()
        {
            //Set the bullet position
            if (_dircetion == Direction.Up)
            {
                _position.Y = _position.Y - _step;
            }
            else if(_dircetion == Direction.Down)
            {
                _position.Y = _position.Y + _step;
            }
            else if (_dircetion == Direction.Left)
            {
                _position.X = _position.X - _step;
            }
            else if (_dircetion == Direction.Right)
            {
                _position.X = _position.X + _step;
            }
        }

        //Bullet drawing
        public void DrawMe(Graphics g)
        {
            g.DrawImage(_bulletBmp, _position);
        }
    }
}

Tank.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace MyTankWar
{
    //Enumerate game states
    public enum GameState
    {
        Close = 1,Open = 2,Pause = 3
    }

    //Enumerate tank movement direction
    public enum Direction
    {
        Up = 1,Down = 2,Left = 3, Right = 4
    }

    //Enumerate both sides
    public enum Side
    {
        Me = 1, Enemy = 2
    }

    class Tank
    {
        #region Private attributes
        //Tank coordinate position
        private Point _position = new Point(200, 200);

        //Direction of tank movement
        private Direction _dircetion = Direction.Up;

        //Tank movement step length
        private int _step = 5;

        //Tank size
        private int _size = 30;

        //Friend or foe sign
        Side _side;

        //Tank bitmap array
        private Bitmap[] _tankBmp = new Bitmap[8];

        //Current tank map
        private Bitmap _nowTankBmp = new Bitmap(30, 30);

        //Rotation mark of tank bitmap
        private Boolean _tankBmpChange = true;
        #endregion

        #region Public attribute

        public Point _Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public Direction _Dircetion
        {
            get { return _dircetion; }
            set { _dircetion = value; }
        }

        public int _Step
        {
            get { return _step; }
            set { _step = value; }
        }

        public int _Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public Side _Side
        {
            get { return _side; }
            set { _side = value; }
        }

        #endregion

        //Class construction method
        public Tank(Side side)
        {
            //Keep our logo
            _side = side;
            if (side == Side.Me)
            {
                //Loading tank bitmap
                _tankBmp[0] = new Bitmap("Images\\MyTankUp1.gif");
                _tankBmp[1] = new Bitmap("Images\\MyTankUp2.gif");
                _tankBmp[2] = new Bitmap("Images\\MyTankDown1.gif");
                _tankBmp[3] = new Bitmap("Images\\MyTankDown2.gif");
                _tankBmp[4] = new Bitmap("Images\\MyTankLeft1.gif");
                _tankBmp[5] = new Bitmap("Images\\MyTankLeft2.gif");
                _tankBmp[6] = new Bitmap("Images\\MyTankRight1.gif");
                _tankBmp[7] = new Bitmap("Images\\MyTankRight2.gif");

                //Let our tank generate in the middle of the lower part of the screen
                _position.X = Screen.PrimaryScreen.Bounds.Width / 2 - _size / 2;
                _position.Y = Screen.PrimaryScreen.Bounds.Height - 150;

                //Set the default movement direction of our tank as up
                _dircetion = Direction.Up;
            }
            else
            {
                //Loading tank bitmap
                _tankBmp[0] = new Bitmap("Images\\EnemyTankUp1.gif");
                _tankBmp[1] = new Bitmap("Images\\EnemyTankUp2.gif");
                _tankBmp[2] = new Bitmap("Images\\EnemyTankDown1.gif");
                _tankBmp[3] = new Bitmap("Images\\EnemyTankDown2.gif");
                _tankBmp[4] = new Bitmap("Images\\EnemyTankLeft1.gif");
                _tankBmp[5] = new Bitmap("Images\\EnemyTankLeft2.gif");
                _tankBmp[6] = new Bitmap("Images\\EnemyTankRight1.gif");
                _tankBmp[7] = new Bitmap("Images\\EnemyTankRight2.gif");

                //Make enemy tanks in the center directly below the screen
                _position.X = Screen.PrimaryScreen.Bounds.Width / 2 - _size / 2;
                _position.Y = _size;

                //Set the default movement direction of enemy tank as up
                _dircetion = Direction.Down;
            }
            //Set tank transparency
            for (int i = 0; i <= 7; i++)
                _tankBmp[i].MakeTransparent(Color.Black);
            
            //The current tank bitmap is a bitmap of upward movement
            _nowTankBmp = _tankBmp[0];
        }

        //Tank Mobile
        public void Move(Direction direction)
        {
            //Save motion direction
            _dircetion = direction;

            if (_dircetion == Direction.Up)
            {
                //Set the position of the tank after moving
                _position.Y = _position.Y - _step;

                //Set the currently displayed tank bitmap (in order to make the tank moving effect more realistic, you need to switch between the two bitmaps)
                if (_tankBmpChange == true)
                    _nowTankBmp = _tankBmp[0];
                else
                    _nowTankBmp = _tankBmp[1];
            }

            else if (_dircetion == Direction.Down)
            {
                //Set the position of the tank after moving
                _position.Y = _position.Y + _step;

                //Set the currently displayed tank bitmap (in order to make the tank moving effect more realistic, you need to switch between the two bitmaps)
                if (_tankBmpChange == true)
                    _nowTankBmp = _tankBmp[2];
                else
                    _nowTankBmp = _tankBmp[3];
            }

            else if (_dircetion == Direction.Left)
            {
                //Set the position of the tank after moving
                _position.X = _position.X - _step;

                //Set the currently displayed tank bitmap (in order to make the tank moving effect more realistic, you need to switch between the two bitmaps)
                if (_tankBmpChange == true)
                    _nowTankBmp = _tankBmp[4];
                else
                    _nowTankBmp = _tankBmp[5];
            }

            else if (_dircetion == Direction.Right)
            {
                //Set the position of the tank after moving
                _position.X = _position.X + _step;

                //Set the currently displayed tank bitmap (in order to make the tank moving effect more realistic, you need to switch between the two bitmaps)
                if (_tankBmpChange == true)
                    _nowTankBmp = _tankBmp[6];
                else
                    _nowTankBmp = _tankBmp[7];
            }

            //Switch tank to bitmap rotation mark
            _tankBmpChange = !_tankBmpChange;

        }

        //Tank rendering
        public void DrawMe(Graphics g)
        {
            g.DrawImage(_nowTankBmp, _position);
        }

        //Tank fired bullets
        public Bullet Fire()
        {
            Bullet myBullet = new Bullet(_side, _dircetion);
            if (_dircetion == Direction.Up)
            {
                myBullet._Position = new Point(_position.X + 8,_position.Y - 15);
            }
            else if (_dircetion == Direction.Down)
            {
                myBullet._Position = new Point(_position.X + 8, _position.Y + 25);
            }
            else if (_dircetion == Direction.Left)
            {
                myBullet._Position = new Point(_position.X - 15, _position.Y + 8);
            }
            else if (_dircetion == Direction.Right)
            {
                myBullet._Position = new Point(_position.X + 25, _position.Y + 8);
            }
            return myBullet;
        }
    }
}

Extant bug

There should be a lot of this, for example, bullets don't really hit the tank or the like / / / after all, it's unfinished products

Published 104 original articles, won praise 7, visited 8316
Private letter follow

Posted by dougp23 on Sun, 12 Jan 2020 06:18:04 -0800