Sign in to follow this  
test86

Help on Designing an OOP Tetris-clone

Recommended Posts

Hi, after the result of my last topic, I decided to continue my long time design of Tetris-clone, so these are the stuff i have designed till now: //-Designed till now:------------------------------------------------------------------------\ Having a: Class for the GameBoard. Class for each piece of Tetris (will make a three level hierarchical inheritance for this, not now probably) Class for MainGame (which will have an instance of each of other stuff, and i think will have control the whole game) Class for Score; managing high scores, displaying it and so on Class for displaying the GameBoard; i hope to make this part as much as independent from the game logic so the porting as easy as possible (on Nintendo DS), and i think in each loop of main game loop, it will have to update itself. All of SDL I hope to be in here and nowhere else so whenever I wanted to port my game on DS, i will just worry and change just this class and others should work as good as before. I think the main loop of the game will be something like this: While(!myGame.IsGameOVer()) { PutATris(); //randomly, will add A.I. here later While the piece can go down //collision detection or something like that will be here { CurrentPiece.MoveDownOnce(); } CheckForScores an interval here, a delay, but it shouldt halt the program or else we will miss the user's pressed key. rendering the scene } //-My concerns on design:------------------------------------------------------------------------\(I need to think about it when designing other parts) 1- As mentioned earlier, I want to design the pieces as best as possible so in later phases I will let the user have the ability of designing His/her own shape of Tetris-pieces no matter how its shape is. It’s because of that I'm making a three level of hierarchical inheritance (one for basic piece which is a 1x1, one inherits from it and another level which are my final shapes) 2- Want to add TCP/IP multiplayer and having something like friends list, so when ur friend opens his game, u can compete and so on (I know it will need a server probably), OR just playing head-to-head. 3- Separating the game logic and graphics (representations) as much as possible to increase portability over DS and other consoles. (Basically it’s a good practice for me to be able to port my bigger projects, easier) 4- Adding A.I. for later, its very important to me. //-Problems------------------------------------------------------------------------\ 1- When I wanted to design the movement of a piece in GameBoard, I faced two possibilities: If I use OOP I’ll have to pass more stuff and compute more (at least with my current knowledge) and basically it should have more reusability but if I do it old-fashioned with lots of IFs and computational stuff like checking like (if (CurrentPiece+2 == '0') then "u can move"), it will be faster and more dirty, which is better? 2- Since I want to my design of pieces be as reusable as possible, I was after some kind of algorithm so whenever i move or rotate anything, I wouldn’t worry about its results since I’m sure the smaller parts of it were well-written. but i cant find the best way since each of these current and default pieces have exceptions and i need to put some IFs for each of them and that will reduce the reusability that I’m after. So I'm after some kind of rotating/moving a singe 1x1 piece, so when I build bigger stuff from it, i will sure that rotation/movement of it is done before. 3- the game should respond real time to any pressed key appropriately, so how should i prepare for a key at anytime? in dos mode i know how to do it, but in windows mode which is event-based, how should i assign my desired keyboard handler to the event of a pressed key? (In C# we have event for that, but I donno his elder brother in C++) //-Need advice:------------------------------------------------------------------------\ 1- Do you suggest having an Array of like int[20][10] for my GameBoard OR treating it like array of Empty Objects? each one has its Pros and cons, having an integer array makes its managing much more easier, but having the object based makes its design more b-e-a-u-tiful but will need more processing power at runtime.

Share this post


Link to post
Share on other sites
Different pieces have no use of inheritance in the same way you do not use inheritance when you add elements to std::list.

Given the nature of problem, there's little reason have any kind of virtual classes - a piece is a piece, just like Bitmap class can contain any bitmap - you do not derive classes if you want to load a picture of a dog instead of a cat.


Also keep in mind, that adding excessive virtuality to piece design doesn't really solve much. What if someone wants hexagonal or triagonal pieces (such Tetris clones exist). Or something more extravagant.

