Advertisement

Tetris Troubles...

Started by January 07, 2001 06:13 PM
8 comments, last by JSCFaith 24 years ago
Hey, I am trying to make a clone of tetris, but I am running into a few problems. My main one is CollisionDetection. I know how to do collision detection and my function works, the problem is knowing where the blocks came from. Whats happeneing is that, lets say you are pressing the left key, right when you hit the top of another block. Well then you just go threw it. Its hard to explain. Download the little tetris game. It's not very far done though. No score, nothing like that. Try pushing the right key while along the wall. See what happens. Just hold it down till you get to the bottom. Well if anyone can help me with this collision detection thing. That would be great. Oh yeah. I included all my code for the tetris blocks. Later guys.. Here is where you can get the tetris.zip that I made. http://www.starkingdomscalculator.8m.com/tetris.htm Edited by - JSCFaith on January 7, 2001 7:14:15 PM Edited by - JSCFaith on January 7, 2001 8:02:43 PM
Please help.
Advertisement
Say hey... I''m doing a tetris clone myself.

For my clone, I don''t do any "collision detection" whatsoever. I think you are thinking about this problem in the wrong way.

In my version (star.tris), I "ask" the board object if it would be possible to "add" a piece object. The board, by checking for overlapping pieces, then tells the game if that move is possible or not.

Think of it this way: If the user presses the right key you check to see if that move would be possible in the first place. If it is possible, THEN you move the piece to the right. If not, you just leave it alone.

If the user presses the rotate key, you first check to see if the rotated piece will fit. If it does fit, THEN you rotate.

------

But, I''m going through your code right now. I''m away from my PC at the moment, and I can''t run your game on my iMac (I know, I know...)

I see that you have everything under one large blocks class. Why have you chosen to do that? Myself, I have three components:

1 - Board class: an object that represents one tetris board.

2 - Piece class: an object that represents one tetris piece.

3 - Shape class: an object that supplies the actual data that represents each piece.

With this arrangement, I can make multiple copies of a particular board, used for AI that can "see" into the future. It''s also much more flexible. I don''t see how you could shoehorn AI into your engine so far.

----

Your rotate function seems to be a little too complicated. It looks like you''re actually rotating the pieces themselves.

I think it''s more efficient to represent each of the 4x7 = 28 pieces separately. Oh yeah... squares can rotate.

----

From what it looks like so far, while it would be possible for you to get this to work, it really doesn''t take advantage of object-oriented design at all, and that''s something that''ll make tetris a whole lot easier. I think you should take another look at the data structures that you''re using to represent everything.

This is the specification I''m working from. It''s from a book we used in my CS225 class:

http://www.banarnar.com/tetris.jpg

Here''s all the possible pieces, which I represent separately. Notice how the square rotates:

http://www.banarnar.com/tetris2.jpg

Sorry about the size.

I don''t know how serious you are about making a tetris clone (serious enough to code one), but I think it would be great if you and I, and anyone else working on this, to share ideas and code, either on this board or otherwise. Obviously, tetris is a tough nut to crack.

I''d like to see someone come up with an answer to that book problem...

I''ve been designing a tetris game for about a week now... and the features that I''ve come up with are:
* skinnable board
* skinnable pieces (not tetriminoes sp?)
* Variable Difficulty
* Score system (more lines taken at a single time the more points)

The system I think would be easiest to implement and would still be pretty fast was where the board and pieces are COMPLETELY seperate, and the board just sent the position and piece index for every piece on the board (a piece being a single block not a tetriminoe).

smarson:
I like the idea of having the 7 tetriminoes seperated into the 28 different tetriminoes, however I think I would be more tempted to store them as an SHORT because of the design used for the board.

Your tetriminoes are different to almost every tetris game out there... any tetris game I''ve seen keeps the blocks on the same line... but it is a gameplay choice, you could change this at any time with the right design.

So each tetriminoe could be represented as a 4*4 board (i.e. 16-bits), so 64-bits per piece. (Not including game def''s i.e. orientation, index yada yada). Then all you need to store is the bmp''s that are skinning the blocks.

AI in a tetris game, is it really needed? you could just tell the game to use a different piece depending on a random number generator.

The book''s problem, was to create a tetris game that was efficient enough to be run on a server so that 1000''s of people could play it at once... When was the book published??? I''m guessing that that number could be upto the millions using today''s technology. However we don''t use a server for processing for dumb terminals today... so I guess you could use it for a net game???

I don''t think I''d like to develop a tetris game which doesn''t rely on the client for any game logic... I mean LAG would be a huge issue in collision detection.

Have fun anyway...




Regards,
Nekosion
Regards,Nekosion
I drew the tetronimoes in such a way that each piece rotates around a specific pivot point, as per the original Tetris.

