Beispiel #1
0
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredBackBufferWidth = 1280;

            Content.RootDirectory = "Content";

            frameCounter = new FrameCounter();

            gameState = GameState.GS_RUNNING;
        }
Beispiel #2
0
        public void LoadNextLevel()
        {
            if (currentLevel != null)
                currentLevel.Dispose();

            ++curLevelIndex;

            currentLevel = new Level(Content, spriteBatch, 1, screenRect);

            string levelPath = string.Format("Content/Level/{0}.txt", curLevelIndex);

            if (File.Exists(levelPath))
            {
                using (Stream fileStream = TitleContainer.OpenStream(levelPath))
                {

                    currentLevel.LoadContent(fileStream);
                }

            }
            else
                gameState = GameState.GS_GAME_OVER;
        }
        public static void UpdatePlaying()
        {
            var gamePadData = GamePad.GetData(0);

            foreach (GameObj g in pieces)
                if(g.isAlive ()) //If an object has been killed, it should not update.
                {
                    g.Update ();
                }
            scoreLabel.Text = "Score: " + score;

            //Allows the player to restart.
            if(gameOver == true)
            {
                menuDisplay = new MenuDisplay(graphics);
                currentGameState = GameState.Dead;
            }

            if(score % 10 == 0 && !newLevel)
            {
                NewGame (score);
            }
        }
Beispiel #4
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();

            keyboardState = Keyboard.GetState();

            if (canToggle && keyboardState.IsKeyDown(Keys.P))
            {
                gameState = (gameState == GameState.GS_RUNNING) ? GameState.GS_PAUSED: GameState.GS_RUNNING;

                canToggle = false;
            }

            if (keyboardState.IsKeyUp(Keys.P))
            {
                canToggle = true;

            }

            if (keyboardState.IsKeyDown(Keys.R))
            {
                currentLevel.Reset();
            }

            if (gameState == GameState.GS_RUNNING)
                currentLevel.Update(gameTime, keyboardState);

            if (currentLevel.reachedExit)
                LoadNextLevel();

            base.Update(gameTime);
        }
        public static void UpdateHighScoreView()
        {
            var gamePadData = GamePad.GetData(0);

            if((gamePadData.Buttons & GamePadButtons.Enter) != 0)
            {
                menuDisplay = new MenuDisplay(graphics);
                currentGameState = GameState.Menu;
            }
        }
        public static void UpdateMenu()
        {
            var gamePadData = GamePad.GetData(0);

            if((gamePadData.Buttons & GamePadButtons.Enter) != 0)
            {
                NewGame(0);
                currentGameState = GameState.Playing;
            }

            if((gamePadData.Buttons & GamePadButtons.Select) != 0)
            {
                menuDisplay = new MenuDisplay(graphics);
                currentGameState = GameState.HighScoreView;
            }

            if((gamePadData.Buttons & GamePadButtons.Back) != 0)
            {
                currentGameState = GameState.Quit;
            }
        }
        public static void UpdateDead()
        {
            var gamePadData = GamePad.GetData(0);

            if((gamePadData.Buttons & GamePadButtons.Enter) != 0)
            {
                menuDisplay = new MenuDisplay(graphics);
                currentGameState = GameState.Menu;
            }

            if((gamePadData.Buttons & GamePadButtons.Back) != 0)
            {
                currentGameState = GameState.Quit;
            }
        }
        public static void UpdateHighScoreAdd()
        {
            var gamePadData = GamePad.GetData(0);
            DrawHUD ();

            float pos = selector.Position.X;

            if((gamePadData.Buttons & GamePadButtons.Right) != 0)
            {
                if(pos <= 781)
                {
                    selector.Position.X += 2.65f;
                }
            }

            if((gamePadData.Buttons & GamePadButtons.Left) != 0)
            {

                if(pos >= 252.4)
                {
                    selector.Position.X -= 2.65f;
                }
            }

            if((gamePadData.Buttons & GamePadButtons.Enter) != 0)
            {
                if (coolDown <= 0) {
                    if(holder.Length < 3)
                    {
                        holder += GetLetterAt(selector.Position.X);
                        coolDown = 10;
                    }
                    else
                    {
                        UpdateHighScore();
                    }
                }

            }

            if((gamePadData.Buttons & GamePadButtons.Select) != 0)
            {
                UpdateHighScore();
                menuDisplay = new MenuDisplay(graphics);
                currentGameState = GameState.Menu;

            }

            if((gamePadData.Buttons & GamePadButtons.Back) != 0)
            {
                UpdateHighScore();
                currentGameState = GameState.Quit;
            }
            coolDown--;
        }
        public static void RenderDead()
        {
            // Clear the screen
            graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            graphics.Clear ();

            bg.Render ();
            go.Position = new Vector3(graphics.Screen.Rectangle.Width / 2 - go.Width / 2,
                                      graphics.Screen.Rectangle.Height / 2 - go.Height / 2, 0);
            go.Render();
            DrawHUD();

            if(score >= highscore)
            {
                holder = "";
                highscore = score;
                currentGameState = GameState.HighScoreAdd;
            }

            else
            {
                menuDisplay.Announcement = null;
                menuDisplay.Announcement2 = null;
                menuDisplay.Announcement3 = null;
                menuDisplay.Announcement4 = "Press Enter to return to the Main Menu or the Back button to quit.";
                menuDisplay.Render();

                // Present the screen
                graphics.SwapBuffers ();
            }
        }