And when you settle for rectangular pieces, each one is nothing more but a small bitmap.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Different pieces have no use of inheritance in the same way you do not use inheritance when you add elements to std::list.


no use of inheritance?! so each piece will have its own move method and its own rotation? i agree that making a universal rotation might be hard, but I think we can have movement resused. like as said, having a well written move for 1x1 piece with all of its checkings and when we want to move a 2x2, (as in C#) you just say For Each myP in CurrentPiece {myP.MoveRight()}
is it possible or not? or is it good design or not?


Quote:
Original post by Antheus
Given the nature of problem, there's little reason have any kind of virtual classes - a piece is a piece, just like Bitmap class can contain any bitmap - you do not derive classes if you want to load a picture of a dog instead of a cat.

my last concern is graphic for this game, my discussion on virtual classes are not for displaying an image, plz dont get insulted but please read my post again, maybe i didnt understand your meaning, or i didnt say mine clearly.

Quote:
Original post by Antheus
Also keep in mind, that adding excessive virtuality to piece design doesn't really solve much. What if someone wants hexagonal or triagonal pieces (such Tetris clones exist). Or something more extravagant.

i hvae no idea about it, i have still difficulties with detection of where solid stuff is or not, i have no idea about triangle pieces, plz help or guide or give clue atleast.

Quote:
Original post by Antheus
And when you settle for rectangular pieces, each one is nothing more but a small bitmap.

yes, but in OOP i can add X and Y for them, if not, how do u want to store the location of your current piece? do u store it permenantly in your array? even when still its moving?





plz answer to my other questions, there are still more important ones there, like movements and design on small basic tile issues.

thnx in advance to all.

Share this post


Link to post
Share on other sites
Quote:
Original post by test86
no use of inheritance?! so each piece will have its own move method and its own rotation? i agree that making a universal rotation might be hard, but I think we can have movement resused.


He means, have a single Piece class, with a single rotation function, a single movement function, and a single position system.

The difference between tetris pieces is shape, not behaviour. This is something that is handled through data, not through inheritance. As mentioned before, a 4x4 bitmap of occupied/free bricks for a piece should be more than enough.

Share this post


Link to post
Share on other sites
Quote:
Original post by test86
1- When I wanted to design the movement of a piece in GameBoard, I faced two possibilities: If I use OOP I’ll have to pass more stuff and compute more (at least with my current knowledge) and basically it should have more reusability but if I do it old-fashioned with lots of IFs and computational stuff like checking like (if (CurrentPiece+2 == '0') then "u can move"), it will be faster and more dirty, which is better?


Use the clean way. But make sure it's actually clean. It's very easy to delude oneself into thinking that a solution is clean just because it uses objects. The practical solution here would be to compute the new position of the piece, query the board for collisions between the piece and existing bricks, and stopping piece movement if collisions exist.

Quote:
2- Since I want to my design of pieces be as reusable as possible, I was after some kind of algorithm so whenever i move or rotate anything, I wouldn’t worry about its results since I’m sure the smaller parts of it were well-written. but i cant find the best way since each of these current and default pieces have exceptions and i need to put some IFs for each of them and that will reduce the reusability that I’m after.


Rotation of a piece is merely a permutation of its component brick elements. It can be entirely expressed in terms of data, and requires neither inheritance nor conditionals.

Quote:
3- the game should respond real time to any pressed key appropriately, so how should i prepare for a key at anytime? in dos mode i know how to do it, but in windows mode which is event-based, how should i assign my desired keyboard handler to the event of a pressed key? (In C# we have event for that, but I donno his elder brother in C++)


Usually, you poll for input at regular intervals.

Quote:
1- Do you suggest having an Array of like int[20][10] for my GameBoard OR treating it like array of Empty Objects?
each one has its Pros and cons, having an integer array makes its managing much more easier, but having the object based makes its design more b-e-a-u-tiful but will need more processing power at runtime.


It's not about design beauty. It's about making sense. The game board being an array of integers does not make any sense to me—it's pretty obvious when playing the game that the game board is an array of bricks (some of which may be missing). So, typedef a brick type (or create a class for it) and create an array of that. Ultimately, depending on your program complexity, it could be a simple boolean indicating brick presence, or a more complex brick class containing rendering information.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by test86
no use of inheritance?! so each piece will have its own move method and its own rotation? i agree that making a universal rotation might be hard, but I think we can have movement resused.


He means, have a single Piece class, with a single rotation function, a single movement function, and a single position system.

The difference between tetris pieces is shape, not behaviour. This is something that is handled through data, not through inheritance. As mentioned before, a 4x4 bitmap of occupied/free bricks for a piece should be more than enough.


i dont get it, how they dont differ just in shape? rotation of an L-shape is not like rotation of a mirrored L-shape, i mean when you rotate each of 7 types of pieaces, each will be in a unique destination and each need a specific collision testing for that target space.
If i can do them all with one type of code, that would be great. plz explain.

you want to treat all of the pieces like a 4x4, right? i dont get it, since a L-Shape will rotate it its way and will be in a different position than another shape, still dont understand.

and BIG THANKS to all people for your fast replies, it really motivates me, since i avoided reading the tetris tutorial and wanted to gain the info, not just some chewed food.

Share this post


Link to post
Share on other sites
A right rotation for a 4x4 board is:


11 21 31 41 -> 14 12 12 11
12 22 32 42 -> 24 23 22 21
13 23 33 43 -> 34 33 32 31
14 24 34 44 -> 44 43 42 41

## ->
## -> ## ## ##
## ## -> ##
->


It does not matter what the values (brick, no brick) of the pieces of the board are, it still performs the same operation of shuffling around the values.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Use the clean way. But make sure it's actually clean. It's very easy to delude oneself into thinking that a solution is clean just because it uses objects. The practical solution here would be to compute the new position of the piece, query the board for collisions between the piece and existing bricks, and stopping piece movement if collisions exist.


so these are the things i'm thinking about, but this is my first OO program/game, i did OOP programs before, but its first time that i design one so please be more specific about my problems, as i see, i tried to do those things with mentioned classes, do u think i have not-needed classes? more than enough?

Quote:
Original post by ToohrVyk
Rotation of a piece is merely a permutation of its component brick elements. It can be entirely expressed in terms of data, and requires neither inheritance nor conditionals.

as i asked in my previous post, i dont understand how you guys treat all the pieces like one. how it is in terms of data? what do u mean?


Quote:
Original post by ToohrVyk
Usually, you poll for input at regular intervals.

poll?! what is this?!
and another thing, i should do this poll thing at certain intervals? can't i just use the way the way of traditional .net? like connecting an event to an event handler? do i have to actually WAIT for an input? the user may press left WHILE my piece is moving down, what about that?

Quote:
Original post by ToohrVyk

It's not about design beauty. It's about making sense. The game board being an array of integers does not make any sense to me—it's pretty obvious when playing the game that the game board is an array of bricks (some of which may be missing). So, typedef a brick type (or create a class for it) and create an array of that. Ultimately, depending on your program complexity, it could be a simple boolean indicating brick presence, or a more complex brick class containing rendering information.


thanks for recommending this, so u mean and array of bricks which have a boolean member like Present? am i right?


and how do u multi qoute my answers? i do it by copy& paste the qoute tag, is there any easier way?


AGAIN, thnx for your time/effort/quick helps, i really really appreciate it.
and sorry for my bad english. :P

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
A right rotation for a 4x4 board is:


11 21 31 41 -> 14 12 12 11
12 22 32 42 -> 24 23 22 21
13 23 33 43 -> 34 33 32 31
14 24 34 44 -> 44 43 42 41

## ->
## -> ## ## ##
## ## -> ##
->




It does not matter what the values (brick, no brick) of the pieces of the board are, it still performs the same operation of shuffling around the values.

oof, cool ASCII art! ;)

i know this, you are talking about rotating a bitmap 90degrees, i know how to do it, thats not the problem (atleast as i understood what u meant), my problem is its target position in my gameboard's array for collision testing.

i think we are talking about two seperate things here!
i'm not concernd about graphics here, even dos is ok with me, my question is how do you treat all objects the same way as they occupy different positions on gameboard (i mean in the array that you store your blocks).

if i'm not clear yet, tell my i'll draw something to show what i mean.
thnx man for ur time/help/caring.

Share this post


Link to post
Share on other sites
Quote:
Original post by test86
i dont get it, how they dont differ just in shape? rotation of an L-shape is not like rotation of a mirrored L-shape, i mean when you rotate each of 7 types of pieaces, each will be in a unique destination and each need a specific collision testing for that target space.
If i can do them all with one type of code, that would be great. plz explain.

you want to treat all of the pieces like a 4x4, right? i dont get it, since a L-Shape will rotate it its way and will be in a different position than another shape, still dont understand.


A piece is a piece. The OO way is "what", not "how".

You can "rotate" a piece. "how" you do it is hidden. But the "rotate" of all pieces is exactly the same.

For tetris, the somewhat optimal representation of pieces is similar to bitmap, and as shown above - use a grid. Your Piece class can then use different array representation, but that is hidden.

Here's an example of how it might look:

class Piece
{
// create new piece with certain max dimensions
Piece(width, height);

// piece creators, add or remove "filled" spots
void fill_spot(int x, int y);
void clear_spot(int x, int y);

// manipulator functions
void rotate_cw();
void rotate_ccw();

void move_by( int dx, int dy );
};

class Board
{
// attempts to push the piece one row down
// returns false if piece would collide
//
bool push_down( Piece p );

// Tests for intersection between a piece and board
// also test for left, right and bottom margins
bool intersects( Piece p );

// Piece is blended into board
void blend( Piece p);
}




Just a quick hack, so it's probably not very functional.

Everything else is a matter of implementation.

If you do want to get OO-ish, observe that both of the above classes have same common functionality - the representation of the board. When you blend the piece into board, you need to perform same functionality as when creating a piece - turning on some blocks, and turning off others.

So now you can merge this:


class Blocks
{
Blocks(int width, int height);

// piece creators, add or remove "filled" spots
void fill_spot(int x, int y);
void clear_spot(int x, int y);
}

class Piece : Blocks
{
void rotate_cw();
void rotate_ccw();

void move_by( int dx, int dy );
}

class Board : Blocks
{
// Re-use paren't class functionality to blend the piece
void blend( Piece p)
{
for (every block in p)
if ( spot in p is filled)
fill_spot(x, y);
}
}




Now you can even reuse the rendering code to render "Blocks" class, and then just pass it either a Piece or a Board object.

Just a few things to think over.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
The difference between tetris pieces is shape

Interesting. In my last tetris clone, I had a class Shape which stored information about the shape (duh). There were 28 instances of this class for the seven basic shapes in four orientations (7*4 = 28).

Then I had a class Piece which stored information about the position, plus it referenced a Shape. rotate() just changed the reference to another shape. No need to rotate every brick by hand :)

