Tetris

Looking to just have a conversation about Spore? Come join in on the many topics or start a new one!
User avatar
NotSure
Creature
Creature
Posts: 49
Joined: Tue May 31, 2011 12:58 am
Age: 39

Tetris

Unread post by NotSure »

Using DarkGDK and Visual C++ 2008, I have a Tetris Project to complete, which requires that I make a full Tetris Game.
I've come across a little problem and wanted to see if any of you programming geniuses had an idea.

Here is my current code, it's no where near complete since I still have to make blocks appear at random, but my problem now consists of having new blocks appear on the top, and to be able to stack the pieces as they land on eachother. Any ideas on how to do this, like an algorithm idea or something, let me know.

Code: Select all

// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
#include <cstdlib>
using namespace std;

int BLOCK_SIZE=20;     //Size of block is 20 units
DWORD CYAN = dbRGB(0,255,255);
DWORD RED =	dbRGB(255,0,0);
DWORD GREEN = dbRGB(0,255,0);
DWORD ORANGE = dbRGB(255, 127, 0);
DWORD YELLOW = dbRGB(255,255,0);
DWORD MAGENTA = dbRGB(255,0,255);
DWORD BLUE = dbRGB(0,0,255);
DWORD WHITE= dbRGB(255,255,255);
DWORD BLACK= dbRGB(0,0,0);
class Block
{
private:
	DWORD color;   //Variable color for later use
public:
	int x,y;
	Block();
	Block(int, int,DWORD);
	void move(int, int);
	void draw();
	void clear();
};
Block::Block()
{
	x = 0;
	y = 0;
	color = WHITE;
}
Block::Block(int dX, int dY, DWORD COLOR)
{
		x=dX;
		y=dY;
		color = COLOR;
}
void Block::move(int dx, int dy)
{
	x = x+dx;
	y = y+dy;
	dbInk(color,WHITE);
	draw();
}
void Block::draw()
{
	int x1 = x * BLOCK_SIZE;
	int y1 = y * BLOCK_SIZE;
	int x2 = x1 + BLOCK_SIZE;
	int y2 = y1 + BLOCK_SIZE;
	dbBox(x1, y1, x2, y2);
}
void Block::clear()
{
	dbInk(BLACK, BLACK);
	draw();
}
class Shape 
{
private:
	Block blocks[4];
	//int coords[8];
public:
	int coords[8];
	DWORD color;
	Shape();
	void shapeMove(int, int);
	void ShapeCreate(DWORD);
	void shapeDraw();
	void O_Tetrino();
	bool Border(int, int);
};
Shape::Shape()
{
	blocks[0]=Block(0,0, WHITE);
	blocks[1]=Block(0,0, WHITE);
	blocks[2]=Block(0,0, WHITE);
	blocks[3]=Block(0,0, WHITE);
}
void Shape::ShapeCreate(DWORD COLOR)
{
	color=COLOR;
	blocks[0]=Block(coords[0],coords[1], color);
	blocks[1]=Block(coords[2],coords[3], color);
	blocks[2]=Block(coords[4],coords[5], color);
	blocks[3]=Block(coords[6],coords[7], color);
}
void Shape::shapeMove(int dx, int dy)
{
	for(int i = 0; i<=3;i++)
	{
		blocks[i].clear();
	}
	for(int i=0; i<=3;i++)
	{
		blocks[i].move(dx,dy);
	}
}
void Shape::shapeDraw()
{
	for(int i = 0; i<=3;i++)
	{
		blocks[i].draw();
	}
}
bool Shape::Border(int dx, int dy)
{
	for (int i=0; i<=4; i++)
	{
		if (blocks[i].y+dy > 19 || blocks[i].x+dx < 0 || blocks[i].x+dx > 9)
		{
			return false;
		}
	}
	return true;
}
class O_Shape : public Shape
{
private:
	DWORD color;
	//int coords[8];
public:
	O_Shape(int, int);

};

O_Shape::O_Shape(int x, int y) : Shape()
{	
	coords[0]=x;
	coords[1]=y;
	coords[2]=x-1;
	coords[3]=y;
	coords[4]=x-1;
	coords[5]=y+1;
	coords[6]=x;
	coords[7]=y+1;
	ShapeCreate(RED);
	
}

class I_Shape : public Shape
{
private:
	DWORD color;
public:
	I_Shape(int, int);

};
I_Shape::I_Shape(int x, int y) : Shape()
{	
	coords[0]=x;
	coords[1]=y;
	coords[2]=x-1;
	coords[3]=y;
	coords[4]=x+1;
	coords[5]=y;
	coords[6]=x+2;
	coords[7]=y;
	ShapeCreate(CYAN);
	
}
class Z_Shape : public Shape
{
private:
	DWORD color;
	//int coords[8];
public:
	Z_Shape(int, int);

};
Z_Shape::Z_Shape(int x, int y) : Shape()
{
	coords[0]=x;
	coords[1]=y;
	coords[2]=x;
	coords[3]=y-1;
	coords[4]=x-1;
	coords[5]=y-1;
	coords[6]=x+1;
	coords[7]=y;
	ShapeCreate(BLUE);
}
void playMusic()
{
	dbLoadMusic("TetrisSong.mp3",5);
	dbPlayMusic(5);
	
}

// the main entry point for the application is this function
void DarkGDK ( void )
{
	//Declare tetris shapes
	Z_Shape Z(1,1);
	I_Shape I(5,6);
	O_Shape O(9,1);

	//Frame Rate control and title
	char* Tetris="TETRIS";
	dbSyncOn();
	dbSyncRate(15);
	//Window Resolution
	dbSetDisplayMode(200, 400, 0);
	dbSetWindowSize(200, 400);
	//Window title
	int i=1;
	int cellGrid[10][20];
	dbSetWindowTitle(Tetris);

	for(int x=0;x<=9;x++)
	{
		for(int y = 0; y <= 19; y++)
		{
			cellGrid[x][y]=0;
		}
	}
	
	while(LoopGDK())
	{
		playMusic();
		if(Z.Border(0,1))
		Z.shapeMove(0,1);
		if(dbRightKey())
		{
			Z.shapeMove(1,0);
		}
		else if(dbLeftKey())
		{
			Z.shapeMove(-1,0);
		}
		else if(dbDownKey())
		{
			Z.shapeMove(0,1);
		}
	
		if(O.Border(0,1))
			O.shapeMove(0,1);
			if(dbRightKey())
		{
			O.shapeMove(1,0);
		}
		else if(dbLeftKey())
		{
			O.shapeMove(-1,0);
		}
		else if(dbDownKey())
		{
			O.shapeMove(0,1);
		}
		if(I.Border(0,1))
		I.shapeMove(0,1);
			if(dbRightKey())
		{
			I.shapeMove(1,0);
		}
		else if(dbLeftKey())
		{
			I.shapeMove(-1,0);
		}
		else if(dbDownKey())
		{
			I.shapeMove(0,1);
		}
		dbSync ( );

	}
		
	// return back to windows
	return;
}


Spammers beware, the wrath of Sparta is near.
User avatar
Davo
The Boss
The Boss
Posts: 2420
Joined: Sun Dec 27, 2009 1:36 am
Age: 39

Re: Tetris

Unread post by Davo »

i dont really understand what you have here.
//i know a few things, obviously

but unless i have something to run, i dont have full capabilities.
i did something like this in actionscript. everything had collision boxes. gravity. but spawning...

maybe label each row. i dont know
if the row is empty, allow this piece to advance to the row below.

use a dice roll system to spawn a random piece.
I DONT KNOW!
Post Reply Previous topicNext topic

Return to “General Discussion”