Beispiel #10
0
        public static void Initialize()
        {
            clock = new Stopwatch();
            clock.Start();

            backgroundMusic = new Bgm("/Application/assets/music.mp3");
            backgroundPlayer = backgroundMusic.CreatePlayer();
            backgroundPlayer.Loop = true;
            backgroundPlayer.Play();

            presentSound = new Sound("/Application/assets/jinglebell.wav");
            presentSoundPlayer = presentSound.CreatePlayer();

            isPlaying = true;

            // Set up the graphics system
            graphics = new GraphicsContext ();
            gen= new Random();

            NewGame(0);

            currentGameState = GameState.Menu;
            menuDisplay = new MenuDisplay(graphics);
        }
Beispiel #11
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

            if (myGameState == GameState.START)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    myGameState = GameState.INGAME;
                }
            }
            else if (myGameState == GameState.INGAME)
            {
                //------------------- move position based on game time
                bPosition += bVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

                //check if ball position is over the screen
                if (bPosition.X > SCREEN_WIDTH - ball.Width || bPosition.X < 0)
                {
                    bVelocity.X *= -1;
                }
                if (bPosition.Y > SCREEN_HEIGHT - ball.Height || bPosition.Y < 0)
                {
                    bVelocity.Y *= -1;
                }

                //--------------------- get input to controll p1 paddle
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    p1Position.Y -= p1Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    p1Position.Y += p1Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                //set paddle bound
                if (p1Position.Y < 0) p1Position.Y = 0;
                if (p1Position.Y > SCREEN_HEIGHT - paddle1.Height) p1Position.Y = SCREEN_HEIGHT - paddle1.Height;

                //---------------------- get input to controll p2 paddle
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                {
                    p2Position.Y -= p2Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                {
                    p2Position.Y += p2Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                //set paddle bound
                if (p2Position.Y < 0) p2Position.Y = 0;
                if (p2Position.Y > SCREEN_HEIGHT - paddle2.Height) p2Position.Y = SCREEN_HEIGHT - paddle2.Height;

                //-------------------- check collision detection between paddle and ball
                bRect = new Rectangle((int)bPosition.X, (int)bPosition.Y, ball.Width, ball.Height);
                p1Rect = new Rectangle((int)p1Position.X, (int)p1Position.Y, paddle1.Width, paddle1.Height);
                p2Rect = new Rectangle((int)p2Position.X, (int)p2Position.Y, paddle2.Width, paddle2.Height);

                this.checkBallCollisionWith(p1Rect);
                this.checkBallCollisionWith(p2Rect);

                //------------------- add score manager
                if (bPosition.X <= 0) //add score to p2
                {
                    p2Score++;
                    bPosition = new Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
                    if (bVelocity.X > 0) bVelocity *= -1;
                }
                if (bPosition.X >= SCREEN_WIDTH - ball.Width) //add score to p1
                {
                    p1Score++;
                    bPosition = new Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
                    if (bVelocity.X < 0) bVelocity *= -1;
                }

                //----------------- check when the game is end;
                if (p1Score == winScore || p2Score == winScore) {
                    myGameState = GameState.END;
                }
            }
            else if (myGameState == GameState.END)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Space)) {
                    //reset the game
                    bPosition = new Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
                    p1Score = 0;
                    p2Score = 0;
                    myGameState = GameState.INGAME;
                }
            }

            base.Update(gameTime);
        }
Beispiel #12
0
        public void ChangeGameState(GameState state, int level)
        {
            killed = 0;
            modelManager.enemyThisLevel = 0;
            currentGameState = state;
            switch (currentGameState)
            {
                case GameState.LEVEL_CHANGE:
                    splashScreen.SetData("Level " + (level + 1),
                     GameState.LEVEL_CHANGE);
                    modelManager.Enabled = false;
                    modelManager.Visible = false;
                    splashScreen.Enabled = true;
                    splashScreen.Visible = true;
                    // Stop the soundtrack loop
                    //trackCue.Stop(AudioStopOptions.Immediate);
                    break;
                case GameState.PLAY:
                    modelManager.Enabled = true;
                    modelManager.Visible = true;
                    splashScreen.Enabled = false;

                    splashScreen.Visible = false;
                    //if (trackCue.IsPlaying)
                    //trackCue.Stop(AudioStopOptions.Immediate);
                    // To play a stopped cue, get the cue from the soundbank again
                    //trackCue = soundBank.GetCue("Tracks");
                    //trackCue.Play();
                    break;
                case GameState.END:
                    splashScreen.SetData("Game Over.\nLevel: " + (level) +
                 "\nScore: " + score, GameState.END);
                    modelManager.Enabled = false;
                    modelManager.Visible = false;
                    splashScreen.Enabled = true;
                    splashScreen.Visible = true;
                    // Stop the soundtrack loop
                    //trackCue.Stop(AudioStopOptions.Immediate);
                    break;
            }
        }
Beispiel #13
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()
 {
     // TODO: Add your initialization logic here
     gameState = new TitleScreenState(this);
     base.Initialize();
 }