Am I the first one to seperate Shape and Piece into two different classes? They are clearly two distinct things.

BTW I don't think representing a Shape with a 4x4 matrix is the most logical or efficient solution. A vector of four coordinate pairs seems better to me, because when you process the piece (check for collisions, insert it into the gameboard), you only have to do four operations instead of 16.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Am I the first one to seperate Shape and Piece into two different classes? They are clearly two distinct things.


Technically, they aren't.

The difference between shape and piece is same as between class and instance.

Class is formal definition. Instance is one version of this class.

All L pieces are the same. Whether they get rotated or not, they do not change the nature of what they are - they are still exactly the same shape, and they will always be pieces.

For example, in an RPG, a human character is just that. Entity <- Creature <- Player. There will be many humans, some tall, some short, some facing left, some right, ....

They are still the same class - PlayerObject, with its race property set to human, it's orientation to some matrix, and position to a vector.

Renderer is the responsible for loading the graphical representation, such as mesh and textures.

In case of tetris, there is no such representation, since the rendering is done by filling blocks.

For tetris I wrote long long time ago, I simply used arrays of 4x4 representing every possible rotation of every object. Each of those also had an achor point around which it was rotate.

If I did this in OO way, it would be:

class Piece {
bool blocks[4][4];
int x_anchor; //(0..3)
int y_anchor; //(0..3)

Piece &rotate();
}
Piece blocks[28];



