Пример #1
0
 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 static void Main(string[] args)
 {
     using (Pong game = new Pong())
     {
         game.Run();
     }
 }
Пример #2
0
 public void InitializeGame()
 {
     if (!offline && (!client.Initialized() || !server.Initialized()))
         throw new Exception("Connection unitialized. Can't start a game");
     if (offline) {
         left = new HumanPlayer(Side.LEFT);
         right = new HumanPlayer(Side.RIGHT);
     } else if (isServer) {
         left = new HumanPlayer(Side.LEFT);
         right = new NetworkPlayer(Side.RIGHT);
     } else {
         left = new NetworkPlayer(Side.LEFT);
         right = new HumanPlayer(Side.RIGHT);
     }
     WorldController wrld = new WorldController(left, right, server, client, isServer);
     Overseer overseer = new Overseer(wrld);
     BallObject ball = new BallObject(left, right, overseer);
     wrld.AttachBall(ball);
     using (var game = new Pong(left, right, ball))
         game.Run();
 }
Пример #3
0
 static void Main()
 {
     using (var game = new Pong())
         game.Run();
 }
Пример #4
0
 public void Update(float timeRatio, Pong pong, RectangleF area, Keys keysEvent,
     Point? mouseLocation)
 {
     this.Intelligence(area, timeRatio, pong.Ball, this, keysEvent, mouseLocation);
 }
Пример #5
0
 static void Main()
 {
     using (var game = new Pong())
         game.Run();
 }
Пример #6
0
 public void Update(float timeRatio, Pong pong, RectangleF area, Keys keysEvent,
                    Point?mouseLocation)
 {
     this.Intelligence(area, timeRatio, pong.Ball, this, keysEvent, mouseLocation);
 }
Пример #7
0
        public void Update(float timeRatio, Pong pong, RectangleF area)
        {
            // Retourne une valeur infinie si le nombre est négatif
            // ou nul
            Func <float, float> PositiveOrInf = (value) => {
                if (value <= 0)
                {
                    return(float.PositiveInfinity);
                }
                else
                {
                    return(value);
                }
            };

            // Déplace la balle sans faire de vérifications
            Action <float> MoveStupid = (moveTime) => {
                this.CenterX = this.CenterX + this.CurrentMove.X * moveTime;
                this.CenterY = this.CenterY + this.CurrentMove.Y * moveTime;
            };

            // Rectangle de la balle
            var rayon  = Size / 2;
            var top    = this.CenterY - rayon;
            var left   = this.CenterX - rayon;
            var bottom = this.CenterY + rayon;
            var right  = this.CenterX + rayon;

            // Bordures du jeu
            var minX = Racquet.GoalDist;
            var maxX = area.Width - Racquet.GoalDist;
            var minY = PlayGround.Margin;
            var maxY = area.Height - PlayGround.Margin;

            // Temps nécéssaire pour toucher les bords
            // Rmks: x / 0 avec un float donne +inf
            var timeMinX = PositiveOrInf((minX - left) / this.CurrentMove.X);
            var timeMaxX = PositiveOrInf((maxX - right) / this.CurrentMove.X);
            var timeMinY = PositiveOrInf((minY - top) / this.CurrentMove.Y);
            var timeMaxY = PositiveOrInf((maxY - bottom) / this.CurrentMove.Y);

            // Sélectionne le bord touché en premier
            if (timeMinX < timeRatio && timeMinX < timeMinY && timeMinX < timeMaxY)
            {
                // Bord gauche
                MoveStupid(timeMinX);

                if (pong.LeftRacquet.OnTheRacquet((int)this.CenterY))
                {
                    // Sur la raquette
                    this.CurrentMove = pong.LeftRacquet.NewMove(
                        this.CurrentMove, (int)this.CenterY
                        );

                    this.Update(timeRatio - timeMinX, pong, area);
                }
                else
                {
                    // Perd le point
                    pong.Scores.AddRight();
                    this.Init();
                }
            }
            else if (timeMaxX < timeRatio && timeMaxX < timeMinY && timeMaxX < timeMaxY)
            {
                // Bord droit
                MoveStupid(timeMaxX);

                if (pong.RightRacquet.OnTheRacquet((int)this.CenterY))
                {
                    // Sur la raquette
                    this.CurrentMove = pong.RightRacquet.NewMove(
                        this.CurrentMove, (int)this.CenterY
                        );

                    this.Update(timeRatio - timeMaxX, pong, area);
                }
                else
                {
                    // Perd le point
                    pong.Scores.AddLeft();
                    this.Init();
                }
            }
            else if (timeMinY < timeRatio && timeMinY < timeMinX && timeMinY < timeMaxX)
            {
                // Bord supérieur
                MoveStupid(timeMinY);
                this.CurrentMove.Y *= -1;
                this.Update(timeRatio - timeMinY, pong, area);
            }
            else if (timeMaxY < timeRatio && timeMaxY < timeMinX && timeMaxY < timeMaxX)
            {
                // Bord inférieur
                MoveStupid(timeMaxY);
                this.CurrentMove.Y *= -1;
                this.Update(timeRatio - timeMaxY, pong, area);
            }
            else
            {
                // Ne touche rien
                MoveStupid(timeRatio);
            }
        }
