Question

I am trying to make my own version of the Tetris game. So far, I am able to show pieces, rotate them. But I am stuck at a point and not sure how to proceed.

enter image description here

I have a

  • BoardClass with static integer array board for the grid. With different integer numbers I am showing different colors.
  • Block class which has methods to generate random blocks and rotate them.
  • A Graphic class which display colors based on the board array, and
  • A GameLogic class for starting, pausing game.

For current piece I have taken a 4x4 array and is placed onto the board in first four rows of board

All works good. But how do I start moving things. How to establish a relation between blocks is where I am stuck.

My problems

  • Am I working on right track?
  • How to automate things, I mean, how will the blocks appear automatically one after another. I know I can have a DispatcherTimer and move block at tick event but drawing and redrawing the complete grid is slow. I want only the portion of the game grid to be redrawn.
  • If I move my piece left/right/down the complete piece array would move and this would mean a 4x4 piece moving and not just the piece which may be in 1x3 or 2x2.
  • How to know if the piece is new or an old settled piece. I know I can take a boolean value but I think a boolean array of 10x20 would be large.

I know these might not be good question to ask on SO but am stuck and any help would be appreciated. And ask me if you want to see any piece of code.

Was it helpful?

Solution

Am I working on right track?

I think you are.

How to automate things, I mean, how will the blocks appear automatically one after another. I know I can have a DispatcherTimer and move block at tick event but drawing and redrawing the complete grid is slow. I want only the portion of the game grid to be redrawn.

Have you measured it? Most of the games redraw the whole scene at a 30 frames per second rate. Make it first work, optimise later on.

If I move my piece left/right/down the complete piece array would move and this would mean a 4x4 piece moving and not just the piece which may be in 1x3 or 2x2.

You need to check the bounds before every move. You can do it for each square, you only have 16 comparisons. It is not very difficult and it won't be slow.

How to know if the piece is new or an old settled piece. I know I can take a boolean value but I think a boolean array of 10x20 would be large.

It is not large.

OTHER TIPS

How to automate things/ drawing and redrawing the complete grid is slow. I want only the portion of the game grid to be redrawn.

When the user scores a line - you'll have to redraw anyway the whole grid. (lower the figures which were on top of the line);

If I move my piece left/right/down the complete piece array would move and this would mean a 4x4 piece moving and not just the piece which may be in 1x3 or 2x2.

I think the mistake is in making a 4x4 array for each figure when you actually need 1x4 List of Blocks (for each Tetris figure); you use 4 times more memory, and space!.

How to know if the piece is new or an old settled piece. I know I can take a boolean value but I think a boolean array of 10x20 would be large.

An array is never too large (if we're not talking about millions); You can take my way of doing it: make a LIST in which you store all the "old" pieces of the game. And make a VARIABLE (a figure object) which will point to the "new" figure on the board. After the "new" figure has landed - you just put it in the LIST in which you have the "old" pieces. And make the VARIABLE point to the "new" added figure on your grid.

Hope that helps.

You can check my tetris implementation example (in Java) here

What your've done so far sounds okay to me. As far as your other questions go:

How to automate things, I mean, how will the blocks appear automatically one after another. I know I can have a DispatcherTimer and move block at tick event but drawing and redrawing the complete grid is slow. I want only the portion of the game grid to be redrawn.

  • You could use a dirty recatangle to figure out where to redraw. But if you really have performance troubles with an application as simple as a tetris game I's rather try to figure out why exactly your drawingcode is so slow. On any hardware that has been produced in the last 10 years you should easily archive 100+ fps, even if you redraw your complete board each frame.

How to know if the piece is new or an old settled piece. I know I can take a boolean value but I think a boolean array of 10x20 would be large.

Quite frankly no. Just use an array. 10x20 = 200 entries in your array. AFAIK bools will take 1 Byte each in an arra, that would mean 200 Bytes, so that should not be a problem for a machine built in the last 30 years. Even if you use integers you'd still use less than 1 kb of memory for the board.

Am I working on right track?

If your desire is to learning game programming without pulling out your hair at every little detail, I would recommend using a library like Box2D to handle world space and collisions of objects.

In addition to Box2D, I would also recommend using a game framework that handles many of the basics of game programming, frequency, inputs, sounds, etc... PlayN and LibGDX are great & free frameworks to give you a jump start.

With these tools, your focus will be more on your game and less on the basics.

https://code.google.com/p/playn/

https://code.google.com/p/libgdx/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top