Similar to your aproach, but I don't separate shape and piece. They are functionally the same, but rotate still returns the new piece.

This aproach however doesn't allow for animated rotations some games use.

Quote:
BTW I don't think representing a Shape with a 4x4 matrix is the most logical or efficient solution. A vector of four coordinate pairs seems better to me, because when you process the piece (check for collisions, insert it into the gameboard), you only have to do four operations instead of 16.


Unsigned short is optimal representation. You know it's 4x4, and each spot can either have a block or not.

You can then use bitmasking to determine collisions. This may potentially be much faster, since you would end up with very simple operations, shifts and "ands", with one single comparison at the end, possibly resulting in good pipelining.

But the above is completely absurd in case of tetris, where performance in this part is pointless.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Am I the first one to seperate Shape and Piece into two different classes?


No. This is basically the Flyweight pattern, if I understand you correctly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
Original post by DevFred
Shape and Piece [...] are clearly two distinct things.

Technically, they aren't.

Maybe I wasnt clear enough on this. The class Shape defines how a Shape looks. Here is the interface:

public interface Shape
{

/**
* returns a new shape which is obtained by cutting the current shape to the
* given thresholds.
*
* @param bot
* everything below bot will be cut
* @param top
* everything above top will be cut
*
* @return null if everything has been cut away, an appropriate shape else
*/

public Shape cut(int bot, int top);

/**
* checks wether the shape contains the given block.
*
* @param x
* the column
* @param y
* the row
*
* @return true if the shape contains the given block
*/

public boolean contains(int x, int y);

public int getBot(int index);

public int getTop(int index);

public int getTetramino();

public int getOrientation();

public int getOffset();

public int getWidth();

public boolean equals(Object o);

public int hashCode();
}


