Пример #1
0
        /// <summary>
        /// Render invaders to the canvas.
        /// </summary>
        /// <param name="graphics"></param>
        /// <returns></returns>
        public InvadersController RenderInvaders(Graphics graphics)
        {
            for (int i = 0; i < _invadersPos.Height; ++i)
            {
                for (int j = 0; j < _invadersPos.Width; ++j)
                {
                    Invader invader = _invaders[i, j];
                    if (invader != null)
                    {
                        float x = (_invadersPos.X + _invaderSize.Width * j) * _scale;
                        float y = (_invadersPos.Y + _invaderSize.Height * i) * _scale;

                        invader.Render(graphics, x, y, _scale, _invaderStepps);
                    }
#if DEBUG
                    else
                    {
                        float x = (_invadersPos.X + _invaderSize.Width * j) * _scale;
                        float y = (_invadersPos.Y + _invaderSize.Height * i) * _scale;

                        float width  = (_invaderSize.Width - 1) * _scale;
                        float height = (_invaderSize.Height - 1) * _scale;

                        graphics.DrawRectangle(Pens.Gray, x, y, width, height);
                        graphics.DrawLine(Pens.Gray, x, y, x + width, y + height);
                        graphics.DrawLine(Pens.Gray, x, y + height, x + width, y);
                    }
#endif
                }
            }

            return(this);
        }
Пример #2
0
        private void nextWave()
        {
            wave++;
            invaderDirection = Direction.Right;
            // if the wave is under 7, set frames skipped to 6 - current wave number
            if (wave < 7)
            {
                framesSkipped = 6 - wave;
            }
            else
            {
                framesSkipped = 0;
            }

            int currentInvaderYSpace = 0;

            for (int x = 0; x < 5; x++)
            {
                ShipType currentInvaderType = (ShipType)x;
                currentInvaderYSpace += invaderYSpacing;
                int currentInvaderXSpace = 0;
                for (int y = 0; y < 5; y++)
                {
                    currentInvaderXSpace += invaderXSpacing;
                    Point newInvaderPoint =
                        new Point(currentInvaderXSpace, currentInvaderYSpace);
                    // Need to add more varied invader score values
                    Invader newInvader =
                        new Invader(currentInvaderType, newInvaderPoint, 10);
                    invaders.Add(newInvader);
                }
            }
        }
Пример #3
0
        private void Fire(Invader theShooter)
        {
            //Centralize the origin of the shot to the center front of the ship
            Point gunOfShip = new Point((theShooter.Area.Right + theShooter.Area.Left) / 2, theShooter.Area.Bottom);

            enemyShots.Add(new Shot(gunOfShip, false, boundaries));
        }
Пример #4
0
        private void CrearInvaders(ShipType currentInvaderType, Point newInvaderPoint, int score)
        {
            Invader enemy = null;

            switch (currentInvaderType)
            {
            case ShipType.Bug:
                enemy = new Bug(currentInvaderType, newInvaderPoint, 10);
                break;

            case ShipType.Satellite:
                enemy = new Satellite(currentInvaderType, newInvaderPoint, 10);
                break;

            case ShipType.Saucer:
                enemy = new Saucer(currentInvaderType, newInvaderPoint, 10);
                break;

            case ShipType.Spaceship:
                enemy = new Spaceship(currentInvaderType, newInvaderPoint, 10);
                break;

            case ShipType.Star:
                enemy = new Invaders.Factory.Star(currentInvaderType, newInvaderPoint, 10);
                break;
            }
            invaders.Add(enemy);
        }
Пример #5
0
        private void returnFire()
        {
            //// invaders check their location and fire at the player
            if (invaderShots.Count == wave)
            {
                return;
            }
            if (random.Next(10) < (10 - wave))
            {
                return;
            }

            var invaderColumns =
                from invader in invaders
                group invader by invader.Location.X into columns
                select columns;

            int randomColumnNumber = random.Next(invaderColumns.Count());
            var randomColumn       = invaderColumns.ElementAt(randomColumnNumber);

            var invaderRow =
                from invader in randomColumn
                orderby invader.Location.Y descending
                select invader;

            Invader shooter         = invaderRow.First();
            Point   newShotLocation = new Point
                                          (shooter.Location.X + (shooter.Area.Width / 2),
                                          shooter.Location.Y + shooter.Area.Height);

            Shot newShot = new Shot(newShotLocation, Direction.Down,
                                    formArea);

            invaderShots.Add(newShot);
        }
