Example #1
0
        void Start()
        {
            StartPosistion = transform.position;
            _game          = FindObjectOfType <PongGame>();
            _ball          = FindObjectOfType <PongBall>();
            if (Side == Player.Player1)
            {
                _grid = new QGrid(16, transform,
                                  new GridSettings {
                    Offset = new Vector3(9.8f, 0, 0), ResolutionX = 1.28f, ResolutionY = 1.28f
                });
                _vect = Vector <float> .Build.Dense(new[] { 1f });

                QAIManager.InitAgent(this, new QOption {
                    LearningRate = 0.005f,
                    NetworkArgs  = new [] { new CNNArgs {
                                                FilterSize = 4, FilterCount = 1, PoolLayerSize = 2, Stride = 2
                                            } },
                    MaxPoolSize      = 2000,
                    BatchSize        = 2000,
                    EpsilonStart     = 0.5f,
                    EpsilonEnd       = 0.1f,
                    Discount         = 0.95f,
                    TrainingInterval = 20,
                    TrainingCycle    = 10,
                });

                if (QAIManager.CurrentMode == QAIMode.Learning || QAIManager.CurrentMode == QAIMode.Testing)
                {
                    Time.timeScale = 5f;
                }
            }
            StartCoroutine(Movement());
        }
Example #2
0
 public void update(GraphicsDevice g, PongBall pongBall)
 {
     //Update players if ball is not paused
     if (!pongBall.IsPaused)
     {
         playerOne.update(g, pongBall);
         playerTwo.update(g, pongBall);
     }
 }
Example #3
0
 void Start()
 {
     _controller = GetComponent <PongController>();
     _ball       = FindObjectOfType <PongBall>();
 }
Example #4
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);
     particle_id = XNANF.ParticleSystem.ParticleEngine.Instance.LoadFromFile("explosion blue", Content);
     Player1 = new PongPlayer(PlayerIndex.One, 1, Content.Load<Texture2D>(@"paddle"));
     Player2 = new PongPlayer(PlayerIndex.One, 2, Content.Load<Texture2D>(@"paddle"));
     ball = new PongBall(Content.Load<Texture2D>(@"ball"), null, new Vector2(1280 / 2, 720 / 2), 1.0f);
     font = Content.Load<SpriteFont>(@"gameFont");
     // TODO: use this.Content to load your game content here
 }
Example #5
0
 public void update(GraphicsDevice g, PongBall pongBall)
 {
     if (ControlMode.Player == controlMode)
     {
         //Movement using input and adjust moveDirection for the curveball
         moveDirection = 0;
         if (InputState.isKeyDown(up))
         {
             rect.Y -= movementSpeed;
             moveDirection = 1; //Is moving up
         }
         if (InputState.isKeyDown(down))
         {
             rect.Y += movementSpeed;
             if(moveDirection == 1)
             {
                 moveDirection = 0; //is NOT moving, the up and down keys are both pressed
             }
             else
             {
                 moveDirection = -1; //Moving down.
             }
         }
     }
     else if(ControlMode.Ai == controlMode)
     {
         //Move towards the ball if the ball is moving towards the AI
         if ((pongBall.Position.X > rect.Center.X && pongBall.Speed.X < 0) || (pongBall.Position.X < rect.Center.X && pongBall.Speed.X > 0))
         {
             //Move towards the ball if the ball is on the AI's half of the screen
             if (pongBall.Position.X < g.Viewport.Width / 2)
             {
                 if (pongBall.Position.Y > rect.Center.Y)
                 {
                     rect.Y += movementSpeed - lives;
                 }
                 if (pongBall.Position.Y < rect.Center.Y)
                 {
                     rect.Y -= movementSpeed - lives;
                 }
             }
         }
     }
     //Restrict movement to viewport
     if (rect.Y < 0)
     {
         rect.Y = 0;
         moveDirection = 0;
     }
     else if (rect.Bottom > g.Viewport.Height)
     {
         rect.Y = g.Viewport.Height - rect.Height;
         moveDirection = 0;
     }
 }