So you can ask a Shape: "Do you have a brick at x=3 and y=1?" for example. Theres 28 instances of a class which implements the interface Shape for the basic shapes.

The class Piece however is instantiated for every piece that you can see on the board. Ten L pieces residing on different positions on the board all share the same shape, but they differ in their position. Excerpt from the class Piece:

/**
* a piece is basically a moving shape.
*
* @author fred
*/

public class Piece implements Flexible, Iterable<Integer>, Cloneable
{
private Shape _shape;

private int _x;
private Moving _y;

/**
* creates a new piece.
*
* @param shape
* the shape
* @param x
* the initial x position
* @param y
* describes how the piece initially moves in the vertical
* direction
*/

public Piece(Shape shape, int x, Moving y)
{
assert shape != null;
assert x >= 0;
assert y != null;

_shape = shape;
_x = x;
_y = y;
}

/**
* moves the piece one unit to the left.
*/

public void moveLeft()
{
_x--;
}

/**
* moves the piece one unit to the right.
*/

public void moveRight()
{
_x++;
}

/**
* changes the moving behaviour according to the given factory.
*/

public void changeMoving(MovingFactory factory)
{
assert factory != null;
_y = factory.create(_y);
}

public int getWidth()
{
return _shape.getWidth();
}

public int getX()
{
return _x;
}

public int getOffset()
{
return _shape.getOffset();
}

public void setShape(Shape shape)
{
assert shape != null;
_shape = shape;
}

public Shape getShape()
{
return _shape;
}

public Moving getMoving()
{
return _y;
}

public int getTetramino()
{
return _shape.getTetramino();
}
}



