Esempio n. 1
0
 public Game1()
 {
     server   = null;
     state    = PongState.Title;
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";
 }
Esempio n. 2
0
 public Game1()
 {
     server = null;
     state = PongState.Title;
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";
 }
Esempio n. 3
0
        void OnClientConnect(object sender, EventArgs e)
        {
            me  = new Paddle(this, new Vector2(80, 300));
            you = new Paddle(this, new Vector2(720, 300));
            Components.Add(me);
            Components.Add(you);

            state = PongState.Game;
        }
Esempio n. 4
0
        void OnServerConnect(object sender, EventArgs e)
        {
            client = Client.SetClient(server.client);

            me  = new Paddle(this, new Vector2(720, 300));
            you = new Paddle(this, new Vector2(80, 300));
            Components.Add(me);
            Components.Add(you);

            state = PongState.Game;
        }
Esempio n. 5
0
        void OnServerConnect(object sender, EventArgs e)
        {
            client = Client.SetClient(server.client);

            me = new Paddle(this, new Vector2(720, 300));
            you = new Paddle(this, new Vector2(80, 300));
            Components.Add(me);
            Components.Add(you);

            state = PongState.Game;
        }
Esempio n. 6
0
        void OnClientConnect(object sender, EventArgs e)
        {
            me = new Paddle(this, new Vector2(80, 300));
            you = new Paddle(this, new Vector2(720, 300));
            Components.Add(me);
            Components.Add(you);

            state = PongState.Game;
        }
