Tetris...

This is a discussion on Tetris... within the Game Programming forums, part of the General Programming Boards category; I'm creating a tetris console-ish game, and I'm having difficulties as in how to stack the blocks on top of ...

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Question Tetris...

    I'm creating a tetris console-ish game, and I'm having difficulties as in how to stack the blocks on top of each other. Does anyone have, or know of where some source code.
    BTW, I'm using ASCII char(219) blocks.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    #define BLOCK 219
    
    if(Block[X][Y + 1] == BLOCK) StopBlockFromFalling();
    Without seeing some code, that is all I can help with...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Basicly a good idea would be to have the board as a 2 dimensional array, and have a class for each shape, and a class to handle(group) those shapes.

    Each (shape) class would have its main member variable be a 2 dimensinal array that describes the shape. Also it would have a few functions like rotate, and move, and check for collision. IE:
    Code:
    class block4 {
    ... // con-de-structors
    Rotate();
    Move();
    CheckCollide();
    
    int shape[4][4]; // lots of seemingly extra indexs for a strait bar.
    };
    
    // rotate function
    block4::Rotate{
     // progmaticaly change indices of shape array 
    /*
    [1][0][0][0]
    [1][0][0][0]
    [1][0][0][0]
    [1][0][0][0]
    to->
    [1][1][1][1]
    [0][0][0][0]
    [0][0][0][0]
    [0][0][0][0]
    etc.
    */
    }
    block4::CheckCollide{
    /* cycle through the shape array (backwards) to see if corresponding board position is filled, if it is, then block needs to remain there.
    
    After youve decided that piece is in its final position, set the shape (indecies with values of 1) in the board array. and destroy the block4 object.
    */
    }
    
    
    /* Now you'll need a function for the board array that checks rows of the array to see if the values are all one (filled). and if sow, sets them to zero, and cycles the board array, moving everything down one. */

    Basic ideas to help.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    You can read my old source code to get an idea on how to go about it. I did it almost exactly the way goten described it.

    My first C++ game is done!

  5. #5
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Now..

    Alright, now I've moved onto CHAR_INFO's instead of blocks, so there is easier movement & stuff. I have it to where you see the blocks dropping, but near the end, I get an error saying some type of fault. Here's the code:

    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    class CBlock
    {
    public:
    int x;
    int y;
    WORD Color;
    bool Done;
    void BlockFall(int x,int y,WORD Color);
    };
    SMALL_RECT Rect= {26 , 0 , 53 , 24};
    COORD Size={27,25};
    COORD ZeroZero={0,0};
    int RandDice(int low, int high);
    void gotoxy(int x, int y);
    void SetUp();
    CBlock Block[50];
    CHAR_INFO Buffer[675];           //27*25
    HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
    int main()
    {
       srand(GetTickCount());
    
       for (int c=0; c<50; c++)
       {
       Block[c].x=RandDice(28,52);
       Block[c].y=1;
       Block[c].Done=false;
       int Color=RandDice(0,4);
       switch(Color)
       {
       case 0:
       Block[c].Color=9;			//blue
       break;
       case 1:
       Block[c].Color=12;		//red
       break;
       case 2:
       Block[c].Color=10;		//green
       break;
       case 3:
       Block[c].Color=11;		//aqua
       break;
       case 4:
       Block[c].Color=14;
       break;
       default:
       return 0;
       }
       }
       SetUp();
       int i=0;
       while(i!=51)
       {
       Block[i].BlockFall(Block[i].x,Block[i].y,Block[i].Color);
       i++;
       }
       Sleep(10000);
       return 0;
    }
    int RandDice(int low, int high)
    {
    return low+rand()%(high-low+1);
    }
    void gotoxy(int x, int y)
    {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(hOutput, coord);
    }
    void SetUp()
    {
    for (int e=0; e<25; e++)
    {
    Buffer[26+(27*e)].Char.AsciiChar='#';
    Buffer[26+(27*e)].Attributes=15;
    }
    for (int f=0; f<25; f++)
    {
    Buffer[0+(27*f)].Char.AsciiChar='#';
    Buffer[0+(27*f)].Attributes=15;
    }
    for (int g=0; g<27; g++)
    {
    Buffer[((24*27)+g)].Char.AsciiChar='#';
    Buffer[((24*27)+g)].Attributes=15;
    }
    gotoxy(5,4);
    cout << "Tetron 2.0 \nCreated By: Stan Schwertly \n(AKA Stan100).";
    cout << char(1);
    gotoxy(54,5);
    cout << "Controls: ";
    gotoxy(54,6);
    cout << "Left: Moves box left ";
    gotoxy(54,7);
    cout << "Right:Moves box right ";
    gotoxy(54,8);
    cout << "Esc: Exit\n";
    }
    void CBlock::BlockFall(int x,int y, WORD Color)
    {
    while(y!=23)
    {
    Buffer[x+(y-1)*27].Char.AsciiChar=219; //This one
    Buffer[x+(y-1)*27].Attributes=0;
    Buffer[x+y*27].Char.AsciiChar=219;
    Buffer[x+y*27].Attributes=Color;
    WriteConsoleOutput(hOutput,Buffer,Size,ZeroZero,&Rect);
    if (Buffer[x+(y+1)*27].Attributes!=Color)
    {
    	if(Buffer[x+(y+1)*27].Attributes==0) {}
    	else
    	{
    		break;
    	}
    }
    if(Buffer[x+(y+1)*27].Attributes==Color) { break;}
    y++;
    Sleep(35);
    }
    }
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  6. #6
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    I'm still working on this and I keep getting a thread access violation near the end. I think I made too many Char_Info's, but I'm not sure. Help?
    Thx
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Idea (Tetris Maze)
    By SlyMaelstrom in forum Game Programming
    Replies: 13
    Last Post: 06-07-2006, 06:48 PM
  2. how to write a Tetris [newbie]
    By lifeisendless4m in forum C Programming
    Replies: 2
    Last Post: 10-12-2004, 10:38 PM
  3. ARGHARHGAHRHGH Help with Tetris
    By KneeGrow in forum Tech Board
    Replies: 3
    Last Post: 09-10-2004, 12:40 AM
  4. Test my new tetris game
    By PJYelton in forum Game Programming
    Replies: 6
    Last Post: 04-19-2003, 06:21 PM
  5. AGS Tetris Clone
    By Damascus in forum Game Programming
    Replies: 1
    Last Post: 03-07-2003, 05:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21