View Full Version : Someone make tetris :d

02-06-2012, 12:17 AM
Can someone try to make tetris? haha
i think its possible with scripting but i dont know anything about python :D
So can someone try and make tetris?

Mr Kidnapper
02-06-2012, 01:09 AM
Tetris? Easy. Make a theoretical boolean grid of arrays X and Y. You probably see where this is going. Make shapes out of four boxes (They're all four boxes if you hadn't noticed) The left arrow key will move each box one space on the grid to the left, and so on for the right key. The up arrow key will switch between rotations, you figure out the box placement on that one. It won't be hard to do it for each of the pieces. The down arrow speeds up the velocity (Let's say the grid move speed was one space per second) by multiplying it with an arbitrary number, say 3.
Make sure that for whatever space on the grid a shape fills, the corresponding X and Ys are marked as filled.
The trick to moving each box down, left, or right is moving them together as a group, or list. If you move them all individually this will fail.

Another difficult task is to determine when the shapes stop moving. How about this. Check all of the boxes inside of the shape together, if the space under one of them is filled, stop the movement method. Do the same thing with the left/right movement and the rotation.
For example, "while index x == false and index y + 1== false, do movement method". This would allow "gravity" to work if the index under the space in the same column was not filled.

Another easy task is to make an entire row disappear if all of its spaces are filled. You can check if a Y is true and all corresponding Xs are true, make all the boxes in the grids disappear and make said Ys and Xs false.
The problem is, you must see all shapes that have already landed as part of one large shape which moves downward as the spaces open up.

If my previous illustration of two X and Y boolean arrays don't work, you can have many Y boolean arrays (As many Y boolean arrays as wide as your tetris will be), each with uniform size. Say if your tetris was 5x9, you'd make 5 Y boolean arrays each 9 items long.

The most difficult thing to do is have it run forever. This is because we have a limited number of shapes, which cloning isn't going to fix. You need referencing. Otherwise feel free to make 20 of each different type of shape so you're covered and can reuse them.

02-06-2012, 01:28 AM
but how would alice determine which boxes to move and which boxes to not move?

02-06-2012, 03:22 PM
but how would alice determine which boxes to move and which boxes to not move?Read it properly, I think he covered that, he covered it quite well actually.

02-06-2012, 08:29 PM
Read it properly, I think he covered that, he covered it quite well actually.

i get that alice can determine when to stop the movement, but how can alice get it to move the 'next' piece?

and also, if I use the method Mr.K told me, will i be able to move the pieces when it's reached the ground? (or when no more boxes are below the shape)
for example, some tetris games allow users to move the shapes couple of times even if they are already on the ground.

Mr Kidnapper
02-06-2012, 09:03 PM
The actual space that the spaces cover can be determined mathematically. Let's say your boxes are 1M by 1M (Since this is Tetris we think in 2D). Then you must create a plane (1M by 1M, otherwise square) and extend it to the length and width of however large you want it. Say it were 5x9, you'd resize it top to bottom by 9 and left to right by 5, if that's correct. Now let's assume that the box is on top of this grid so that its bounding box touches the bounding box of the grid. If you don't understand that, the objects are touching but they aren't going through each other.

What you can do is this: Divide the height by 9 and the width by 5. Make these calculations a number variable. It's kind of obvious that this seems roundabout, but it's just in case the planes happen to not be 1x1 meters. Right now your bottommost box is at coordinates (0,X). X because I don't care where you initially place a piece. Now if/else check if a space under the box is filled. It isn't? Dandy. Move down plane height/9. Awesome. One space down, nine to go. Keep doing this until you reach the "bottom", or until one of the four boxes hit a "false" as for a filled location.
You can do the same thing with the left and right arrow keys.

If you're referring to tetris games allowing players to move a piece left or right for a few seconds after it hits the "bottom", that's all up to you. The thing is, you simply need to make a time lag between the shape hitting the bottom, and the shape becoming the "bottom", which at that point the shape is no longer controllable and the next shape will drop.