Esempio n. 7
0
        protected override void Update(GameTime gameTime)
        {
            // Recebe o estado do teclado
            KeyboardState keyState = Keyboard.GetState();
            switch (state)
            {
                case PongState.IntroScreen:
                    // Entradas do jogador
                    if (keyState.IsKeyDown(Keys.D1))//Se pressionar a tecla 1
                    {
                        state = PongState.SinglePlayer;
                        RestartGame();
                    }
                    if (keyState.IsKeyDown(Keys.D2))//Se pressionar a tecla 2
                    {
                        state = PongState.MultiPlayer;
                        RestartGame();
                    }
                    // Se pressionar a tecla ESC encerra o jogo
                    if (keyState.IsKeyDown(Keys.Escape))
                        Exit();
                    break;

                case PongState.SinglePlayer:
                case PongState.MultiPlayer:
                    // Se pressionar a tecla ESC encerra o jogo
                    if (keyState.IsKeyDown(Keys.Escape))
                    {
                        state = PongState.IntroScreen;
                    }
                    // Entradas do jogador 1 (barra do lado esquerdo)
                    if (keyState.IsKeyDown(Keys.W))
                    {
                        if (barra1.Posicao.Y > 78.0f)
                            barra1.Direcao = new Vector2(0.0f, -1.0f);
                        else barra1.Direcao = Vector2.Zero;
                    }
                    else if (keyState.IsKeyDown(Keys.S))
                    {
                        if (barra1.Posicao.Y + barra1.Imagem.Height < 562.0f)
                            barra1.Direcao = new Vector2(0.0f, 1.0f);
                        else barra1.Direcao = Vector2.Zero;
                    }
                    else
                        barra1.Direcao = Vector2.Zero;
                    // Atualiza a posição da barra 1
                    barra1.Update(gameTime);

                    if (state == PongState.SinglePlayer)
                    {
                        // Se o jogo for para single player o computador
                        // é atualizado automáticamente
                        MoveIA();
                    }
                    else
                    {
                        // Entradas do jogador 2 (barra do lado direito)
                        if (keyState.IsKeyDown(Keys.Up))
                        {
                            if (barra2.Posicao.Y > 78.0f)
                                barra2.Direcao = new Vector2(0.0f, -1.0f);
                            else barra2.Direcao = Vector2.Zero;
                        }
                        else if (keyState.IsKeyDown(Keys.Down))
                        {
                            if (barra2.Posicao.Y + barra1.Imagem.Height < 562.0f)
                                barra2.Direcao = new Vector2(0.0f, 1.0f);
                            else barra2.Direcao = Vector2.Zero;
                        }
                        else
                            barra2.Direcao = Vector2.Zero;
                    }
                    // Atualiza a posição da barra 2
                    barra2.Update(gameTime);

                    // Checa colisões da bola com as paredes do campo
                    // Verifica se a bola colidiu em baixo
                    if (bola.Posicao.Y + bola.Imagem.Height > 570.0f)
                    {
                        // Inverte a direção em Y do vetor
                        bola.Direcao *= new Vector2(1.0f, -1.0f);
                        // Toca o efeito sonoro de colisão com o campo
                        soundToc1.Play();
                    }
                    // Verifica se a bola colidiu em baixo
                    if (bola.Posicao.Y < 70.0f)
                    {
                        // Inverte a direção em Y do vetor
                        bola.Direcao *= new Vector2(1.0f, -1.0f);
                        // Toca o efeito sonoro de colisão com o campo
                        soundToc1.Play();
                    }
                    // Atualiza a posiação da bola
                    bola.Update(gameTime);
                    // Verifica se alguem marcou ponto
                    // Se a bola passar pela direita da tela
                    if (bola.Posicao.X + bola.Imagem.Width > 800.0f)
                    {
                        // Toca o efeito sonoro para marcar pontos
                        soundPoint.Play();
                        score[0] += 1; // aumenta um ponto ao score do jogador 1
                        // Coloca a bola no centro do campo novamente
                        bola.Posicao = new Vector2(386.0f, 310.0f);
                        // Muda a direção de disparo da bola em X
                        bola.Direcao *= new Vector2(-1.0f, 1.0f);
                    }
                    // Se a bola passar pela esquerda da tela
                    else if (bola.Posicao.X < 0.0f)
                    {
                        // Toca o efeito sonoro para marcar pontos
                        soundPoint.Play();
                        score[1] += 1; // aumenta um ponto ao score do jogador 2
                        // Coloca a bola no centro do campo novamente
                        bola.Posicao = new Vector2(386.0f, 310.0f);
                        // Muda a direção de disparo da bola
                        bola.Direcao *= new Vector2(-1.0f, 1.0f);
                    }

                    // Checa a colisão da bola com as raquetes dos jogadores
                    // Jogador 1 (Barra da esquerda)
                    if (bola.GetBounding().Intersects(barra1.GetBounding()))
                    {
                        // Centro da bola
                        Vector2 cBall = new Vector2(bola.GetBounding().Center.X, bola.GetBounding().Center.Y);
                        cBall.Normalize();
                        // Centro da barra a esquerda (jogador 1)
                        Vector2 cBat = new Vector2(barra1.GetBounding().Center.X, barra1.GetBounding().Center.Y);
                        cBat.Normalize();
                        // Angulo de direção
                        double angDir = Math.Atan2(cBall.Y - cBat.Y, cBall.X - cBat.X);
                        // Inverte a direção X da bola
                        bola.Direcao = new Vector2((float)Math.Cos(angDir), (float)Math.Sin(angDir));
                        // Toca o efeito sonoro de colisão com a bola
                        soundToc2.Play();
                    }
                    // Jogador 2 (Barra da direita)
                    if (bola.GetBounding().Intersects(barra2.GetBounding()))
                    {
                        // Centro da bola
                        Vector2 cBall = new Vector2(bola.GetBounding().Center.X, bola.GetBounding().Center.Y);
                        cBall.Normalize();
                        // Centro da barra a esquerda (jogador 2)
                        Vector2 cBat = new Vector2(barra2.GetBounding().Center.X, barra2.GetBounding().Center.Y);
                        cBat.Normalize();
                        // Angulo de direção
                        double angDir = Math.Atan2(cBall.Y - cBat.Y, cBall.X - cBat.X);
                        // Inverte a direção X da bola
                        bola.Direcao *= new Vector2(-1.0f, 1.0f);
                        // Toca o efeito sonoro de colisão com a bola
                        soundToc2.Play();
                    }
                    // Verifica se algum jogador chegou ao limite de pontos da partida
                    // Se o jogador 1 ganhou
                    if (score[0] >= POINT_COUNT)
                    {
                        state = PongState.GameOver;
                    }
                    // Se o jogador 2 ganhou
                    if (score[1] >= POINT_COUNT)
                    {
                        state = PongState.GameOver;
                    }
                    break;

                case PongState.GameOver:
                    // Entradas do usuario
                    if (keyState.IsKeyDown(Keys.Enter)) // Se pressionar ENTER
                    {
                        state = PongState.IntroScreen;
                    }
                    break;
            }
            base.Update(gameTime);
        }