![]() |
|
ScreenHack 2008 Tetris |
kenmasters1976
Member #8,794
July 2007
|
What's this about?A few days ago I found the long version of my old ScreenHack 2008 entry and thought that I could upload it if I worked a bit on the code style and added some comments. So I went and changed that mess into something a little more readable. Hopefully I didn't break anything in the process. I made a few changes to the code for readability but I didn't rewrite it all. The logic and the basic way it works remains the same as in the ScreenHack 2008 entry. So, what's new?After doing some changes to the code and compressing it, it was about 23 lines long so I thought I might add some more features. First, I thought about adding sound. One option was to add some sound effect for complete lines but I didn't like the idea. Another option would be to load and play some background tune (MIDI or WAV) but, while I liked this idea, I had to upload a tetris tune and I wasn't sure if that was OK as I wanted to keep it in the original ScreenHack spirit. So, with sound discarded there was two options left:
Controls
Left/Right to move the piece. Screenshots
{"name":"604145","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/3\/d3b81075c75cacd2078f95355ec37ba3.png","w":263,"h":432,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/3\/d3b81075c75cacd2078f95355ec37ba3"} In the long version, I included some different functions for drawing the blocks that may result in versions of the code longer than the ScreenHack limit. I can't help but to think that pieces need more variety in colors, though. ScreenHack codeThis is the ScreenHack code for the version in the first image: 1#include <allegro.h>
2#define f(a,b) for (a = 0; a < b; a++)
3int c[25],l[25][16],P[]={0xF0,0x2222,0xF00,0x4444,0x660,0x660,0x660,0x660,0xC60
4,0x4C8,0xC60,0x4C8,0x360,0x462,0x360,0x462,0x470,0x226,0x71,0x322,0x170,0x622,
50x74,0x223,0x270,0x262,0x72,0x232},_,a,h,b,j,p,q,A,M,z,L,J,r,x,t,d,s,n,e,g,u,D,
6E,m[4],k=16,y=13,S=4;BITMAP*i;_a(){n=0;f(_,25){if(c[_]==0xFFFF){H(screen,k*2,k*
7_,k*12,k*_+y,-1);n++;f(a,_){c[_-a]=c[_-a-1];memcpy(l[_-a],l[_-a-1],sizeof(int)*
816);}}}if(n)rest(100);D=s/10+1;E=E<2?2:48-D*2;t+=pow(n,2)*1024*D;s+=n;}H(T,U,v,
9G,V,W){rectfill(T,U,v,G,V,W);}R(b,a,v){int K=240-a*k,w=b*k,N=K+y,O=w+y;H(i,K,w,
10N,O,v);H(i,K+S,w+S,N,O,v<<1);}o(Y,X){f(_,4)m[_]=((P[Y]>>_*4)&0xF)<<X;}Q(b){d=1;
11f(_,4)d=d&&!(c[b+_]&m[_]);return d;}B(){f(_,4){c[b+_]^=m[_];f(a,16)if((m[_]&1<<
12a))l[b+_][a]=0xFFF4D4*(j/4+1);}}I(h,b,Z,G){textprintf_ex(i,font,h,b,-1,-1,Z,G);
13}main(){allegro_init();set_color_depth(24);set_gfx_mode(2,256,400,0,0);
14install_keyboard();i=create_bitmap(256,400);f(_,25){f(a,16)l[_][a]=0xFFA0A0;c[_
15]=0xC00F;}c[24]=-1;srand(time(NULL));q=rand()%28;p=s=t=u=0;while(!key[59]){
16while(retrace_count>0&&!u){if(!p){j=q;q=rand()%28;p=1;h=7;b=0;o(j,h);B();}e=h;g
17=j;M=E;z=32;_=key[83];if(_||key[82]){if(J++>r){J=0;e+=_?-1:1;r=r/2+1;}}else{r=
1832;J=31;}_=key[1];a=key[19];if(a||_){if(!x){if(_&&g%4==0)g+=4;g+=_?-1:1;if(a&&g
19%4==0)g-=4;x=1;}}else x=0;if(key[85]){M=z=1;t++;}B();o(g,e);f(_,4){d=c[b+_]&m[_
20];if(d==0xC000){e-=2;break;}if(d==0x4000){e--;break;}if(d==0xC){e+=2;break;}if(
21d==0x8){e++;break;}}o(g,e);if(Q(b)){h=e;j=g;}o(j,h);if(Q(b+1)){A=0;if(++L>M){b
22++;L=0;}}else{A++;L=0;}if(A>z){p=0;if(b==0)u=1;}_a();B();retrace_count--;}
23clear_to_color(i,0x5F);f(_,25){f(a,16)if(c[_]&1<<a)R(_,a,l[_][a]);}f(_,16)if(P[
24q]&1<<_)R(_/4+2,_%4,0x5F);I(32,0,"Score:%14d",t);I(32,10,"Lines:%14d",s);if(u)I
25(82,200,"GAME OVER",0);blit(i,screen,0,0,0,0,256,400);rest(15);}}END_OF_MAIN()
Finally
|
Yodhe23
Member #8,726
June 2007
|
Not sure if I "like" it, but I am hands-down impressed. I wish I was as a competant coder as yourself, and then I wouldn't of been un(der)employed my entire adult life (which is half my total life, for those wondering how old I am). Anyway, WOW. And now I am going to stare at those 25 lines of poetry (for me they are) and see if I can gleen any insights. www.justanotherturn.com |
kenmasters1976
Member #8,794
July 2007
|
Thanks, Yodhe23. But, to be honest, I don't consider myself a good coder. I'm sure my Tetris implementation is by no means optimal and yet you can do it in less than 25 lines of code, leaving you plenty of space to add some more things, which is just what I did. I just thought I could share the long version of the code as it is
|
Neil Roy
Member #2,229
April 2002
![]() |
Reminds me of when we used to program on the Commodore 64 where every space was wasted memory and so coding like this was common. Very impressive though, nice work. --- |
kenmasters1976
Member #8,794
July 2007
|
Thanks. I realized that there was a very obvious way to avoid wasting so much space in the wall kicking code so I fixed that and used the extra space to add proper "official" colors instead of keep trying to use a formula to get a nice set of colors. The result looks a lot better, in my opinion. Attached is the updated zip with both versions of the code and the Allegro 4.4 binary for Windows. Screenshot and ScreenHack code follows. {"name":"604155","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/f\/9f8308028c87c9ab096b15f443f8ac01.png","w":262,"h":434,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/f\/9f8308028c87c9ab096b15f443f8ac01"} 1#include <allegro.h>
2#define f(a,b) for (a = 0; a < b; a++)
3int g[25],o[25][16],M[]={0xF0,0x2222,0xF00,0x4444,0x660,0x660,0x660,0x660,0xC60
4,0x4C8,0xC60,0x4C8,0x360,0x462,0x360,0x462,0x470,0x226,0x71,0x322,0x170,0x622,
50x74,0x223,0x270,0x262,0x72,0x232},S[]={0xDFFF,0xF0F0A0,0xFF8080,0xDF00,0xA0FF,
60xFFD500,0xEFA0FF},_,a,h,b,j,w,u,C,J,B,K,D,s,x,q,c,p,n,d,e,r,E,G,k,l,A,H,m[4],t
7=16,y=13,Q=4;BITMAP*i;X(){n=0;f(_,25){if(g[_]==0xFFFF){k=32;l=t*_;A=192;H=t*_+y
8;I(screen,-1);n++;f(a,_){g[_-a]=g[_-a-1];memcpy(o[_-a],o[_-a-1],sizeof(int)*16)
9;}}}if(n)rest(100);E=p/10+1;G=G<2?2:48-E*2;q+=pow(n,2)*1024*E;p+=n;}I(Y,R){
10rectfill(Y,k,l,A,H,R);}O(b,a,P){k=240-a*t;l=b*t;A=k+y;H=l+y;I(i,P);k+=Q;l+=Q;I(
11i,P<<1);}v(V,U){f(_,4)m[_]=((M[V]>>_*4)&0xF)<<U;}N(b){c=1;f(_,4)c=c&&!(g[b+_]&m
12[_]);return c;}z(){f(_,4){g[b+_]^=m[_];f(a,16)if((m[_]&1<<a))o[b+_][a]=S[j/4];}
13}L(h,b,W,T){textprintf_ex(i,font,h,b,-1,-1,W,T);}main(){f(_,25){f(a,16)o[_][a]=
140xF0A0A0;g[_]=0xC00F;}g[24]=-1;allegro_init();set_color_depth(24);set_gfx_mode(
152,256,400,0,0);install_keyboard();i=create_bitmap(256,400);srand(time(NULL));u=
16rand()%28;w=p=q=r=0;while(!key[59]){while(retrace_count>0&&!r){if(!w){j=u;u=
17rand()%28;w=1;h=7;b=0;v(j,h);z();}d=h;e=j;J=G;B=32;_=key[83];if(_||key[82]){if(
18D++>s){D=0;d+=_?-1:1;s=s/2+1;}}else{s=32;D=31;}_=key[1];a=key[19];if(a||_){if(!
19x){if(_&&e%4==0)e+=4;e+=_?-1:1;if(a&&e%4==0)e-=4;x=1;}}else x=0;if(key[85]){J=B
20=1;q++;}z();v(e,d);c=0;f(_,4)c|=g[b+_]&m[_];if(c&0x8000)d--;if(c&0x4000)d--;if(
21c&0x8)d++;if(c&0x4)d++;v(e,d);if(N(b)){h=d;j=e;}v(j,h);if(N(b+1)){C=0;if(++K>J)
22{b++;K=0;}}else{C++;K=0;}if(C>B){w=0;if(b==0)r=1;}X();z();retrace_count--;}
23clear_to_color(i,0x5F);f(_,25){f(a,16)if(g[_]&1<<a)O(_,a,o[_][a]);}f(_,16)if(M[
24u]&1<<_)O(_/4+2,_%4,0x5F);L(32,0,"Score:%14d",q);L(32,10,"Lines:%14d",p);if(r)L
25(82,200,"GAME OVER",0);blit(i,screen,0,0,0,0,256,400);rest(15);}}END_OF_MAIN()
One more thing is that I just remembered that I always get complaints that my monitor looks too dark. I tuned the colors to look fine on my monitor so now I realize that it may not look as good on other monitors. The background color, for example, looks near black on my monitor and the 3D effect of the blocks is clearly visible on all pieces. So, let me know if it doesn't look good.
|
MiquelFire
Member #3,110
January 2003
![]() |
I 'prettified' both codes and noticed that you MUST be using a program to compress your code. If not, then why is the function names for calling Allegro function multiple times have different names in the second revision? --- |
kenmasters1976
Member #8,794
July 2007
|
Yes, I'm using a tool posted by Tobias Dammers in the original ScreenHack 2008 thread. Otherwise, just the thought of having to do it by hand would be enough to kill all motivation to work on this.
|
Peter Wang
Member #23
April 2000
|
We broke screenhacks with A5
|
kenmasters1976
Member #8,794
July 2007
|
Quote: We broke screenhacks with A5 I'm afraid so.
|
Trent Gamblin
Member #261
April 2000
![]() |
Not at all, our screens are getting larger. Smallest screen anyone is likely to have is 1280x800, so:
|
|