Пример #1
0
        private void gameLoop_Tick(object sender, EventArgs e)
        {
            foreach (Box b in boxList)
            {
                b.Move();
            }
            boxCountdown--;
            Form1.currentScore++;

            if (Form1.currentScore % 1000 == 0)
            {
                scoreSound.Play();
            }
            #region difficulty scaling
            if (Form1.currentScore % 1000 == 0 && Form1.currentScore <= 7000) //the speed will gradually increase to a point, at which point it becomes a test of endurance
            {
                foreach (Box b in boxList)
                {
                    b.speed++;
                }
                boxSpeed++;
                if (newBoxCounter > 0)
                {
                    newBoxCounter--;
                }
                rightColumnBoxLocation -= 5;
            }
            if (Form1.currentScore % 2500 == 0 && Form1.currentScore <= 5000) //the player will gradually get faster to a point
            {
                maxPlayerSpeed++;
            }
            #endregion

            scoreLabel.Text = "Score: " + (Form1.currentScore + 1); //adding 1 to sync up the displayed score with the actual score

            #region creates boxes in the "random" phase
            if (randomSectionTimer > 0)
            {
                randomSectionTimer--;

                if (randomSectionTimer < phaseBuffer) //waits for a few timer ticks as a buffer between phases
                {
                    colourCounterUp++;
                    colourCounterDown--;
                    //if (colourCounterDown == 0)
                    //{
                    //    colourCounterDown = 255;
                    //}
                    //if (colourCounterUp == 255)
                    //{
                    //    colourCounterUp = 0;
                    //}
                    if (boxCountdown <= 0)
                    {
                        Box b = new Box(randnum.Next(0, this.Width * Convert.ToInt16(0.75)), -boxSize, boxSize, boxSpeed, 255, 255, 255);
                        boxList.Add(b);
                        boxCountdown = newBoxCounter;
                    }
                    if (backColour == "red")
                    {
                        backColourRed   = colourCounterDown;
                        backColourGreen = colourCounterUp;
                        backColourBlue  = 0;
                        this.BackColor  = Color.FromArgb(backColourRed, backColourGreen, backColourBlue);
                        if (colourCounterDown <= 0)
                        {
                            backColour        = "green";
                            colourCounterDown = 255;
                            colourCounterUp   = 0;
                        }
                    }
                    if (backColour == "green")
                    {
                        backColourRed   = 0;
                        backColourGreen = colourCounterDown;
                        backColourBlue  = colourCounterUp;
                        this.BackColor  = Color.FromArgb(backColourRed, backColourGreen, backColourBlue);
                        if (colourCounterDown <= 0)
                        {
                            backColour        = "blue";
                            colourCounterDown = 255;
                            colourCounterUp   = 0;
                        }
                    }
                    if (backColour == "blue")
                    {
                        backColourRed   = colourCounterUp;
                        backColourGreen = 0;
                        backColourBlue  = colourCounterDown;
                        this.BackColor  = Color.FromArgb(backColourRed, backColourGreen, backColourBlue);
                        if (colourCounterDown <= 0)
                        {
                            backColour        = "red";
                            colourCounterDown = 255;
                            colourCounterUp   = 0;
                        }
                    }
                }

                if (randomSectionTimer <= 0)
                {
                    transitioning     = true;; //1875 ticks is equal to 15 seconds at 125 fps
                    columnBoxLocation = randnum.Next(5, this.Width - rightColumnBoxLocation - boxSize);
                }
            }
            #endregion

            #region transition to column phase
            else if (transitioning)
            {
                int leftDistanceRequired  = columnBoxLocation;
                int rightDistanceRequired = this.Width - columnBoxLocation - rightColumnBoxLocation - boxSize;
                int generalDistanceRequired;
                if (leftDistanceRequired > rightDistanceRequired)
                {
                    generalDistanceRequired = leftDistanceRequired;
                }
                else
                {
                    generalDistanceRequired = rightDistanceRequired;
                }

                if (boxCountdown <= 0)
                {
                    //Gets the inverse of the background colour, for guarenteed aesthetic pleasing-ness
                    int redValue   = 255 - backColourRed;
                    int greenValue = 255 - backColourGreen;
                    int blueValue  = 255 - backColourBlue;

                    Box b1 = new Box(columnBoxLocation - generalDistanceRequired + transitionCounterThing, -boxSize, boxSize, boxSpeed, redValue, greenValue, blueValue);
                    boxList.Add(b1);
                    Box b2 = new Box(columnBoxLocation + rightColumnBoxLocation + generalDistanceRequired - transitionCounterThing, -boxSize, boxSize, boxSpeed, redValue, greenValue, blueValue);
                    boxList.Add(b2);
                    transitionCounterThing += boxSize;
                    boxCountdown            = newBoxCounter;
                }
                if (transitionCounterThing >= generalDistanceRequired)
                {
                    transitioning          = false;
                    columnSectionTimer     = randnum.Next(500, 1251); //change back to 600 after
                    transitionCounterThing = 0;
                }
            }
            #endregion

            #region creates boxes in the "column" phase
            else if (columnSectionTimer > 0)
            {
                columnSectionTimer--;
                boxColourUp   += 5;
                boxColourDown -= 5;


                if (boxCountdown <= 0)
                {
                    //boxes in the column phase are the inverse of whatever colour the background was when the phase started
                    int redValue   = 255 - backColourRed;
                    int greenValue = 255 - backColourGreen;
                    int blueValue  = 255 - backColourBlue;

                    //if the boxes are supposed to be moving left, their x value will decrease
                    if (direction == "left")
                    {
                        columnBoxLocation -= 5;

                        if (columnBoxLocation <= 0)
                        {
                            counter = randomNumber; //changes the direction the column is shifting
                        }
                        counter++;
                    }

                    else if (direction == "right")
                    {
                        columnBoxLocation += 5;
                        if (columnBoxLocation + rightColumnBoxLocation >= this.Width - boxSize)
                        {
                            counter = randomNumber; //changes the direction the column is shifting
                        }
                        counter++;
                    }



                    boxCountdown = newBoxCounter;

                    if (columnBoxLocation < 0 || columnBoxLocation + rightColumnBoxLocation + boxSize > this.Width)
                    {
                        counter = randomNumber;
                    }
                    if (counter == randomNumber)
                    {
                        randomNumber = randnum.Next(10, 51);
                        counter      = 0;

                        if (direction == "left")
                        {
                            direction = "right";
                        }
                        else if (direction == "right")
                        {
                            direction = "left";
                        }
                    }


                    Box b1 = new Box(columnBoxLocation, -boxSize, boxSize, boxSpeed, redValue, greenValue, blueValue);
                    boxList.Add(b1);
                    Box b2 = new Box(columnBoxLocation + rightColumnBoxLocation, -boxSize, boxSize, boxSpeed, redValue, greenValue, blueValue);
                    boxList.Add(b2);
                }

                if (columnSectionTimer <= 0)
                {
                    randomSectionTimer = randnum.Next(500, 1501); //the timer is greater than the max timer for the randomsection so that there is a delay between the two phases
                    phaseBuffer        = randomSectionTimer - bufferTicks;
                }
            }
            #endregion

            //removes box after it has gone offscreen
            if (boxList.Count > 0)
            {
                if (boxList[0].y > this.Height && boxList.Count > 0)
                {
                    boxList.RemoveAt(0);
                }
            }
            #region player movement and collision
            //Move player
            if (leftArrowDown)
            {
                player.Move("left");
                if (currentPlayerSpeed < maxPlayerSpeed) //&& (Form1.currentScore / 2) % 2 == 0)
                {
                    currentPlayerSpeed = currentPlayerSpeed * 8 / 5;
                    if (currentPlayerSpeed > maxPlayerSpeed)
                    {
                        currentPlayerSpeed = maxPlayerSpeed;
                    }
                    player.speed = currentPlayerSpeed;

                    //player.speed = Convert.ToInt16(Math.Round(currentPlayerSpeed));
                }

                if (player.x < 0) //if the player is too far to the left
                {
                    player.x += player.speed;
                }
            }

            if (rightArrowDown)
            {
                player.Move("right");
                if (currentPlayerSpeed < maxPlayerSpeed) //&& (Form1.currentScore / 2) % 2 == 0)
                {
                    currentPlayerSpeed = currentPlayerSpeed * 8 / 5;
                    if (currentPlayerSpeed > maxPlayerSpeed)
                    {
                        currentPlayerSpeed = maxPlayerSpeed;
                    }
                    player.speed = currentPlayerSpeed;

                    //player.speed = Convert.ToInt16(Math.Round(currentPlayerSpeed));
                }

                if (player.x > this.Width - player.size) //if the player is too far to the right
                {
                    player.x -= player.speed;
                }
            }
            else if (rightArrowDown == false && leftArrowDown == false)
            {
                currentPlayerSpeed = 2;
            }

            //Check for collision between player and boxes
            foreach (Box b in boxList)
            {
                Boolean hasCollided = false;

                hasCollided = player.Collision(b);

                if (hasCollided)
                {
                    gameLoop.Stop();
                    Refresh();


                    //plays a sound effect once the player gets hit / loses
                    SoundPlayer soundplayer = new SoundPlayer(Properties.Resources.smb_mariodie);
                    soundplayer.Play();

                    //waits for the sound effect to finish before continuing on to the next screen
                    Thread.Sleep(500);

                    Form f = this.FindForm();
                    f.Controls.Remove(this);

                    LoseScreen ls = new LoseScreen();
                    ls.Location = new Point((f.Width - ls.Width) / 2, (f.Height - ls.Height) / 2);

                    f.Controls.Add(ls);
                }
            }
            #endregion
            Refresh();
        }
