Example #1
0
        public static Tuple<int, int> Think(World world, Shape shape)
        {
            gridWidth = world.Columns;
            gridHeight = world.Rows;
            lastScore = 0;
            bestScore = 0;
            xMoves = 0;
            rotations = 0;

            //Check each possible space and rotation
            for (int xCheck = -(gridWidth / 2); xCheck <= gridWidth / 2; xCheck++)
            {
                for (int rotCheck = 0; rotCheck <= 3; rotCheck++)
                {
                    //Create a new GhostShape
                    GhostShape ghostShape = new GhostShape(world, shape);
                    //Calculate the score
                    lastScore = ghostShape.CalculateScore(xCheck, rotCheck);
                    //Remember the highest score, and that movement and rotation
                    if (lastScore > bestScore)
                    {
                        bestScore = lastScore;
                        xMoves = xCheck;
                        rotations = rotCheck;
                    }
                    lastScore = 0;
                }
            }
            //Return the moves belonging to the best score
            var moves = Tuple.Create(xMoves, rotations);
            return moves;
        }
Example #2
0
 // The four squares this block consists of
 public Block(Square[] squares, Shape shape, Texture2D texture)
 {
     Squares = squares;
     BlockShape = shape;
     BlockTexture = texture;
     NextDirection = Directions.East;
     PreviousDirection = Directions.North;
 }
Example #3
0
 public GhostShape(World world, Shape shape)
 {
     this.world = world;
     this.shape = shape;
     this.grid = shape.Grid;
     gridCenter = new Vector2(grid.GetLength(0) - 1, grid.GetLength(1) - 1) / 2;
     location = new Point(world.Columns / 2 - 1, (int)gridCenter.Y);
     //If can't spawn. kill world
     if (!CanMove(new Point(0, 0), world, shape.Grid))
         world.Kill();
 }
Example #4
0
        public World(Rectangle rect, int offset, ControlMode controlMode, bool muteShape = true)
        {
            this.rect = rect;
            this.borderOffset = offset;
            this.controlMode = controlMode;
            this.mute = muteShape;

            this.currentShape = new Shape(this, controlMode);

            //Combo emitters
            List<ParticleModifier> p = new List<ParticleModifier>();
            p.Add(new GravityModifier(new Vector2(0, -0.5f)));
            p.Add(new RandomSpeedModifier(new Vector2(0.1f, 0.1f)));
            this.comboEmitter = new Emitter(rect.Width / 100f, 0f, Color.Orange * 0.6f, Color.Red, 20, 1, new RandomSpawnSpeed(Vector2.Zero, Vector2.Zero), Assets.Textures.Particle, new RectangleSpawnShape(rect.Width, rect.Height), p);
            this.comboEmitter.Position = new Vector2(rect.Center.X, rect.Center.Y);

            List<ParticleModifier> cp = new List<ParticleModifier>();
            cp.Add(new GravityModifier(new Vector2(0, -0.5f)));
            cp.Add(new RandomSpeedModifier(new Vector2(1f, 1f)));
            this.epicComboEmitter = new Emitter(rect.Width / 90f, 0f, Color.Orange * 0.5f, Color.Blue, 20, 1.5f, new RandomSpawnSpeed(Vector2.Zero), Assets.Textures.Particle, new RectangleSpawnShape(rect.Width, rect.Height), cp);
            this.epicComboEmitter.Position = new Vector2(rect.Center.X, rect.Center.Y);

            //Get rect of one block for size
            Rectangle blockRect = CalculateBlockRectangle(0,0);
            List<ParticleModifier> ep = new List<ParticleModifier>();
            ep.Add(new GravityModifier(new Vector2(0, 0.3f)));
            explosionEmitter = new Emitter(
                (float)blockRect.Width / (float)Assets.Textures.Block.Width,
                (float)blockRect.Width / (float)Assets.Textures.Block.Width,
                Color.Red,
                Color.Red,
                1,
                2f,
                new RandomSpawnSpeed(new Vector2(12, -rect.Width / 30), new Vector2(-12, -rect.Width / 50)),
                Assets.Textures.Block,
                new RectangleSpawnShape(0, 0),
                ep
            );
            stats = new Stats(Assets.Fonts.BasicFont, Color.White, rect);

            //Tetris emitter
            tetrisEmitter = new Emitter(rect.Width / 100f, 0f, Color.Red * 0.4f, Color.Blue, 100, 0.5f, new RandomSpawnSpeed(new Vector2(-30,-9), new Vector2(30,-4)), Assets.Textures.Particle, new RectangleSpawnShape(rect.Width, 0), new List<ParticleModifier>());
            tetrisEmitter.Position = new Vector2(rect.Center.X, rect.Bottom);
        }
Example #5
0
 public bool TryHold(Shape shape)
 {
     if (_canHold)
     {
         _nextFromHold = _holdShape;
         _holdShape = shape;
         _canHold = false;
         return true;
     }
     else
         return false;
 }