Quote:

All L pieces are the same.

No, they differ in their position on the board.

Quote:

This aproach however doesn't allow for animated rotations some games use.

Why not?

Quote:
Quote:
A vector of four coordinate pairs seems better to me, because [...] you only have to do four operations instead of 16.

Unsigned short is optimal representation. You know it's 4x4, and each spot can either have a block or not.

You can then use bitmasking to determine collisions. This may potentially be much faster, since you would end up with very simple operations, shifts and "ands", with one single comparison at the end, possibly resulting in good pipelining.

This is the optimal representation if you want to save memory, but I was talking about clear code (plus my solution will be faster, but I don't care about that).

Think about it: if you use a 4x4 matrix (however you represent it does not matter), you have to do two nested loops. 16 times you say: "if there's a brick in the piece and a brick in the board at the current position (x,y), then there e is a collision."

With my solution, the code reads: "for every position in piece: if there's a brick at that position on the board, there is a collision". Much cleaner.

Quote:

But the above is completely absurd in case of tetris, where performance in this part is pointless.

I concur.

BTW I used another approach in the code above, don't confuse it with the idea of four vectors ;) Basically I have a list of vertical slices which allows for even more efficient collision detection.

Share this post


Link to post
Share on other sites
hi people, i was working on a homework for my Simulation course, i first did it in procedural, then i converted it to single object, to practice me OOP. if anyone intrested, i can post it too, if you have time to observ.

first of all THANKS to all the contributors, I pray to God for you all.

so, more about Tetris that is filling my mind. (i really want to write a platformer, a clone mario u say, but i know i have to write Teris and other stuff first. How to store the ball in pong is keeping my mind busy too).

01- I just don’t feel the "4x4 block" be the Object Oriented way, I still think having a class hierarchy is much better design.

02- As ToohrVyk recommended having an array of Bricks for game board instead of integers (as he again recommended) seemed
confusing to me, so there is a Boolean in Brick which shows whether there is a brick on corresponding board or not, but with
The "4x4 Block" way you recommended i cant use them together.
do u mean having a struct called Brick which has a boolean which shows its free or not and a X and Y for coordination’s?
Anything else? Did I understand correctly? Or a m!sunderstood?

03- What moves a piece in the board? I mean whenever we get the action of "move current block to right", which method of
Which object should handle it? Should I have a MoveRight() for my Block? Should I tell the Board to move it for me?
If i tell the piece to move right (which I think is better comparing to real world which WE actually move, Earth doesn’t move
us), I have to public the game board (or keep it public and have a get/set method for it).
(maybe its right, tell me if this method is good, I donno, i just know that i have to keep everything as private as possible,
donno why really)

04- could u describe more about "4x4 block" method? as i understood, u are telling me to SEE everything as a 4x4block of
Boolean (or whatever), so it arises some problems for me:

4-A: when u decide to put an L-shape on the screen, give the constructor a matrix (or a similar data type), so it will fill
The asked blocks to fill? or anything else?

04-B: how do u put it on the board? does the board have a method to accept the newly created block? How do u put it actually
on the board?

04-C: how a piece and the board relate? (i think they call it class diagram), i mean when u move the piece one-piece to
right, do u have to ask 16times that if the target is free or not? is there anyway to public an data type just for an special
object?

04-D: do make a "24 by 12" game board? 20 x 10 is standard, two more for showing the boarders? i think we need two more block
for each dimension since we are treating the blocks as 4x4, so i think we need "24x14 of Bricks".

05: i still have problems with getting Real time input from keyboard in C++, where should i wait for a key to press?
i have a main loop for game, and inside it i manage stuff, there should be a hooking thing like the one in C# that i hook the
keyboard with a method that handles it, tell me how to do it in VC++.


THANKs!

Share this post


Link to post
Share on other sites
Quote:
Original post by test86
01- I just don’t feel the "4x4 block" be the Object Oriented way, I still think having a class hierarchy is much better design.

Different classes behave differently. How would an L piece behave any differently then a J piece? L pieces and J pieces have EVERYTHING in common: they can be moved, they can be rotated, and they have a distinct form.

Just because each peace might have a different form does not mean you need different classes! For example, you can have a string containting "hello" and a string containing "world!". Both are strings. You don't need seperate classes for every string literal possible :)

Quote:

having an array of Bricks

What is a brick? What state does a brick have? I'm just a little confused on terminology.

Quote:

What moves a piece in the board?

user presses left arrow key
tell the piece to move one to the left (this will just decrease some x position member in piece)
ask the board if the piece collides with the rest of the board
if it collides, move it back, i.e. one to the right

Quote:

If i tell the piece to move right [...], I have to public the game board

No. Just implement a method "bool collidesWith(const Piece &piece)" in board. The board is responsible for testing if the piece fits or not, because it knows its own internal structure.

Quote:

4-A: when u decide to put an L-shape on the screen, give the constructor a matrix (or a similar data type), so it will fill
The asked blocks to fill? or anything else?

You could give the constructor an integer between 0 and 27, and then you use that integer to index a precomputed array which holds 28 of these 4x4 blocks.

Quote:

04-B: how do u put it on the board? does the board have a method to accept the newly created block? How do u put it actually on the board?

myBoard.put(piece)

Quote:

04-C: how a piece and the board relate?

A piece does not know about the existence of a board. Basically all the board has to know about pieces is this: Is there a brick at (x/y)? As long as the piece provides such a method, everything is fine.

Quote:

04-D: do make a "24 by 12" game board? 20 x 10 is standard, two more for showing the boarders? i think we need two more block
for each dimension since we are treating the blocks as 4x4, so i think we need "24x14 of Bricks".

I used a width of 16 in my old implementations, but I've forgotten why :)

Share this post


Link to post
Share on other sites
Hi, thnx for answering my questions.
I have another problem with input.
I know how to do it in C# but in C++, i donno.
the problem is, where should i wait for user input? as u know when user doesnt press any button, the piece will go down once in each interval, but when user preses down, it has to move corresponding to the input.
i'll have a game loop where i first check for gameover, if not, i'll try to move down the current piece,etc. what should i do to get user input? should i WAIT in some place? or it can be event driven and like C#, hook keyboard input to a funcion so whenever a key is pressed, the funcion will be called automatically and no need for waiting.
for my old dos based Snake, i think i used Waiting method, which i waited at somepoint, if i remember correctly.

and today i'll start coding tetris, i'll post whatever i'll finish till tonight.

thnx in advance.

Share this post


Link to post
Share on other sites
cuz i'm planning for future.
as i said in last topic, i'm planning to work in game industry and THE language is C++, i'm planning to write games for nintendo and both DS and Wii as far as i know are C++ based, like most of engines, graphically or visual or anything.
and most frameworks,OSes, Engines support C++ if not recommended.
C# made me lazy, mostly on things that hides from programmer, like pointers and stuff, i had to learn these things years ago but C# came out and i escaped from C++ to C#. add the framework that you have to have in order to run your game and as you know, current consoles dont have .net framework, maybe xbox360, which i'm not planning to work on.

thats it, and please dont talk about this topic in here and please let this topic be as its name tells, about my OOP tetris clone, lets talk about this subject in a more apropriate topic, or PM if its just you and me.
------
would anyone please answer to my last question about keyboard input?

Share this post


Link to post
Share on other sites
Quote:
Original post by test86
cuz i'm planning for future.

So I can restate the question: why do you want to do it in C++ then?
Quote:

as i said in last topic, i'm planning to work in game industry and THE language is C++, i'm planning to write games for nintendo and both DS and Wii as far as i know are C++ based, like most of engines, graphically or visual or anything.
and most frameworks,OSes, Engines support C++ if not recommended.

Oh, I see. But you must understand that the language syntax is not difficult to learn - the most difficult thing is to learn how to use an object oriented language. So I suggest you to stick to C# now, and learn C++ later - you still have some time to learn it. Or, to put it using other words: Learn the concepts, then implement the concepts in the different languages.
Quote:

C# made me lazy, mostly on things that hides from programmer, like pointers and stuff, i had to learn these things years ago but C# came out and i escaped from C++ to C#. add the framework that you have to have in order to run your game and as you know, current consoles dont have .net framework, maybe xbox360, which i'm not planning to work on.

If you know how pointers works, there's little chance that you'll forget that. I mean, once you get them, it's so simple that you can't forget them.

Quote:

thats it, and please dont talk about this topic in here and please let this topic be as its name tells, about my OOP tetris clone, lets talk about this subject in a more apropriate topic, or PM if its just you and me.

I understand this, but I still prefer to answer here [smile].

Quote:
would anyone please answer to my last question about keyboard input?

Input handling is done differently on different platforms (I mean, whether you are using the console or Windows). You have to tell us what kind of API you are using - pure Win32, Windows.Forms, (managed) DirectX, XNA, OpenGL, a cross platform library like SDL?) if you want to get some usefull answer here.