Пример #2
0
        private void gameLoop_Tick(object sender, EventArgs e)
        {
            //TODO - update location of all boxes (drop down screen)
            foreach (Box b in boxes)
            {
                b.Move();
                //check for collision between hero and boxes
            }
            Form1.currentScore = sideCounter;
            scoreLabel.Text    = "Levels Completed: " + Convert.ToString(sideCounter);

            //add new box if it is time
            newBoxCounter++;
            //Level 1
            if (sideCounter == 0)
            {
                level1();
            }
            //Level 2
            if (sideCounter == 1)
            {
                level2();
            }
            //level 3
            if (sideCounter == 2)
            {
                level3();
            }
            //level 4
            if (sideCounter == 3)
            {
                level4();
            }
            //level 5
            if (sideCounter == 4)
            {
                level5();
            }
            //level 6
            if (sideCounter == 5)
            {
                level6();
            }
            //level 7
            if (sideCounter == 6)
            {
                level7();
            }
            //level 8
            if (sideCounter == 7)
            {
                level8();
            }
            //level 9
            if (sideCounter == 8)
            {
                level9();
            }
            //level 10
            if (sideCounter == 9)
            {
                level10();
            }
            //level 11
            if (sideCounter == 10)
            {
                level11();
            }
            if (sideCounter == 11)
            {
                level12();
            }
            if (sideCounter == 12)
            {
                level13();
            }
            if (sideCounter == 13)
            {
                level14();
            }
            if (sideCounter == 14)
            {
                if (Form1.gamemode == 2)
                {
                    gameLoop.Stop();

                    Form form = this.FindForm();
                    Menu ms   = new Menu();
                    ms.Location = new Point((form.Width - ms.Width) / 2, (form.Height - ms.Height) / 2);

                    form.Controls.Add(ms);
                    form.Controls.Remove(this);
                }
                if (Form1.gamemode == 1)
                {
                    level15();
                }
            }
            if (sideCounter == 15)
            {
                gameLoop.Stop();


                LoseScreen ls   = new LoseScreen();
                Form       form = this.FindForm();
                ls.Location = new Point((form.Width - ls.Width) / 2, (form.Height - ls.Height) / 2);

                form.Controls.Add(ls);
                form.Controls.Remove(this);
            }
            //this is a placeholder for more levels
            //sideCounter = 0;

            List <int> boxesToRemove = new List <int>();

            //remove box if it has gone of screen vertical
            foreach (Box b in boxes)
            {
                if (b.yspeed < 0 && (b.y + b.size) < -100)
                {
                    boxesToRemove.Add(boxes.IndexOf(b));
                }
                if (b.yspeed > 0 && (b.y + b.size) > this.Height + 100)
                {
                    boxesToRemove.Add(boxes.IndexOf(b));
                }
            }
            boxesToRemove.Sort();
            boxesToRemove.Reverse();

            //remove boxes from its original list based on index values
            foreach (int i in boxesToRemove)
            {
                boxes.RemoveAt(i);
            }

            boxesToRemove.Clear();

            //remove box if it has gone of screen horizontal
            foreach (Box b in boxes)
            {
                if (b.xspeed < 0 && (b.x + b.size) < this.Width - 800)
                {
                    boxesToRemove.Add(boxes.IndexOf(b));
                }
                if (b.xspeed > 0 && (b.x + b.size) > this.Width - 100)
                {
                    boxesToRemove.Add(boxes.IndexOf(b));
                }
            }
            boxesToRemove.Sort();
            boxesToRemove.Reverse();

            //remove boxes from its original list based on index values
            foreach (int i in boxesToRemove)
            {
                boxes.RemoveAt(i);
            }

            if (hero.x > this.Width - 50)
            {
                //Random randonGen = new Random();
                //Color randomColor = Color.FromArgb(randonGen.Next(255), randonGen.Next(255),
                //randonGen.Next(255));
                sideCounter++;
                hero.Move("right");
                hero.Move("right");
            }

            //move hero
            if (leftArrowDown)
            {
                hero.Move("left");
            }
            if (rightArrowDown)
            {
                hero.Move("right");
            }
            if (upArrowDown)
            {
                hero.Move("up");
            }
            if (downArrowDown)
            {
                hero.Move("down");
            }

            foreach (Box b in boxes)
            {
                if (hero.Collision(b))
                {
                    if (Form1.gamemode == 1)
                    {
                        gameLoop.Stop();


                        LoseScreen ls   = new LoseScreen();
                        Form       form = this.FindForm();
                        ls.Location = new Point((form.Width - ls.Width) / 2, (form.Height - ls.Height) / 2);

                        form.Controls.Add(ls);
                        form.Controls.Remove(this);
                        break;
                    }
                    if (Form1.gamemode == 2)
                    {
                        hero = new Box(50, 250, 20, heroyspeed, 0);
                        break;
                    }
                }
            }
            Refresh();
        }