Example #6
0
        //===================================================================== FUNCTIONS
        public Shape PopNext()
        {
            Shape next;

            if (_nextFromHold != null)
            {
                next = Generate(_nextFromHold.ID);
                _nextFromHold = null;
            }
            else
            {
                next = _nextShapes[0];
                _nextShapes.RemoveAt(0);
                Generate();
                _canHold = true;
            }

            return next;
        }
Example #7
0
 private bool IsCollision(Shape shape)
 {
     for (int y = 0; y < Shape.SIZE; y++)
     {
         for (int x = 0; x < Shape.SIZE; x++)
         {
             if (!shape.IsEmpty(x, y))
             {
                 if (IsOutOfBounds(shape.X + x, shape.Y + y) || !IsEmpty(shape.X + x, shape.Y + y))
                     return true;
             }
         }
     }
     return false;
 }
Example #8
0
        private void GenerateShape()
        {
            _curShape = _shapeManager.PopNext();
            CalculateGhostShapeY();

            _gravity.ResetCountdown();
            _lockDelayLeft = _lockDelay;

            _state = (_curShape.Y == _ghostShapeY ? State.Locking : State.Falling);
        }
Example #9
0
        public void Update()
        {
            //Update emitters
            comboEmitter.Update();
            epicComboEmitter.Update();
            explosionEmitter.Update();
            tetrisEmitter.Update();

            if (currentShape == null)
            {
                //Create a new shape
                currentShape = new Shape(this, controlMode);
                //Reset the combo
                stats.Combo = 1;
            }

            if (isAlive)
            {
                //Update shape
                currentShape.Update();

                if (stats.Combo >= 1)
                {
                    comboEmitter.Shoot();
                }
                if (stats.Combo >= 2)
                {
                    epicComboEmitter.Shoot();
                }
            }
        }
Example #10
0
        public void DestroyFullRows()
        {
            //Get all full rows
            List<int> fullRows = GetFullRows();
            //Destroy them and move down all blocks above them
            for (int i = 0; i < fullRows.Count(); i++)
            {
                //Explode graphic
                for (int x = 0; x < columns; x++)
                {
                    Rectangle blockRect = CalculateBlockRectangle(new Point(x, fullRows[i]));
                    explosionEmitter.ForcePosition(new Vector2(blockRect.Center.X, blockRect.Center.Y));
                    explosionEmitter.SetColor(grid[x, fullRows[i] + i].Color);
                    explosionEmitter.Shoot();
                }
                //Move down grid
                for (int y = fullRows[i] + i; y > 0; y--)
                {
                    for (int x = 0; x < columns; x++)
                        grid[x, y] = grid[x, y - 1];
                }
            }
            if (fullRows.Count() > 0)
            {
                //Remove row 0
                for (int x = 0; x < columns; x++)
                    grid[x, 0] = null;
                //Calculate the score
                stats.CalculateScore(fullRows.Count());
                //create new shape
                this.currentShape = new Shape(this, controlMode);

                //Add 1 row to all other worlds
                foreach(World world in GameManager.GameWorld)
                {
                    if(world != this)
                        world.AddRow();
                }
            }

            if (!mute)
            {
                //Play sound
                switch (fullRows.Count)
                {
                    case 0:
                        //Assets.Audio.LockSound.Play();
                        break;
                    case 1:
                        Assets.Audio.Single.Play();
                        break;
                    case 2:
                        Assets.Audio.Double.Play();
                        break;
                    case 3:
                        Assets.Audio.Triple.Play();
                        break;
                    case 4:
                        Assets.Audio.Tetris.Play();
                        break;
                }
            }
        }
Example #11
0
 public Shadow(World world, Shape shape)
     : base(world, ControlMode.None)
 {
     this.shape = shape;
 }
Example #12
0
     /*   private int GetShapeType()
        {
            int shapeType = 1;
            
        }*/

        private void menuNewGame_Click(object sender, EventArgs e)
        {
           
            SetUpGame();
            shapeType = GetShapeType();
            aShape = new Shape(shapeType, mainScreen.screenWidth, mainScreen.screenHeight, false);
            nextShapeType = GetShapeType();
          
            aShape.shapeMoving = true;
            isGameOver = false;
            GameTimer.Interval = gameSpeed;
            GameTimer.Enabled = true;
            GameTimer.Start();
        }