Best regards,

Share this post


Link to post
Share on other sites
I followed a C++ tetris tutorial, but converted it to javascript code, and added some of my own features.

This is the basic concept...

//psuedo
if(!gameOver)
{
if(!paused)
{
if(dropTimer>=fallSpeed)
{
checkHitOtherBlocks();
checkHitBottom();
if(!hitBlock && !hitBottom)
{
moveShapeDown();
//reset dropTimer
}
if((hitBlock || hitBottom)&& !sliding)//lets the user slide the shape for a second
{
sliding=true;
//start slideTimer
}
if(sliding && slideTimer>=.5)
{
handleHitBottom();
}
}
}
}



The way I did it, was each 'shape' consisted of 4 'blocks'. When I rotated the shape, I would move all the blocks to a center pivot point, do x=y, y=-x, then move them back out to their proper positions. (This rotates it 90 degrees)

In my 'handleHitBottom()' I would add the current shapes block's to the 'deadBlocks' array.

It might not be the best way to do it but it was ok for my first real game. There is a slight delay when your near the top because it has to scan through all of the old blocks each time the shape drops for collision detection.

Heres a link to my site with my tetris clone 'FourSquare' if you want to check it out.

BUnzaga.com

Share this post


Link to post
Share on other sites
Quote:
Quote:

