You are on page 1of 9

Tracking pieces already on the board

If you only have 1bit graphics that pretty simple isnt it?.. Your graphics and your game
state are the same thing. When you detect a collision with already settled blocks you get
OR the falling blocks bits into your existing state..

>making sure that rotation

I wonder why hes bother to rotate (graphically) pieces in code when it would easier just
to have the pieces rotated in the program data He says hes used one method over the
other for speed..

>wont cause a collision with another piece

again, that isnt hard.. somewhere in your struct for your pieces you have something that
says how big it is, before you move a piece left/right or swap the piece out for a rotation
you check that tetro_width + offset_x or go out-of-bounds, and looking for completed
>lines all add up to one bid headache.

so you have 16 entry long uint8 array.. you do some like this to check for complete lines.

bool completelinedetector(){
for (i = 0; i < 16; i++){
if(checkarray[i] == 0xFF){
return true;
}
}
return false;
}

if you count the complete lines you can write a routine to animate out the completed lines
too..

"collision" detection is a case of comparing your incoming byte (a byte the represents the
bottom of the current tetro shifted to it's current offset) with byte below the current y
offset of the tetro in the check array learning how shifts, boolean logic work in your
language of choice is a must.

I do like hackaday.. but you guys really over hype stuff sometimes.

Posted at 1:40 pm on Jun 20th, 2010 by cantido

Excuse the typos etc. :P

Posted at 1:51 pm on Jun 20th, 2010 by cantido


@cantido
Yes, someone codes differently than you and people think thats neat therefore: HaD
must over hype things up.
/sarcasm

The guy did it his own way, what are you complaining about? At least he bothered to do
something, to reach out and learn; after all isnt that what hacking is about? To learn,
share, and grow

Posted at 2:05 pm on Jun 20th, 2010 by Comrade

@Comrade

>The guy did it his own way,..

I didnt say the guy was doing it wrong. Read this line from the hack a day text;

>>ourselves we understand how difficult


>>it is to explain how the program works.

No, it isnt. If youve got more than a few bytes of memory to play with you dont even
need to do bit shifting You can be very lazy and use your smallest native unit for each
pixel! You have an 2 dimension array that has your game state and an array + x/y bounds
descriptors for the pieces, the idea is to OR the pieces on to the game state array. You can
do bounds checking by checking that the left edge of the piece isnt out of the screen and
left edge + piece width + current offset isnt bigger than the screen when the user tries to
move it left or right. Each time the piece moves down you compare the bottom of the
piece array with what is below it in the state array, then you check the next line up,.. If
there is a collision at any point the piece stops falling (I think the official rules include
some grace time in which the player can adjust the piece). before the piece rotates you
have to check the rotation will fit in the current state.. which is a just case of iterating
over the state and piece arrays.
When the piece has stopped falling you OR it into the state array. Then you check for
complete lines.. which is (again!) just a case of iterating over the state array to find lines
that are complete.

There you go, Ive just explained this terribly complex piece of computer science for you.
Use it wisely.

The data structures and logic are all very simple. Which is why there are lots and lots of
tetris-like games out there on everything from desktop computers to hacked printer
firmwares.

Posted at 2:53 pm on Jun 20th, 2010 by cantido

cantido:
I agree. HackaDays summary makes Tetris sound impressive but it really isnt. I
commend the guy for his efforts, but I have to be honest here weve all done tetris!

I think it was about a decade ago that I threw together Javascript Tetris. It took me close
to 6 hours to create from scratch (much of that just learning the javascript) this is with
no former programming experience

Tetris isnt that hard if you can think even slightly analytically

But then again, maybe my perspective is biased? Since then Ive found everything
including mario clones, 2D rendering engines, compiler frontends, and java servers to be
challenging yet doable. I assumed HackaDay would be filled with more analytical
engineer types than creative artists, but maybe Im wrong? There are a lot of very
creative hacks and gadgets posted to this site many requiring extensive technical
expertise but that says nothing about the editors.

So maybe his rotation algorithm really isnt obvious to HaDs editors? Maybe it is really
impressive to them? :/

Posted at 3:11 pm on Jun 20th, 2010 by BikeHelmet

@Comrade

oh and here is how you do rotation,.. notice that you dont actually need to do any
complex maths;

your struct looks something like this;

000: length
001: height
002: piece data .. 000 for nothing, 0xff for a
. block or something like that.
. If you can manage shifts etc you can use
. bits for this *wowza!*, if you are using
. tilemaps i.e have more than 1 bit graphics
. you would have a tile number here and check
. for anything that isnt 000 in your
. checking routines.

008: pointer to next rotation (length depends on how big your pointers are) The last
rotation points back to the original form.

When the piece is rotated you dont need to do any rotation in ram.. you can find what
you need using the pointer and because the last piece loops back to first you dont need
any logic to wrap around.
You can use a table of pointers to the pieces and use a random offset to piece the
starting piece + rotation (I think the official rules state that all pieces start at the initial
form though.. so you would only put those into your table). On a micro you are likely to
have lots of flash rom and very little data memory so use static data over generating stuff
in ram where ever you can.

Please feel free to use this highly sensitive data under the terms of the GPL
documentation license.
Posted at 3:31 pm on Jun 20th, 2010 by cantido

Pretty nice bit of project. See? Things dontt have to be sloppy.

Posted at 4:28 pm on Jun 20th, 2010 by George Johnson

The whole project emerged from neccessity for a good example in how to manipulate
LEDs and draw 2D graphics, to help others learn.

His solution may not have been the most elegant ever, and its certainly general in nature.
But that was the point to provide a relatively easy to understand method for 2D rotation
that works on a wide scale.

He did it well. Its good that it was put on HaD, to maybe pique the interest of those new
to embedded programming and/or 2D graphics.

So, in summary: Criticizing the programming technique in an example for novices?


Really?

Posted at 7:05 pm on Jun 20th, 2010 by Roboguy

As to the referenced article, I think its commendable to try to help those out who need it.

I remember when I did Tetris (brand new to the language, to 2D graphics, and to
programming in general). It was a great learning experience, but pretty tough. I think its
great to walk people through the process and make it easier on them.

Posted at 7:09 pm on Jun 20th, 2010 by Roboguy


I cant thank and respect this dude enough for not using a frickin arduino. Neat project. I
need to get some displays so I can try graphics on PIC.

Posted at 12:34 am on Jun 21st, 2010 by M4CGYV3R

@Roboguy

So, in summary, if you actually read what people write.. you would notice that I didnt
criticise his code all that much. I criticised hack a day for saying this is hard to
describe. Which it isnt. Its very simple.

If I was going to criticise anything it might be the fact that hes used a massive micro
when he really didnt need to. But thats probably just using whats at hand..

Posted at 2:05 am on Jun 21st, 2010 by cantido


Leave a Reply

Name (required)

Mail (will not be published) (required)

Website
Notify me of follow-up comments via email.

Notify me of site updates

You might also like