Пример #6
0
        //Use link to figure out which invaders should be able to return fire
        public void ReturnFire(int wave, Random random)
        {
            if (wave + 1 == enemyShots.numberOfShots())
            {
                return;
            }

            if (random.Next(10) < 10 - wave)
            {
                return;
            }

            //Linq query to extract the invader to return fire
            var theShootingInvaders = (from invader in invaders
                                       group invader by invader.Location.X
                                       into invaderGroup
                                       orderby invaderGroup.Key descending
                                       select invaderGroup);


            if (invaders.Count() > 0)
            {
                //Gets the first invader in each column last will take the closest invaders while first while take the invaders in the back
                Invader theShooter = theShootingInvaders.ElementAt(random.Next(theShootingInvaders.Count())).Last <Invader>();

                Fire(theShooter);
            }
        }
Пример #7
0
 private void btnStart_Click(object sender, EventArgs e)
 {
     moeilijkheidsgraad = numUpDownSnelheidInv.Value * 1;
     for (int i = 0; i < numUpDownAantalInv.Value; i++)
     {
         Button b = new Button();
         b.Height    = 30;
         b.Width     = 30;
         b.FlatStyle = FlatStyle.Flat;
         b.FlatAppearance.BorderSize = 0;
         b.BackColor = Color.Black;
         b.Left      = random.Next(10, 580);
         Controls.Add(b);
         Invader invader = new Invader(b, 5);
         invaders.Add(invader);
         b.Click += new EventHandler(b_Click);
     }
     timerInvaders.Start();
     btnStart.Hide();
     numUpDownSnelheidInv.Hide();
     lblMoeilijkheid.Hide();
     numUpDownAantalInv.Hide();
     label2.Hide();
     label3.Hide();
 }
Пример #8
0
 public GuidedMissle(Point location, Direction direction)
     : base(location, direction)
 {
     if (this.upOrDown == Direction.Up)
     {
         Game game = Game.GetInstance();
         target = game.Invaders[0];
     }
 }
Пример #9
0
 /// <summary>
 /// This method checks to see if an invader was hit by an attack.
 /// </summary>
 /// <param name="hitInvader">The invader to check for hits.</param>
 /// <param name="attackArea">The AreaUser reference to use to get 
 /// a list of points representing all points along the outer edge of the attack.</param>
 /// <returns>A boolean indicating whether or not the invader was hit.</returns>
 private bool WasHit(Invader hitInvader, AreaUser attackArea)
 {
     List<Point> hitPointList = attackArea.AllCollisionPoints;
     List<Point> invaderCollisionList = hitInvader.AllCollisionPoints;
     foreach (Point point in hitPointList)
         if (hitInvader.Area.Contains(point)) return true;
     foreach (Point point in invaderCollisionList)
         if (attackArea.Area.Contains(point)) return true;
     return false;
 }
Пример #10
0
        } // end method

        /// <summary>
        /// This method adds 6 invaders of a certain type to the invaders list collection. It returns the position
        /// of the next row on the screen.
        /// </summary>
        /// <param name="invaderType">The type of invader to add.</param>
        /// <param name="score">The amount of points for killing the invader</param>
        /// <param name="xMove">Amount of pixels to move across after creating an invader.</param>
        /// <param name="yMove">Amount of pixels to move down after creating a row of invaders.</param>
        /// <param name="xPosition">X-Coordinate of the starting position for the current row.</param>
        /// <param name="yPosition">Y-Coordinate of the starting position for the current row.</param>
        /// <returns>The position where the next row should start.</returns>
        private int AddInvaderRow(ShipType invaderType, int score, int xMove, int yMove, int xPosition, int yPosition)
        {
            for (int j = 1; j <= 6; j++)
            {
                Invader newInvader = new Invader(invaderType, new Point(xPosition, yPosition), score);
                invaders.Add(newInvader);
                xPosition += xMove;
            }
            yPosition += yMove;
            return(yPosition);
        } // end method AddInvaderRow
