Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TankWar.Tank"/> class.  4 arugments
        /// </summary>
        /// <param name="content">Content.</param>
        /// <param name="spriteName">Sprite name.</param>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        public Tank( ContentManager content,string spriteName, int x, int y )
        {
            //Load Tank Picture
            Sprite = content.Load<Texture2D> (spriteName);

            //than make active
            Active = true;
            //Load Projectile
            ProjectileSprite = content.Load<Texture2D> ("Projectile");
            GunDirection = Direction.Up;
            CoolTime=GameConstants.COOL_TIME;

            //Moving speed
            MovingSpeed = GameConstants.TANK_MOVING_SPEED_PLAYER;
            CurrentMovingTime = 0;

            //Seperate the different 4 parts of tanks
            FrameWidth = Sprite.Width / 2;
            FrameHeight = Sprite.Height / 2;

            //the original rctangle
            DrawRectangle =new Rectangle(x, y,
                                         FrameWidth, FrameHeight);
            //the rectangle to show
            ShowRectangle = new Rectangle (0, 0,FrameWidth, FrameHeight);
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TankWar.Projectile"/> class.
        /// </summary>
        /// <param name="sprite">Sprite. texture2d</param>
        /// <param name="direction">Direction. fire location</param>
        /// <param name="location">Location. the tank  location (left up corner)</param>
        /// <param name="frameWidth">Frame width. tank width</param>
        /// <param name="frameHeight">Frame height. tank height</param>
        public Projectile(Texture2D sprite,Direction direction, Vector2 location,int frameWidth,
		                  int frameHeight)
        {
            this.direction = direction;
            this.sprite = sprite;
            this.location = location;
            this.tankWidth = frameWidth;
            this.tankHeight = frameHeight;

            //Get the right gunlocation for fire
            GetDrawRectangle ();
        }
Пример #3
0
        /// <summary>
        /// Update the specified gameTime and keyboard. Use keyboard to control tank.
        /// </summary>
        /// <param name="gameTime">Game time.</param>
        /// <param name="keyboard">Keyboard.</param>
        public void Update(GameTime gameTime, KeyboardState keyboard)
        {
            //frequency in update is 60 hz defautly, update 60 times in 1 seconds

            //bound
            Bound ();
            bool halt =CheckHalt();
            if (Active) {

                // di not interact other cubes than move
                if (!halt) {
                    //Move smoothly
                    MoveSmoothly (gameTime);

                    if (keyboard.IsKeyDown (keyLeft) && (!halt)) {
                        MoveLeft ();
                        GunDirection = Direction.Left;

                    } else if (keyboard.IsKeyDown (keyRight) && (!halt)) {
                        MoveRight ();
                        GunDirection = Direction.Right;

                    } else if (keyboard.IsKeyDown (keyUp) && (!halt)) {
                        MoveUp ();
                        GunDirection = Direction.Up;

                    } else if (keyboard.IsKeyDown (keyDown) && (!halt)) {
                        MoveDown ();
                        GunDirection = Direction.Down;

                    }
                    //if interacts then move back a little
                } else {
                    switch (GunDirection) {
                    case Direction.Up:
                        DrawRectangle.Y += GameConstants.TANK_MOVING_SPEED_PLAYER;
                        break;
                    case Direction.Right:
                        DrawRectangle.X -= GameConstants.TANK_MOVING_SPEED_PLAYER;
                        break;
                    case Direction.Left:
                        DrawRectangle.X += GameConstants.TANK_MOVING_SPEED_PLAYER;
                        break;
                    case Direction.Down:
                        DrawRectangle.Y -= GameConstants.TANK_MOVING_SPEED_PLAYER;
                        break;
                    }
                }

                // Press F and shoot
                // Use This support cool time and multi click to shoot
                if (keyboard.IsKeyDown (Keys.F) && CanShoot) {
                    Shooting ();
                    CanShoot = false;
                }
                if (!CanShoot) {
                    CurrentShootingTime += gameTime.ElapsedGameTime.Milliseconds;
                    if (CurrentShootingTime >= CoolTime ||
                        keyboard.IsKeyUp (Keys.F)) {
                        CanShoot = true;
                        CurrentShootingTime = 0;
                    }

                }

            } else {

                Projectiles.Clear ();

            }
            //update projectiles & Remove the deactive projectiles
            ProjectilesUpdate (gameTime);
        }