Beispiel #1
0
 public void gameover(Player winner)
 {
     gameball.stop();
     gametimer.Stop();
     gametimer.Reset();
     endmessage = (winner.Name + " Wins!!....... Press Esc to Go to The Menu");
     gameend = true;
     winner.isWinner = true;
 }
Beispiel #2
0
 public void addPlayer(Player p, int playerIndex)
 {
     if (playerIndex == 1)
     {
         playerOne = p;
     }
     else if (playerIndex == 2)
     {
         playerTwo = p;
     }
 }
Beispiel #3
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            ScreenWidth = GraphicsDevice.Viewport.Width;
            ScreenHeight = GraphicsDevice.Viewport.Height;

            player1 = new Player();
            player2 = new Player();
            ball = new Ball();

            base.Initialize();
        }
Beispiel #4
0
        public void CollideToPlayer(Player player)
        {
            if (rect.Intersects(player.CollisionRectangle))
            {
                //Add a bounce in the stats
                Stats.addBounce();

                collisionRect.X -= (int)speed.X;
                if (!collisionRect.Intersects(player.CollisionRectangle))
                {
                    Assets.Audio.HitSound.Play();
                    bounceEmitter.shoot();

                    //Put the ball in the right position
                    if (speed.X > 0)
                    {
                        rect.X = player.CollisionRectangle.X - rect.Width - (rect.Right - player.CollisionRectangle.X);
                    }
                    else if(speed.X < 0)
                    {
                        rect.X = player.CollisionRectangle.X + player.CollisionRectangle.Width + player.CollisionRectangle.Right - rect.X;
                    }
                    speed.X = -speed.X;
                    //Curve effect when playerdirection is opposite of ball direction
                    if ((speed.Y >= 0 && player.MoveDirection == 1) || (speed.Y <= 0 && player.MoveDirection == -1))
                    {
                        //Add a curveball in the stats
                        Stats.addCurveball();

                        curveDirection = player.MoveDirection;
                    }
                    else
                    {
                        curveDirection = 0;
                    }
                }
                else
                {
                    collisionRect.X += (int)speed.X;
                    collisionRect.Y -= (int)speed.Y;
                    if (!collisionRect.Intersects(player.CollisionRectangle))
                    {
                        speed.Y = -speed.Y;
                    }
                }
            }
        }
Beispiel #5
0
 protected void gamestart()
 {
     playerleft = new Player(new Vector2(50, 480), font,"Player Left");
     playerright = new Player(new Vector2(600, 480), font,"Player Right");
     Left = new Paddle(0, 213, paddle, Keys.W, Keys.S);
     Right = new Paddle(740, 213, paddle, Keys.Up, Keys.Down);
     upb = new Boundary(0, 0, 750, 20);
     downb = new Boundary(0, 480, 750, 20);
     gameball = new Ball(ball, upb.boundary, downb.boundary, Left.boundary, Right.boundary, font);
     timestart.Start();
     timerstarted = true;
     gameball.keepinposition();
     gamescreen = GameScreens.GameState.Play;
     gametimer.Start();
 }
Beispiel #6
0
        private void createGamefield(Canvas c)
        {

          

            // right player = player one
            int height_rectangles = 120;
            int width_rectangles = 20;

            POne = new Player((int)c.ActualWidth-(50 + width_rectangles), (int)c.ActualHeight / 2-60);
            POne.setMin(0);
            POne.setMax((int)c.ActualHeight - height_rectangles);
            playerOne = new Rectangle();
            playerOne.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 0));
            playerOne.Width = width_rectangles;
            playerOne.Height = height_rectangles;

            // left player = player two
            PTwo = new Player(50,(int)c.ActualHeight/2-60);
            PTwo.setMin(0);
            PTwo.setMax((int)c.ActualHeight - height_rectangles);
            playerTwo = new Rectangle();
            playerTwo.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 0));
            playerTwo.Width = width_rectangles;
            playerTwo.Height = height_rectangles;

            // draw ´both player to canvas
            this.playField.Children.Add(playerTwo);
            this.playField.Children.Add(playerOne);

            // creats and draws ball to canvas
            ball.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255,255,255,0));
            ball.Width = 20;
            ball.Height = 20;
          //  ball.SetValue(Canvas.LeftProperty, 250);
          //  ball.SetValue(Canvas.TopProperty, 150);

            moving_ball = new Ball(350,250);
            moving_ball.setMax(500-20);
            moving_ball.setMin(0);

            this.playField.Children.Add(ball);
        }
