• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TuringComplete

Contents of the board not moving down after a scoring move

4 posts in this topic

Hello,

 

This is my first post on the site and I am not sure my question is posted in the right forum, so I apologize if this is the wrong place. Anyway, this is my very first game and I am doing it in C# and XNA. Currently everything in my tetris game so far is working (left/right movements, dropping down quickly, rotations, etc) and the board is being drawn correctly except when the player fills 10 squares in a row what happens is that the row is just deleted and the rows above it don't fall down. What is confusing is that I am updating the board to shift every row down, but for some reason it is not being updated graphically. Here are the relevant parts of my code.

 

 

TetrisBoard class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tetris
{
    public class TetrisBoard
    {
        public const int boardCols = 10;
        public const int boardRows = 22;
        public Square[,] board;

        public TetrisBoard()
        {
            board = new Square[boardRows, boardCols];
        }

        public bool isCellFull(int row, int col)
        {
            return board[row, col] != null;
        }

        public void fillCell(int row, int col, Square square)
        {
            board[row, col] = square;
        }

        public void deleteCell(int row, int col)
        {
            board[row, col] = null;
        }

        public void fillTetromino(Tetromino tetromino)
        {
            for (int i = 0; i < tetromino.squares.Length; i++)
            {
                int col = tetromino.squares[i].square.X / Square.width;
                int row = tetromino.squares[i].square.Y / Square.height;
                fillCell(row, col, tetromino.squares[i]);
            }
        }

        public bool canMoveDown(Square square)
        {
            int col = (square.square.X / Square.width);
            int row = (square.square.Y / Square.height) + 1;
            if (row >= boardRows)
                return false;
            return !isCellFull(row, col);
        }

        public bool canMoveDown(Tetromino tetromino)
        {
            for(int i = 0; i < tetromino.squares.Length; i++)
                if(!canMoveDown(tetromino.squares[i]))
                    return false;
            return true;
        }

        public bool canAccelerateDown(Square square)
        {
            int col = (square.square.X / Square.width);
            int row = (square.square.Y / Square.height) + 2;
            if (row >= boardRows)
                return false;
            return !isCellFull(row, col);
        }
        
        public bool canAccelerateDown(Tetromino tetromino)
        {
            for (int i = 0; i < tetromino.squares.Length; i++)
                if (!canAccelerateDown(tetromino.squares[i]))
                    return false;
            return true;
        }

        public bool canMoveLeft(Square square)
        {
            int col = (square.square.X / Square.width) - 1;
            int row = (square.square.Y / Square.height);
            if (col < 0)
                return false;
            return !isCellFull(row , col);
        }

        public bool canMoveLeft(Tetromino tetromino)
        {
            for (int i = 0; i < tetromino.squares.Length; i++)
                if (!canMoveLeft(tetromino.squares[i]))
                    return false;
            return true;
        }

        public bool canMoveRight(Square square)
        {
            int col = (square.square.X / Square.width) + 1;
            int row = (square.square.Y / Square.height);
            if(col >= boardCols)
                return false;
            return !isCellFull(row, col);
        }

        public bool canMoveRight(Tetromino tetromino)
        {
            for (int i = 0; i < tetromino.squares.Length; i++)
                if (!canMoveRight(tetromino.squares[i]))
                    return false;
            return true;
        }

        public bool hasLanded(Tetromino tetromino)
        {
            return !canMoveDown(tetromino);
        }

        public void checkTenInARow()
        {
            for (int i = boardRows - 1; i >= 0; i--)
            {
                bool score = true;
                for (int j = 0; j < boardCols; j++)
                {
                    if (!isCellFull(i, j))
                    {
                        score = false;
                        break;
                    }
                }
                if (score)
                {
                    printBoard();
                    dropDownSquares(i);
                    printBoard();
                }
            }
        }

        public void dropDownSquares(int row)
        {
            for (int i = row; i > 0; i--)
            {
                for (int j = 0; j < boardCols; j++)
                {
                    fillCell(i, j, board[i - 1, j]);
                    deleteCell(i - 1, j);
                }
            }

        }

        public void printBoard()
        {
            for (int i = 0; i < boardRows; i++)
            {
                for (int j = 0; j < boardCols; j++)
                    if (isCellFull(i, j))
                        Console.WriteLine(i + " " + j);
            }
        }
    }
}


 

 

And this is part of the Game1.cs class

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            timeSinceLastInput += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (timeSinceLastInput >= minTimeSinceLastInput)
                keyboardInputHandler();

            timeSinceLastMove += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (!pause)
            {
                if (timeSinceLastMove >= minTimeSinceLastMove)
                {
                    if (board.canMoveDown(currentPiece))
                    {
                        currentPiece.moveDown();
                        timeSinceLastMove = 0;
                    }
                }
                if (board.hasLanded(currentPiece))
                {
                    board.fillTetromino(currentPiece);
                    board.checkTenInARow();
                    spawnRandomTetromino();
                }
            }
            base.Update(gameTime);
        }


        public void drawTetromino()
        {
            for (int i = 0; i < currentPiece.squares.Length; i++)
                drawSquare(currentPiece.squares[i]);
        }

        public void drawSquare(Square square)
        {
            spriteBatch.Draw(squareTex, square.square, Color.White);
        }

        public void drawBoard()
        {
            for (int i = 0; i < TetrisBoard.boardRows; i++)
            {
                for (int j = 0; j < TetrisBoard.boardCols; j++)
                {
                    if (board.isCellFull(i, j))
                        drawSquare(board.board[i, j]);
                }
            }
        }



        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            drawBoard();
            drawTetromino();
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
 

I have been trying to figure out this bug for a while, and I sure could use a new pair of eyes to look at it. I have attached the whole project if you need to see more of the code. Thanks smile.png

0

Share this post


Link to post
Share on other sites

Hey, don't take this the wrong way, but figuring out this bug yourself would be a big help for you. It sounds like something's position is not being updated when you clear a row. I would step through the code after you clear a row and make sure that the code is doing what it should. If it is, make sure that when you are drawing the tiles that their position/texture whatnot is the same as your boards.

1

Share this post


Link to post
Share on other sites

Well your physics is updating, it's just the visuals that aren't!
Your problem is that when you call the dropDownSquares() function, it sets the deleted square equal to the one above it (which includes its rectangles X and Y values). 

 

Here's a quick fix, you might wanna find a prettier one, given that the rest of your code is nicely organised:

 

New dropDownSquares:

public void dropDownSquares(int row)
        {
            for (int i = row; i > 0; i--)
            {
                for (int j = 0; j < boardCols; j++)
                {
                  
                    moveCell(i, j, board[i - 1, j]);
                    deleteCell(i - 1, j);
                }
            }
        } 

where the moveCell function is just the fillCell with the changed Y value added:

public void moveCell(int row, int col, Square sq)
        {
            board[row, col] = sq;
            if(sq!=null)board[row, col].square.Y += 16;
        }
Edited by gchewood
1

Share this post


Link to post
Share on other sites

No problem. I agree with Seabolt though- if you're new to debugging code, it's good to get the practice. The more complicated your game gets, the less likely 
other people will be able to help!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0