Пример #11
0
 /// <summary>
 /// This method removes an invader from the invaders list and 
 /// triggers an explosion where the invader died.
 /// </summary>
 /// <param name="deadInvader"></param>
 private void KillInvader(Invader deadInvader)
 {
     Explosion newDeadInvaderExplosion = new Explosion(deadInvader.Location, random);
     deadInvaderExplosions.Add(newDeadInvaderExplosion);
     ScoreFlash newDeadInvaderScoreFlash = new ScoreFlash(deadInvader.Location, deadInvader.Score);
     deadInvaderScores.Add(newDeadInvaderScoreFlash);
     score += deadInvader.Score;
     if (gotPointsFromShots) bombScore += deadInvader.Score;
     invaders.Remove(deadInvader);
     if (deadInvader is MotherShip) motherShip = null;
 } // end method KillInvader
Пример #12
0
        private void InvaderReturnFire()
        {
            if (invaderShots.Count() < wave + 1 && random.Next(10) > 10 - wave)
            //if (true)
            {
                var groups = invaders.GroupBy(i => i.Location.X).OrderBy(k => k.Key);
                IGrouping <int, Invader> group = groups.ToArray()[random.Next(groups.Count())];
                Invader invader = group.OrderByDescending(i => i.Location.Y).First();

                invaderShots.Add(new Shot(new Point(invader.Area.X + invader.Area.Width / 2, invader.Area.Y), Direction.Down, boundaries));
            }
        }
Пример #13
0
        public Army()
        {
            float width = 0;

            for (int x = 0; x < 15; x++)
            {
                width = (float)(x * 40);
                for (int y = 0; y < 6; y++)
                {
                    soldier[x, y]            = new Invader(Invader.type.hobo);
                    soldier[x, y].FrameCount = rand.Next(0, 4);

                    soldier[x, y].Location = new Vector2(width, (y * 40) + 40);
                }
            }
        }
Пример #14
0
        private void b_Click(object sender, EventArgs e)
        {
            score += (Convert.ToInt32(moeilijkheidsgraad) * 1);
            for (int i = 0; i < invaders.Count; i++)
            {
                Invader invader = invaders[i];
                if (invader.getElement() == sender)
                {
                    invader.VerliesLeven(random.Next(0, 580));

                    if (invader.getLevens() <= 0)
                    {
                        invaders.Remove(invader);
                        this.Controls.Remove((Button)sender);
                    }
                }
            }
        }
Пример #15
0
 private void ReturnFire()
 {
     if (invaderShots.Count >= wave + 1 || random.Next(10) < 10 - wave)
     {
         return;
     }
     else
     {
         var groupedInvaders = from invader in invaders
                               group invader by invader.Area.X
                               into invaderXcoordinate
                               orderby invaderXcoordinate.Key descending
                               select invaderXcoordinate;
         var     randGroup       = groupedInvaders.ElementAt(random.Next(groupedInvaders.Count()));
         Invader shootingInvader = randGroup.Last();
         invaderShots.Add(new Shot(new Point(shootingInvader.Area.X + shootingInvader.Area.Width / 2, shootingInvader.Area.Y), Direction.Down, boundaries));
     }
 }
Пример #16
0
        /// <summary>
        /// Remove empty columns on eather side of the field.
        /// </summary>
        /// <param name="removedCol"></param>
        private void TidyInvaderArray(int removedCol)
        {
            int  numRemoved  = 0;
            bool columnEmpty = true;
            int  col         = removedCol;

            while (columnEmpty && col >= 0 && col < _invadersPos.Width)
            {
                for (int k = 0; k < _invadersPos.Height; ++k)
                {
                    if (_invaders[k, col] != null)
                    {
                        columnEmpty = false;
                        break;
                    }
                }

                if (columnEmpty)
                {
                    ++numRemoved;
                }

                col = col + (removedCol == 0 ? 1 : -1);
            }

            if (numRemoved > 0)
            {
                _invadersPos.Width  -= numRemoved;
                Invader[,] Xinvaders = new Invader[_invadersPos.Height, _invadersPos.Width];
                for (int i = 0; i < _invadersPos.Height; ++i)
                {
                    for (int j = 0; j < _invadersPos.Width; ++j)
                    {
                        Xinvaders[i, j] = _invaders[i, j + (removedCol == 0 ? numRemoved : 0)];
                    }
                }
                _invaders = Xinvaders;

                if (removedCol == 0)
                {
                    _invadersPos.X += numRemoved * _invaderSize.Width;
                }
            }
        }