Example #13
0
        private void GameTimer_Tick(object sender, System.EventArgs e)
        {
            if (isGamePaused == true) {
                Graphics gpause = startScreen.GetGraphics();
                startScreen.erase();
                gpause.DrawString("Pause", new Font("ComicSans", 18), new SolidBrush(Color.White), 5, 100);
                startScreen.flip();
            }
            if (aBtnClicked)
            {
            }
            if (startBtnPressed)
            {
                SetUpGame();
 
               
 
                shapeType = GetShapeType();
                aShape = new Shape(shapeType, mainScreen.screenWidth, mainScreen.screenHeight, false);
               // nextShapeType = GetShapeType();
                nextShapeType = 1;

                aShape.shapeMoving = true;
                isGameOver = false;
                GameTimer.Interval = gameSpeed;
                GameTimer.Enabled = true;
                GameTimer.Start();
                startBtnPressed = false;
                return;
            }
            if (aShape != null)
            {
                if (aShape.shapeMoving)
                {
                    if (dropBtnPressed)
                    {
                        GameTimer.Interval = dropRate;
                        dropBtnPressed = false;
                    
                    }
                    rectangleShape = aShape.moveShapeDown(dropRate, gameGrid.GetGameGrid());
                    DrawScreen();
 
                }
 
                else
                {
                    int XCoordinate;
                    int YCoordinate;
                  
                    for (int i = 0; i < 4; i++)
                    {
                        if (!rectangle.Contains(rectangleShape[i]))
                        {
                            isGameOver = true;
                            break;
                        }
                    }
                    if (!isGameOver)
                    {
                        int[] intYCoordinates = new int[4];
                        
                        for (int i = 0; i < 4; i++)
                        {
                            XCoordinate = rectangleShape[i].X;
                            YCoordinate = rectangleShape[i].Y;
                            intYCoordinates[i] = YCoordinate / 10;
                           
                            gameGrid.SetShapeLocation(YCoordinate / 10, XCoordinate / 10, rectangleShape[i], shapeType);
                        }
                      
                        Array.Sort(intYCoordinates);
                        for (int i = 0; i < 4; i++)
                        {
                            isRowFull = true;
                           
                            for (int j = 0; j < numberOfCols; j++)
                            {
                                if (gameGrid.IsGridLocationEmpty(intYCoordinates[i], j))
                                {
                                    isRowFull = false;
                                    break;
                               
                                }
                            }
                            if (isRowFull)
                            {
                                
                                for (int k = intYCoordinates[i]; k > 0; k--)
                                {
                                    
                                    for (int l = 0; l < numberOfCols; l++)
                                    {
                                        
                                        gameGrid.DropRowsDown(k, l);
                                    }
                                }
                               
                                gameGrid.SetTopRow();
                             
                                UpdateScore(intYCoordinates[i]);
                                bonusHeight = drawingAreaCanvas.Height - 1;
                                bonusStep = 5;
                            }
                        }
                        shapeType = nextShapeType;
                        aShape = new Shape(shapeType, mainScreen.screenWidth, mainScreen.screenHeight, false);
                        nextShapeType = GetShapeType();
                      
                        aShape.shapeMoving = true;
                        
                        GameTimer.Interval = gameSpeed;
                        isDropped = false;
                    }
                    else
                    {
                        GameTimer.Stop();
                      
                        DrawGameOver();
                      
                    }
                }
            }
           
        }
Example #14
0
        private void LayoutForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            string strKeyPress = null;
            strKeyPress = e.KeyCode.ToString();
            if (!isGameOver)
            {
                switch (strKeyPress.ToUpper())
                {
                    case "A":
                        if (aShape.shapeMoving) rectangleShape = aShape.moveShapeLeft(10, gameGrid.GetGameGrid());
                   
                        break;
                    case "D":
                        if (aShape.shapeMoving) rectangleShape = aShape.moveShapeRight(10, gameGrid.GetGameGrid());
                    
                        break;
                    case "W":
                        if (aShape.shapeMoving) rectangleShape = aShape.FlipShape("right", gameGrid.GetGameGrid());
                    
                        break;
               
                    case "Q":
                        GameTimer.Stop();
                        SetUpGame();
                        DrawStart();
                        break;
                    case "E":
                        Application.Exit();
                        break;
                    case "ESCAPE":
                        if (!isGamePaused)
                        {
                           // DrawGamePaused();
                            GameTimer.Stop();
                            
                            isGamePaused = true;

                            Graphics gpause = startScreen.GetGraphics();
                            startScreen.erase();
                            gpause.DrawString("Pause", new Font("Comic Sans", 18), new SolidBrush(Color.Red), 46, 100);
                            startScreen.flip();
                        }
                        else
                        {
                            GameTimer.Start();
                            isGamePaused = false;
                        }
                        break;
                    case "R":
                        DrawGameOver();
                        break;
                    case "SPACE":
                        if (aShape.shapeMoving)
                        {
                      
                            GameTimer.Interval = dropRate;
                            isDropped = true;
                        }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch (strKeyPress.ToUpper())
                {
                    case "SPACE":
                     
                        SetUpGame();
                        shapeType = GetShapeType();
                        aShape = new Shape(shapeType, mainScreen.screenWidth, mainScreen.screenHeight, false);
                        nextShapeType = GetShapeType();
                       
                        aShape.shapeMoving = true;
                        isGameOver = false;
                        GameTimer.Interval = gameSpeed;
                        GameTimer.Enabled = true;
                        GameTimer.Start();
                        break;
                    default:
                        break;
                }
            }
        }
Example #15
0
 /// <summary>
 /// Add shape to the Grid
 /// </summary>
 /// <param name="shape"></param>
 void AddShape(Shape shape)
 {
 }