Пример #8
0
        static void Main(string[] args)
        {
            Pong game = new Pong();

            // todo: kick off 2 threads , one for rendering the game and one for processing inputs
        }
Пример #9
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            KeyboardState Keystate          = Keyboard.GetState();
            MouseState    CurrentMouseState = Mouse.GetState();
            MouseState    PrevMouseState    = CurrentMouseState;

            switch (CurrentGameState)
            {
            case Gamestate.MainMenu:
                m_Lives1.Reset();
                m_Lives2.Reset();
                Back.IsClicked = false;
                if (PlayButton.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released)
                {
                    CurrentGameState = Gamestate.Playing;
                }
                PlayButton.Update(CurrentMouseState);
                //if (Options.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released) { CurrentGameState = Gamestate.Options; }
                //Options.Update(CurrentMouseState);
                break;

            /*case Gamestate.Options:
             *  if (Full.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released && CurrentMouseState.LeftButton == ButtonState.Released)
             *  {
             *      graphics.ToggleFullScreen();
             *      Full.IsClicked = false;
             *  }
             *  Full.Update(CurrentMouseState);
             *  if (Back.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released)
             *      CurrentGameState = Gamestate.MainMenu;
             *  Back.Update(CurrentMouseState);
             *  break;*/
            case Gamestate.Playing:
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    m_Bar1.SetVel(m_Bar1.GetMaxVel());
                }
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    m_Bar1.SetVel(-m_Bar1.GetMaxVel());
                }
                if (Keystate.IsKeyDown(Keys.Down))
                {
                    m_Bar2.SetVel(m_Bar2.GetMaxVel());
                }
                if (Keystate.IsKeyDown(Keys.Up))
                {
                    m_Bar2.SetVel(-m_Bar2.GetMaxVel());
                }
                if (m_Bar1.GetPosY() <= 0 && m_Bar1.GetVel() < 0 || Keyboard.GetState().IsKeyUp(Keys.S) && m_Bar1.GetVel() > 0)
                {
                    m_Bar1.SetVel(0);
                }
                if (m_Bar1.GetPosY() + m_Bar2.GetHeight() >= graphics.GraphicsDevice.Viewport.Height && m_Bar1.GetVel() > 0 || Keyboard.GetState().IsKeyUp(Keys.W) && m_Bar1.GetVel() < 0)
                {
                    m_Bar1.SetVel(0);
                }
                if (m_Bar2.GetPosY() <= 0 && m_Bar2.GetVel() < 0 || Keyboard.GetState().IsKeyUp(Keys.Down) && m_Bar2.GetVel() > 0)
                {
                    m_Bar2.SetVel(0);
                }
                if (m_Bar2.GetPosY() + m_Bar2.GetHeight() >= graphics.GraphicsDevice.Viewport.Height && m_Bar2.GetVel() > 0 || Keyboard.GetState().IsKeyUp(Keys.Up) && m_Bar2.GetVel() < 0)
                {
                    m_Bar2.SetVel(0);
                }
                if (m_Ball.GetPosX() <= m_Bar1.GetWidth())
                {
                    if (m_Ball.GetMidPos() <= m_Bar1.GetPosY() + m_Bar1.GetHeight() && m_Ball.GetMidPos() >= m_Bar1.GetPosY())
                    {
                        m_Ball.InverseVelX();
                        m_Ball.SetPosX(m_Bar1.GetWidth() + 1);
                        m_Ball.IncreaseVel();
                        float DTM = (m_Bar1.GetMiddlePos() - m_Ball.GetMidPos()) / (-m_Bar1.GetHeight() / 2);
                        m_Ball.ModVelY(DTM);
                        Ping.Play();
                    }
                    else
                    {
                        if (m_Ball.GetPosX() <= -m_Ball.GetSize())
                        {
                            m_Ball.SetPos(new Vector2((graphics.GraphicsDevice.Viewport.Width / 2), (graphics.GraphicsDevice.Viewport.Height / 2)));
                            m_Ball.SetVelX(-m_Ball.GetStartVelX());
                            m_Ball.SetMaxVelY(m_Ball.GetStartVelY());
                            m_Ball.SetVelY((float)(m_Bar1.GetMiddlePos() - (m_Ball.GetMidPos())) / ((graphics.GraphicsDevice.Viewport.Width / 2) / m_Ball.GetStartVelX()));
                            m_Lives1.RemoveOne();
                            if (m_Lives1.GetLivesInt() == 0)
                            {
                                CurrentGameState = Gamestate.GameOver;
                            }
                        }
                    }
                }
                if (m_Ball.GetPosX() + m_Ball.GetSize() >= graphics.GraphicsDevice.Viewport.Width - m_Bar2.GetWidth())
                {
                    if (m_Ball.GetMidPos() <= m_Bar2.GetPosY() + m_Bar2.GetHeight() && m_Ball.GetMidPos() >= m_Bar2.GetPosY())
                    {
                        m_Ball.InverseVelX();
                        m_Ball.SetPosX(graphics.GraphicsDevice.Viewport.Width - m_Bar2.GetWidth() - m_Ball.GetSize() - 1);
                        m_Ball.IncreaseVel();
                        float DTM = (m_Bar2.GetMiddlePos() - m_Ball.GetMidPos()) / (-m_Bar2.GetHeight() / 2);
                        m_Ball.ModVelY(DTM);
                        Pong.Play();
                    }
                    else
                    {
                        if (m_Ball.GetPosX() >= graphics.GraphicsDevice.Viewport.Width)
                        {
                            m_Ball.SetPos(new Vector2((graphics.GraphicsDevice.Viewport.Width / 2), (graphics.GraphicsDevice.Viewport.Height / 2)));
                            m_Ball.SetVelX(m_Ball.GetStartVelX());
                            m_Ball.SetMaxVelY(m_Ball.GetStartVelY());
                            m_Ball.SetVelY((float)(m_Bar2.GetMiddlePos() - (m_Ball.GetMidPos())) / ((graphics.GraphicsDevice.Viewport.Width / 2) / m_Ball.GetStartVelX()));
                            m_Lives2.RemoveOne();
                            if (m_Lives2.GetLivesInt() == 0)
                            {
                                CurrentGameState = Gamestate.GameOver;
                            }
                        }
                    }
                }

                float MovedPos1     = m_Bar1.GetPosY() + m_Bar1.GetVel() * (float)gameTime.ElapsedGameTime.TotalSeconds;
                float MovedPos2     = m_Bar2.GetPosY() + m_Bar2.GetVel() * (float)gameTime.ElapsedGameTime.TotalSeconds;
                float MovedBallPosX = m_Ball.GetPosX() + m_Ball.GetVelX() * (float)gameTime.ElapsedGameTime.TotalSeconds;
                float MovedBallPosY = m_Ball.GetPosY() + m_Ball.GetVelY() * (float)gameTime.ElapsedGameTime.TotalSeconds;

                m_Bar1.SetPos(MovedPos1);
                m_Bar2.SetPos(MovedPos2);
                m_Ball.SetPosX(MovedBallPosX);
                m_Ball.SetPosY(MovedBallPosY);

                if (m_Ball.GetPosY() + m_Ball.GetSize() >= graphics.GraphicsDevice.Viewport.Height)
                {
                    m_Ball.InverseVelY();
                    Pang2.Play();
                }
                else if (m_Ball.GetPosY() < 0 && m_Ball.GetVelY() < 0)
                {
                    m_Ball.InverseVelY();
                    Pang2.Play();
                }
                break;

            case Gamestate.GameOver:
                PlayButton.IsClicked = false;
                MouseState PrevMouseState3 = Mouse.GetState();
                if (Back.IsClicked == true && PrevMouseState3.LeftButton == ButtonState.Released)
                {
                    CurrentGameState = Gamestate.MainMenu;
                }
                Back.Update(CurrentMouseState);
                break;
            }

            base.Update(gameTime);
        }
