Exemple #1
0
        public Block(Block b)
        {
            this.graphicsDevice = b.graphicsDevice;
            this.BlockShape = b.BlockShape;
            this.Position = b.Position;
            this.BlockSize = b.BlockSize;
            this.NibSetIndex = b.NibSetIndex;

            this.NibSets = new List<List<Nibbit>>();
            foreach (List<Nibbit> l in b.NibSets)
            {
                List<Nibbit> tmp = new List<Nibbit>();

                foreach (Nibbit n in l)
                    tmp.Add(new Nibbit(n));

                NibSets.Add(tmp);
            }
        }
        private bool ValidPosition(Block b)
        {
            // test against locked nibbits
            foreach (Nibbit n in LockedNibbits)
            {
                if (b.Contains(n))
                    return false;
            }

            // Test against edges
            return b.InBounds();
        }
        private void UpdateBlocks()
        {
            if (lastSeconds >= dropDelay)
            {
                // Do we need to drop some rows?
                if (RowsToDrop.Count != 0)
                {
                    DropRows();
                }
                else // move the current piece down
                {
                    Block newBlock = new Block(CurrentBlock);
                    newBlock.Translate(new Point(0, 1));

                    // lock block
                    if (!newBlock.AboveGround() || !ValidPosition(newBlock))
                    {
                        LockedNibbits.AddRange(CurrentBlock.EmancipateNibbits());
                        RemoveRows();
                        CurrentBlock = new Block(GraphicsDevice);
                    }
                    else
                        CurrentBlock = newBlock;

                    // Are any nibbits overflowing?
                    foreach (Nibbit n in LockedNibbits)
                    {
                        if (n.CompareRow(0) < 1)
                            State = GameState.Lose;
                    }
                }

                lastSeconds = 0;
            }
        }
        private void HandleMovement()
        {
            if (KeyPressed(Keys.Left))
            {
                Block newBlock = new Block(CurrentBlock);
                newBlock.Translate(new Point(-1, 0));

                if (ValidPosition(newBlock))
                    CurrentBlock = newBlock;
            }

            if (KeyPressed(Keys.Right))
            {
                Block newBlock = new Block(CurrentBlock);
                newBlock.Translate(new Point(1, 0));

                if (ValidPosition(newBlock))
                    CurrentBlock = newBlock;
            }

            if (KeyPressed(Keys.Up))
            {
                Block newBlock = new Block(CurrentBlock);
                newBlock.Rotate(true);

                if (ValidPosition(newBlock))
                    CurrentBlock = newBlock;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Down))
                dropDelay = 0.0625;
            else
                dropDelay = 0.5;
        }
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // dumb hack, use a 1x1 pixel to draw lines and stuff
            pixel = new Texture2D(GraphicsDevice, 1, 1);
            pixel.SetData(new Color[] { Color.White });

            scoreFont = Content.Load<SpriteFont>("Segoe");
            CurrentBlock = new Block(GraphicsDevice);

            // Set up various rectangles/lines
            scoreSideRect = new Rectangle(Constants.NIBBIT_SIZE * Constants.PLAYFIELD_WIDTH, 0,
                    Constants.NIBBIT_SIZE * Constants.PLAYFIELD_WIDTH,
                    Constants.NIBBIT_SIZE * Constants.PLAYFIELD_HEIGHT);

            midlineRect = new Rectangle(Constants.NIBBIT_SIZE * Constants.PLAYFIELD_WIDTH, 0, 1,
                Constants.NIBBIT_SIZE * Constants.PLAYFIELD_HEIGHT);

            pauseOverlayRect = new Rectangle(0, 0,
                Constants.NIBBIT_SIZE * Constants.PLAYFIELD_WIDTH * 2,
                Constants.NIBBIT_SIZE * Constants.PLAYFIELD_HEIGHT);
            pauseOverlayColor = Color.FromNonPremultiplied(new Vector4(0, 0, 0, 0.5f));

            #region Vectors
            // Precalculate position vectors for text
            Vector2 tmp = scoreFont.MeasureString("Paused");
            pauseVec1 = new Vector2(
                Constants.PLAYFIELD_WIDTH * Constants.NIBBIT_SIZE - tmp.X * 0.5f,
                Constants.PLAYFIELD_HEIGHT * Constants.NIBBIT_SIZE * 0.25f - tmp.Y);

            tmp = scoreFont.MeasureString("Press 'P' to resume");
            pauseVec2 = new Vector2(
                Constants.PLAYFIELD_WIDTH * Constants.NIBBIT_SIZE - tmp.X * 0.5f,
                Constants.PLAYFIELD_HEIGHT * Constants.NIBBIT_SIZE * 0.25f);

            tmp = scoreFont.MeasureString("Game Over");
            gameoverVec = new Vector2(
                Constants.PLAYFIELD_WIDTH * Constants.NIBBIT_SIZE * 1.5f - tmp.X * 0.5f,
                Constants.PLAYFIELD_HEIGHT * Constants.NIBBIT_SIZE * 0.5f - tmp.Y * 1.5f);

            tmp = scoreFont.MeasureString("Press Enter to play");
            startVec = new Vector2(
                Constants.PLAYFIELD_WIDTH * Constants.NIBBIT_SIZE * 1.5f - tmp.X * 0.5f,
                Constants.PLAYFIELD_HEIGHT * Constants.NIBBIT_SIZE * 0.5f - tmp.Y * 0.5f);

            scoreVec = new Vector2(Constants.PLAYFIELD_WIDTH * Constants.NIBBIT_SIZE + 10, 5);

            // clamp values to ints otherwise text looks rubbish
            ClampVector(ref pauseVec1);
            ClampVector(ref pauseVec2);
            ClampVector(ref gameoverVec);
            ClampVector(ref startVec);
            ClampVector(ref scoreVec);
            #endregion
        }