How does Tetris work?

Blitz3D Forums/Blitz3D Beginners Area/How does Tetris work?

QuickSilva(Posted 2005) [#1]
I constantly hear that Tetris is the best game to start with for beginners due to its simplicity but I still find it quite difficult to figure out how it works. I can create Pong, Space Invaders but Tetris still proves a problem.

Can someone here who has experience of making a clone of Tetris please share their knowlegde into how the thing works.

Thanks,
Jason.


VP(Posted 2005) [#2]
Wow, this brings back a few memories. I wrote a Tetris clone using a DOS based BASIC variant (can't remember the name, but it was an editor/compiler instead of an interpreter) during lulls in class at college.

I can't remember too much but I can remember it was quite straightforward.

I defined the shapes (and all rotations of the shapes) as arrays and read them in using Read/Data

I think the array itself was laid out as shapes(shape,angle,x,y)

Where shape was the ID of the shape (The 'L', the 'I', the 'S' etc), angle was 0-3 for the 4 possible rotations of a shape (I know, some shapes only looked different in 2 rotations, but this was a quick and dirty code). X and Y were the 'grid' of the shape, eg: -

0,0,0,1
0,0,1,1
0,0,1,0
0,0,0,0

I defined the playing area as a 2D array. A zero meant no block was present, a one meant there was.

It was really quite simple after that. You store the current shape's X and Y (relative to the playing grid array) and you check to see what is underneath your shape on in the playing grid array.


Perhaps this should be a community project? ;)


QuickSilva(Posted 2005) [#3]
Thanks for the help vinylpusher but this is exactly my point. When I was a complete beginner I would not have understood how all this worked, especially the array part. I wonder how many beginners here could actually make a Tetris clone?

Jason.


VP(Posted 2005) [#4]
The best way to find out is to try. I remember when I first wrote my Tetris (called Twatris!) clone, I sat and thought things like:

"Right, I know where the shape is, how do I see if it's about to land on a block in the playarea?"

That ended up being the most complex part of the whole program. Pen and paper was used to figure the logic for that one!

If you loop through the shape%() array and compare it with the playarea%() array (but one 'block' below current chape's position) and you see a 1 there, you know you're about to land the shape. If you've looped through the whole shape%() array and a 1 never appeared, you know you're safe to lower the shape on the next update.


pseudocode

safetodrop=true
for y=0 to 3
for x=0 to 3
if playarea(x + current_shape_x , y + current_shape_y + 1) = 1 and shape(x,y) = 1 then safetodrop = false (there is an obstruction, we're about to land).
next
next

You have to cater for when the shape is at the bottom of the playarea (so that you dont try and check the playarea() array for a y position out of bounds). Essentially though, that's how you 'do' Tetris.

checking for filled lines in the playarea is quite simple and shouldn't present much of a problem.


VP(Posted 2005) [#5]
Code below is really cruddy and not what I would write if I were serious about creating a Tetris clone.

Only the framework is present. Nothing actually works, other than the screen updates and you can move a shape.

For the included 'keyboard.bb' go to http://www.blitzbasic.com/codearcs/codearcs.php?code=1483
Better code below...



octothorpe(Posted 2005) [#6]
I remember there being a couple of quirks.

Firstly, since I was rotating the shapes myself instead of storing pre-rotated versions (I was building 3d meshes and RotateEntity()ing,) there were a few differences between my rotations and the original game's which I had to code for.

Secondly, there are many different ways to handle rotation attempts when there isn't room to rotate (i.e. when the rotated shape would intersect with the mess of blocks in the well.) Some games (like the original) simply don't allow rotation that would move through other blocks. Other games (like the awful GameCube version) move the active shape up until it doesn't intersect, allowing players to "bounce" a shape indefinitely on the well even at the highest game speeds. Still others (like the wonderful New Tetris for N64) have special rules for rotations to allow you to pull off some pretty cool moves. For example:

|###         |       |#           |
|#%%%%%%%%%%%|  -->  |#%%%%%%%%%%%|
|  %%%%%%%%%%|       |##%%%%%%%%%%|
|%%%%%%%% %%%|       |%%%%%%%% %%%|


I recall that last one being tricky, because the shape has to both move and rotate it to work. I believe I had to do two checks to see if rotation would work: one with the shape's y-position incremented by one.

Of course, the original Tetris didn't support this, so it's not a requirement that you have to. :)

P.S. vinylpusher: [codebox]


VP(Posted 2005) [#7]
octo: [code] works fine if your source doesn't have long lines of text. Mine doesn't go over 80 columns.

[code] also lets you have a look through the code without having to copy&paste it elsewhere.

you would have to have a very narrow browser window to cause you to have to scroll.

**EDIT** I see your point now, file has been vertically squished ;)


octothorpe(Posted 2005) [#8]
OT:

Personally, anything over a pageful I'd rather read elsewhere with syntax highlighting. My qualm is simply that large amounts of included code break up conversations visually. The cool thing about [codebox] is that you can <ctrl>+A inside of it to quickly and easily select the entire contents for copying-and-pasting.

I think your code was cleaner before you vertically squished it; specifically the Data, the content of which is no longer obvious.


VP(Posted 2005) [#9]
You're right octo, updated version below:

Controls:
Arrow left/right = move shape left/right.
Z = Spin counter-clockwise.
X = Spin clockwise.
Space or arrow down = fast drop.
Esc = quit.

New features:
o) Colours! Woo!
o) Shapes can now land in the playarea.

Doesn't work:
x) Array bound checking (you can move too far left or right).
x) Rotation validity.
x) Line filling (i.e., the idea of the game!).






QuickSilva(Posted 2005) [#10]
Nice, thanks for taking the time to do this, much appreciated and I now have a much clearer idea of how to make my own.

Cheers,
Jason.


WolRon(Posted 2005) [#11]
Funny no one mentioned my Tetris tutorial...

bare-bones Tetris

:(


I already did the hard part for ya...


EDIT: wow, vinylpushers code is amazingly close to mine.


VP(Posted 2005) [#12]
WolRon, I'll have a look at your code in a bit. i'm just finishing off re-doing the gameboy tetris theme in Reason ;)

**EDIT** Just had a peek. Your code is a lot cleaner than mine. Then again, mine was written in under an hour and without the 7 P's (prior preparation and planning prevents p*ss poor performance).

**EDIT** Lazy me, I've converted the NES midi of the Tetris theme.

Download:
WAV 6.46MiB
APE 3.24MiB (insane lossless compression, requires conversion before use).
OGG 300KiB (low quality)

Nicer version 2:
WAV 10.83MiB
APE 4.98MiB (extra high lossless compression)
OGG 792KiB (medium quality)


QuickSilva(Posted 2005) [#13]
I`ll check your code out to WolRon, thanks :)

Jason.


Hotshot2005(Posted 2005) [#14]
Teris is hard to do for me but I know it come with Grids arrays and shape. I think the hard part is rotation and Colisions.

I like his new tutorial as he got some great tutorial in there
http://home.cmit.net/rwolbeck/programmingtutorial/index.htm


Ross C(Posted 2005) [#15]
The rotations are easy. Any time a block falls, is moved left or right, or is rotated, you check the array for overlapping 1's. If there found, don't allow rotation/movement. In the case of moving downwards, paste the data from the falling array, into the main grid array, and your sorted. :o)


VP(Posted 2005) [#16]
Ross C: Almost, but 'true' Tetris will let you rotate your block even if doing so would put part of it outside of either the left or right-hand wall, it will move the block left or right in order to rotate it, unless there are 1's in the play-area ;)

Many people miss this part out, which is why their Tetris game is not quite as good as the original.

Possibly the most graphically and sonically pleasing of 2D Tetris clones was on the Amiga. I can't remember the name of it, but your blocks moved very smoothly, instead of 'jumping' like they do in my code. Any collisions resulted in a nice little 'chink' sound, made you feel the blocks were made of marble or something :)


Ross C(Posted 2005) [#17]
Ah, sorry, i thought we were describing lazy tetris ;) Not really too hard though to implement that i wouldn't say :D I wish i had the pleasure of owning an amiga :o(


VP(Posted 2005) [#18]
Ross C: These days, when I run WinUAE, it emulates at such as speed that no real Amiga would ever come close to it. Even a fully loaded A1200 with 75MHz 68060 and SCSI HD would be a number of times slower than WinUAE on my machine.

It's really quite a shame. I loved my Amiga.

I still can't get over the fact that I had a 50MHz CPU with 16MiB RAM and thought it was pretty good. Now I have a 2200MHz CPU and 1024MiB RAM and it's just about OK for what I want it to do.

**EDIT**

And another thing, Gameboy Advances are more powerful than an Amiga. Bah, progress.


KristoDJ(Posted 2008) [#19]
Vinylpusher:
"Possibly the most graphically and sonically pleasing of 2D Tetris clones was on the Amiga. I can't remember the name of it, but your blocks moved very smoothly, instead of 'jumping' like they do in my code. Any collisions resulted in a nice little 'chink' sound, made you feel the blocks were made of marble or something :)"

Yes! That was TWINTRIS, by SVEIN BERGE, the most beautiful tetris I've ever played!
I'm reading your posts because I want to create a Twintris clone in BMax, let's see what will I be able to do! ;)


pc_tek(Posted 2008) [#20]
Twintris...ahh yes.

I already am writing one and is nearly completed.

http://www.retroremakes.com/forum2/showthread.php?t=11033


Blitzplotter(Posted 2008) [#21]
Twatris....LMAO....(;-). Certainly an intersting coding project. The hours I spent on tetris on my LCD gameboy!


KristoDJ(Posted 2008) [#22]
WOW! I'm writing one too, but I have some troubles handling the smooth-moving blocks, how did you do that?


pc_tek(Posted 2008) [#23]
It's not very straight forward...and quite frankly, I'm unsure on how to explain it in this little box. I may need to write a lesson or two on the subject.


Ross C(Posted 2008) [#24]
Does it scroll smoothly sideways too? If so, what do you do when it lands on a block misaligned? Just curious :o)


pc_tek(Posted 2008) [#25]
Technically, the block doesn't land until it's finished moving sideways.


Terry B.(Posted 2008) [#26]
Tetris.... Hmm...

*walks off*

*comes back with a chair and smashes his computer*

I don't like tetris...


KristoDJ(Posted 2008) [#27]
YES! Why don't you write a lesson? The world MUST know! ;o)
Anyway, I made the smooth sideway scrolling, but the downwards scroll is still very buggy...
you can try my work if you want:

www.kristodj.eu/bmax/download/quattris.rar

I've implemented nothing yet but the very basics of tetris, so the program is very simple just fill as many rows as you can, then, "GAMEOVER" ;)

commands are arrow keys: up to rotate, left - right and down to (guess what...), and space to drop.


pc_tek(Posted 2008) [#28]
Quote: 'YES! Why don't you write a lesson? The world MUST know! ;o)'

Have I upset you? I simply offered to allow an insight into the way I coded my version. Maybe 'lesson' is the wrong word to use and I apologise for it's use in this context.


KristoDJ(Posted 2008) [#29]
NONO! I'm sorry, I didn' want to seem offensive... I was absolutely friendly, and seriously hope you want to share your informations with us... Sorry if I seemed to be in some way aggressive... :(


pc_tek(Posted 2008) [#30]
Hi Kris, sorry for not getting in touch sooner...been a little busy bee here.

My game is finished now and available for download. I'll stick a link on these boards soon.


KristoDJ(Posted 2008) [#31]
Don't worry! ;)
Hope you're fine now, and can't wait to try your game! :)