Example #6
0
        /// <summary>
        /// Update the game manager.
        /// This method has to be called every loop. It updates a part of the game after determining the gamestate.
        /// </summary>
        /// <param name="g">GraphicsDevice used for screen size calculations.</param>
        public void update(GraphicsDevice g)
        {
            //Check what the gamestate is.
            switch (gameState)
            {
                case GameState.StartScreen: //The game is on the startscreen. Not in the menu yet.
                    if (InputState.isKeyPressed(startButton))
                    {
                        //If the startbutton is pressed, the game will move on to the menu.
                        gameState = GameState.Menu;
                    }
                    break;
                case GameState.Playing: //The game is playing. All game logic is done here.
                    //Update playermanager
                    playerManager.update(g, pongBall);
                    //Update the ball
                    pongBall.Update(g);

                    //Collide players to ball
                    pongBall.CollideToPlayer(playerManager.playerOne);
                    pongBall.CollideToPlayer(playerManager.playerTwo);

                    //Kill ball if outside screen (point scored)
                    if (pongBall.Position.X > g.Viewport.Width)
                    {
                        //Player 2 loses life
                        playerManager.playerTwo.Lives--;
                        pongBall.Create(g);
                        Assets.Audio.Death.Play();

                        //Reset players
                        playerManager.reset(g);
                    }
                    else if (pongBall.Position.X < 0)
                    {
                        //Player 1 loses life
                        playerManager.playerOne.Lives--;
                        pongBall.Create(g);
                        Assets.Audio.Death.Play();
                        //Reset players
                        playerManager.reset(g);
                    }

                    //Update particles
                    ParticleManager.update(g);

                    //Pause game
                    if (InputState.isKeyPressed(pauseButton))
                    {
                        gameState = GameState.Paused;
                        pongBall.Pause();
                    }
                    //Game Over when a player is dead
                    if (playerManager.playerOne.Lives <= 0)
                    {
                        gameState = GameState.GameOver;
                        playerDead = 0;
                        playing = false;
                        //Kill ball
                        pongBall = null;
                    }
                    else if (playerManager.playerTwo.Lives <= 0)
                    {
                        gameState = GameState.GameOver;
                        playerDead = 1;
                        playing = false;
                        //Kill ball
                        pongBall = null;
                    }
                    else
                    {
                        playing = true;
                    }
                    break;
                case GameState.Paused:
                    //Unpause game
                    if (InputState.isKeyPressed(pauseButton))
                    {
                        gameState = GameState.Playing;
                    }
                    //Highlight text when hovering over it
                    if (InputState.currentMouse.Y > g.Viewport.Height / 3 + 2 * Assets.MenuFont.MeasureString(pausedContinue).Y - rectYOffset && InputState.currentMouse.Y < g.Viewport.Height / 3 + 3 * Assets.MenuFont.MeasureString(pausedContinue).Y - rectYOffset)
                    {
                        pauseSelected = 1;
                    }
                    else if (InputState.currentMouse.Y > g.Viewport.Height / 3 + 4 * Assets.MenuFont.MeasureString(toMenu).Y - rectYOffset && InputState.currentMouse.Y < g.Viewport.Height / 3 + 5 * Assets.MenuFont.MeasureString(toMenu).Y - rectYOffset)
                    {
                        pauseSelected = 2;
                    }
                    else
                    {
                        pauseSelected = 0;
                    }
                    //Check for clicking
                    if (InputState.leftClick())
                    {
                        if (InputState.currentMouse.Y > g.Viewport.Height / 3 + 2 * Assets.MenuFont.MeasureString(pausedContinue).Y - rectYOffset && InputState.currentMouse.Y < g.Viewport.Height / 3 + 3 * Assets.MenuFont.MeasureString(pausedContinue).Y - rectYOffset)
                        {
                            gameState = GameState.Playing;
                        }
                        else if (InputState.currentMouse.Y > g.Viewport.Height / 3 + 4 * Assets.MenuFont.MeasureString(toMenu).Y - rectYOffset && InputState.currentMouse.Y < g.Viewport.Height / 3 + 5 * Assets.MenuFont.MeasureString(toMenu).Y - rectYOffset)
                        {
                            gameState = GameState.Menu;
                        }
                        pauseSelected = 0;
                    }
                    break;
                case GameState.GameOver:

                    //Check for clicking
                    if (InputState.leftClick() && statsSelected == 1)
                    {
                        gameState = GameState.Menu;
                    }
                    break;
                case GameState.Menu:
                    //Highlight text when hovering over it
                    if (InputState.currentMouse.Y > g.Viewport.Height / 2 + 50 && InputState.currentMouse.Y < g.Viewport.Height / 2 + 80)
                    {
                        menuSelected = 1;
                    }
                    else if (InputState.currentMouse.Y > g.Viewport.Height / 2 + 80 && InputState.currentMouse.Y < g.Viewport.Height / 2 + 110)
                    {
                        menuSelected = 2;
                    }
                    else if (InputState.currentMouse.Y > g.Viewport.Height / 2 + 110 && InputState.currentMouse.Y < g.Viewport.Height / 2 + 140)
                    {
                        menuSelected = 3;
                    }
                    else
                    {
                        menuSelected = 0;
                    }
                    //Check for clicking
                    if (InputState.leftClick())
                    {
                        if (menuSelected == 1)
                        {
                            //Start a singleplayer game, clear stats
                            startGame(GameMode.Singleplayer, g);
                            Stats.clearStats();
                        }
                        else if (menuSelected == 2)
                        {
                            //Start a multiplayer game, clear stats
                            startGame(GameMode.Multiplayer, g);
                            Stats.clearStats();
                        }
                        else if (menuSelected == 3)
                        {
                            checkQuitClicked = true;
                        }
                        menuSelected = 0;
                    }
                    break;
            }
        }
Example #7
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;
        }