//Description: Create a new random shape private void CreateNewShape() { if (mNextShape.Type == Shape.ShapeType.NotSet) { mNextShape.Type = ((Shape.ShapeType)(RandomNumber(0, 6))); }//end if (mNextShape.Type == Shape.ShapeType.NotSet) mCurrentShape.Type = mNextShape.Type; mNextShape.Type = ((Shape.ShapeType)(RandomNumber(0, 6))); mCurrentShape.PositionX = 5; mCurrentShape.PositionY = 0; mCurrentShape.Rotation = 0; // Andrew: Update Next Shape Message m = new Message(); m.Type = Message.MessageType.UpdateNextShape; m.ShapeType = mNextShape.Type; m.PositionX = mNextShape.PositionX; m.PositionY = mNextShape.PositionY; m.Rotation = mNextShape.Rotation; msgQ.Enqueue(m); }
//Description: Automatically move the current piece down towards the bottom of the playing area as time passes private void AutoFall(float elapsedSeconds) { mAutoFallTimer += elapsedSeconds; if (mAutoFallTimer >= mAutoFallThreshold) { mCurrentShape.PositionY += 1; mAutoFallTimer = 0; if (mPlayfield.CheckCollision(mCurrentShape) == true) { if (mCurrentShape.PositionY <= 1) { mCurrentState = GameState.GameOver; return; }//end if (mCurrentShape.PositionY <= 0) //Add the shape to the playfield mPlayfield.AddShape(mCurrentShape); // Andrew: Update Playfield Message m = new Message(); m.Type = Message.MessageType.UpdatePlayfield; m.playField = mPlayfield.playField; msgQ.Enqueue(m); //Create a new shape CreateNewShape(); }//end if (mPlayfield.CheckCollision(mCurrentShape) == true) }//end if (mAutoFallTimer >= 1) }
//Update the playfield area, clearing lines, updating score and playing sounds protected void UpdatePlayfield() { //Check for full rows int aNumberOfLinesCleared = mPlayfield.CheckRows(); if (aNumberOfLinesCleared > 0) { mNumberOfLinesCleared += 1; mTotalLines += 1; // Andrew: Update Playfield Message m = new Message(); m.Type = Message.MessageType.UpdatePlayfield; m.playField = mPlayfield.playField; msgQ.Enqueue(m); // Andrew: Update Score m = new Message(); m.Type = Message.MessageType.UpdateScoreboard; m.Score = mScore; m.TotalLines = mTotalLines; m.Level = mLevel; msgQ.Enqueue(m); } else { if (mNumberOfLinesCleared >= 3) { mScore += 1000; // Andrew: Update Score Message m = new Message(); m.Type = Message.MessageType.UpdateScoreboard; m.Score = mScore; m.TotalLines = mTotalLines; m.Level = mLevel; msgQ.Enqueue(m); } else if (mNumberOfLinesCleared > 0) { mScore += 100 * mNumberOfLinesCleared; // Andrew: Update Score Message m = new Message(); m.Type = Message.MessageType.UpdateScoreboard; m.Score = mScore; m.TotalLines = mTotalLines; m.Level = mLevel; msgQ.Enqueue(m); }//end if (mNumberOfLinesCleared >= 3) mNumberOfLinesCleared = 0; }//end if (aNumberOfLinesCleared > 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 void UpdateThread() { #if XBOX Thread.CurrentThread.SetProcessorAffinity(3); #endif while (true) { waitUpdate.WaitOne(); Thread.MemoryBarrier(); //The time since Update was called last float elapsed = (float)mGameTime.ElapsedGameTime.TotalSeconds; //Get the current state of the keyboard (what keys are and are not being pressed) KeyboardState aKeyboard = kbState; //If the Escape key has been pressed, then exit the game if (aKeyboard.IsKeyDown(Keys.Escape) == true || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); }//end if (aKeyboard.IsKeyDown(Keys.Escape) == true) //Update the game objects UpdateGameObjects(elapsed, aKeyboard); // Andrew: Update Current Shape Message m = new Message(); m.Type = Message.MessageType.UpdateCurrentShape; m.ShapeType = mCurrentShape.Type; m.PositionX = mCurrentShape.PositionX; m.PositionY = mCurrentShape.PositionY; m.Rotation = mCurrentShape.Rotation; msgQ.Enqueue(m); // Wake up Draw Thread waitDraw.Set(); } }
//Update the game to the next level if appropriate protected void UpdateLevel() { //Update the level if the number of lines cleared is great enough to move to the next level if (mTotalLines >= (mLevel + 1) * 10) { mLevel += 1; mBlockColor = GetBlockColor(); mAutoFallThreshold -= .1F; // Andrew: Update Score Message m = new Message(); m.Type = Message.MessageType.UpdateScoreboard; m.Score = mScore; m.TotalLines = mTotalLines; m.Level = mLevel; msgQ.Enqueue(m); }//end if (mTotalLines >= (mLevel + 1) * 10) }