as i said in last topic, i'm planning to work in game industry and THE language is C++, i'm planning to write games for nintendo and both DS and Wii as far as i know are C++ based, like most of engines, graphically or visual or anything.
and most frameworks,OSes, Engines support C++ if not recommended.

Oh, I see. But you must understand that the language syntax is not difficult to learn - the most difficult thing is to learn how to use an object oriented language. So I suggest you to stick to C# now, and learn C++ later - you still have some time to learn it. Or, to put it using other words: Learn the concepts, then implement the concepts in the different languages.


Also consider that, by the time you are quite proficient in this stuff, C# may well be 'THE language'. These things take time, and chances are you are not special (i.e., you are not likely to avoid taking years to become *good* at programming; and the fact that you take years to become *good* doesn't mean there's anything wrong with you; it's just like any other art or craft.)

Quote:
Quote:
C# made me lazy, mostly on things that hides from programmer, like pointers and stuff, i had to learn these things years ago but C# came out and i escaped from C++ to C#. add the framework that you have to have in order to run your game and as you know, current consoles dont have .net framework, maybe xbox360, which i'm not planning to work on.

If you know how pointers works, there's little chance that you'll forget that. I mean, once you get them, it's so simple that you can't forget them.


Also consider that ("real") laziness is considered a virtue for programmers. [smile]

@Emmanuel: Why not put the 'Best Regards,' text as part of your signature instead of repeating it every time? [smile]

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