コード例 #1
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
        public bool check_for_collision(Point point, ScreenDimensions wr)
        {
            int width = game.width;
            int index_of_tile;
            int index_of_pic_field;

            for (int i = 0; i < wr.height; i++)
            {
                for (int j = 0; j < wr.width; j++)
                {
                    index_of_tile      = (wr.bottom + j) + ((wr.top + i) * tile_size);
                    index_of_pic_field = ((point.x + point.y * width) + j) + i * width;
                    if ((index_of_pic_field >= 0) && (index_of_pic_field < arrField.Length))
                    {
                        if (tiles_array[index_of_tile] && ((Tiles_shapes)arrField[index_of_pic_field]).isBlock)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #2
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
 public Game_Class(ScreenDimensions wr)
 {
     game             = new ScreenDimensions(0, 0, wr.width / Block.Block_Width, wr.height / Block.Block_Height);
     pic_field_width  = wr.width;
     pic_field_height = wr.height;
     create_pic_field();
 }
コード例 #3
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
        public Image Draw(Graphics graphics, Point point, ScreenDimensions wnd, bool flag = false)
        {
            if (flag == true)
            {
                return(Game_Class.generate_pic_field(graphics, wnd));
            }
            else
            {
                if ((wnd.width > 0) && (wnd.height > 0))
                {
                    Image    temp = new Bitmap(wnd.width * this.Block_Width, wnd.height * this.Block_Height);
                    Graphics g    = Graphics.FromImage(temp);

                    for (int rows = wnd.top; rows < wnd.top + wnd.height; rows++)
                    {
                        for (int columns = wnd.bottom; columns < wnd.bottom + wnd.width; columns++)
                        {
                            if (tiles_array[columns + rows * tile_size])
                            {
                                obj.Draw(g, new System.Drawing.Point(this.Block_Width * (columns - wnd.bottom), this.Block_Height * (rows - wnd.top)), imageType(Shape));
                            }
                        }
                    }
                    graphics.DrawImage(temp, new PointF(this.Block_Width * (point.x + wnd.bottom - wnd.bottom), this.Block_Height * (point.y + wnd.top - wnd.top)));
                    g.Dispose();
                    temp.Dispose();
                }
                return(null);
            }
        }
コード例 #4
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
        public ScreenDimensions Block_Rotation(block_rotation rot)
        {
            ScreenDimensions wr = new ScreenDimensions();

            Rotation = rot;
            Build();
            set_block_position(ref wr);
            return(wr);
        }
コード例 #5
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
        public void blocksMovements(Point point, ScreenDimensions wr)
        {
            int index_of_tile;
            int index_of_pic_field;

            for (int i = 0; i < wr.height; i++)
            {
                for (int j = 0; j < wr.width; j++)
                {
                    index_of_tile      = (wr.bottom + j) + (wr.top + i) * Block.Size;
                    index_of_pic_field = (point.x - game.bottom + j) + (point.y - game.top + i) * game.width;
                    if (tiles_array[index_of_tile])
                    {
                        arrField[index_of_pic_field] = new Tiles_shapes(Block.color(Block.Shape), true);
                    }
                }
            }

            rowsCompletion();
        }
コード例 #6
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
        public void blockPrivew(Graphics graphics, ScreenDimensions wnd, Tiles t)
        {
            ScreenDimensions wnd_rect = new ScreenDimensions();

            bool[] array_for_blocks_creation = Blocks_Creation(t);
            Point  point = new Point();

            set_block_position(ref wnd_rect, array_for_blocks_creation);
            point.x = (wnd.width - wnd_rect.width * this.Block_Width) / 2;
            point.y = (wnd.height - wnd_rect.height * this.Block_Height) / 2;
            for (int rows = wnd_rect.top; rows < wnd_rect.top + wnd_rect.height; rows++)
            {
                for (int columns = wnd_rect.bottom; columns < wnd_rect.bottom + wnd_rect.width; columns++)
                {
                    if (array_for_blocks_creation[columns + rows * tile_size])
                    {
                        obj.Draw(graphics, new System.Drawing.Point((point.x + this.Block_Width * (columns - wnd_rect.bottom)), (point.y + this.Block_Height * (rows - wnd_rect.top))), imageType(t.type));
                    }
                }
            }
        }
コード例 #7
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
        public static Image generate_pic_field(Graphics e, ScreenDimensions wr)
        {
            Image      temp = new Bitmap(pic_field_width, pic_field_height);
            Graphics   g    = Graphics.FromImage(temp);
            int        w    = game.width;
            int        h    = game.height;
            BlockClass b    = new BlockClass();

            for (int rows = 0; rows < h; rows++)
            {
                for (int columns = 0; columns < w; columns++)
                {
                    if (((Tiles_shapes)arrField[columns + rows * w]).isBlock)
                    {
                        b.obj.Draw(g, new System.Drawing.Point(columns * b.Block_Width, rows * b.Block_Height), b.imageColor(((Tiles_shapes)arrField[columns + rows * w]).color));
                    }
                }
            }
            g.Dispose();

            return(temp);
        }
コード例 #8
0
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!check_for_start_game)
            {
                return;
            }

            // check if key input is valid (up, down, left, right, space, escape)
            if (!(e.KeyCode.Equals(Keys.Up) || e.KeyCode.Equals(Keys.Down) ||
                  e.KeyCode.Equals(Keys.Left) || e.KeyCode.Equals(Keys.Right) ||
                  e.KeyCode.Equals(Keys.Space) || e.KeyCode.Equals(Keys.Escape)))
            {
                return;
            }

            Point   newPos      = pt_obj; // get block current position
            Boolean isValidMove = false;

            switch (e.KeyCode)
            {
            case Keys.Right:     // Right
                // could go right?
                if ((newPos.x + scren_dimensions.width) * game.Block.Block_Width < picField.Width)
                {
                    newPos.x++;
                }

                if (newPos.x.Equals(pt_obj.x))
                {
                    return;
                }
                break;

            case Keys.Left:     // Left
                // could go left?
                if (newPos.x > 0)
                {
                    newPos.x--;
                }

                if (newPos.x.Equals(pt_obj.x))
                {
                    return;
                }
                break;

            case Keys.Down:     // Down
                // could go down?
                if ((newPos.y + scren_dimensions.height) * game.Block.Block_Height < picField.Height)
                {
                    newPos.y++;
                }

                if (newPos.y.Equals(pt_obj.y))
                {
                    return;
                }
                break;

            case Keys.Up:        // Up       (rotate)
            case Keys.Space:     // Spacebar (rotate)
                ScreenDimensions newBlockAdj = new ScreenDimensions();

                // Save old angle.
                Tetris.block_rotation saveAngle = game.Block.Rotation;

                // try clockwise
                newBlockAdj = game.Block.Block_Rotation(game.Block.rotationType(0));

                if ((newPos.x + newBlockAdj.width) * game.Block.Block_Width > picField.Width)
                {
                    newPos.x = picField.Width / game.Block.Block_Width - newBlockAdj.width;
                }
                if ((newPos.y + newBlockAdj.height) * game.Block.Block_Height > picField.Height)
                {
                    return;
                }

                if (game.check_for_collision(new Point(newPos.x, newPos.y), newBlockAdj))
                {
                    // try counter-clockwise
                    newBlockAdj = game.Block.Block_Rotation(game.Block.rotationType(1));

                    if ((newPos.x + newBlockAdj.width) * game.Block.Block_Width > picField.Width)
                    {
                        newPos.x = picField.Width / game.Block.Block_Width - newBlockAdj.width;
                    }
                    if ((newPos.y + newBlockAdj.height) * game.Block.Block_Height > picField.Height)
                    {
                        return;
                    }

                    if (game.check_for_collision(new Point(newPos.x, newPos.y), newBlockAdj))
                    {
                        check_for_rotation = false;
                    }
                    else
                    {
                        check_for_rotation = true;
                    }
                }
                else
                {
                    check_for_rotation = true;
                }

                if (check_for_rotation)
                {
                    if (scren_dimensions.bottom.Equals(newBlockAdj.bottom) &&
                        scren_dimensions.top.Equals(newBlockAdj.top) &&
                        scren_dimensions.width.Equals(newBlockAdj.width) &&
                        scren_dimensions.height.Equals(newBlockAdj.height))
                    {
                        // nothing has changed, just leave it.
                        return;
                    }

                    // can rotate, apply the new settings.
                    scren_dimensions = newBlockAdj;
                    pt_obj           = newPos;
                    isValidMove      = true;
                }
                else
                {
                    // can't rotate, restore old angle.
                    game.Block.Block_Rotation(saveAngle);
                    return;
                }
                break;
            }

            if (!(e.KeyCode.Equals(Keys.Space) || e.KeyCode.Equals(Keys.Up)))
            {
                if (!game.check_for_collision(new Point(newPos.x, newPos.y), scren_dimensions))
                {
                    pt_obj      = newPos;
                    isValidMove = true;
                }
            }

            if (isValidMove)
            {
                picField.Invalidate();
            }
        }
コード例 #9
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
        public void set_block_position(ref ScreenDimensions wr, bool[] array_for_blocks_creation)
        {
            wr = new ScreenDimensions();
            bool check_for_block_position;
            int  rows, columns;

            check_for_block_position = true;
            for (columns = 0; columns < tile_size; columns++)
            {
                for (rows = 0; rows < tile_size; rows++)
                {
                    if (array_for_blocks_creation[columns + rows * tile_size])
                    {
                        check_for_block_position = false;
                        break;
                    }
                }
                if (check_for_block_position)
                {
                    wr.bottom++;
                }
                else
                {
                    break;
                }
            }
            check_for_block_position = true;
            for (rows = 0; rows < tile_size; rows++)
            {
                for (columns = 0; columns < tile_size; columns++)
                {
                    if (array_for_blocks_creation[columns + rows * tile_size])
                    {
                        check_for_block_position = false;
                        break;
                    }
                }

                if (check_for_block_position)
                {
                    wr.top++;
                }
                else
                {
                    break;
                }
            }
            check_for_block_position = true;
            for (columns = tile_size - 1; columns >= 0; columns--)
            {
                for (rows = 0; rows < tile_size; rows++)
                {
                    if (array_for_blocks_creation[columns + rows * tile_size])
                    {
                        check_for_block_position = false;
                        break;
                    }
                }

                if (check_for_block_position)
                {
                    wr.width++;
                }
                else
                {
                    break;
                }
            }
            wr.width = tile_size - (wr.bottom + wr.width); //exact width of block
            check_for_block_position = true;
            for (rows = tile_size - 1; rows >= 0; rows--)
            {
                for (columns = 0; columns < tile_size; columns++)
                {
                    if (array_for_blocks_creation[columns + rows * tile_size])
                    {
                        check_for_block_position = false;
                        break;
                    }
                }

                if (check_for_block_position)
                {
                    wr.height++;
                }
                else
                {
                    break;
                }
            }
            wr.height = tile_size - (wr.top + wr.height); //exact height of block
        }
コード例 #10
0
ファイル: Tetris.cs プロジェクト: umairmunir-95/Tetris
 public void set_block_position(ref ScreenDimensions wr)
 {
     set_block_position(ref wr, tiles_array);
 }