It looks as though I would represent each piece in it''s own 4x4 block, but that would be a tremendous waste of memory. Instead each induvidual Brick is a separate object with 3 properties; X, Y, and TYPE. A tetronimo then becomes a string of BrickNodes, that is, a linked list that represents each of the Bricks in the shape. This follows the book in that the pieces could be extended to pentonimoes and soforth.

If you look at my original drawing (http://www.banarnar.com/tetris2.jpg) the first piece in the top-left corner would be represented as:

(0,1), (1,1), (1,2), (2,1), NULL

Because the board is represented by a 2D array, you simply add these values to the xy location of the piece to determine where you are on the board, and keep going until you hit null. Very, very, very fast.

---

And AI is very necessary if you want the computer to be able to whip your butt. One player is easy. Two player is harder. N-player is a total challenge.

If you''re going to do AI, you have to design the game in such a way that will allow the AI to quickly identify all the possible moves that it can make with the current piece. This includes keeping track of all possible orientations and rotations, as well as how many points can be scored, and how many points MIGHT be scored in the future.

The book was written in 1991, even though it looks much older. The name is "Data Structures & Their Algorithms". A very necessary book, by the way. Solving the problem as they lay it out is very difficult, but it''s a good way to practice design.

In many ways, I think that a Tetris AI has the potential to be much more complicated than a Quake or Unreal bot. For one, the AI and the player have access to the same information, which can be the catalyst for a HUGE amount of strategy.

My overal goal is to design unbeatable Tetris AI, write a paper on it, and get into grad school with it. But, that''s just me.
4*4 block a waste of memory???
4*4 = 16, a 16-bit block is a waste of memory?

It is only 2 chars, instead of (as your system is (I think) 2 chars per brick). Basically I am talking about using bits instead of bytes for knowledge if a brick is at a certain point.

This would be a VERY memory efficient algorithm and would also make much of the code reusable for every tetriminoe (sp?).

The board that I am using will probably be stored in much the same way, but with a bit more logic involved. I think that I will make the board of variable size and then figure out some formula to find the score depending on the size of the board.

Or so I think at the moment.


Regards,
Nekosion
Regards,Nekosion
Advertisement
It is a waste of memory when you consider the fact that most of the 4x4 array is empty.

The goal isn''t to have memory-efficient pieces. There are only (in this case) 28 pieces. No copies will be made in memory. Only pointers to BrickNodes are passed around. The real benefit of this structure comes in the most cpu-intensive operation in tetris, move validation or "collision detection."

By having Brick and BrickNode objects, initially, it might be a bit larger, but it allows for the most growth. If I wanted to change the game to 3 dimensions, I would just assign more BrickNodes. If I wanted to add special bricks such as bombs and powerups, I can add that functionality to the Brick object, and the change will be reflected in by the rest of the code.

I think it''s important for the code to allow as much growth as possible. Then you can avoid painting yourself in a corner with a one-sided design. I think JSCFaith is running into problems because his design is very rigid.

---

This is a good topic! Tetris algorithms and AI generalize into so many other problems. When you think of it, RTS games played on 2D grid systems can be represented in similar ways. I''ve been up all night working on this!

I''ll be posting my design notes and data structures once I get a better grip on it. Then we''ll see how many holes you guys can poke through my design...

... but now, I must sleep.
Thanks guys. One question. So you guys are saying that you make an array for the board. Then you copy the 4*4 array for each shape into the board. Am I right? Or did I misunderstand you?

Thanks again.

BTW. I would love to make a tetris clone. One thing that we should do if we decided to do this is make it multiplayer over the internet. Might be hard, but we can do it if we try. Later..
Hey there. I made a tetris clone using vb and came up with a very simple way to store the blocks. Just have 4 coord pairs. For example, the L shape would be stored as (0,1), (0,0), (0,-1), (1,-1) which would draw the block in the following order..

1
2
34

Now, you can rotate these by using x=y, y=-x for a clockwise turn, or x=-y, y=x for a counter-clockwise turn. Voila!

Doing a clockwise turn to the above example yields (1,-1), (0,0), (-1, 0), (-1, -1).

or...

321
4

I liked this method because it saves you from coding all the initial 28 blocks... now you only need to code 7. Small code is happy code!
I thought about the multiplayer aspect of it, and it would be cool if it supported lots of people. You would directly compete against the person to the left and right of you, and they would compete against their left and right. (compete as in, double lines they score adds to your stack...) As people came in, they''d be put on either end of the ladder. (the ladder is sideways.. hehe) Would be pretty cool I think. Anyway, seems like a lot of work, but it would be fun.

This topic is closed to new replies.

Advertisement