Пример #17
0
        private void CheckForInvaderTakShot()
        {
            foreach (Shot shot in playerShots.ToList())
            {
                Invader deathInvader = invaders.FirstOrDefault(i => i.Area.Contains(shot.Location));

                if (deathInvader != null)
                {
                    playerShots.Remove(shot);
                    invaders.Remove(deathInvader);
                    score += deathInvader.Score;
                }
            }

            if (invaders.Count() == 0)
            {
                NextWave();
            }
        }
Пример #18
0
        } // end method IsTouchingBorder

        /// <summary>
        /// This method makes the invaders at the bottom fire shots if there aren't enough
        /// shots (one more than the current wave number) on screen already.
        /// </summary> 
        /* Big Note: I got help online for the LINQ in the method below. Before, the book
         * made it seem like I had to order by Location.X descending after
         * grouping by Location.X. However, this led to any Invader in the 
         * column being able to shoot. After trying some failed things regarding
         * Location.Y, I finally looked online at the Head First site forums and saw
         * one of the authors say you had to sort by Y position first.
         * 
         * This is the only method I got ANY online help for.
         */
        private void ReturnFire()
        {
            if(invaderShots.Count >= currentInvaderWave + 1) return;
            else if (random.Next(10) < (10 - currentInvaderWave)) return; 
            else {
                if (invaders.Count > 0)
                {
                    var invaderLocationGroups =
                                from bottomInvader in invaders
                                orderby bottomInvader.Location.Y descending
                                group bottomInvader by bottomInvader.Location.X
                                    into invaderLocationGroup
                                    select invaderLocationGroup;
                    int randomInvaderNumber = random.Next(invaderLocationGroups.Count());
                    Invader shooter = invaderLocationGroups.ElementAt(randomInvaderNumber).First();
                    Shot newShot = new Shot(shooter.BottomMiddle, Direction.Up, boundaries);
                    invaderShots.Add(newShot); 
                } // end if 
            } // end else
        } // end method ReturnFire
Пример #19
0
 public override void Move()
 {
     base.Move();
     Game game = Game.GetInstance();
     if (this.upOrDown == Direction.Down)
     {
         if (this.Location.X < game.ShipLocation.X)
             this.Location.X++;
         else if (this.Location.X > game.ShipLocation.X)
             this.Location.X--;
     }
     else
     {
         if (!game.Invaders.Contains(target)) target = game.Invaders[game.Invaders.Count > 1?1:0]; // If our target has died, get a new one.
         if (this.Location.X > targetPoint.X)
             this.Location.X--;
         else if (this.Location.X < targetPoint.X)
             this.Location.X++;
     }
 }
Пример #20
0
 private void timerInvaders_Tick(object sender, EventArgs e)
 {
     labelScore.Text = "Score: " + score.ToString();
     for (int i = 0; i < invaders.Count; i++)
     {
         Invader invader = invaders[i];
         invader.InvadersToAarde(moeilijkheidsgraad);
         if (invader.IsInvaderOnEarth())
         {
             invaders.Remove(invader);
             ClearButtons();
             btnStart.Show();
             numUpDownSnelheidInv.Show();
             lblMoeilijkheid.Show();
             numUpDownAantalInv.Show();
             label2.Show();
             label3.Show();
             score = 0;
         }
     }
 }
Пример #21
0
        public bool Overlaps(int dx, int dy, Invader invader)
        {
            bool yes = false;

            for (int i = 0; i < _height; ++i)
            {
                for (int j = 0; j < _width; ++j)
                {
                    int ti = i + dy;
                    int tj = j + dx;
                    if (ti > 0 && tj > 0 && ti < invader._height && tj < invader._width &&
                        _frames[0][i, j] != Color.Transparent && invader._frames[0][ti, tj] != Color.Transparent)
                    {
                        yes = true;
                        break;
                    }
                }
            }

            return(yes);
        }
Пример #22
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            buttonStart.Enabled = false;
            numericUpDownAantalSteden.Enabled    = false;
            numericUpDownInvaderLevens.Enabled   = false;
            numericUpDownInvaderSnelheid.Enabled = false;

            invaderLevens   = Convert.ToInt32(numericUpDownInvaderLevens.Value);
            invaderSnelheid = Convert.ToInt32(numericUpDownInvaderSnelheid.Value);
            aantalSteden    = Convert.ToInt32(numericUpDownAantalSteden.Value);

            labelAantalStedenValue.Text = aantalSteden.ToString();

            for (int i = 0; i < invaders.Length; i++)
            {
                invaders[i] = new Invader(invaderLevens);
                foreach (PictureBox invaderBox in pictureBoxes)
                {
                    invaderBox.Visible = true;
                }
            }

            timer1.Start();
        }