Пример #10
0
        protected override void DoWork()
        {
            // Wait until we can start
            this.Query(EntityType.SIGNAL,"start");
            int leftPlayerId  = 1;
            int rightPlayerId = 2;

            // Read the player names
            PlayerInfo leftplayer  = (PlayerInfo)this.Query(EntityType.PLAYERINFO,leftPlayerId,typeof(string),typeof(int));
            PlayerInfo rightplayer = (PlayerInfo)this.Query(EntityType.PLAYERINFO,rightPlayerId,typeof(string),typeof(int));

            // Keep iterating while the state is 'running'
            while (this.QueryP(EntityType.SIGNAL,"running",true) != null)
            {
                // Get the position so we can update it
                Pong pong = (Pong)this.Get(EntityType.PONG,typeof(double),typeof(double),typeof(double),typeof(double),typeof(double));

                // Calculate new position based on the direction
                Vector newPosition = pong.Position + pong.Direction * pong.Speed;

                // Check if the pong is beyond top and bottom walls
                // if is has hit the walls, then change the direction
                if (newPosition.Y < 0 || newPosition.Y >= this.height)
                {
                    pong.Direction = this.ChangeDirection(pong.Direction,false);
                    newPosition    = pong.Position + pong.Direction * pong.Speed;
                }

                // Check if the poing has reached the left most wall
                if (newPosition.X <= 0)
                {
                    // Read the latest position of the player
                    Position playerPosition = (Position)this.Query(EntityType.POSITION,leftPlayerId,typeof(double),typeof(double));

                    // Check if the pong hit the player pad, then change the position and direction
                    // otherwise reward the other player
                    if ((int)playerPosition.Y == (int)newPosition.Y)
                    {
                        pong.Direction = this.ChangeDirection(pong.Direction,true);
                        newPosition    = pong.Position + pong.Direction * pong.Speed;
                    }
                    else
                    {
                        this.IncreasePlayerScore(rightplayer.Name,leftplayer.Name);
                        continue;
                    }
                }

                // Check if the poing has reached the right most wall
                if (newPosition.X >= this.width)
                {
                    // Read the latest position of the player
                    Position playerPosition = (Position)this.Query(EntityType.POSITION,rightPlayerId,typeof(double),typeof(double));

                    // Check if the pong hit the player pad, then change the position and direction
                    // otherwise reward the other player
                    if ((int)playerPosition.Y == (int)newPosition.Y)
                    {
                        pong.Direction = this.ChangeDirection(pong.Direction,true);
                        newPosition    = pong.Position + pong.Direction * pong.Speed;
                    }
                    else
                    {
                        this.IncreasePlayerScore(leftplayer.Name,rightplayer.Name);
                        continue;
                    }
                }

                // Update the pong information based on potential changes
                double pongY = Math.Max(newPosition.Y,0d);
                pongY   = Math.Min(pongY,(double)(this.height - 1));
                pong[1] = newPosition.X;
                pong[2] = pongY;
                this.Put(pong);
                Thread.Sleep(100);
            }
        }