Beispiel #7
0
        private void createGamefield(Canvas c)
        {

          

            // right player = player one
        //    int height_rectangles = 80;
          //  int width_rectangles = 20;

            POne = new Player((int)c.ActualWidth-(50 + width_rectangles), (int)c.ActualHeight / 2-60);
            POne.setMin(0);
            POne.setMax((int)c.ActualHeight - height_rectangles);
            playerOne = new Rectangle();
            playerOne.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 255, 0, 0));
            playerOne.Width = width_rectangles;
            playerOne.Height = height_rectangles;

            // left player = player two
            PTwo = new Player(50,(int)c.ActualHeight/2-60);
            PTwo.setMin(0);
            PTwo.setMax((int)c.ActualHeight - height_rectangles);
            playerTwo = new Rectangle();
            playerTwo.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 255, 0, 0));
            playerTwo.Width = width_rectangles;
            playerTwo.Height = height_rectangles;


            // draw obstacles
            obstacleOne = new Obstacle((int)c.ActualWidth / 2 - 10, (int)c.ActualHeight / 2 - 40);
            obstacleOne.min=0;
            obstacleOne.max=(int)c.ActualHeight - height_rectangles;
            obstacle1 = new Rectangle();
            obstacle1.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 123, 23, 123));
            obstacle1.Width = width_rectangles;
            obstacle1.Height = height_rectangles;

       //     Canvas.SetLeft(obstacle1, 300);
       //     Canvas.SetTop(obstacle1, 200);

       //     obstacle1.SetValue(Canvas.LeftProperty, 300);
       //     obstacle1.SetValue(Canvas.TopProperty, 300);

            this.level1.Children.Add(obstacle1);


            // draw ´both player to canvas
            this.level1.Children.Add(playerTwo);
            this.level1.Children.Add(playerOne);

            // creats and draws ball to canvas
            ball.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255,255,255,0));
            ball.Width = 20;
            ball.Height = 20;
          //  ball.SetValue(Canvas.LeftProperty, 250);
          //  ball.SetValue(Canvas.TopProperty, 150);

            moving_ball = new Ball(400,250);
            moving_ball.setMax(500-20);
            moving_ball.setMin(0);

            this.level1.Children.Add(ball);
        }
Beispiel #8
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Background = Content.Load<Texture2D>("Background.png");

            Texture2D playerTextureVertical = Content.Load<Texture2D>("padle.png");
            Texture2D playerTextureHorizontal = Content.Load<Texture2D>("padleHorizontal.png");

            Texture2D ballTexture = Content.Load<Texture2D>("Ball.png");

            ScoreFontP1 = Content.Load<SpriteFont>("SpriteFont1");
            ScoreFontP2 = Content.Load<SpriteFont>("SpriteFont1");


            padEffect = Content.Load<SoundEffect>("Blopp");



            for (int x = 0; x < players.Length; x++ )
            {
                if (x == 0 || x == 1)
                {
                    players[x] = new Player(playerTextureVertical, screenRect);
                }
                if(x == 2 || x == 3)
                {
                    players[x] = new Player(playerTextureHorizontal, screenRect);
                }
            }




            //player = new Player(playerTextureVertical, screenRect);
            //player2 = new Player(playerTextureVertical, screenRect);
            //player3 = new Player(playerTextureHorizontal, screenRect);
            //player4 = new Player(playerTextureHorizontal, screenRect);

            ball = new Ball(ballTexture, screenRect);


            for (int x = 0; x < players.Length; x++)
            {
                if(x == 0)
                {
                    players[x].SetFlapStartPos(5, graphics.PreferredBackBufferHeight / 2);
                }
                if (x == 1)
                {
                    players[x].SetFlapStartPos(graphics.PreferredBackBufferWidth - 5 - (playerTextureVertical.Width), graphics.PreferredBackBufferHeight / 2);
                }
                if (x == 2)
                {
                    players[x].SetFlapStartPos(graphics.PreferredBackBufferHeight - 5 - (playerTextureVertical.Height), graphics.PreferredBackBufferWidth / 2);
                }
                if (x == 3)
                {
                    players[x].SetFlapStartPos(graphics.PreferredBackBufferWidth / 2, 5);
                }
            }
            //player.SetFlapStartPos(5, graphics.PreferredBackBufferHeight / 2);
            //player2.SetFlapStartPos(graphics.PreferredBackBufferWidth - 5 - (playerTextureVertical.Width), graphics.PreferredBackBufferHeight / 2);
            //player3.SetFlapStartPos(graphics.PreferredBackBufferHeight - 5 - (playerTextureVertical.Height), graphics.PreferredBackBufferWidth / 2);
            //player4.SetFlapStartPos(graphics.PreferredBackBufferWidth / 2, 5);

            

            // TODO: use this.Content to load your game content here
        }