Пример #23
0
        protected static Invader MakeInvader(Color color, bool widthIsOdd, params bool[][,] bodies)
        {
            int     height  = bodies[0].GetLength(0);
            int     width   = bodies[0].GetLength(1);
            Invader invader = new Invader(width * 2 - (widthIsOdd ? 1 : 0), height);

            foreach (bool[,] b in bodies)
            {
                Color[,] body = new Color[invader.Height, invader.Width];

                for (int i = 0; i < invader.Height; ++i)
                {
                    for (int j = 0; j < invader.Width; ++j)
                    {
                        bool cellSet = b[i, j < width ? j : 2 * width - j - 1 - (widthIsOdd ? 1 : 0)];
                        body[i, j] = cellSet ? color : Color.Transparent;
                    }
                }

                invader.AddFrame(body);
            }

            return(invader);
        }
Пример #24
0
        public void NewRound(int round)
        {
            Invaders.Clear();

            Position = new Vector2(
                (ScreenSize.Width - Width) / 2,
                1);

            Random random = new Random();
            Direction = (Direction)random.Next(2, 4);

            for (int i = 0; i < SquadHeight; i++)
            {
                for (int j = 0; j < SquadWidth; j++)
                {
                    Invader invader = new Invader();
                    invader.Initialize(
                        (Animation)Animations[SquadHeight - 1 - i].Clone(),
                        CalcInvaderPosition(Position, j, i),
                        Direction,
                        Score[SquadHeight - 1 - i],
                        j, i);

                    Invaders.Add(invader);
                }
            }

            MoveSpeed = 1.0f + round * 0.3f;
            IsCrossedBottom = false;
            maxShot = round + 2;
            ShootDelay = 700 - 5 * round;
        }
Пример #25
0
        private void nextWave()
        {
            wave++;
            invaderDirection = Direction.Right;
            // if the wave is under 7, set frames skipped to 6 - current wave number
            if (wave < 7)
            {
                framesSkipped = 6 - wave;
            }
            else
                framesSkipped = 0;

            int currentInvaderYSpace = 0;
            for (int x = 0; x < 5; x++)
            {
                ShipType currentInvaderType = (ShipType)x;
                currentInvaderYSpace += invaderYSpacing;
                int currentInvaderXSpace = 0;
                for (int y = 0; y < 5; y++)
                {
                    currentInvaderXSpace += invaderXSpacing;
                    Point newInvaderPoint =
                        new Point(currentInvaderXSpace, currentInvaderYSpace);
                    // Need to add more varied invader score values
                    Invader newInvader =
                        new Invader(currentInvaderType, newInvaderPoint, 10);
                    invaders.Add(newInvader);
                }
            }
        }
Пример #26
0
 public void SpawnExplosion(Invader invader)
 {
     explosions.Add(new Explosion(invader));
 }
Пример #27
0
 public Explosion(Invader invader)
 {
     this.Size = invader.Hitbox.Size;
     this.Point = new Point(invader.Hitbox.X + (int)(invader.Hitbox.Width / 2),
         invader.Hitbox.Y + (int)(invader.Hitbox.Height / 2));
     created = DateTime.Now;
     this.Text = invader.Score.ToString();
 }
Пример #28
0
        } // end method CheckForInvadersBombed

        /// <summary>
        /// This method checks to see if an invader was hit by an attack.
        /// </summary>
        /// <param name="hitInvader">The invader to check for hits.</param>
        /// <param name="hitPointList">The list of points representing all points along the outer edge of the attack.</param>
        /// <returns>A boolean indicating whether or not the invader was hit.</returns>
        private bool WasHit(Invader hitInvader, List<Point> hitPointList) {
            foreach(Point point in hitPointList)
                if(hitInvader.Area.Contains(point)) return true;
            return false;
        }
Пример #29
0
 public Texture2D getTexture(ref Invader invader)
 {
     return(_skin[(int)invader.Type]);
 }