private void StartGame() { isWritten = false; // sets if the written method has been done to false P1bat.startPosP1(); //sets the starting position of the bat P2bat.startPosP2(); // sets the starting position of the bat ball.StartPosBall(P1bat.GetBoundary(), true); //sets starting pos of ball bricks = new Bricks[bricksWidth, bricksHeight]; //creates a new array of width and high changes colour depending on which row the bricks are in for (int i = 0; i < bricksHeight; i++) { Color colour = Color.White; switch (i) { case 0: colour = Color.HotPink; break; case 1: colour = Color.Red; break; case 2: colour = Color.MidnightBlue; break; case 3: colour = Color.Orange; break; case 4: colour = Color.Green; break; } for (int x = 0; x < bricksWidth; x++) { bricks[x, i] = new Bricks(brickImage, new Rectangle(x * brickImage.Width, i * brickImage.Height + 300, brickImage.Width, brickImage.Height), colour); // creates rectangle ofr each brick created } } }
//FUNCTGION TO START THE GAME private void StartGame() { isWritten = false; P1bat.startPosP1(); //sets the starting position of the bat P2bat.startPosP2(); ball.StartPosBall(P1bat.GetBoundary(), true); //sets starting pos of ball //CREATES THE BRICKS AND SETS THE COLOUR DEPENDING ON WHICH ROW THEY ARE bricks = new Bricks[bricksWidth, bricksHeight]; for (int i = 0; i < bricksHeight; i++) { Color colour = Color.White; switch (i) { case 0: colour = Color.Red; break; case 1: colour = Color.MidnightBlue; break; case 2: colour = Color.Green; break; case 3: colour = Color.Orange; break; case 4: colour = Color.Pink; break; } for (int x = 0; x < bricksWidth; x++) { bricks[x, i] = new Bricks(brickImage, new Rectangle(x * brickImage.Width, i * brickImage.Height + 300, brickImage.Width, brickImage.Height), colour); } } }
/// <summary> /// Updates the state of the game. This method checks the GameScreen.IsActive /// property, so the game will stop updating when the pause menu is active, /// or if you tab away to a different application. /// </summary> public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, false); // Gradually fade in or out depending on whether we are covered by the pause screen. if (coveredByOtherScreen) { pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1); } else { pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0); } // if the screen is active if (IsActive) { if (p1lives != 0) { ball.UpdatePos(); // checks the collision for each the bricks AND WHICH PLAYER TO AWWARD SCORE TO foreach (Bricks brick in bricks) { if (lastcollp1) { p1score += brick.CollisionCheck(ball); } else { p2score += brick.CollisionCheck(ball); } } //checsk if the ball hits which bat lastcollp1 = ball.BatCollision(P1bat.GetBoundary(), true, lastcollp1); lastcollp1 = ball.BatCollision(P2bat.GetBoundary(), false, lastcollp1); //checsk fi the ball leaves the bottom of the screen if so remove player one lives lives by one awward player 2 score if (ball.BottomCheck()) { p1lives -= 1; ball.StartPosBall(P2bat.GetBoundary(), false); lastcollp1 = false; p2score += 20; } //checks if the ball leave the top of the screen if so remove player 2 lives by one and awward player 1 score if (ball.TopCheck()) { p2lives -= 1; ball.StartPosBall(P1bat.GetBoundary(), true); lastcollp1 = true; p1score += 20; } } if (p1lives == 0 || p2lives == 0)//checks if gameover { //if player 1 > is bigger the player 2 score then p1 wins nad write that to database if (p1score > p2score) { inputscore = (int)p1score; } else { inputscore = (int)p2score; } if (!isWritten)//chesk if this method has been run before { //resets pos of ball ball.StartPosBall(P1bat.GetBoundary(), true); //gets the name of the user userinput = Microsoft.VisualBasic.Interaction.InputBox("Name", "Please Enter Your Name", "AAAAA").ToString(); // max length of 10 characters int maxstrlength = 10; // if the user inputs more than 10 chars it will cut off any chars moe than 10 if (userinput.Length > maxstrlength) { userinput = userinput.Substring(0, maxstrlength); } //add tehs name ot the player table dbConn.addNametoDb(userinput); //ads the score and mode to the highscores table dbConn.addtoDB(gamemode, inputscore); isWritten = true; } } } }
/// <summary> /// Updates the state of the game. This method checks the GameScreen.IsActive /// property, so the game will stop updating when the pause menu is active, /// or if you tab away to a different application. /// </summary> public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, false); // Gradually fade in or out depending on whether we are covered by the pause screen. if (coveredByOtherScreen) { pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1); } else { pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0); } // if the screen is active if (IsActive) { if (lives != 0) { //moves tbe ball position ball.UpdatePos(); // checks the collision for each the bricks foreach (Bricks brick in bricks) { score += brick.CollisionCheck(ball); } //checsk if the ball hits the bat ball.BatCollision(P1bat.GetBoundary(), true); ball.BatCollision(P2bat.GetBoundary(), false); //checsk fi the ball leaves the bottom of the screen if so remove lives by one and set it back to ontop of the bat if (ball.BottomCheck()) { lives -= 1; ball.StartPosBall(P1bat.GetBoundary(), true); } //checks if the ball leave the top of the screen if so remove lives by one and set it back to ontop of the bat if (ball.TopCheck()) { lives -= 1; ball.StartPosBall(P1bat.GetBoundary(), true); } } //checks if the lives are = 0 if (lives == 0) { //checks if this method has been run before if (!isWritten) { //sets the inputscore to the score of the game int inputscore = (int)score; //moves the ball back ontop of the bat ball.StartPosBall(P1bat.GetBoundary(), true); //asks the user for there name userinput = Microsoft.VisualBasic.Interaction.InputBox("Name", "Please Enter Your Name", "AAAAA").ToString(); // max length of 10 characters int maxstrlength = 10; // if the user inputs more than 10 chars it will cut off any chars moe than 10 if (userinput.Length > maxstrlength) { userinput = userinput.Substring(0, maxstrlength); } //addsthe name to the player database dbConn.addNametoDb(userinput); //adds the gamemode and score to the highscores table dbConn.addtoDB(gamemode, inputscore); //sets that this method has been run before isWritten = true; } } } }