Beispiel #9
0
        public void startGame(GameMode gameMode, GraphicsDevice g)
        {
            playerManager = new PlayerManager();

            if (gameMode == GameMode.Multiplayer)
            {
                //Create players
                Player p1 = new Player(new Rectangle(10, g.Viewport.Height / 2 - 50, 20, 100), Assets.Colors.FlashyGreen, 3);
                p1.setControls(Keys.W,Keys.S);
                playerManager.addPlayer(p1, 1);

                Player p2 = new Player(new Rectangle(g.Viewport.Width - 30, g.Viewport.Height / 2 - 50, 20, 100), Assets.Colors.FlashyGreen, 3);
                p2.setControls(Keys.Up, Keys.Down);
                playerManager.addPlayer(p2, 2);
            }
            else if (gameMode == GameMode.Singleplayer)
            {
                //Create players
                Player p1 = new Player(new Rectangle(10, g.Viewport.Height / 2 - 50, 20, 100), Assets.Colors.FlashyGreen, 3);
                p1.setControls(ControlMode.Ai);
                playerManager.addPlayer(p1, 1);

                Player p2 = new Player(new Rectangle(g.Viewport.Width - 30, g.Viewport.Height / 2 - 50, 20, 100), Assets.Colors.FlashyGreen, 3);
                p2.setControls(Keys.Up, Keys.Down);
                playerManager.addPlayer(p2, 2);
            }

            //Spawn a ball
            pongBall = new PongBall();
            pongBall.Create(g);

            //Change state to start game
            gameState = GameState.Playing;
        }
Beispiel #10
0
        public void move(Canvas gameField, Ellipse ball, Player POne, Player PTwo)
        {

            int height_canvas = 500;
            int width_canvas = 700;
            int height_rectangles = 120;

            LastHitWIthPaddle++;


            this.x += ballspeedX;
            this.y += ballspeedY;
            Canvas.SetLeft(ball, getX());
            Canvas.SetTop(ball, getY());

            if (this.y < 0 || this.y > height_canvas - ball.Height)
            {
                ballspeedY *= -1;
            }

            if (this.x < 0 || this.x > width_canvas - ball.Width)
            {
                ballspeedX *= -1;
            }

            var playerOne = POne.getY();
            var playerTwo = PTwo.getY();

            // POne = rechter Spieler!!!
            // PTwo = linker Spieler!!!!
            // Deshalb <80 & >420 vertauschen!!!

            if (LastHitWIthPaddle < 10)
            {
                return;
            }

            if (this.x > POne.getX() + 20 || this.x < PTwo.getX())
            {

                return;
            }

            double distanceToPaddleOneMiddle = this.y + 10 - (POne.getY() + (height_rectangles / 2));
            double distanceToPaddleTwoMiddle = this.y + 10 - (PTwo.getY() + (height_rectangles / 2));

            if (this.y < POne.getY() + height_rectangles && this.y + 20 > POne.getY())
            {
                var x = ballspeedY < 0 ? this.x : this.x + 20;

                if (x > 610 && x < 630)
                {

                    ballspeedY = ballspeedY * ((distanceToPaddleOneMiddle / 10) / ballspeedY);

                    System.Diagnostics.Debug.WriteLine(ballspeedY);

                    this.ballspeedX = ballspeedX + Math.Abs(ballspeedY);
                    this.ballspeedX = ballspeedX > 0 ? ballspeedX : ballspeedX * -1;
                    ballspeedX *= -1;

                    LastHitWIthPaddle = 0;
                }
            }

            if (this.y < PTwo.getY() + height_rectangles && this.y + 20 > PTwo.getY())
            {


                if (x < 70 && x > 50)
                {

                    ballspeedY = ballspeedY * ((distanceToPaddleTwoMiddle / 10) / ballspeedY);

                    this.ballspeedX = ballspeedX + Math.Abs(ballspeedY);
                    this.ballspeedX = ballspeedX > 0 ? ballspeedX : ballspeedX * -1;

                    LastHitWIthPaddle = 0;
                }
            }
        }