/// <summary>
 /// Bullet Movement
 /// </summary>
 /// <param name="b">Bullet Object</param>
 public void move(Bullets b)
 {
     if (b.direction == "left")
     {
         b.x -= b.speed;
     }
     else if (b.direction == "right")
     {
         b.x += b.speed;
     }
     else if (b.direction == "down")
     {
         b.y += b.speed;
     }
     else if (b.direction == "up")
     {
         b.y -= b.speed;
     }
 }
Exemple #2
0
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            #region Player Movement

            //checks to see if any keys have been pressed and adjusts the X or Y value
            //for the Player appropriately
            if (leftArrowDown)
            {
                if (p.x >= 0)
                {
                    p.move(p, "left");
                    direction = 2;
                }
            }
            else if (downArrowDown)
            {
                if (p.y <= this.Height - Properties.Resources.RedGuyDown.Height)
                {
                    p.move(p, "down");
                    direction = 0;
                }
            }
            else if (rightArrowDown)
            {
                if (p.x <= this.Width - Properties.Resources.RedGuyDown.Width)
                {
                    p.move(p, "right");
                    direction = 3;
                }
            }
            else if (upArrowDown)
            {
                if (p.y >= 60)
                {
                    p.move(p, "up");
                    direction = 1;
                }
            }

            #endregion

            #region Bullet Movement

            shotOk--;           //Lowers shot count

            if (shot)           //If spacebar has been clicked
            {
                if (shotOk < 0) //If shot count is 0
                {
                    if (direction == 0)
                    {
                        Bullets b = new Bullets(p.x + p.width / 2, p.y + p.height / 2, 10, 10, "down");
                        bulletsList.Add(b);
                        shotOk = 25;
                        monsterSpawn--;
                    }
                    else if (direction == 1)
                    {
                        Bullets b = new Bullets(p.x + p.width / 2, p.y + p.height / 2, 10, 10, "up");
                        bulletsList.Add(b);
                        shotOk = 25;
                        monsterSpawn--;
                    }
                    else if (direction == 2)
                    {
                        Bullets b = new Bullets(p.x + p.width / 2, p.y + p.height / 2, 10, 10, "left");
                        bulletsList.Add(b);
                        shotOk = 25;
                        monsterSpawn--;
                    }
                    else if (direction == 3)
                    {
                        Bullets b = new Bullets(p.x + p.width / 2, p.y + p.height / 2, 10, 10, "right");
                        bulletsList.Add(b);
                        shotOk = 25;
                        monsterSpawn--;
                    }
                }
            }

            foreach (Bullets b in bulletsList)
            {
                b.move(b);
            }

            //Remove Bullets if they have left the screen
            foreach (Bullets b in bulletsList)
            {
                if (b.x + b.size < 0 || b.x + b.size > this.Width)
                {
                    bulletsList.Remove(b);
                    break;
                }
                else if (b.y + b.size < 55 || b.y + b.size > this.Height)
                {
                    bulletsList.Remove(b);
                    break;
                }
            }

            #endregion

            #region Monster Spawning

            if (monsterList.Count() == 0)//Spawns if there are no monsters on screen
            {
                monsterDirection = randGen.Next(0, 4);
                if (monsterDirection == 0 || monsterDirection == 1)
                {
                    monsterWidth  = Properties.Resources.monsterDown.Width;
                    monsterHeight = Properties.Resources.monsterDown.Height;
                }
                else if (monsterDirection == 2 || monsterDirection == 3)
                {
                    monsterWidth  = Properties.Resources.monsterLeft.Width;
                    monsterHeight = Properties.Resources.monsterLeft.Height;
                }
                int monsterX, monsterY;

                monsterX = randGen.Next(0, this.Width - Properties.Resources.monsterDown.Width - 20);
                monsterY = randGen.Next(60, this.Height - Properties.Resources.monsterDown.Height - 20);

                //Check if monster is close to player, if so move monster's initial X or Y
                if (monsterY + monsterHeight < p.y + p.height && monsterY + monsterHeight > p.y)
                {
                    if (p.y > this.Height / 2)
                    {
                        monsterY -= 150;
                    }
                    else if (p.y < this.Height / 2)
                    {
                        monsterY += 150;
                    }
                }

                if (monsterX + monsterWidth < p.x + p.width && monsterX + monsterWidth > p.y)
                {
                    if (p.x > this.Width / 2)
                    {
                        monsterX -= 150;
                    }
                    else if (p.x < this.Width / 2)
                    {
                        monsterX += 150;
                    }
                }

                //Create new monster object and add it to list
                Monster m = new Monster(monsterX, monsterY, monsterWidth, monsterHeight, monsterSpeed, monsterDirection, monsterImage);
                monsterList.Add(m);
                monsterSpawn = 20; //Reset number of bullets needed to be fired
                monsterSpeed++;    //Increase monster speed for next monster to be spawned
            }

            //If 20 bullets have been fired, add new monster
            if (monsterSpawn == 0)
            {
                monsterDirection = randGen.Next(0, 4);
                if (monsterDirection == 0 || monsterDirection == 1)
                {
                    monsterWidth  = Properties.Resources.monsterDown.Width;
                    monsterHeight = Properties.Resources.monsterDown.Height;
                }
                else if (monsterDirection == 2 || monsterDirection == 3)
                {
                    monsterWidth  = Properties.Resources.monsterLeft.Width;
                    monsterHeight = Properties.Resources.monsterLeft.Height;
                }

                int monsterX, monsterY;

                monsterX = randGen.Next(0, this.Width - Properties.Resources.monsterDown.Width - 20);
                monsterY = randGen.Next(0, this.Height - Properties.Resources.monsterDown.Height - 20);

                //Check if monster is close to player, if so move monster's initial X or Y
                if (monsterY + monsterHeight < p.y + p.height && monsterY + monsterHeight > p.y)
                {
                    if (p.y > this.Height / 2)
                    {
                        monsterY -= 150;
                    }
                    else if (p.y < this.Height / 2)
                    {
                        monsterY += 150;
                    }
                }

                if (monsterX + monsterWidth < p.x + p.width && monsterX + monsterWidth > p.y)
                {
                    if (p.x > this.Width / 2)
                    {
                        monsterX -= 150;
                    }
                    else if (p.x < this.Width / 2)
                    {
                        monsterX += 150;
                    }
                }

                //Add new monster
                Monster m = new Monster(monsterX, monsterY, monsterWidth, monsterHeight, monsterSpeed, monsterDirection, monsterImage);
                monsterList.Add(m);
                monsterSpawn = 20; //Reset number of bullets needed to be fired
                monsterSpeed++;    //Add to monster speed for next monster
            }

            #endregion

            #region Monster Move

            mDirectionChange--;

            foreach (Monster m in monsterList)
            {
                if (m.x >= 0 && m.y >= 60 && m.y < this.Height + m.height && m.x < this.Width + m.width)
                {
                    m.move(m, m.mDirection);
                }

                if (m.x < 5 || m.y < 60 || m.x + m.width > this.Width - 5 || m.y + m.height > this.Width - 5)
                {
                    if (m.mDirection == 0)
                    {
                        m.mDirection     = randGen.Next(1, 4);
                        m.y             -= 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                    else if (m.mDirection == 1)
                    {
                        m.mDirection     = randGen.Next(2, 4);
                        m.y             += 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                    else if (m.mDirection == 2)
                    {
                        m.mDirection     = randGen.Next(0, 2);
                        m.x             += 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                    else if (m.mDirection == 3)
                    {
                        m.mDirection     = randGen.Next(0, 3);
                        m.x             -= 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                }

                if (mDirectionChange == 0)
                {
                    if (m.mDirection == 0)
                    {
                        m.mDirection     = randGen.Next(1, 4);
                        m.y             -= 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                    else if (m.mDirection == 1)
                    {
                        m.mDirection     = randGen.Next(2, 4);
                        m.y             += 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                    else if (m.mDirection == 2)
                    {
                        m.mDirection     = randGen.Next(0, 2);
                        m.x             += 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                    else if (m.mDirection == 3)
                    {
                        m.mDirection     = randGen.Next(0, 3);
                        m.x             -= 10;
                        mDirectionChange = randGen.Next(20, 101);
                        break;
                    }
                }
            }

            #endregion

            #region Collision

            //Player and Monster Collision
            foreach (Monster m in monsterList)
            {
                if (p.collision(p, m))
                {
                    gameTimer.Stop();

                    monsterList.Remove(m);

                    Form f = this.FindForm();
                    f.Controls.Remove(this);
                    GameOver go = new GameOver();
                    f.Controls.Add(go);

                    break;
                }
            }

            //Monster and Bullet Collison
            foreach (Monster m in monsterList)
            {
                foreach (Bullets b in bulletsList)
                {
                    if (m.collision(m, b))
                    {
                        //Remove monster and Bullet involved in collison
                        monsterList.Remove(m);
                        bulletsList.Remove(b);
                        score += 15; //Add 15 to score if collision occurs
                        break;
                    }
                }
                break;
            }

            #endregion

            scoreIncrement--;

            if (scoreIncrement == 0)
            {
                score++;
                scoreIncrement = 15;
            }

            scoreLabel.Text = "Score: " + Convert.ToString(score);

            Refresh();
        }