It's Friday night and I don't have a party to go to...
And it seems TINS isn't happening anytime soon.
So, what to do during these most dire circumstances? A game coding competition of course! Following in the footsteps of the most excellent 2004 Allegro ScreenHack (thread), I suggest another 80x25 programming contest.
Rules (same as back in 2004)
Post some C or C++ source code that will completely fit into an 80-character by 25-character screen (the standard DOS prompt size) and that will compile on its own (along with a full complement of Allegro/system headers, that is). The best entry (I'm talking games here) will be chosen by vote sometime in the near future. You are allowed to submit as many entries as you like.
I'm going to start coding my entry now, and will be delighted if anyone decides to join in.
Good luck!
Set a deadline, and I'll try for something.
Other clarifications:
will compile on its own (along with a full complement of Allegro/system headers, that is).
Does this mean "no libraries but Allegro" or something of the sort? If so, then are standard C++ libs like vector okay?
Yep, vector will be fine. You can use all standard C and C++ stuff and allegro, but no other libraries.
Deadline is, umm, whenever people loose interest in this thread. It took about a month in the 2004 competition.
Sweet! I remember wanting to add syntax highlighting to my previous entry, but never getting around to it. Also it is 2008.
checks watch
Holy shit, you're right!
EDIT:
My entry, version 1. I'll try to add some sound tomorrow 
(Edit: Newer version below)
| 1 | #include <allegro.h>//////////////////////////////////////////////Untangle v1.0 |
| 2 | #include <math.h>////////////////////////////////////////////////By Per Larsson |
| 3 | #define p(i) x<i>,y<i>///////////////////////////////////////////////////////// |
| 4 | x[99],y[99],e[99][99],w=457;*c(a,b){return a<b?e[a]+b:e<b>+a;}_(a,b,c){return b |
| 5 | <c?a>=b&&a<=c:a>=c&&a<=b;}q(w,e,r,t,y,u,i,o){int A=t-e,B=w-r,C=A*w+B*e,D=o-u,E= |
| 6 | y-i,F=D*y+E*u,d=A*E-D*B,X=(E*C-B*F),Y=(A*F-D*C);return d?_(X/d,w,r)&&_(Y/d,e,t) |
| 7 | &&_(X/d,y,i)&&_(Y/d,u,o):0;}main(){int l,i,I,j,J,f,F,a,b,s=-1,X,Y,H,h;BITMAP*B; |
| 8 | allegro_init();set_gfx_mode(GFX_SAFE,w,w,0,0);B=create_bitmap(w,w);*c(1,2)=*c(2 |
| 9 | ,3)=1;install_keyboard();install_mouse();text_mode(-1);*c(0,1)=*c(0,2)=*c(0,3)= |
| 10 | *c(1,3)=1;for(l=4;l<99;l++){for(i=0;i<l;i++){double a=(i*6.3/l)+1;x<i>=cos(a)*w |
| 11 | /3+w/2;y<i>=sin(a)*w/3+w/2;}do{F=0;key[KEY_ESC]?exit(0):0;clear_to_color(B,15); |
| 12 | for(i=0;i<l;i++){for(j=0;j<l;j++){f=0;if(*c(j,i)){for(I=0;I<l;I++)for(J=0;J<l;J |
| 13 | ++)f|=j!=I&&i!=J&&i!=I&&j!=J&&*c(I,J)&&q(p(i),p(j),p(I),p(J));for (a=-1;a<2;a++ |
| 14 | )for (b=-1;b<2;b++)line(B,a+p(i)+b,a+p(j)+b,f?12:10);}F|=f;}}for(i=0;i<l;i++){ |
| 15 | circlefill(B,p(i),8,0);circlefill(B,p(i),6,s==i?7:8);s = mouse_b?s:-1;if(mouse\ |
| 16 | _b&&s < 0 && _(mouse_x-x<i>,-7,7) && _(mouse_y-y<i>,-7,7))s = i;}if(s>=0){x[s]= |
| 17 | mouse_x;y[s]=mouse_y;}textprintf(B,font,10,10,0,"Level %d",l-3);draw_sprite(B, |
| 18 | mouse_sprite,mouse_x,mouse_y);blit(B,screen,0,0,0,0,w,w);}while(F||mouse_b);H=0 |
| 19 | ;for(j=0;j<2000;j++){h=0;x[l]=rand()%w*2-w/2;y[l]=rand()%w*2-w/2;for(i=0;i<l; |
| 20 | i++){f=0;for(I=0;I<l;I++)for(J=0;J<l;J++)f|=l!=I&&i!=J&&i!=I&&l!=J&&*c(I,J)&&q( |
| 21 | p(i),p(l),p(I),p(J));h+=1-f;}if (h>H){H=h;X=x[l];Y=y[l];}}x[l]=X;y[l]=Y;for(i=0 |
| 22 | ;i<l;i++){f=0;for(I=0;I<l;I++)for(J=0;J<l;J++)f|=l!=I&&i!=J&&i!=I&&l!=J&&*c(I,J |
| 23 | )&&q(p(i),p(l),p(I),p(J));*c(i,l)=1-f;}}}END_OF_MAIN();/*What?? Wasted space?*/ |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
That's hilarious. Now make is scroll and you got matrix codes.
Untangle v1.0 By Per Larsson is crashing here...
2007?
[edit]
Erm, it was already mentioned.
I remember seeing it briefly at the end of this little clip, and i thought it would be small enough to squeeze into a screenhack. It's also quite fun... and frustrating. I got to level 11 yesterday
.
Cool game! Fun and addictive. Made it till level 12.
I first misunderstood the idea of this competition and wondered why this game is using real graphic instead of ASCII art on SCREEN 1 or so... OK, i got it. It's not the graphic to fit into 80x25 but the code.
Okay, version 2.0 is ready.
I've added a (crappy) sound effect and tweaked the difficulty a bit by randomly removing a few edges before each level which makes the game more interesting, and a bit easier.
This version uses DIGI_DIRECTX(0) as an audio driver, since I for some reason got choppy playback with DIGI_AUTODETECT. If you're not on windows you might want to change this. You should be able to find it even in this mess.
| 1 | #include <allegro.h>///////////////////////////////////////////////Untangle v2.0 |
| 2 | #define p(i) x<i>,y<i>////////////////////////////////////////////By Per Larsson |
| 3 | x[99],y[99],e[99][99],w=600,l;*c(a,b){return a<b?e[a]+b:e<b>+a;}_(a,b,c){return |
| 4 | b<c?a>=b&&a<=c:a>=c&&a<=b;}q(w,e,r,t,y,u,i,o){int A=t-e,B=w-r,C=A*w+B*e,D=o-u,E= |
| 5 | y-i,F=D*y+E*u,d=A*E-D*B,X=(E*C-B*F),Y=(A*F-D*C);return d?_(X/d,w,r)&&_(Y/d,e,t) |
| 6 | &&_(X/d,y,i)&&_(Y/d,u,o):0;}M(i,j){int I,J,f=0;for(I=0;I<l;I++)for(J=0;J<l;J++)f |
| 7 | +=j!=I&&i!=J&&i!=I&&j!=J&&*c(I,J)&&q(p(i),p(j),p(I),p(J));return f;}main(){int i |
| 8 | ,I,j,J,f,F,a,b,s=-1,X,Y,H,h;BITMAP*B;SAMPLE*S;allegro_init();*c(0,1)=*c(0,2)=*c( |
| 9 | 0,3)=1;set_gfx_mode(GFX_AUTODETECT_WINDOWED,w,w,0,0);install_sound(DIGI_DIRECTX( |
| 10 | 0),MIDI_NONE,0);B=create_bitmap(w,w);install_keyboard();install_mouse();*c(2,3)= |
| 11 | *c(1,2)=*c(1,3)=1;text_mode(-1);S=create_sample(8,0,9999,9999);for(i=0;i<9999;i |
| 12 | ++){((unsigned char*)S->data)<i>=(((i+(fixsin(i<<13)>>13))%100+(i)%152)*i)/9000; |
| 13 | }for(l=4;l<99;l++){play_sample(S,255,55,1600,0);play_sample(S,255,200,1604,0); |
| 14 | for(i=0;i<l;i++){x<i>=((fixcos((i<<24)/l)*w/3)>>16)+w/2;y<i>=((fixsin((i<<24)/l) |
| 15 | *w/3)>>16)+w/2;}do{F=0;key[KEY_ESC]?exit(0):0;clear_to_color(B,15);for(i=0;i<l;i |
| 16 | ++){for(j=0;j<l;j++){f=0;if(*c(j,i)){f=M(i,j);for(a=-1;a<2;a++)for(b=-1;b<2;b++) |
| 17 | line(B,a+p(i)+b,a+p(j)+b,f?12:10);}F|=f;}}for(i=0;i<l;i++){circlefill(B,p(i),8,0 |
| 18 | );circlefill(B,p(i),6,s==i?7:8);s=mouse_b?s:-1;if(mouse_b&&s<0&&_(mouse_x-x<i>,- |
| 19 | 7,7)&&_(mouse_y-y<i>,-7,7))s=i;}if(s>=0){x[s]=mouse_x;y[s]=mouse_y;}textprintf(B |
| 20 | ,font,10,10,0,"Level %d",l-3);draw_sprite(B,mouse_sprite,mouse_x,mouse_y);blit(B |
| 21 | ,screen,0,0,0,0,w,w);}while(F||mouse_b);H=0;for(j=0;j<2000;j++){h=0;x[l]=rand()% |
| 22 | w*2-w/2;y[l]=rand()%w*2-w/2;for(i=0;i<l;i++){h+=M(i,l)==0;}if(h>H){H=h;X=x[l];Y= |
| 23 | y[l];}}x[l]=X;y[l]=Y;for(i=0;i<l;i++){*c(i,l)=M(i,l)==0;*c(rand()%l,rand()%l)=0; |
| 24 | }}}END_OF_MAIN();/////////////////////////////////////////////////////////////// |
Is anyone else coding or am I the only one doing this?
Anyway, I encourage everyone to try coding up a screenful of code, it's not as hard as it looks (and it's fun!).
EDIT1: And I was able to compile (and run) it right out of the box:
gcc game.c -o game.exe
What?... not even -lalleg?...
Doesn't matter. Version 2 is working fine. I don't know what was wrong before.
I'm trying to find time to do something. I also need an idea of what to do.
[edit]In my compiler, It needs some variable type definitions.
Heck, I can't make it compile at all right now. I included alleg.h.
| 1 | #include <allegro.h>/////////////////////////////////////////////////////////// |
| 2 | BITMAP*B;int w,tt,i,ts=0;volatile int t=0;void i_t(){t++;}END_OF_FUNCTION(i_t() |
| 3 | )class C{public:double x,y,vx,vy;void L(){x+=vx;y+=vy;vy*=.95;}};class P:public |
| 4 | C{public:double r;void L(){C::L();vx*=.95;}P(){r=4;x=200;y=150;vx=vy=0;}void D( |
| 5 | ){circlefill(B,x,y,r,w);}}p;class E:public C{public:void L(){C::L();vx*=.98f;if |
| 6 | (((vx>0&&((x>p.x+p.vx&&x+vx<p.x)||(x+vx>p.x+p.vx&&x<p.x)))||(vx<0&&((x<p.x+p.vx |
| 7 | &&x+vx>p.x)||(x+vx<p.x+p.vx&&x>p.x))))&&y<p.y&&y+20>p.y){p.x=x+vx;p.vx=-p.vx+vx |
| 8 | *2;}if(x>400){y=rand()%280;x=400;vx=-(rand()%21+85)*.1f;}if(x<0){y=rand()%280;x |
| 9 | =0;vx=(rand()%21+85)*.1f;}}E(){x=-1;vy=0;}void D(){rectfill(B,x,y,x+vx,y+20,w); |
| 10 | }};void main(){E e[100];allegro_init();install_keyboard();set_color_depth(32); |
| 11 | set_gfx_mode(GFX_AUTODETECT_WINDOWED,400,300,0,0);install_timer();bool go=1; |
| 12 | LOCK_FUNCTION(i_t);LOCK_VARIABLE(t);install_int(i_t,20);B=create_bitmap(400,300 |
| 13 | );w=makecol(255,255,255);tt=0;while(!key[KEY_ESC]&&go){clear(B);p.D();for(i=0;i |
| 14 | <ts;e[i++].D());if(tt<50)textout_ex(B,font,"STAY ON SCREEN SUCKER!",20,100,w,-1 |
| 15 | );else textprintf_ex(B,font,1,1,w,-1,"points: %i",tt);blit(B,screen,0,0,0,0,400 |
| 16 | ,300);for(;tt<t;tt++){p.L();if(key[KEY_UP])p.vy-=.5f;if(key[KEY_DOWN])p.vy+=.5f |
| 17 | ;if(key[KEY_LEFT])p.vx-=.5f;if(key[KEY_RIGHT])p.vx+=.5f;for(i=0;i<ts;e[i++].L() |
| 18 | );ts=tt/100;if(p.x>400||p.x<0||p.y<0||p.y>300)go=0;}}clear(B);textprintf_ex(B, |
| 19 | font,20,50,w,-1,"game over score: %i",tt);blit(B,screen,0,0,0,0,400,300); |
| 20 | clear_keybuf();while(!key[KEY_ESC]);}END_OF_MAIN() |
OK, now I'll really start preparing for A-levels...
80x25, eh? I'll put together something next weekend. I've got exams this week.
Suggestion, put a suggested file name to use in the comment area
Well, I'm still trying to think up an idea. This always takes a while.
I am working on something, but its slow going, but I am definitely in for this.
Decided to try my hands at this.. The below code generates a random "dungeon" in which you can walk around and.. not much more.
Screenshot is attached.
| 1 | #include <allegro.h> |
| 2 | #define P(a,b) if(a>1&&b>1&&a<79&&b<24) |
| 3 | BITMAP *bf;char m[25][80];char fog[25][80];int px, py;void a(int x,int y,int |
| 4 | w,int h){int _x, _y;for(_y=y;_y<y+h;_y++)for(_x=x;_x<x+w;_x++)P(_x,_y)m[_y][ |
| 5 | _x]=1;}void b(int x,int y,int l,int d){int _x=x,_y=y;if(d==0){for(_x=x;_x<x+ |
| 6 | l;_x++)P(_x,_y)m[_y][_x]=1;}else if(d==1){for(_y=y-l;_y<y;_y++)P(_x,_y)m[_y] |
| 7 | [_x]=1;}else if(d==2){for(_x=x-l;_x<x;_x++)P(_x,_y)m[_y][_x]=1;}else if(d==3 |
| 8 | ){for(_y=y;_y<y+l;_y++)P(_x,_y)m[_y][_x]=1;}}void g(){int r=10+rand()%10;int |
| 9 | n;int x=1+rand()%79;int y=1+rand()%24;for(n=0;n<r;n++){int w=4+rand()%4;int |
| 10 | h=4+rand()%4;int d=rand()%4;int l=4+rand()%12;a(x,y,w,h);b((!d)?x+w:(d==2)?x |
| 11 | :x+rand()%w,(d==1)?y:(d==2)?y+h:y+rand()%h,l,d);int lx=x;int ly=y;x=(!d)?x+l |
| 12 | :(d==2)?x-l:x-rand()%w;y=(d==3)?y+l:(d==1)?y-l:y-rand()%h;if (x<1||x>79||y<1 |
| 13 | ||y>24){x=1+rand()%79;y=1+rand()%24;while(lx!=x){lx=(lx<x)?lx+1:lx-1;m[ly][ |
| 14 | lx]=1;}while(ly!=y){ly=(ly<y)?ly+1:ly-1;m[ly][lx]=1;}}}do{px=rand()%80;py=r\ |
| 15 | and()&25;}while(m[py][px]!=1);}void c(){clear_bitmap(bf);int x,y;for(y=0;y< |
| 16 | 25;y++)for(x=0;x<80;x++)if(fog[y][x])textout_ex(bf,font,((m[y][x]==1)?".": |
| 17 | "#"),x*8,y*8,(fog[y][x]==1)?24:18,-1);textout_ex(bf,font,"@",px*8,py*8,14,-1 |
| 18 | );blit(bf,screen,0,0,0,0,640,240);}main(){allegro_init();srand(time(NULL)); |
| 19 | memset(m,0,2000);memset(fog,0,2000);install_keyboard();set_gfx_mode(GFX_AUT\ |
| 20 | ODETECT_WINDOWED,640,240,0,0);bf=create_bitmap(640, 240);g();while(!key[KEY\ |
| 21 | _ESC]){int x,y;for(y=0;y<25;y++)for(x=0;x<80;x++)if(fog[y][x]==1)fog[y][x]=2 |
| 22 | ;for(y=py-6;y<=py+6;y++)for(x=px-6;x<=px+6;x++)if((x>=0&&x<80&&y>=0&&y<25)&& |
| 23 | (px-x)*(px-x)+(py-y)*(py-y)<=4)fog[y][x]=1;c();int tx=px,ty=py;int k=readkey |
| 24 | ()>>8;if(k==KEY_RIGHT)px++;if(k==KEY_LEFT)px--;if(k==KEY_DOWN)py++;if(k==KE\ |
| 25 | Y_UP)py--;if(m[py][px]!=1){px=tx;py=ty;}}}END_OF_MAIN() |
I decided to take my shot on this. It's my first time and I thought I could manage to get a working Tetris clone on a 80x25 code file, so I dedicated an afternoon to work on it, but turns out I was wrong.
It seems I can't get it under 34 lines without sacrificing gameplay and leaving it heavily crippled.
I'm taking a rest on it. Maybe I'll come back later and try to strip it down.
I still need a game idea before I start coding.
you don't. look at me
OK, here it is. I had never coded a Tetris before 'cause I've always believed that the world doesn't need yet another Tetris clone, but a 80x25 compo seemed like a perfect excuse.
I had to sacrifice the blinking for removing completed lines and the next piece preview which was a terrible lost, but I decided to rather sacrifice that than messing with the controls.
I don't think I'm going to try to do something like this again. It was a pain to make it fit and I don't think it can be pushed much more further.
| 1 | #include <allegro.h> // SHTetris.c |
| 2 | #define B() blit(off,screen,0,0,0,0,640,480) /////////////////////////////// |
| 3 | #define RF(xo,yo,x,y,c) rectfill(off,xo+x*16,yo+y*16,xo+x*16+16,yo+y*16+16,c) |
| 4 | int F[25];int ps[8][8]={1,1,1,241,1,1,1,241,0,32,39,52,0,48,17,23,0,16,20,55,0, |
| 5 | 48,39,33,0,16,54,35,0,16,54,35,0,32,51,22,0,32,51,22,0,2,35,114,0,1,115,33,0,0, |
| 6 | 51,51,0,0,51,51};int i,j,p,np=0,m[4],xcm[4],x,y,nx,lk,pa=FALSE,o,no=0,ct,lx,lv, |
| 7 | c=0,xc=0,oc=0,cl,ctl,ksp,kl,kr,kap,lt,rt,ltl,rtl;BITMAP *off;int uF(){for(i=23; |
| 8 | i>0;i--){if((F<i>&0xFFFF)==0xFFFF)for(j=i;j>=1;j--)F[j]=F[j-1];}} int DP(int xo |
| 9 | ,int yo){for(j=0;j<4;j++)for(i=0;i<16;i++){if(m[j]&(1<<i))RF(xo,yo,i,(y-j-1), |
| 10 | 0x0000FF);}} int DF() {for(j=0;j<25;j++)for(i=0;i<16;i++){if(F[j]&(1<<i))RF(0,0 |
| 11 | ,i,j,0xFF0000);else RF(0,0,i,j,0xFFFF00);}}int main(){srand(time(NULL)); |
| 12 | allegro_init();set_color_depth(24);set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480 |
| 13 | ,0,0);install_keyboard();off=create_bitmap(640,480);F[24]=0xFFFFFFFF;while(!key |
| 14 | [KEY_ESC]){if(!pa){p=np;np=rand()%7;o=no;no=rand()%4;ct=0;pa=TRUE;x=6;nx=6;y=0; |
| 15 | cl=10;ctl=32;}lv=(o<2)?3:7;lx=(o%2==0)?0:1;for(i=0;i<4;i++){m<i>=ps[p][lv-i];m[ |
| 16 | i]=(lx==0)?(m<i>&0xF0)>>4:m<i>&0x0F;}if(key[KEY_LEFT]){lt++;if(lt>ltl){lt=0;nx= |
| 17 | x-1;ltl/=2;if(ltl<4)ltl=4;}}if(key[KEY_RIGHT]){rt++;if (rt>rtl){rt=0;nx=x+1;rtl |
| 18 | /=2;if(rtl<4)rtl=4;}}if(key[KEY_SPACE]&&!ksp){o=(o+1)%4;ksp=1;}if(!key[ |
| 19 | KEY_SPACE])ksp=0;if(!key[KEY_LEFT]){ltl=32;lt=31;}if(!key[KEY_RIGHT]){rtl=32;rt |
| 20 | =31;}if(key[KEY_DOWN]){cl=1;ctl=1;}if(!key[KEY_DOWN]){cl=10;ctl=32;}for(i=0;i<4 |
| 21 | ;i++){xcm<i>=m<i><<nx;}if(((F[y-1]&xcm[0])==0)&&((F[y-2]&xcm[1])==0)&&((F[y-3]& |
| 22 | xcm[2])==0)&&((F[y-4]&xcm[3])==0)){x=nx;}for(i=0;i<4;i++){m<i>=m<i><<x;}if(((F[ |
| 23 | y]&m[0])==0)&&((F[y-1] & m[1])==0)&&((F[y-2]&m[2])==0)&&((F[y-3]&m[3])==0)){ct= |
| 24 | 0;c++;if(c>cl){y++;c=0;}}else{ct++;}if(ct>ctl){for(i=0;i<4;i++){F[y-i-1]=F[y-i- |
| 25 | 1]|m<i>;}pa=FALSE;ct=0;}DF();DP(0,0);uF();B();rest(15);}}END_OF_MAIN() |
KEYS: LEFT, RIGHT, DOWN, SPACE
http://www.allegro.cc/files/attachment/595142
---------------------------------------
PS. The game has some flaws, I know it.
[EDIT]: Screenshot attached.
It seems I can't get it under 34 lines without sacrificing gameplay and leaving it heavily crippled.
Didn't ML put together a working tetris game for the 20 lines of code contest? IIRC, that contest only allowed 20 lines with one semi colon per line. 
Edit: I think someone even did Pac Man!
Well, it was hard enough for me to make this 80x25 code. Anything under that is obviously out of my league.
The 20 line competition was different because it went by semicolons. Some people cheated by using commas as semicolons, but I avoided that.
http://www.matthewleverton.com/misc/20lines.html
I think 80x25 is a harder challenge simply because there is a finite limit to the number of characters you can use. (Compare that to my line 7, which is quite long.)
And here is my and Ted's entry for the competition. For you who haven't played Cursor*10 the game probably needs some instructions. The goal of the game is to get as high score as possible.
Blue boxes give score points when clicked and unlocks the door to the next level
Red boxes are closed doors
Green boxes are open doors
Yellow boxes are extra lives
Red circles open doors when pushed
Black boxes are openings to the previous level, however you can't use them to go back.
At most levels you need to click more than one blue box and/or push more than one red circle in order to unlock the door to the next level. It may seem tough at first, but you'll get help as the game progresses.
Oh, and one more thing, when the time is up you loose a life and have to restart from the first level.
| 1 | #include <allegro.h>/////////////////Cursor*n v1.2 By Olof Naessén and Ted Steen |
| 2 | #define F(i,j)for(i=0;i<j;i++)////////////////////////////////////////////////// |
| 3 | #define R(x,y,c)rectfill(B,x*f,y*f,x*f+f,y*f+f,c);////////////////////////////// |
| 4 | #define P(x,y,t,v)textprintf(B,font,x,y,0,t,v);///////////////////////////////// |
| 5 | int*B,A[99][99999][5],C[99],D[49][10][10][2],E[49][10][10],g=1,o,d,a,r,k,b,i,t,s |
| 6 | =400,l=3,w,q=3000,*u,*e,h=1,f=40,p=10,j=49;void Q(int z,int x,int y,int t,int s) |
| 7 | {u=D[z][x][y];if(*u!=0){*u=t;u[1]=s;}}void S(){srand(6);F(i,g)C<i>=1;F(i,j)F(k,p |
| 8 | )F(r,p)D<i>[r][k][0]=-1;F(i,j){r=rand()%p;k=(rand()%9)+1;Q(i,r,k,0,0);*D[i+1][r] |
| 9 | [k]=3;F(a,rand()%(i+1)+i/2)Q(i,rand()%p,(rand()%9)+1,1,rand()%((i/2)+2)+1);F(a, |
| 10 | rand()%2)if(i>5)Q(i,rand()%p,(rand()%9)+1,4,0);}}void v(int c){F(i,j){o=1;F(k,p) |
| 11 | {F(r,p){u=D<i>[r][k];if(*u==0){a=r;b=k;if(i==c)if(u[1]==0)R(r,k,4)else R(r,k,p)} |
| 12 | if(*u==1)if(u[1]!=0){o=0;if(i==c){R(r,k,3)P(r*f+18,k*f+18,"%d",u[1])}}if(*u==3&c |
| 13 | ==i)R(r,k,0)if(*u==4){if(u[1]==0){o=0;w=4;}else w=p;if(i==c)circlefill(B,r*f+20, |
| 14 | k*f+20,20,w);}if(i==c&&E<i>[r][k]==1)R(r,k,14)}}D<i>[a]<b>[1]=o;}}void main(){S( |
| 15 | );F(i,j)E<i>[rand()%p][rand()%9+1]=1;allegro_init();text_mode(-1);install_mouse( |
| 16 | );install_keyboard();set_gfx_mode(2,s,s,0,0);B=create_bitmap(s,s);A[g-1][d][3]=0 |
| 17 | ;while(!key[59]){if(d>q){l--;g++;d=0;S();if(l==0){P(155,200,"Game Over",0)P(100, |
| 18 | 209,"Highest level reached %d",h)}else rest(q);}if(l!=0){clear_to_color(B,15);P( |
| 19 | 9,19,"Score %d",t)P(9,29,"Lives %d",l)P(150,9,"Time %d",q-d)e=A[g-1][d];v(C[g-1] |
| 20 | );*e=mouse_x;e[1]=mouse_y;e[4]=0;if(mouse_b)e[3]=1;if(!mouse_b&&A[g-1][d-1][3]){ |
| 21 | e[3]=0;e[4]=1;}F(i,j)F(r,p)F(k,p){u=D<i>[r][k];if(*u==4)u[1]=0;}F(i,g){u=A<i>[d] |
| 22 | ;r=*u/f;k=u[1]/f;o=C<i>;if(C<i>==C[g-1])draw_sprite(B,mouse_sprite,*u,u[1]);if(i |
| 23 | ==g-1)circlefill(B,*u,u[1],4,4);e=D[o][r][k];if(*e==4&&u[3])e[1]=1;if(u[4]){if(* |
| 24 | e==0&&e[1]==1){C<i>++;h=MAX(h,C<i>);}if(*e==1&&e[1]!=0){e[1]--;t++;}u=&E[o][r][k |
| 25 | ];if(*u==1){*u=0;l++;}}}d++;}blit(B,screen,0,0,0,0,s,s);rest(9);}}END_OF_MAIN(); |
Edit: We managed to squeeze in a small pause between rounds.
Edit: Yet another update. We now have a Game Over message where you can see the highest level reached.
Edit: Fixed a MinGW issue, the code should now compile with MinGW.
@kenmasters1976: Dude, that has maybe the most awesome bug/misfeature I have ever seen in a Tetris game! Don't like the piece you got, just toss it off to a side.
I am currently enjoying some sun in Croatia.
I already made something before I left, I'll post it when I come back in Saturday.
One question, must all of the 80x25 space be filled with code or can there be gaps in between? I'm just wondering.
Use comments like this:
codecodeblah//comment!
or
codecodeblah/////////
to pad your lines. Ideally, the finished program's code will all be in a rectangle, 80 characters wide by 25 lines high. Or, if you wish, less than 25 lines high.
@kenmasters1976: Dude, that has maybe the most awesome bug/misfeature I have ever seen in a Tetris game! Don't like the piece you got, just toss it off to a side.
I know... I ran out of space for coding.
I may try to fix that later... maybe.
I've only managed to compile the tetris game, the rest give me errors.
Now with deallocation of the buffer bitmap.
This should compile. As a cpp file btw.
Hmm... 5 lines left. Maybe I'll add a highscore list.
| 1 | #include <allegro.h>/////cpp file///////////////////////////by Seppl/////////// |
| 2 | BITMAP*B;int w,tt,i,ts=0;volatile int t=0;void i_t(){t++;}END_OF_FUNCTION(i_t() |
| 3 | )class C{public:double x,y,vx,vy;void L(){x+=vx;y+=vy;vy*=.95;}};class P:public |
| 4 | C{public:double r;void L(){C::L();vx*=.95;}P(){r=4;x=200;y=150;vx=vy=0;}void D( |
| 5 | ){circlefill(B,x,y,r,w);}}p;class E:public C{public:void L(){C::L();vx*=.98f;if |
| 6 | (((vx>0&&((x>p.x+p.vx&&x+vx<p.x)||(x+vx>p.x+p.vx&&x<p.x)))||(vx<0&&((x<p.x+p.vx |
| 7 | &&x+vx>p.x)||(x+vx<p.x+p.vx&&x>p.x))))&&y<p.y&&y+20>p.y){p.x=x+vx;p.vx=-p.vx+vx |
| 8 | *2;}if(x>400){y=rand()%280;x=400;vx=-(rand()%21+85)*.1f;}if(x<0){y=rand()%280;x |
| 9 | =0;vx=(rand()%21+85)*.1f;}}E(){x=-1;vy=0;}void D(){rectfill(B,x,y,x+vx,y+20,w); |
| 10 | }};void main(){E e[100];allegro_init();install_keyboard();set_color_depth(32)// |
| 11 | ;set_gfx_mode(GFX_AUTODETECT_WINDOWED,400,300,0,0);install_timer();bool go=1;// |
| 12 | LOCK_FUNCTION(i_t);LOCK_VARIABLE(t);install_int(i_t,20);B=create_bitmap(400,300 |
| 13 | );w=makecol(255,255,255);tt=0;while(!key[KEY_ESC]&&go){clear(B);p.D();for(i=0;i |
| 14 | <ts;e[i++].D());if(tt<50)textout_ex(B,font,"STAY ON SCREEN SUCKER!",20,100,w,-1 |
| 15 | );else textprintf_ex(B,font,1,1,w,-1,"points: %i",tt);blit(B,screen,0,0,0,0,400 |
| 16 | ,300);for(;tt<t;tt++){p.L();if(key[KEY_UP])p.vy-=.5f;if(key[KEY_DOWN])p.vy+=.5f |
| 17 | ;if(key[KEY_LEFT])p.vx-=.5f;if(key[KEY_RIGHT])p.vx+=.5f;for(i=0;i<ts;e[i++].L() |
| 18 | );ts=tt/100;if(p.x>400||p.x<0||p.y<0||p.y>300)go=0;}}clear(B);textprintf_ex(B// |
| 19 | ,font,20,50,w,-1,"game over score: %i",tt);blit(B,screen,0,0,0,0,400,300) |
| 20 | ;clear_keybuf();while(!key[KEY_ESC]);destroy_bitmap(B);}END_OF_MAIN()////////// |
EDIT:
Every entry here compiles fine for me.
We didn't think we would be able to squeeze more stuff into our game, but we did! We added some eye candy in the form of a shake effect when clicking boxes.
Also, I've attached a pre-compiled binary. See my previous post for game instructions.
| 1 | #include <allegro.h>/////////////////Cursor*n v1.3 By Olof Naessén and Ted Steen |
| 2 | #define F(i,j)for(i=0;i<j;i++)////////////////////////////////////////////////// |
| 3 | int*B,A[99][99999][5],C[99],D[99][10][10][3],E[99][10][10],g=1,o,d,a,r,k,b,i,t,s |
| 4 | =400,l=3,w,q=3000,*u,*e,*z,h=1,f=40,p=10,j=99,X=0,Y=0;*Q(z,x,y,t,s){u=D[z][x][y] |
| 5 | ;if(*u!=0){*u=t;u[1]=s;u[2]=0;}}int L(int v){return rand()%v;}*R(x,y,v){rectfill |
| 6 | (B,x*f+X,y*f+Y,x*f+f+Y,y*f+f+Y,v);}*P(x,y,v){textprintf(B,font,x+X,y+Y,0,z,v);}* |
| 7 | v(int c){F(i,j){o=1;F(k,p){F(r,p){u=D<i>[r][k];if(*u==0){a=r;b=k;if(i==c)if(u[1] |
| 8 | ==0)R(r,k,4);else R(r,k,p);}if(*u==1)if(u[1]!=0){o=0;if(i==c){if(u[2]>0){X=-u[2] |
| 9 | +L(u[2]*2);Y=-u[2]+L(u[2]*2);u[2]--;}R(r,k,3);z="%d";P(r*f+18,k*f+18,u[1]);X=Y=0 |
| 10 | ;}}if(*u==4){if(u[1]==0){o=0;w=4;}else w=p;if(i==c)circlefill(B,r*f+20,k*f+20,20 |
| 11 | ,w);}if(*u==3&c==i)R(r,k,0);if(i==c&&E<i>[r][k]==1)R(r,k,14);}}D<i>[a]<b>[1]=o;} |
| 12 | }*S(){srand(3);F(i,j)F(k,p)F(r,p)*D<i>[r][k]=-1;F(i,j){C<i>=1;r=L(p);k=L(9)+1;Q( |
| 13 | i,r,k,0,0);*D[i+1][r][k]=3;F(a,L(i+1)+i/2)Q(i,L(p),L(9)+1,1,L((i/2)+2)+1);F(a,L( |
| 14 | 2))if(i>5)Q(i,L(p),L(9)+1,4,0);}}*main(){allegro_init();S();F(i,j)E<i>[L(p)][L(9 |
| 15 | )+1]=1;text_mode(-1);install_mouse();install_keyboard();set_gfx_mode(2,s,s,0,0); |
| 16 | B=create_bitmap(s,s);A[g-1][d][3]=0;while(!key[59]){if(d>q){l--;g++;d=0;S();if(l |
| 17 | ==0){z="Game Over";P(155,200,0);z="Highest level reached %d";P(100,209,h);}else |
| 18 | rest(q);}if(l!=0){clear_to_color(B,15);z="Time %d";P(150,9,q-d);z="Score %d";P(9 |
| 19 | ,19,t);z="Lives %d";P(9,29,l);e=A[g-1][d];v(C[g-1]);*e=mouse_x;e[1]=mouse_y;e[4] |
| 20 | =0;if(mouse_b)e[3]=1;if(!mouse_b&&A[g-1][d-1][3]){e[3]=0;e[4]=1;}F(i,j)F(r,p)F(k |
| 21 | ,p){u=D<i>[r][k];if(*u==4)u[1]=0;}F(i,g){u=A<i>[d];r=*u/f;k=u[1]/f;o=C<i>;if(C[i |
| 22 | ]==C[g-1])draw_sprite(B,mouse_sprite,*u,u[1]);if(i==g-1)circlefill(B,*u,u[1],4,4 |
| 23 | );e=D[o][r][k];if(*e==4&&u[3])e[1]=1;if(u[4]){if(*e==0&&e[1]==1){C<i>++;h=MAX(h, |
| 24 | C<i>);}if(*e==1&&e[1]!=0){e[1]--;e[2]+=4;t++;}u=&E[o][r][k];if(*u==1){*u=0;l++;} |
| 25 | }}d++;}blit(B,screen,0,0,0,0,s,s);rest(9);}}END_OF_MAIN()/////tinyurl.com/6l3ay7 |
I've only managed to compile the tetris game, the rest give me errors.
What was your error with our entry? The game has to be compiled with C, not C++.
Wow, I actually finished something and it was fun too!
Move left and right with arrowkeys, use X to jump and double-jump in midair and Z to boost yourself even higher up in the air. You get the boosts when collecting the coins. You win when all coins are collected and you lose if you fall off the screen. Should compile in both C and C++. Oh and press R to generate a new level.
| 1 | #include <allegro.h>///////////////////////////////////BoxJump v2.0 by arvidsson |
| 2 | #define P(x,y,t,a) textprintf_ex(B,font,x,y,makecol(255,255,255),-1,t,a);/////// |
| 3 | #define Q(m,n) ((rand()%((m+1)-n)+n))/////////////////////////////////////////// |
| 4 | int w=640,t,x,y,b[20][2],c[5][3],n,i,j,k,m,q,o,p,z;float g; int C(int x1,int y1, |
| 5 | int w1,int h1,int x2,int y2,int w2,int h2){if((x1>x2+w2)||(y1>y2+h2)||(x2>x1+w1) |
| 6 | ||(y2>y1+h1))return 0;return 1;};void G(){n=j=g=z=0;x=Q(580,20);y=Q(40,0);for(i= |
| 7 | 0;i<20;i++){V:b<i>[0]=Q(580,20);b<i>[1]=Q(580,20);k=0;do{if(k!=i)if(C(b<i>[0],b[ |
| 8 | i][1],19,19,b[k][0],b[k][1],19,19))goto V;k++;}while(k<i);}for(i=0;i<5;i++){J:c[ |
| 9 | i][0]=Q(580,20);c<i>[1]=Q(580,20);c<i>[2]=1;for(k=0;k<20;k++){if(C(b[k][0],b[k][ |
| 10 | 1],19,19,c<i>[0]-10,c<i>[1]-10,10,10))goto J;}}}void T(){t++;}void main(){srand( |
| 11 | time(0));allegro_init();install_keyboard();set_gfx_mode(2,w,w,0,0);install_int(T |
| 12 | ,20); BITMAP* B=create_bitmap(w,w); set_keyboard_rate(0,0);G(); while(!key[59]){ |
| 13 | while(t>0){t--;if(keypressed()){i=readkey();if((i&0xff)=='x'&&j){g=-2;j--;} else |
| 14 | if((i&0xff)=='z'&&z){g=-5;z--;}}if(key[82])x-=2;if(key[83])x+=2;if(g>6)g=6;for(i |
| 15 | =0;i<20;i++){if(C(x,y,9,9,b<i>[0],b<i>[1],19,19)){m=MAX(x,b<i>[0]);q=MAX(y,b<i>[ |
| 16 | 1]);o=MIN(x+9,b<i>[0]+19);p=MIN(y+9,b<i>[1]+19);if(o-m>p-q){if(q==b<i>[1]){y=b[i |
| 17 | ][1]-10;j=2;g=0;}else{y=b<i>[1]+20;}}else{if(m==b<i>[0]){x=b<i>[0]-10;}else{x=b[ |
| 18 | i][0]+20;}}}}for(i=0;i<5;i++)if(C(x,y,9,9,c<i>[0]-5,c<i>[1]-5,9,9)&&c<i>[2]){c[i |
| 19 | ][2]=0;n++;z++;}if(x<0||x>640||y>640){P(300,300,"FAIL!",0);blit(B,screen,0,0,0,0 |
| 20 | ,w,w);readkey();G();}if(n>=5){P(275,300,"YOU ARE WIN!",0);blit(B,screen,0,0,0,0, |
| 21 | w,w);readkey(); G();}g+=0.1;y+=g;if(key[18])G();}rect(B,x,y,x+9,y+9,makecol(255, |
| 22 | 255,255));P(10,10,"Boost: %d",z);for(i=0;i<5;i++)if(c<i>[2])circlefill(B,c<i>[0] |
| 23 | ,c<i>[1],5,makecol(255,255,0)); for(i=0;i<20;i++)rect(B,b<i>[0],b<i>[1],b<i>[0]+ |
| 24 | 19, b<i>[1]+19, makecol(0,0,255)); blit(B,screen,0,0,0,0,w,w); clear_bitmap(B);} |
| 25 | destroy_bitmap(B);}END_OF_MAIN()//////////////////////////////////////////////// |
{"name":"595181","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/7\/e703b0e31767c4718ab280baa62362f5.png","w":323,"h":336,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/7\/e703b0e31767c4718ab280baa62362f5"}
edit: Oops, seems my entry is 79x25. Scite fooled me.
edit2: Updated my entry. It is now in 80x25 format and destroys the buffer like a good little hack. Didn't add anything new gameplay wise, just optimized the use of space a little. Added screenshot too!
It may seem tough at first, but you'll get help as the game progresses.
Is the help those other cursors that appear? At first I thought I had lagged the game really bad or something because the second cursor appeared to do exactly what I had done on the first level.
I need an idea for a game to write.
Is the help those other cursors that appear? At first I thought I had lagged the game really bad or something because the second cursor appeared to do exactly what I had done on the first level.
Yes, that's your mouse from the previous round helping you. You need to do some clever cooperation with yourself to get the high scores!
The current world My record is 1510 points, I reached level 11. (see attachment)
Edit:
Now my best score is 2749. I didn't beat Jeff, but I beat Olof 
http://www.allegro.cc/files/attachment/595182
The current world record is 1510 points, I reached level 11. (see attachment)
I beat your record Ted, 2720 points and I reached level 14!
I beat your record Ted, 2720 points and I reached level 14!
pwned: 3139, level 15.
http://www.allegro.cc/files/attachment/595179
It may just be a quick hack, but it's a really fun game.;D
Updated mine too. Tweaked the drawing code, reduced almost all variable names to single letter ones and replaced all Allegro constants with numbers to save space and have room to improve it.
I made the playfield 10 blocks wide as usual in Tetris games - this also fixed the problem that MiquelFire mentioned (and likely created new ones). Also added the features I couldn't include in the previous one and reduced window size to fit just the game area.
| 1 | #include <allegro.h> // SHTetris.c |
| 2 | #define f() for(i=0;i<4;i++) ///////////// |
| 3 | #define B() blit(O,screen,0,0,0,0,640,480) |
| 4 | #define RF(c) rectfill(O,xo+j*16,yo+y*16,xo+j*16+16,yo+y*16+16,c) |
| 5 | int F[25];int ps[8][16]={0,0,0,15,1,1,1,1,0,0,0,15,1,1,1,1,0,2,2,3,0,0,7,4,0,3, |
| 6 | 1,1,0,0,1,7,0,1,1,3,0,0,4,7,0,3,2,2,0,0,7,1,0,1,3,2,0,0,6,3,0,1,3,2,0,0,6,3,0,2 |
| 7 | ,3,1,0,0,3,6,0,2,3,1,0,0,3,6,0,0,7,2,0,1,3,1,0,0,2,7,0,2,3,2,0,0,3,3,0,0,3,3,0, |
| 8 | 0,3,3,0,0,3,3};int i,j,p,q=0,m[4],x,y,w,a=0,o,no=0,d,e,g,k,lt,rt,ltl,rtl, cf,h, |
| 9 | cm,rp,cr;BITMAP *O;int UF(){for(i=23;i>=0;i--)if(F<i>==0xFFFF){DL(0,0,i,0xFFFF, |
| 10 | 0xFFFFFF,-1);B();rest(50);DL(0,0,i,0xFFFF,0x44A3FF,-1);B();rest(50);}for(i=23;i |
| 11 | >0;i--){if(F<i>==0xFFFF)for(j=i;j>=1;j--)F[j]=F[j-1];}}int DL(int xo,int yo,int |
| 12 | y,int d,int c,int sc){for(j=0;j<16;j++)if(d&1<<j)RF(c);else if(sc)RF(sc);} int |
| 13 | main() { allegro_init(); set_color_depth(24); set_gfx_mode(2, 256, 400, 0, 0); |
| 14 | install_keyboard();O=create_bitmap(640,480);F[24]=0xFFFFFFFF;for(i=0;i<24;i++)F |
| 15 | <i>=0xE007;srand(time(NULL));while(!key[59]){if(!a){p=q;q=rand()%7;o=no;no=rand |
| 16 | ()%4;d=0;a=1;x=6;y=3;w=6;e=10;g=32;}if(h>e){y++;h=0;}if(cm)x=w;rp=0;if(key[82]) |
| 17 | {lt++;if(lt>ltl){lt=0;w=x-1;ltl/=2;if(ltl<4)ltl=4;}}if(key[83]){rt++;if(rt>rtl) |
| 18 | {rt=0;w=x+1;rtl/=2;if(rtl<4)rtl=4;}}if(key[75]&&!k){o=(o+1)%4;rp=1;k=1;}if(!key |
| 19 | [75])k=0;if(!key[82]){ltl=32;lt=31;}if(!key[83]){rtl=32;rt=31;}if(key[85]){e=1; |
| 20 | g=1;}if(!key[85]){e=10;g=32;}if(rp){f()m<i>=ps[p][(o)*4+i];cr=1;f(){cr=cr&&((F[ |
| 21 | y-i]&(m<i>)<<x)==0);}if(!cr){o=o==0?3:o-1;}}f()m<i>=ps[p][(o)*4+i];cm=1; f()cm= |
| 22 | cm&&(F[y-1-i]&m<i><<w)==0;cf=1; f()cf=cf&&(F[y-i]&m<i><<x)==0; if(cf){d=0;h++;} |
| 23 | else d++;if(d>g){f()F[y-i-1]|=m<i><<x;a=0;}for(i = 0;i < 25; i++)DL(0,0,i,F<i>, |
| 24 | 0xFFA6A6,0xFFFFA9);f()DL(0,0,y-1-i,m<i><<x,0x44A3FF,0);UF();f(){m<i>=ps[q][no*4 |
| 25 | +i];DL(208,100,5-i,m<i>,0x44A3FF,0xFFA6A6);}B();rest(15);}}END_OF_MAIN() |
{"name":"595180","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/7\/57c86258f645a0ce3fed379913e2afa0.gif","w":261,"h":431,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/7\/57c86258f645a0ce3fed379913e2afa0"}
kenmasters1976, your tetris game is really starting to shape up. The only thing I miss is a score counter.
3139, level 15
I'm impressed.
Kenmasters1976, That is one squeezed game of tetris! but you could double the fun with a score counter!
My suggestion is to replace these defines with functions instead.
#define B() blit(O,screen,0,0,0,0,640,480) #define RF(c) rectfill(O,xo+j*16,yo+y*16,xo+j*16+16,yo+y*16+16,c)
will turn into something like this
Now you can almost fit them into one line.
A few minor optimizations would be to replace
BITMAP *O
with
int*O
And replace
int DL(int xo,int yo,int y,int d,int c,int sc)
with
*DL(xo,yo,y,d,c,sc)
Now... that should be enough for a score counter
What the hell is wrong with me? Another entry, that is what's wrong with me.
ARE YOU UP TO THE TASK TO TAKE COMMAND AND DODGE FUNNY LOOKING ASTEROIDS?
Steer your spacecraft with arrowkeys. Reset with R. Quit with ESC. Every 15 seconds the difficulty level is increased which means more asteroids (or space balls if you'd like) to dodge. The maximum level is 10 which means 50 asteroidballs onscreen at ALL times. I almost made it to level 5, level 10 must be hell I suppose (only hardcore gamers need apply
). The blue box on the ship (aka the cockpit) is the hit area.
| 1 | #include <allegro.h>////////////////////////////////SpaceDodge v1.0 by arvidsson |
| 2 | int w=400,i,j,x,y,c,l,q,z,t,u,k,s[75][4],e[50][6];R(m,n){return(rand()%(m+1-n))+ |
| 3 | n;}C(x1,y1,w1,h1,x2,y2,w2,h2){if((x1>x2+w2)||(y1>y2+h2)||(x2>x1+w1)||(y2>y1+h1)) |
| 4 | return 0;return 1;}void T(){t++;u++;}N(){l=1;c=u=q=k=z=0;for(i=0;i<75;i++){s<i>[ |
| 5 | 0]=R(400,0);s<i>[1]=R(400,0);s<i>[2]=R(5,2);j=s<i>[2]*51;s<i>[3]=makecol(j,j,j); |
| 6 | }for(i=0;i<50;i++)e<i>[5]=0;x=w/2;y=350;}main(){allegro_init();set_gfx_mode(2,w, |
| 7 | w,0,0); install_keyboard();install_int(T,20);BITMAP *B=create_bitmap(w,w);srand( |
| 8 | time(0));N();while(!key[59]){while(t>0){t--;if(u>=50){k++;q++;u=0;}if(q>=15){l++ |
| 9 | ;q=0;}if(l>=10)l=10;for(i=0;i<75;i++){s<i>[1]+=s<i>[2];if(s<i>[1]>w){s<i>[1]=0;s |
| 10 | <i>[0]=R(400,0);s<i>[2]=R(5,2);j=s<i>[2]*51;s<i>[3]=makecol(j,j,j);}}i=0;while(i |
| 11 | <50&&c<l*5){if(!e<i>[5]){c++;e<i>[5]=1;e<i>[4]=makecol(R(255,0),R(255,0),R(255,0 |
| 12 | ));int d=R(3,1);if(d==1){e<i>[0]=R(w,0);e<i>[1]=-10;e<i>[2]=R(2,0);e<i>[3]=R(5,1 |
| 13 | );}if(d==2){e<i>[0]=-10;e<i>[1]=R(300,0);e<i>[2]=R(5,1);e<i>[3]=R(2,0);}if(d==3) |
| 14 | {e<i>[0]=410;e<i>[1]=R(300,0);e<i>[2]=-R(5,1);e<i>[3]=R(2,0);}}i++;}for(i=0;i<50 |
| 15 | ;i++){if(e<i>[5]){e<i>[0]+=e<i>[2];e<i>[1]+=e<i>[3];}if(e<i>[0]<-20||e<i>[0]>420 |
| 16 | ||e<i>[1]<-20||e<i>[1]>400){e<i>[5]=0;c--;}}for(i=0;i<50;i++){if(e<i>[5])if(C(x- |
| 17 | 2,y,4,5,e<i>[0]-5,e<i>[1]-5,5,5)){e<i>[5]=0;z=1;}}if(key[82])x-=4;if(key[83])x+= |
| 18 | 4;if(key[84])y-=4;if(key[85])y+=4;if(key[18])N();if(x<10)x=10;if(x>390)x=390;if( |
| 19 | y<10)y=10;if(y>390)y=390;}while(!t)rest(1);for(i=0;i<75;i++)putpixel(B,s<i>[0],s |
| 20 | <i>[1],s<i>[3]);for(i=0;i<50;i++)if(e<i>[5])circlefill(B,e<i>[0],e<i>[1],5,e<i>[ |
| 21 | 4]);if(z)textprintf_centre_ex(B,font,w/2,w/2,15,-1,"GAME OVER!");if(!z){triangle |
| 22 | (B,x,y-10,x+10,y+10,x-10,y+10,15);rectfill(B,x-2,y,x+2,y+5,32);}textprintf_ex(B, |
| 23 | font,10,10,15,-1,"Level: %d Time: %d",l,k);blit(B,screen,0,0,0,0,w,w);//////woot |
| 24 | clear_bitmap(B);if(z){i=0;while(!i){clear_keybuf();j=readkey();if((j>>8)==59)/// |
| 25 | return;else if((j>>8)==18){N();i=1;}}}}destroy_bitmap(B);}END_OF_MAIN()///////// |
{"name":"595186","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/0\/90c88f30bfdf23fdcc43fd926665e9aa.png","w":403,"h":430,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/0\/90c88f30bfdf23fdcc43fd926665e9aa"}
This contest will be the end of me, I already have a new thingy I want to implement. Curses, programming is supposed to be boring, no? 
edit: I actually got to level 7 and discovered a cheat which is fixed now.
*B(){blit(O,screen,0,0,0,0,640,480);}
*R(c){rectfill(O,xo+j*16,yo+y*16,xo+j*16+16,yo+y*16+16,c);}
Why is everyone prefixing their K&R-style default functions with a * ? I don't see the purpose... you're returning no value (especially not a pointer to int) and wasting a character in your code.
Why is everyone prefixing their K&R-style default functions with a * ? I don't see the purpose... you're returning no value (especially not a pointer to int) and wasting a character in your code.
You are right, I just assumed that you needed to specify the return type, and * is smaller than void. C is not my "native language" 
Yay! Now we have four more characters to play with!! Olof - let's add network support!
Urg, I had an idea, but it was really boring compared to these... I'm starting over now.
Tetris
{"name":"595191","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/9\/8975a23dbdf433e7ee0863aff039dacd.png","w":304,"h":388,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/9\/8975a23dbdf433e7ee0863aff039dacd"}
| 1 | #include <allegro.h> // Tetris, by Matthew Leverton |
| 2 | int s[]={0xcc00cc00,0xcc00cc00,0x4e004640,0xe404c40,0xe2044c0,0x8e006440, |
| 3 | 0xe80c440,0x2e004460,0x6c008c40,0x6c008c40,0xc6004c80,0xc6004c80,0xf004444, |
| 4 | 0xf004444},w[24],i,q,r,y,x,R,Y,X,c,d,e=0,f=0,g;S(){return s[q*2+r/2]>>(1-r%2)*16 |
| 5 | ;}D(){if((d=(100-e)/10)<2)d=2;};p(A,B){int s=c;for(i=16;--i>=0;s>>=1){B=(s&1)<<( |
| 6 | x+i%4);if(A)w[y+i/4]|=B;else if(w[y+i/4]&B)return 1;}return 0;}z(){y=0;x=7;q= |
| 7 | rand()%7;r=0;D();c=S();};main(){allegro_init();install_keyboard();set_gfx_mode( |
| 8 | GFX_GDI,288,352,0,0);BITMAP *b=create_bitmap(640,480);srand(time(0));z();i=24; |
| 9 | while(i--)w<i>=i>19?0x3ffff:0x3c00f;while(!key[59]){if(retrace_count>3){X=x,Y=y, |
| 10 | R=r;if(!--d)y++;else{if(key[83])x++;if(key[82])x--;if(key[85]){y++;f++;}if(key[ |
| 11 | 84])r=(r+1)%4;}c=S();if(p(0)){x=X,y=Y,r=R,c=S();if(!d){p(1);i=20;g=10;while(i--) |
| 12 | {if(w<i>==0x3ffff){memmove(w+1,w,i++*4);w[0]=0x3c00f;++e;f+=g;g*=10;}}z();if(p(0 |
| 13 | )){rest(5000);break;}}}else if(!d||key[85])D();for(i=0;i<378;++i)rectfill(b,(i% |
| 14 | 18)*16,(i/18)*16,(i%18)*16+15,(i/18)*16+15,w[i/18]&1<<(i%18)?((e/10)%15)+1:0);i= |
| 15 | 16;while(i--){if(c&1)rectfill(b,(x+i%4)*16,(y+i/4)*16,(x+i%4)*16+15,(y+i/4)*16+ |
| 16 | 15,3);c>>=1;}textprintf(b,font,0,340,15," Lines: %d Score: %d",e,f);blit(b, |
| 17 | screen,0,0,0,0,288,352);retrace_count=0;}};}END_OF_MAIN() |
Controls are a bit sluggish and scoring for pressing the down key is generous (especially if you bump against the side), but it's good enough for demo #1.
Change GFX_GDI to the constant for windowed mode if you aren't on Windows. It also assumes integers are 4 bytes.
Those of you making Tetris should figure out a way to use more color
.
I'd love to try this, but I'm not sure if I could come up with anything. Perhaps I'll give it a go anyway...
At last! My entry is done. Use left and right to move, press up to flap. If you hit the boxes or leave the screen, it's game over. I may add highscore records later.
| 1 | #include <allegro.h>/////////////////////////////////////////////////////////// |
| 2 | #define W makecol(255, 255, 255)/////////////////////////////////////////////// |
| 3 | #define G makecol(255, 255, 0)///////////////////////////////////////////////// |
| 4 | BITMAP*b;int u=KEY_UP,r=KEY_RIGHT,l=KEY_LEFT,sx=4,sy=0,bx[99],by[99],nb=0,hs=0, |
| 5 | i=0,lt=0,t=0,ln=0,x=320,y=240,j=0,g_o=0;int J(){return j/8;}bool H(){for(i=0;i< |
| 6 | nb;i++)return((x>bx<i>)&&(y>by<i>)&&(x<bx<i>+16)&&(y<by<i>+16));}void T(){t++;} |
| 7 | void A(){if(nb>98)return;bx[nb]=640;by[nb]=rand()%480;nb++;}void V(){for(i=00;i |
| 8 | <nb;i++){if(bx<i><0){nb--;bx<i>=bx[nb];by<i>=by[nb];i--;}}}void main(){//////// |
| 9 | allegro_init();set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);////////////// |
| 10 | install_keyboard();install_timer();install_int_ex(T,MSEC_TO_TIMER(1));srand(/// |
| 11 | time(0));b=create_bitmap(640,480);while(!key[KEY_ESC]){lt=t;if((key<u>)&&(j<10) |
| 12 | )j=70;if(key[r])x+=5;if(key[l])x-=5;j-=3;y-=J();if(ln%8==0)A();V();if(ln%150==0 |
| 13 | ){sx=rand()%4+2;sy=rand()%3-1;}for(i=0;i<nb;i++){bx<i>-=sx;by<i>+=sy;rect(b,bx[ |
| 14 | i],by<i>,bx<i>+16,by<i>+16,W);}if(key<u>)vline(b,x,y,y+9,W);else hline(b,x-9,y, |
| 15 | x+9,W);if(j<-60)j=-60;rect(b,1,1,638,478,W);if((ln>hs)&&(!g_o))hs=ln;if((H())|| |
| 16 | (x<0)||(y<0)||(x>640)||(y>480))g_o=1;textprintf_ex(b,font,10,10,W,-1,"time: %i" |
| 17 | ,ln/62);textprintf_ex(b,font,10,20,G,-1,"best: %i", hs/62);if(g_o==1){///////// |
| 18 | textprintf_centre_ex(b,font,320,240,W,-1,"game over");}blit(b,screen,0,0,0,0,// |
| 19 | 640,480);clear_to_color(b,0);while(t<lt+16)rest(1);ln++;}destroy_bitmap(b);//// |
| 20 | remove_int(T);}END_OF_MAIN()/////////////////////////////////////////////////// |
I can fly right through the boxes on yours Kikaru.
EDIT:
I made one!
Left and right turn, space fires. As you can see, there is a lot of room for improvement (both in adding more code and improving gameplay).
| 1 | #include <math.h>/////////////////////////////////////////////////////////////// |
| 2 | #include <allegro.h>//////////////////////////////////////////////////////////// |
| 3 | #define F for(int i=0; i<10; i++)/////////////////////////////////////////////// |
| 4 | #define T textout_ex(buffer,font,"Lose",10,10,c,-1);//////////////////////////// |
| 5 | #define CC clear_to_color(buffer,makecol(0,0,0));/////////////////////////////// |
| 6 | int Square(int a){return a*a;}int main(){allegro_init();install_keyboard();///// |
| 7 | set_color_depth(16);set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);////////// |
| 8 | BITMAP* buffer=create_bitmap(640,480);double ex[10];double ey[10];bool ea[10];// |
| 9 | srand(time(0));double MA=2*3.14;double A2=MA/4;double a=4.78;double x=320.0;//// |
| 10 | double y=440.0;double x2=0.0;double y2=0.0;double bx=0.0;double by=0.0;///////// |
| 11 | double bs=0.0;double ba=0.0;bool bal=false;int bt=0;int c=makecol(255,255,255); |
| 12 | int Score=0;while(!key[KEY_ESC]){CC;F{if(ea<i>){if(ex<i><x)ex<i>+=rand()%3+2;/// |
| 13 | if(ex<i>>x)ex<i>-=rand()%3+2;if(ey<i><y)ey<i>+=rand()%3+2;////////////////////// |
| 14 | if(ey<i>>y)ey<i>-=rand()%3+2;if(sqrt(Square(x-ex<i>)+Square(y-ey<i>))<11){CC;T; |
| 15 | readkey();break;}if(sqrt(Square(bx-ex<i>)+Square(by-ey<i>))<11){ea<i>=false;//// |
| 16 | bt=0;bal=false;Score+=100;}circle(buffer,ex<i>,ey<i>,10,c);}if(!ea<i>){///////// |
| 17 | ex<i>=rand()%640;ey<i>=rand()%200;ea<i>=true;}}if(key[KEY_RIGHT])a+=.1;///////// |
| 18 | if(key[KEY_LEFT])a-=.1;if(key[KEY_SPACE]&&bal==false){bx=x+7*cos(a);//////////// |
| 19 | by=y+7*sin(a);ba=a;bs=15;bal=true;bt=20;}if(bal){bx+=bs*cos(ba);by+=bs*sin(ba); |
| 20 | bt--;if(bt==0)bal=false;circlefill(buffer,bx,by,2,c);}if(a>MA)a-=MA;//////////// |
| 21 | if(a<0)a+=MA;x2=x+7*cos(a);y2=y+7*sin(a);line(buffer,x,y,x2,y2,c);////////////// |
| 22 | circlefill(buffer,x,y,3,c);textprintf_ex(buffer,font,2,2,c,-1,"Score: %d",////// |
| 23 | Score);draw_sprite(screen,buffer,0,0);rest(50);}}END_OF_MAIN()////////////////// |
| 24 | //////////////////////////////////////////////////////////////////////////////// |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
<quote name=""Næssén"">
kenmasters1976, your tetris game is really starting to shape up. The only thing I miss is a score counter.
</quote>
Thanks. I'll try to add a score counter next time.
<quote name=""Ted Steen"">
kenmasters1976, That is one squeezed game of tetris! but you could double the fun with a score counter!
My suggestion is to...
</quote>
Thanks. I'll try to add a score counter next time... I mean, thanks, I'll consider your suggestions. They might help a lot.
Matthew Leverton's Tetris is only 17 lines long!. There must be something really wrong with mine. I might try to steal something from his code...
Now, today I watched a Saint Seiya marathon on TV, that means no activity today and no time to check the new entries. I'll get up to date tomorrow.
Those of you making Tetris should figure out a way to use more color
.
Mine uses all 16 colors from the classic CGA palette.
Matthew Leverton's Tetris is only 17 lines long!. There must be something really wrong with mine. I might try to steal something from his code...
Mine is pretty minimalistic, although there are a couple things that I need to change up to save some space.
To make Tetris, all you need to do per frame is:
save position
process keys and move piece
check for collision, if true:
revert to old position
if it was a timed drop: lock the piece, check for complete lines, reset
[/list]
The collision check and piece set is similar logic and can easily be combined (my 'p' function).
It's just about storing the data in as compact of a way without sacrificing ease of access. I used a bit field; I think with some creativity, it could be a lot simpler.
You never really need random access to the data. It's always done in a 4x4 loop, so I think one should optimize with that in mind.
Kikaru: I can fly through the boxes and sometimes when I go offscreen I can continue flying even though it's game over. Nice idea though!
Neil: Hmm, when the game starts it's all black. When I press a key it says lose and when I shoot, the bullets seem to only update when I hold a key. And I don't see anything to shoot at.
| 1 | #include <allegro.h> // Tetris by Matthew Leverton |
| 2 | int j[]={0,1,3,5,7,8,9,1,4,4,4,2,2,2},i,q,r,y,s[]={0xcc00,0x46404e00,0x4c400e40, |
| 3 | 0x44c00e20,0x64408e00,0xc4400e80,0x44602e00,0x8c406c00,0x4c80c600,0x44440f00},w[ |
| 4 | 24],x,R,Y,X,c,d,e=0,f=0,g,*b;S(){return s[j[q]+r/2]>>(r%2)*16;}F(A,B,C){rectfill |
| 5 | (b,A*16,B*16,A*16+15,B*16+15,C);}z(){y=0;x=7;q=rand()%7;r=0;d=10;c=S();}p(A,B,C) |
| 6 | {C=c;for(i=16;--i>=0;C>>=1){B=(C&1)<<(x+i%4);if(A)w[y+i/4]|=B;else if(w[y+i/4]&B |
| 7 | )return 1;}return 0;}main(){srand(time(0));i=24;allegro_init();install_keyboard( |
| 8 | );set_gfx_mode(GFX_GDI,288,352,0,0);b=create_bitmap(640,480);z();while(i--)w<i>= |
| 9 | i>19?0x3ffff:0x3c00f;while(!key[59]){X=x,Y=y,R=r;if(!--d)y++;else{if(key[83])x++ |
| 10 | ;if(key[82])x--;if(key[85]){y++;f++;}if(key[84])r=(r+1)%j[q+7];}c=S();if(p(0)){x |
| 11 | =X,y=Y,r=R,c=S();if(!d){p(1);i=20;g=10;while(i--)if(w<i>==0x3ffff){memmove(w+1,w |
| 12 | ,i++*4);w[0]=0x3c00f;++e;f+=g;g*=10;}z();if(p(0))break;}}else if(!d||key[85])d=( |
| 13 | d=10-e/10)<2?2:d;for(i=0;i<378;++i)F(i%18,i/18,w[i/18]&1<<(i%18)?((e/10)%15)+1:0 |
| 14 | );textprintf(b,font,8,340,15,"Lines: %d Score: %d",e,f);i=16;while(i--){if(c&1)F |
| 15 | (x+i%4,y+i/4,3);c>>=1;}blit(b,screen,0,0,0,0,288,352);rest(50);}}END_OF_MAIN() |
Slightly more compact than my first version. I removed duplicate positions in the s[] array (e.g., the square only has one "angle"). But obviously that made the logic a bit more complex. Thus, I introduced j[] to point to the first position in s[] per block and to declare how many angles each piece has.
j[q+7] just equals j[q+1]-j[q], but it was slightly more compact to expand it since it simplified the math and let me store the square piece as 2 bytes.
I also created a F() shortcut for rectfill and replaced retrace_count with just a simple rest().
Sokoban Clone!
You are the greyish block. Move and push with arrowkeys. Press R to restart level. Your goal is to push the orange blocks into the red ones. There are 5 levels. I hope they are beatable 
The levels were made by Yoshio Murase
| 1 | #include <allegro.h>///////////////////////////////////Sokoban v1.0 by arvidsson |
| 2 | int w=160,i,j,k,*B,x,y,c=0,C[8][8],v=1,a,b,d,e;int l[5][8][8]={{{0,0,0,0,0,0,0,0 |
| 3 | },{0,0,3,0,0,0,3,0},{0,0,4,0,4,4,3,0},{0,0,4,1,1,4,5,0},{0,0,4,4,1,4,4,0},{0,0,4 |
| 4 | ,4,0,4,4,0},{0,0,4,4,0,0,0,0},{0,0,0,0,0,0,0,0}},{{0,0,0,0,0,0,0,0},{0,0,0,4,4,0 |
| 5 | ,0,0},{0,4,4,4,1,4,0,0},{0,4,0,3,0,5,0,0},{0,4,0,1,4,3,0,0},{0,4,4,3,1,4,0,0},{0 |
| 6 | ,0,4,4,4,0,0,0},{0,0,0,0,0,0,0,0}},{{0,0,0,0,0,0,0,0},{0,0,0,4,4,4,0,0},{0,4,4,3 |
| 7 | ,4,2,4,0},{0,4,3,1,3,0,5,0},{0,0,4,0,4,1,4,0},{0,0,4,1,4,0,4,0},{0,0,0,0,4,4,4,0 |
| 8 | },{0,0,0,0,0,0,0,0}},{{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,4,4,4,2,4,4,0},{0,4 |
| 9 | ,2,4,3,4,4,0},{0,4,2,2,2,4,0,0},{0,4,1,4,2,4,0,0},{0,0,4,5,4,4,0,0},{0,0,0,0,0,0 |
| 10 | ,0,0}},{{0,0,0,0,0,0,0,0},{0,0,0,0,4,4,0,0},{0,4,3,1,2,4,0,0},{0,4,4,4,1,4,4,0}, |
| 11 | {0,4,3,0,4,3,4,0},{0,4,2,0,1,5,0,0},{0,0,4,4,4,4,0,0},{0,0,0,0,0,0,0,0}}};R(x,y, |
| 12 | c){x*=20;y*=20;rectfill(B,x,y,x+19,y+19,c);}main(){allegro_init();set_gfx_mode(2 |
| 13 | ,w,w,0,0);install_keyboard();set_keyboard_rate(0,0);B=create_bitmap(w,w);while(! |
| 14 | key[59]){if(v) {v=0;if(c<5){for(i=0;i<8;i++)for(j=0;j<8;j++){C<i>[j]=l[c]<i>[j]; |
| 15 | if(C<i>[j]>4){x=j;y=i;C<i>[j]=4;}}}}k=0;for(i=0;i<8;i++)for(j=0;j<8;j++)if(C<i>[ |
| 16 | j]==3)k++;if(!k){c++;v=1;}for(i=0;i<8;i++){for(j=0;j<8;j++){if(C<i>[j]){if(C<i>[ |
| 17 | j]==4)k=15;if(C<i>[j]==3)k=4;if(C<i>[j]<3)k=42;}else k=16; R(j,i,k);}}R(x,y,25); |
| 18 | if(c<5) textprintf_ex(B,font,5,5,15,-1,"Level: %d",c+1); else textout_ex(B,font, |
| 19 | "WINNER!",5,5,15,-1);blit(B,screen,0,0,0,0,w,w);if(!v)k=readkey();a=b=d=e=0;if(k |
| 20 | >>8==82){a=-1;b=-2;}if(k>>8==83){a=1;b=2;}if(k>>8==84){d=-1;e=-2;}if(k>>8==85){d |
| 21 | =1;e=2;}if(k>>8==18)v=1;if(C[y+d][x+a]){if(C[y+d][x+a]>2){x+=a;y+=d;}else if(C[y |
| 22 | +d][x+a]==1){if(C[y+e][x+b]==3){C[y+e][x+b]=2;C[y+d][x+a]=4;x+=a;y+=d;}else if(C |
| 23 | [y+e][x+b]==4){C[y+e][x+b]=1;C[y+d][x+a]=4;x+=a;y+=d;}} else if(C[y+d][x+a]==2){ |
| 24 | if(C[y+e][x+b]==3){C[y+e][x+b]=2;C[y+d][x+a]=3;x+=a;y+=d;}else if(C[y+e][x+b]==4 |
| 25 | ){C[y+e][x+b]=1;C[y+d][x+a]=3;x+=a;y+=d;}}}}destroy_bitmap(B);}END_OF_MAIN()//// |

This will be my last entry. Life called.
The problem would be if you go through the boxes without triggering game over. Otherwise, it's just because I was low on codespace, and if I tried to stop movement when you die, it crashes. I'll figure it out a little later.
[EDIT]Grr... It's not working. Something seems to be wrong with H(), my collision function. It doesn't seem to be recognising collisions most of the time. Also, as of now, the "leaving the screen" code doesn't work either, but that should be easy to fix.
| 1 | #include <allegro.h>/////////////////////////////////////////////////////////// |
| 2 | #include <stdio.h>///////////////////////////////////////////////////////////// |
| 3 | #define W makecol(255, 255, 255)/////////////////////////////////////////////// |
| 4 | #define G makecol(255, 255, 0)///////////////////////////////////////////////// |
| 5 | BITMAP*b;int u=KEY_UP,r=KEY_RIGHT,l=KEY_LEFT,sx=4,sy=0,bx[99],by[99],nb=0,hs=0, |
| 6 | i=0,lt=0,t=0,ln=0,x=320,y=240,j=0,g_o=0;int J(){return j/8;}bool H(){for(i=0;i< |
| 7 | nb;i++)return((x>bx<i>)&&(y>by<i>)&&(x<bx<i>+24)&&(y<by<i>+24));}void T(){t++;} |
| 8 | void A(){if(nb>98)return;bx[nb]=640;by[nb]=rand()%480;nb++;}void V(){for(i=00;i |
| 9 | <nb;i++){if(bx<i><0){nb--;bx<i>=bx[nb];by<i>=by[nb];i--;}}}void main(){//////// |
| 10 | allegro_init();set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);////////////// |
| 11 | install_keyboard();install_timer();install_int_ex(T,MSEC_TO_TIMER(1));srand(/// |
| 12 | time(0));b=create_bitmap(640,480);while(!key[KEY_ESC]){lt=t;if((key<u>)&&(j<11) |
| 13 | )j=70;if(key[r])x+=5;if(key[l])x-=5;j-=3;if(ln%9==0)A();V();if(ln%150==0){sx=// |
| 14 | rand()%4+2;sy=rand()%3-1;}for(i=0;i<nb;i++){bx<i>-=sx;by<i>+=sy;rect(b,bx<i>,by |
| 15 | <i>,bx<i>+24,by<i>+24,W);}if(j>10)vline(b,x,y,y+9,W);else hline(b,x-9,y,x+9,W); |
| 16 | if(j<-60)j=-60;rect(b,1,1,638,478,W);if((ln>hs)&&(!g_o))hs=ln;y-=J();if(H())g_o |
| 17 | =1;textprintf_ex(b,font,10,10,W,-1,"time: %i",t/1000);textprintf_ex(b,font,10, |
| 18 | 20,G,-1,"best: %i",t/1000);if(g_o!=0){textprintf_centre_ex(b,font,320,240,W,-1, |
| 19 | "game over");}blit(b,screen,0,0,0,0,640,480);clear_to_color(b,0);while(t<lt+16) |
| 20 | rest(1);ln++;if((0<x<640)&&(0<y<480));else g_o=1;}destroy_bitmap(b);remove_int(T) |
| 21 | ;}END_OF_MAIN()//////////////////////////////////////////////////////////////// |
New code, which still doesn't work.
#define W makecol(255, 255, 255)///////////////////////////////////////////////
#define G makecol(255, 255, 0)/////////////////////////////////////////////////
Now that's what I call a huge waste...
You can also replace GFX_AUTODETECT_WINDOWED with 2.
oh, nice. I'll probably do another game, as I really didn't like this one very much. Probably a shooter of some kind. I'm pretty good at making those.
Macros are a waste of space unless they take up the entire line. Using functions is generally better if possible:
#define W makecol(255, 255, 255)/////////////////////////////////////////////// #define G makecol(255, 255, 0)///////////////////////////////////////////////// W(){return makecol(255,255,255);}G(){return makecol(255,255,0);} // but it's just as easy to do this in your main routine: int W=0xffffff,G=makecol(255,255,0);
Use global variables to avoid passing function parameters. Also, this is an evil way to save some space:
void a(){int A=1;} a(){int A=1;} a(A){A=1;}
Stuff like this can really add up, giving you that extra line.
I think almost all of my functions were int, but I'll use that for my void functions.
Mine uses all 16 colors from the classic CGA palette.
CGA only had 4 colors in 320 x 200, 2 colors in 640 x 200. Maybe you meant EGA?
I cooked up this today. It's ugliest piece of code I've made in years, and I haven't even tried to opimize space yet since there's plenty left.
Hmm, I wonder.. Isn't this the first time ever that I've participated in a coding event?!
| 1 | #include <allegro.h> // Crimson by Anders Andersson |
| 2 | int i,j,*b,a,e,k=0,h[100],t,d;float x[100],y[100],v[100],w[100],X,Y;T(){++t;} |
| 3 | END_OF_FUNCTION(T)LOCK_VARIABLE(t);LOCK_FUNCTION(T);main(){srand(time(0));x[0]= |
| 4 | 400;y[0]=300;h[0]=99;for(i=1;i<100;++i)h<i>=0;allegro_init();install_keyboard(); |
| 5 | install_timer();install_mouse();install_int(T,50);set_gfx_mode(2,800,600,0,0);b= |
| 6 | create_bitmap(800,600);while(!key[59]){rest(1);if(h[0]>0){if(!--t)continue;if( |
| 7 | key[83])x[0]++;if(key[82])x[0]--;if(key[85])y[0]++;if(key[84])y[0]--;a=0;e=0;for |
| 8 | (i=1;i<100;++i){if(h<i><=0){if(i<20&&!e++){x<i>=rand()%800;y<i>=10;h<i>=10;}else |
| 9 | if(i>50&&mouse_b&&!a++){x<i>=x[0];y<i>=y[0];v<i>=(mouse_x-x[0])/100;w<i>=( |
| 10 | mouse_y-y[0])/100;h<i>=1;}}else{if(i<50){float a=atan2(y[0]-y<i>,x[0]-x<i>);x<i> |
| 11 | +=cos(a);y<i>+=sin(a);circle(b, x<i>,y<i>,5,makecol(255, 255, 255));for(j=50; j< |
| 12 | 100;++j){X=x<i>-x[j];Y=y<i>-y[j];if(X*X+Y*Y<99 && h[j]>0){h[j]=0;if(--h<i><=0) |
| 13 | ++k;}}X=x<i>-x[0];Y=y<i>-y[0];if(X*X+Y*Y<99)--h[0];}else{if(x<i><0||x<i>>800|| |
| 14 | y<i><0||y<i>>600)h<i>=0;x<i>+=v<i>;y<i>+=w<i>;circle(b,x<i>,y<i>,2,makecol(0,0, |
| 15 | 255));}}}circle(b, x[0],y[0],5,makecol(0, 255, 0));circle(b, mouse_x,mouse_y,5, |
| 16 | makecol(255,255,0));textprintf(b,font,8,580,15,"Kills: %d Health: %d",k,h[0]);} |
| 17 | else{textprintf(b,font,300,300,15,"Game over: %d kills",k);if(key[KEY_ENTER]){ |
| 18 | x[0]=400;y[0]=300;h[0]=99;for(i=1;i<100;++i)h<i>=0;}}blit(b,screen,0,0,0,0,800, |
| 19 | 600);clear(b);}}END_OF_MAIN() |
I think almost all of my functions were int, but I'll use that for my void functions.
The functions default to int, but you can use them as if they were void. Best of both worlds.
CGA only had 4 colors in 320 x 200, 2 colors in 640 x 200. Maybe you meant EGA?
I meant CGA. It has a 16 color palette; it's just that they aren't all simultaneously usable in graphics mode.
int W=-1;
Trezker: I can't get your code to compile. b isn't given a type, and you don't convert float to fix. Also, my compiler won't let you declare functions without types.
Trezker: I can't get your code to compile. b isn't given a type, and you don't convert float to fix. Also, my compiler won't let you declare functions without types.
Try a C compiler instead of a C++ compiler.
Here's my entry. I wrote it before I saw Matthew's comment on using functions over macros for the color. I still ended up saving space with the macro though.
Player 1 Controls:
Move Up - Up Arrow
Move Down - Down Arrow
Player 2 Controls: (if you have a player 2)
Move Up - W Key
Move Down - S Key
PONG, 1 or 2 players
| 1 | #include <allegro.h>// Pong, by Michael Swiger //////////////////////////////// |
| 2 | #include <time.h>////////////////////////////////////////////////////////////// |
| 3 | #define W makecol(255,255,255)///////////////////////////////////////////////// |
| 4 | volatile long s;void isc(){s++;}END_OF_FUNCTION(isc) int main(){allegro_init(); |
| 5 | install_keyboard();install_timer();set_color_depth(desktop_color_depth());///// |
| 6 | set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);BITMAP * b=create_bitmap(640, |
| 7 | 480);srand(time(0));install_int_ex(isc,BPS_TO_TIMER(50)); r: int p1s=0,p2s=0,p; |
| 8 | while(1){textout_centre_ex(screen,font,"(1)1 Player, (2)2 Players, (ESC) Quit", |
| 9 | 320,240,W,0);int v=readkey();if(v>>8==KEY_1){p=1;goto s;};if(v>>8==KEY_2) {p=2; |
| 10 | goto s;}if(v>>8==KEY_ESC)exit(0);} s: float bx=310,by=230,pbx,pby,vbx,vby,pvbx, |
| 11 | pvby,i[4]={1,-1,0.5,-0.5};int p1x=10,p1y=205,p2x=610,p2y=205,t=rand()%490;vbx=i |
| 12 | [rand()%4];vby=i[rand()%4]; while(1){bx+=vbx;by+=vby;if(bx<=30&&p1y+70>=by&&p1y |
| 13 | <=by&&p1x<=bx&&p1x+20>=bx){vbx=-vbx;vbx+=.15;bx=p1x+20;t=rand()%490;}if(bx+20>= |
| 14 | 610&&p2y+70>= by&&p2y<=by&&p2x<=bx+20&&p2x+20>=bx+20){vbx=-vbx;vbx-=0.15;bx=p2x |
| 15 | -20;}if(bx+20<=0){p2s++;goto s;} if(bx>=640){p1s++;goto s;}if(by<=0){vby=-vby;} |
| 16 | if(by>=460){vby=-vby;}for(pbx=bx,pby=by,pvbx=vbx,pvby=vby;pbx<=610&&pbx>=30;pbx |
| 17 | +=pvbx){if((pvby<0&&pby<=0)||(pvby>0&&pby>=620))pvby=-pvby;pby+=pvby;}while(s>0 |
| 18 | ){if(key[KEY_UP]&&p1y>1)p1y-=6;if(key[KEY_DOWN]&&p1y<409)p1y+=6;if(key[KEY_W]&& |
| 19 | p2y>1&&p==2)p2y-=6;if(key[KEY_S]&&p2y<409&&p==2)p2y+=6;if(vbx>0&&p==1) {if(p2y+ |
| 20 | 35>pby&&p2y>0&&bx>=t)p2y-=6;if(p2y+35<pby&&p2y<410&&bx>=t)p2y+=6;}else if(p==1) |
| 21 | {if(p2y>205)p2y-=6; if(p2y<205)p2y+=6;} if(key[KEY_ESC]){clear_keybuf(); clear( |
| 22 | screen ); goto r; } s--;} textprintf_ex (b,font,100,2,W,0,"P1 Score: %d", p1s); |
| 23 | textprintf_ex(b,font,450,2,W,0,"P2 Score: %d",p2s); rectfill (b,p1x,p1y,p1x+20, |
| 24 | p1y+70,W);rectfill(b,p2x,p2y,p2x+20,p2y+70,W); rectfill(b,bx,by,bx+20,by+20,W); |
| 25 | blit(b,screen,0,0,0,0,640,480);clear(b);}allegro_exit();return 0;}END_OF_MAIN() |
Uses every available character
.
LOCK_VARIABLE(t);LOCK_FUNCTION(T);main(){
Those should be in the main function, not after the end of the timer function. They only really apply to DOS anyway (IIRC).
Some more tweaking due to applying almost every Ted Steen's suggestion. The underlying code remains the same. To improve it beyond this, I would need to fully revamp the logic to something more ala Matthew Leverton. I don't know if I have the will to do it.
What's new?, score counter at last and a new presentation. Also added a somewhat forced game over handling (the game will just rest() for 2 seconds and then exit).
| 1 | #include <allegro.h> // SHTetris.c |
| 2 | #define f(i,x) for(i=0;i<x;i++) ///////////// |
| 3 | #define B(x) PS(); stretch_blit(O,screen,0,0,320,240,0,0,640,480); rest(x) |
| 4 | #define RF(a,b,c,w) rectfill(O,xo+a+j*8,yo+b+y*8,xo+a+j*8+w,yo+b+y*8+w,c) |
| 5 | int F[25];int ps[8][16]={0,0,0,15,2,2,2,2,0,0,0,15,2,2,2,2,0,2,2,3,0,0,7,4,0,3, |
| 6 | 1,1,0,0,1,7,0,1,1,3,0,0,4,7,0,3,2,2,0,0,7,1,0,1,3,2,0,0,6,3,0,1,3,2,0,0,6,3,0,2 |
| 7 | ,3,1,0,0,3,6,0,2,3,1,0,0,3,6,0,0,7,2,0,1,3,1,0,0,2,7,0,2,3,2,0,0,3,3,0,0,3,3,0, |
| 8 | 0,3,3,0,0,3,3};int i,j,p,q=0,m[4],x,y,w,a=0,o,no=0,d,e,g,k,lt,rt,ltl,rtl, cf,h, |
| 9 | cm,rp,cr,xo,yo,l,S=0;BITMAP *O;UF(){l=0;f(i,24)if(F<i>==0xFFFF){l++;DL(i,-1,-1, |
| 10 | -1);B(50);DL(i,-1,0x44A3FF,-1);B(50);}f(i,24)if(F<i>==0xFFFF)f(j,i)F[i-j]=F[i-j |
| 11 | -1];S+=pow(l,2)*1024;}DL(y,d,c,sc){f(j,16)if(d&1<<j){RF(1,1,c,6);RF(3,3,c<<4,4) |
| 12 | ;}else if(sc)RF(0,0,sc,8);}PS(){textprintf_ex(O,font,17,0,0,-1,"%10d",S);}int |
| 13 | main(){F[24]=-1;allegro_init();set_color_depth(24);set_gfx_mode(2,256,400,0,0); |
| 14 | install_keyboard();O=create_bitmap(320,240);f(i,24)F<i>=0xF003;srand(time(NULL) |
| 15 | );while(!key[59]){if(!a){p=q;q=rand()%7;o=no;no=rand()%4;d=0;a=1;x=6;y=3;w=6;e= |
| 16 | 10;g=32;}if(h>e){y++;h=0;}if(cm)x=w;rp=0;if(key[82]){lt++;if(lt>ltl){lt=0;w=x-1 |
| 17 | ;ltl/=2;if(ltl<4)ltl=4;}}if(key[83]){rt++;if(rt>rtl){rt=0;w=x+1;rtl/=2;if(rtl<4 |
| 18 | )rtl=4;}}if(key[75]&&!k){o=(o+1)%4;rp=1;k=1;}if(!key[75])k=0;if(!key[82]){ltl= |
| 19 | 32;lt=31;}if(!key[83]){rtl=32;rt=31;}if(key[85]){e=1;g=1;if(!d)S++;}if(!key[85] |
| 20 | ){e=10;g=32;}if(rp){f(i,4)m<i>=ps[p][(o)*4+i];cr=1;f(i,4){cr=cr&&((F[y-i]&(m<i> |
| 21 | )<<x)==0);}if(!cr){o=o==0?3:o-1;}}f(i,4)m<i>=ps[p][(o)*4+i];cm=1;f(i,4)cm=cm&&( |
| 22 | F[y-1-i]&m<i><<w)==0;cf=1;f(i,4)cf=cf&&(F[y-i]&m<i><<x)==0;if(cf){d=0;h++;}else |
| 23 | d++;if(d>g){f(i,4)F[y-i-1]|=m<i><<x;a=0;if(y<4){rest(2000);exit(0);}}xo=yo=0 |
| 24 | ;f(i,25)DL(i,F<i>,0xFFA6A6,0xFFFFA9);f(i,4)DL(y-1-i,m<i><<x,0x44A3FF,0);UF();xo |
| 25 | =96;yo=72;f(i,4){m<i>=ps[q][no*4+i];DL(5-i,m<i>,0x8F,0);}B(15);}}END_OF_MAIN() |
{"name":"595211","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/4\/f46ce186f9ea72a810719bde12796964.gif","w":264,"h":432,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/4\/f46ce186f9ea72a810719bde12796964"}
Another entry, a space invaderish game where you can only destroy the enemies the same colour as yourself. The player colour cycles through seven different, changing with each shot (Z to fire, space resets if you die).
| 1 | #include <math.h>/************************************************************/ |
| 2 | #include <allegro.h>/*********************************************************/ |
| 3 | int t;void it(){t++;}END_OF_FUNCTION(it);typedef struct{int x,y,c,dd,dr,s,d;}_e |
| 4 | ;typedef struct{float x,y,s,a,l;int c;}_p;_p p[256];_e d[36];int np=0,h,e=0,g=0 |
| 5 | ,q=160,w=228,s=0,c=1,l;BITMAP *b;A(float x,float y,int c){_p pa={x,y,3,rand()% |
| 6 | 360*M_PI/180,10,c};p[np]=pa;np++;}D(){clear_bitmap(b);int n;if(!l){line(b,q,w,q |
| 7 | -8,w+8,c);line(b,q,w,q+8,w+8,c);line(b,q-8,w+8,q+8,w+8,c);floodfill(b,q,w+4,c); |
| 8 | if(s)line(b,e,g,e,g-4,(c>1)?c-1:7);for(n=0;n<36;n++)if(!d[n].dd)circlefill(b,d[ |
| 9 | n].x,d[n].y,8,d[n].c);textprintf_ex(b,font,8,8,15,0,"Score: %i",h);}else texto\ |
| 10 | ut_centre_ex(b,font,"Game over.",160,120,15,0);for(n =0;n<np;n++)circlefill(b,p |
| 11 | [n].x,p[n].y,2,p[n].c);blit(b,screen,0,0,0,0,320,240);}S(){int n,x=0,y=0;q=160; |
| 12 | w=228;l=h=0;for(n=0;n<36;n++){_e e={96+x*16,-64+y*16,1+rand()%7,0,y%2,1,0};d[n] |
| 13 | =e;x++;if(x>8){x=0;y++;}}}C(x,y,n){return(x-d[n].x)*(x-d[n].x)+(y-d[n].y)*(y-d[ |
| 14 | n].y)<=64;}main(){allegro_init();install_keyboard();install_timer();install_in\ |
| 15 | t_ex(it,BPS_TO_TIMER(60));set_gfx_mode(2,320,240,0,0);srand(time(NULL));b=crea\ |
| 16 | te_bitmap(320,240);S();while(!key[59]){while(t>0){int n,i;for(n=0;n<np;n++){p[n |
| 17 | ].x+=p[n].s*cos(p[n].a);p[n].y+=p[n].s*sin(p[n].a);p[n].l--;if(p[n].l<0||p[n].x |
| 18 | <0||p[n].x>320||p[n].y<0||p[n].y>240){for(i=n;i<np-1;i++)p<i>=p[i+1];np--;n--;} |
| 19 | }if(l==0){if(key[83])q+=3;if(key[82])q-=3;if(key[26]){if(!s){s=1;e=q;g=w;if(c++ |
| 20 | >7)c=1;}};if(s){g-=5;if(g<0)e=g=s=0;}for(n=0;n<36;n++){if(C(q,w,n))l=1;if(!d[n] |
| 21 | .dd){if(!d[n].d){d[n].x+=(d[n].dr)?d[n].s:-d[n].s;if(d[n].x<=16||d[n].x>=304)d[ |
| 22 | n].d=16;}else{d[n].y++;d[n].d--;if(!d[n].d)d[n].dr=!d[n].dr;}if(C(e,g,n)){e=g=s |
| 23 | =0;h+=(d[n].dd=(((c>1)?c-1:7)==d[n].c)?1:0)?10:0;if(d[n].dd)for (i=0;i<10;i++)A |
| 24 | (d[n].x,d[n].y,d[n].c);}}else{int dr=rand()%2;_e e={(dr)?96:224,-64,1+rand()%7, |
| 25 | 0,dr,1,0};d[n]=e;}}}else if(key[75])S();t--;}D();rest(1);}}END_OF_MAIN()/*****/ |
{"name":"595212","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/4\/34a98388b07634a25219a3d39043895e.png","w":319,"h":262,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/4\/34a98388b07634a25219a3d39043895e"}
[edited] - Added the minimum number of moves possible in finished message, and added penalty moves for viewing the maze.
Here's my entry. The objective is to find your way out of a randomly generated maze.
| 1 | #include <allegro.h>///////HardTranceFan's Entry into Allegro ScreenHack 2008/// |
| 2 | int g[64][48],r[5]={0,-1,0,1,0},i,j,n,o,x,y;int w(int x,int y,int a,int q){if(m( |
| 3 | x+2*r[a],y+2*r[a+1],q+1))g[x+r[a]][y+r[a+1]]=1;}int c(int d){return makecol(d,d, |
| 4 | d);}int m(int x,int y,int q){int b;int z=rand()%4;if(x==1&&y==1)o=q;if(g[x][y]|| |
| 5 | x*y<0||x>62||y>46)return 0;g[x][y]=1;for(b=0;b<4;b++){w(x,y,z,q);z=(++z)%5;} |
| 6 | return 1;}main(){allegro_init();set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0 |
| 7 | );install_keyboard();srand(time(0));BITMAP*b=create_bitmap(640,480);int f=0,t; |
| 8 | for(i=0;i<64;i++)for(j=0;j<48;j++)g<i>[j]=0;g[1][0]=1;x=33;y=25;i=m(x,y,0); |
| 9 | while(!f&&!key[KEY_ESC]){n++;if(key[KEY_RIGHT]&&g[x+1][y])x++;else if(key |
| 10 | [KEY_LEFT]&&g[x-1][y])x--;else if(key[KEY_DOWN]&&g[x][y+1])y++;else if(key |
| 11 | [KEY_UP]&&g[x][y-1])y--;else n--;clear(b);for(i=0;i<63;i++)for(j=0;j<47;j++){if( |
| 12 | !g<i>[j]){if(key[KEY_SPACE])rectfill(b,i*10+5,j*10+5,i*10+15,j*10+15,c(125)); |
| 13 | else{t=125-sqrt((i-x)*(i-x)+(j-y)*(j-y))*20;if(t<0)t=0;if(t>255)t=255;rectfill(b |
| 14 | ,i*10+5,j*10+5,i*10+15,j*10+15,c(t));}}}textprintf_ex(b,font,180,465,c(125),-1, |
| 15 | "Arrow keys - move, space - view whole maze.");circlefill(b,x*10+10,y*10+10,4, |
| 16 | c(255));blit(b,screen,0,0,0,0,640,480);if(key[KEY_SPACE])n+=10;while( |
| 17 | key[KEY_SPACE]);f=(x==1&&y==0);rest(50);}if(f){clear(screen);textprintf_ex( |
| 18 | screen,font,140,230,c(200),-1,"Well done, You escaped in %i moves out of %i.",( |
| 19 | n+1)/2,o);}while(!key[KEY_ESC]);destroy_bitmap(b);}END_OF_MAIN() |
http://www.allegro.cc/files/attachment/595219
http://www.allegro.cc/files/attachment/595220
#include <allegro.h> // Crimson by Anders Andersson int i,j,*b,a,r=10,e,k=0,h[100],t,d;char u;float x[100],y[100],v[100],w[100],X,Y; T(){++t;}C(N,S,C){circle(b,x[N],y[N],S,C);}main(){srand(time(0));x[0]=400;y[0]= 300;h[0]=99;for(i=1;i<100;++i)h[i]=0;allegro_init();install_keyboard(); install_timer();install_mouse();install_int(T,50);set_gfx_mode(2,800,600,0,0);b= create_bitmap(800,600);while(!key[59]){rest(1);if(h[0]>0){if(!--t)continue;if( key[83])x[0]++;if(key[82])x[0]--;if(key[85])y[0]++;if(key[84])y[0]--;++a;++e;for (i=1;i<99;++i){if(h[i]<=0){if(i<50&&e>99){e-=99;y[i]=rand()%2?-10:610;x[i]=rand( )%2?-10:810;if(rand()%2)x[i]=rand()%800;else y[i]=rand()%600;h[i]=10;}else if(i> 50&&mouse_b&&a>r){a-=r;x[i]=x[0];y[i]=y[0];float a=atan2(mouse_y-y[0],mouse_x- x[0]);v[i]=cos(a);w[i]=sin(a);h[i]=1;}}else{if(i<50){float a=atan2(y[0]-y[i], x[0]-x[i]);x[i]+=cos(a);y[i]+=sin(a);C(i,5,4);for(j=50;j<99;++j){X=x[i]-x[j];Y= y[i]-y[j];if(X*X+Y*Y<99&&h[j]>0){h[j]=0;if(--h[i]<=0){++k;u=rand()%2?'M':'L'; x[99]=x[i];y[99]=y[i];h[99]=1;}break;}}X=x[i]-x[0];Y=y[i]-y[0];if(X*X+Y*Y<99) --h[0];}else{if(x[i]<0||x[i]>800||y[i]<0||y[i]>600)h[i]=0;x[i]+=v[i];y[i]+=w[i]; C(i,1,7);}}}if(h[99]){X=x[99]-x[0];Y=y[99]-y[0];if(X*X+Y*Y<99){if(u=='L'){h[99]= 0;r=2;}if(u=='M'){h[99]=0;r=10;}}textprintf(b,font,x[99],y[99],15,"%c",u);}C(0,5 ,3);circle(b,mouse_x,mouse_y,3,9);textprintf(b,font,8,580,15, "Kills: %d Health: %d",k,h[0]);}else{textprintf(b,font,300,300,15, "Game over: %d kills",k);if(key[KEY_ENTER]){x[0]=400;y[0]=300;h[0]=99;for(i=1;i< 100;++i)h[i]=0;}}blit(b,screen,0,0,0,0,800,600);clear(b);}}END_OF_MAIN()
Here's my first draft, it should compile without any warnings/errors:
| 1 | #include <allegro.h>///Jeff Bernard//////////////////////////Centipede.c//////// |
| 2 | #define F(FF) for(i=0;i<((FF)<1?2816:(FF)<2?32:(FF));i++)/////////////////////// |
| 3 | int x[6],c,i,j,v[64][44],m[32][3];S(M,L){F(M){m<i>[0]=L?630-i*10:-1;m<i>[1]=0;m[ |
| 4 | i][2]=-1;}}BITMAP*B;W(S,D,Y){textprintf_centre_ex(B,font,320,Y,c%16,-1,"%s %d",S |
| 5 | ,D);}H(N){F(10+4*(N<2))if(j=v[(x[0]-(!N?1:N<2?-11:-i))/10][(x[1]-20-(N<2?-i:N<3? |
| 6 | 1:-15))/10])return j;return 0;}A(){F(1)if(m<i>[0]>-1)return 0;}main(){////////// |
| 7 | allegro_init();install_keyboard();set_color_depth(8);set_gfx_mode(2,640,480,0,0) |
| 8 | ;Q:c=0;x[0]=320;x[1]=440;x[2]=-505;x[3]=-1;x[4]=0;x[5]=1;S(32,0);srand(time(0)); |
| 9 | F(0)v[i%64][i/64]=rand()%15/14;B=create_bitmap(640,480);while(!key[59]&x[5]){/// |
| 10 | clear_bitmap(B);W("Level:",c,6);W("Score:",x[2],467);rectfill(B,x[0],x[1],x[0]+ |
| 11 | 10,x[1]+14,4);hline(B,0,20,639,c%16);hline(B,0,460,639,c%16);vline(B,x[3],x[4],x |
| 12 | [4]+3,c%15+1);F(0)if(v[i%64][i/64])circlefill(B,i%64*10+5,i/64*10+25,5,c%16);F(1 |
| 13 | )if(m<i>[0]>-1){circlefill(B,m<i>[0]+5,m<i>[1]+25,5,c%15+1);m<i>[0]+=(c+1)/2*(m[ |
| 14 | i][2]*=j=v[m<i>[0]/10+(m<i>[2]>0?1:0)][m<i>[1]/10]|m<i>[0]<1|m<i>[0]>630?-1:1); |
| 15 | if(j<0)m<i>[1]+=10;if(x[3]>=m<i>[0]&x[3]<=m<i>[0]+10&x[4]-9>m<i>[1]&x[4]-21<m<i> |
| 16 | [1]){x[2]+=50;v[x[3]/10][(x[4]-10)/10]=m<i>[0]=-1;}if(m<i>[1]>420|(m<i>[0]+5>=x[ |
| 17 | 0]&m<i>[0]+5<=x[0]+10&m<i>[1]+25>=x[1]&m<i>[1]+25<=x[1]+14))x[5]=0;}if(A()){c++; |
| 18 | x[2]+=500;S(10+c,1);}x[0]-=key[82]?1*!H(0)*(x[0]>0):key[83]*(x[0]<629)*!H(1);x[1 |
| 19 | ]-=key[84]*(x[1]>20)?!H(2):key[85]*(x[1]<445)*!H(3);x[4]-=(x[3]>0)*10;if(x[4]<20 |
| 20 | )x[3]=-1;if(key[75]&x[3]<0){x[3]=x[0]+5;x[4]=x[1];}if(v[x[3]/10][(x[4]-10)/10]){ |
| 21 | v[x[3]/10][(x[4]-10)/10]=0;x[2]+=5;x[3]=-1;}rest(10);blit(B,screen,0,0,0,0,640, |
| 22 | 480);}if(!x[5]){c=(j=c)%15+1;W("Game Over! Score:",x[2],237);W("Press 'Spac\ |
| 23 | e' to restart. Level:",j,251);blit(B,screen,0,0,0,0,640,480);destroy_bitmap(B); |
| 24 | while(key[75]);while(!key[59])if(key[75])goto Q;}}END_OF_MAIN()///////////////// |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
http://www.allegro.cc/files/attachment/595221http://www.allegro.cc/files/attachment/595223
EDIT--Instructions: Move with arrow keys, shoot with space bar, quit with escape.
Some parts are really unoptimized, because I realized that Centipede wasn't as complex as I had originally thought, so it can definitely be slimmed down. Sometime this weekend I'll probably add a reset (so you don't have to exit the game when you lose) and make it so you can still see the board after you lose.
EDIT--Added those two features.
Oh, and I may have to do something about the timing. Hopefully it'll run roughly the same on every computer, I just used a rest to slow it down. Maybe I'll put in an Allegro timer with all the extra space I've got...
HardTranceFan: I really like the lighting effect, but it's kinda too easy, I think, since you view the entire maze. Also, it'd be nice if it would generate another maze after get out.
Trezker:
netcat: Cool twist to Space Invaders.
kenmasters1976: Very nice, the current version is a vast improvement from the first on you posted.
Arvidsson (Sokoban): I can't beat level 2. 
Matthew Leverton: Tetris?... I already played a Tetris entry... oh, and you can keep getting points by holding down, even after the piece has hit the bottom, I assume it's a glitch?
If I didn't comment on yours it's because I already have/will later or it didn't compile for me (exclusively C at the moment). I'll try the ones that didn't compile with another compiler later on.
What... No comment on me?
Well, here's version 0.3.
+Varying power on bullets
+Wimsy seeker bullet, really powerful but may take some time to find target
| 1 | #include <allegro.h> // Crimson by Anders Andersson |
| 2 | int i,j,*b,a,r=10,e,k=0,h[100],t,s=0,m,p=2;char u;float x[100],y[100],v[100], |
| 3 | w[100],X,Y;T(){++t;}C(N,S,C){circle(b,x[N],y[N],S,C);}R(N){return rand()%N;}main |
| 4 | (){srand(time(0));x[0]=400;y[0]=300;h[0]=99;for(i=1;i<100;++i)h<i>=0; |
| 5 | allegro_init();install_keyboard();install_timer();install_mouse();install_int(T, |
| 6 | 50);set_gfx_mode(2,800,600,0,0);b=create_bitmap(800,600);while(!key[59]){rest(1) |
| 7 | ;if(h[0]>0){if(!--t)continue;if(key[83])x[0]++;if(key[82])x[0]--;if(key[85]) |
| 8 | y[0]++;if(key[84])y[0]--;mouse_b?++a:a;++e;for(i=1;i<99;++i){if(h<i><=0){if(i<50 |
| 9 | &&e>99){e-=99;y<i>=R(2)?-10:610;x<i>=R(2)?-10:810;if(R(2))x<i>=R(800);else y<i>= |
| 10 | R(600);h<i>=10;}else if(i>50&&mouse_b&&a>r){a-=r;x<i>=x[0];y<i>=y[0];float a= |
| 11 | atan2(mouse_y-y[0],mouse_x-x[0]);v<i>=cos(a);w<i>=sin(a);h<i>=p;}}else{if(i<50){ |
| 12 | float a=atan2(y[0]-y<i>,x[0]-x<i>);x<i>+=cos(a);y<i>+=sin(a);C(i,5,4);for(j=50;j |
| 13 | <99;++j){X=x<i>-x[j];Y=y<i>-y[j];if(X*X+Y*Y<99&&h[j]>0){h<i>-=h[j];h[j]=0;if( |
| 14 | h<i><=0){++k;u=R(2)?'M':R(2)?'L':'S';x[99]=x<i>;y[99]=y<i>;h[99]=1;}break;}}X= |
| 15 | x<i>-x[0];Y=y<i>-y[0];if(X*X+Y*Y<99)--h[0];}else{if(x<i><0||x<i>>800||y<i><0|| |
| 16 | y<i>>600)h<i>=0;m=R(49)+1;if(s&&h[m]>0){float a=atan2(y[m]-y<i>,x[m]-x<i>);v<i> |
| 17 | +=cos(a);w<i>+=sin(a);}x<i>+=v<i>;y<i>+=w<i>;C(i,1,7);}}}if(h[99]){X=x[99]-x[0]; |
| 18 | Y=y[99]-y[0];if(X*X+Y*Y<99){s=0;h[99]=0;if(u=='L'){r=2;p=1;}if(u=='M'){r=10;p=2; |
| 19 | }if(u=='S'){s=1;r=25;p=10;}}textprintf(b,font,x[99],y[99],15,"%c",u);}C(0,5,3); |
| 20 | circle(b,mouse_x,mouse_y,3,9);textprintf(b,font,8,580,15,"Kills: %d Health: %d", |
| 21 | k,h[0]);}else{textprintf(b,font,300,300,15,"Game over: %d kills",k);if( |
| 22 | key[KEY_ENTER]){x[0]=400;y[0]=300;h[0]=99;for(i=1;i<100;++i)h<i>=0;}}blit(b, |
| 23 | screen,0,0,0,0,800,600);clear(b);}}END_OF_MAIN() |
Matthew Leverton: Tetris?... I already played a Tetris entry... oh, and you can keep getting points by holding down, even after the piece has hit the bottom, I assume it's a glitch?
scoring for pressing the down key is generous (especially if you bump against the side), but it's good enough for demo #1.
There are some really fun entries. Well done everybody!.
Arvidsson, great job on all your entries.
I find the first one actually quite fun. It would be nice if you could save boosts between levels.
In your second entry I think some asteroids are pretty hard to see. I made it to level 7, time 92... by simply staying in a corner almost all time.
I'm no good at Sokoban. I tried level 2 twice and give up.
Neil Black, all I see is a black screen and a shooter. Nothing to shot at. Yet scored 600 pts.
Mokkan, your pong entry works fine. What else can I say?.
netcat, your space invaders idea seems interesting but I can hardly see the shots.
HardTranceFan, the maze seems too long. Perhaps that's the point?.
Jeff Bernard, all I get is a black screen with the red square at the bottom.
Trezker, nice game. Got 93 kills in version 0.3.
Kikaru, you know... you can go trough boxes.
Matthew Leverton, wow! 15 lines?.
I added color (each piece is a different color that is 'remembered' when placed) and now its up to 1,180 characters—almost 16 lines.
In order to accomplish that, I had to replace the bitfield system with a more traditional char W[y][x].
So I have 740 characters to add the "next" indicator, sound effects, music, multiple modes of play including multiplayer (joystick, mouse, network, and artificial intelligence).
What... No comment on me?
I forgot, I wasn't sure what to say, really. I just played the newest version though, and I like the homing weapon.
all I get is a black screen with the red square at the bottom.
Hmmm... I wonder if it's possible that the default palette would have the colors in different orders on different machines in 8 bit mode? Speaking of color, I hope I remembered to ensure that the centipede is never black... I'll look into this.
scoring for pressing the down key is generous (especially if you bump against the side), but it's good enough for demo #1.
So my assumption was essentially correct. Man, I don't need to read anyone's posts!
Here's my "final" version:
http://www.allegro.cc/files/attachment/595225
Windows binary: tetris.exe.
OS X binary: tetris.gz
| 1 | #include <allegro.h> // Tetris by Matthew Leverton |
| 2 | int S,Y,x,R,j[]={0,1,2,3,4,6,8,1,2,2,2,4,4,4},D[]={0x0145,0x048C0123,0x14580156, |
| 3 | 0x04591245,0x15691456,0x14594569,0x159A2456,0x126A4568,0x12590456,0x269A456A},X, |
| 4 | a,c,d,l=0,s=0,g,*b,i,p,q,r,y,u,v,h=1,z,_=0,k=0;SAMPLE *e;float m=0,n;char W[24][ |
| 5 | 18],*K,*J;G(A,B,C,D){rectfill(b,A*16,B*16,A*16+D,B*16+D,C);}F(A,B,C){G(A,B,C>0&& |
| 6 | C<16?C<8?0:C-8:C,15);G(A,B,C,13);}H(A,B,C,D){i=4;while(i--){F(A+(C&3),B+((C>>2)& |
| 7 | 3),D+9);C/=16;}}B(){for(i=0;i<378;++i)F(i%18,i/18+1,W[i/18][i%18]);if(h)H(x,1+y, |
| 8 | c,q),H(0,1,a,p);textprintf(b,font,4,4,15,"Lines: %d Score: %d / %d",l,s,v);blit( |
| 9 | b,screen,0,0,0,0,288,352);rest(50);}C(A){c=D[j[A]+r/2]>>(r%2)*16;}Z(){y=0;x=7;q= |
| 10 | p;p=rand()%7;r=0;d=10;C(p);a=c;C(q);}P(A){int C=c,D=0;for(i=0;i++<4;C>>=4){J=&W[ |
| 11 | y+(C>>2&3)][x+(C&3)];D+=A?*J=q+1:*J;}return D;}M(A,B){memset(W[A]+4,B,10);}main( |
| 12 | ){allegro_init();install_sound(-1,-1,0);srand(time(0));e=create_sample(8,0,22050 |
| 13 | ,8192);J=(char*)e->data;for(i=0;i<e->len;){n=(e->len-i++)/(float)e->len*255;m=m* |
| 14 | 0.75+(((rand()&255)/255.0)-0.5)/4.0;*(J++)=128+m*n;}v=get_config_int("s","h",0); |
| 15 | set_gfx_mode(GFX_GDI,288,352,0,0);install_keyboard();b=create_bitmap(288,352);Z( |
| 16 | );Z();memset(W,8,432);i=20;while(i--)M(i,0);while(h){_++;clear(b);X=x,Y=y,R=r,S= |
| 17 | s;h=(K=key)[59]?0:1;if(--d){if(_-k>2){z=0;if(K[83])x++;else if(K[82])x--;else if |
| 18 | (K[85])y++,s++;else z=1;k=z?0:k?k:_;}if(K[84]){if(!u)r=(r+1)%j[q+7],u=1;}else u= |
| 19 | 0;}else y++;C(q);if(P(0)){x=X,y=Y,r=R,s=S,C(q);if(!d){P(1);Z();g=10;S=20;while(S |
| 20 | --)if(!memchr(&W[S][4],0,10)){memmove(&W[1],W,S++*18);play_sample(e,255,55,1600, |
| 21 | 0);M(0,0);++l;s+=g;g*=10;B();rest(50);}if(P(0))h=0;}}else if(!d)d=(d=10-l/10)<2? |
| 22 | 2:d;if(s>v)v=s;B();}x=20;while(x--){M(x,8);B();}textout(screen,font,"Game Over", |
| 23 | 108,172,15);clear_keybuf();readkey();set_config_int("s","h",v);}END_OF_MAIN() |
I still have two lines left, but I don't really feel like adding anything else. So it appears that I can write 23 lines of code before getting tired of the project.
The game features awesome Tetris gameplay, sound effects*, and a high score.
* What a waste of a couple lines of code.
I'll try to come up with a more original entry in the next couple days.
I find it great that there has been quite a few neat little game ideas programmed, and I'm quite surprised at what can be achieved with the limitations set.
You guys might find this interesting, if you haven't seen it before. A basic version of Tetris in one line of BASIC (six or seven screen-lines, though, when wrapped at 80 col).
Here's code, just cause it's fun to look at:
0d=d:IFdVDUd:a=POINT(32*POS,31-VPOS<<5):RETURNELSEMODE9:GCOL-9:CLG:O FF:d=9:REPEATVDU30:REPEATGOSUBFALSE:IFPOS=28VDUPOS,15,VPOS,24;11,26:IF0E LSEIFa=0PRINT:UNTIL0ELSEUNTILVPOS=25:v=ABSRNDMOD7:i=0:VDU4895;3:REPEATm= 9-INKEY6MOD3:FORr=TRUETO1:t=rANDSGNt:IFt=rCOLOURv-15:VDUrEORm:i+=m=7AND9 -6*r:IF0ELSEFORn=0TO11:d=n/3OR2EORd:GOSUBFALSE:IF1<<(n+i)MOD12AND975AND& C2590EC/8^vVDU2080*ABSr;:t+=a:IF0ELSENEXT,:VDU20:UNTILt*LOGm:UNTILVPOS=3
I like Matthew's a whole lot more (totally awesome), but that's as small as I've ever seen the basic gameplay represented.
The clever part of Rheolism is how all seven shapes are encoded into a single 32-bit integer. If you can do that in a way that's easy to access, then the hard part is done.
I didn't try to do that because given 25 lines, it's not needed. For my final version, I just used a simple system:
0 1 2 3 4 5 6 7 8 9 A B C D E F
So the square would be 0x0145. This works nicely because it actually represents the offsets:
y x
0 => 00 00 => 0,0
1 => 00 01 => 0,1
4 => 01 00 => 1,0
5 => 01 01 => 1,1
When I need to test against a shape, I just read the right four bits as (y,x) then bit shift to get the next block.
Yes, I know my game doesn't work. No matter how hard I try, I can't get the collision code to register consistently.
Alright, I gave it a shot. Unfortunately, I'm not elite enough, because my code fits in a 80x31.
If I have time to cut out 400 characters somewhere, I might, but I probably won't as I have more important things to do. Still, doesn't mean I'm not going to do anything with the work I did (as it is a pretty solid game)! Wa-lah:
http://www.allegro.cc/files/attachment/595239
How to Play:
Water starts from the pipe on the green square. Place pipes without leaving any holes and connect a path to the blue square. You earn points for every second (more depending on which level you are on).
BUG Note: Make sure you don't have the cursor (red box) near where the water is filling. I becomes erratic and you will lose. 
Commands:
Arrows: Move the current pipe, red square shows position
Space Bar: Place the pipe at that point.
F Key: Hurry the water along (gives you extra points!)
The break in the code below is where I went over the limit.
| 1 | #include <allegro.h> /* Line Dreams by Onewing //////////////////////////////*/ |
| 2 | int st,ti;void t(){st++;}END_OF_FUNCTION(t);db(BITMAP *p){destroy_bitmap(p);} |
| 3 | main(){allegro_init();set_color_depth(8);int bs=91,ex,ey,ft=0,go=0,h=200,c=15,i, |
| 4 | it=0,j,l=0,next,sc=0,sct=0,sx,sy,w=300,wd,wt=0,wx,wy,x=0,y=0;BITMAP *g[10][10], |
| 5 | *b=create_bitmap(bs,bs),*b2=create_bitmap(bs,bs),*b3=create_bitmap(w,h+10),*p[8] |
| 6 | ;srand(time(0));install_sound(-1,-1,0);install_keyboard();set_gfx_mode(GFX_GDI,w |
| 7 | ,h+10,0,0);for(i=0;i<8;i++){p<i>=create_bitmap(9,9);clear(p<i>);}line(p[1],4,0,4 |
| 8 | ,9,c);BITMAP *cur=p[rand()%6+1],*n=p[rand()%6+1];line(p[2],0,4,9,4,c);line(p[3], |
| 9 | 4,0,4,4,c);line(p[3],4,4,9,4,c);install_int_ex(t,BPS_TO_TIMER(60));for(i=1;i<4; |
| 10 | i++)rotate_sprite(p[i+3],p[3],0,0,itofix(i*64));rectfill(p[7],0,0,9,9,3);while(! |
| 11 | key[59]&&!go){for(i=0;i<10;i++)for(j=0;j<10;j++)g<i>[j]=p[0];for(i=0;i<l;i++){g[ |
| 12 | rand()%8+1][rand()%8+1]=p[7];g[rand()%8+1][rand()%8+1]=p[rand()%6+1];}sx=sy=ex= |
| 13 | ey=1;while(sx!=0&&sy!=0){sx=rand()%9;sy=rand()%9;}while(ex!=9&&ey!=9){ex=rand()% |
| 14 | 10;ey=rand()%10;}wx=0;wy=0;if(sx==0){g[sx][sy]=p[2];wy=sy*9+4;}else{g[sx][sy]=p[ |
| 15 | 1];wx=sx*9+4;}if(ex==9)g[ex][ey]=p[2];else g[ex][ey]=p[1];wd=1000-(l*50);if(wd< |
| 16 | 200)wd=200;rectfill(b2,0,0,bs,bs,0);while(key[6]){}next=0;while(!key[59]&&!next) |
| 17 | {while(st){st--;ti++;if(key[75]&&g[x][y]==p[0]){g[x][y]=cur;cur=n;n=p[rand()%6+1 |
| 18 | ];}if(key[6]){wd=0;sc++;}if(ti-it>4){it=ti;if(x<9&&key[83])x++;if(x>0&&key[82]) |
| 19 | x--;if(y<9&&key[85])y++;if(y>0&&key[84])y--;}if(ti-sct>60){sct=ti;sc+=(l+1)*5;} |
| 20 | if(ti-wt>wd){wd=35;wt=ti;if(getpixel(b,wx-1,wy)==15)wx--;else if(15==getpixel(b, |
| 21 | wx+1,wy))wx++;else if(getpixel(b,wx,wy-1)==15)wy--;else if(getpixel(b,wx,wy+1)== |
| 22 | 15)wy++;else{if(wx>ex*9&&wx<ex*9+9&&wy>ey*9&&wy<ey*9+9)l++;else{textprintf( |
| 23 | screen,font,10,90,15,"Fin! N: New, Q: Quit");while(!key[14]&&!key[17]){}if(key[ |
| 24 | 14]){sc=0;l=0;ti=0;st=0;sct=0;}if(key[17])go=1;}next=1;}}}clear(b);clear(b3); |
| 25 | rectfill(b,sx*9,sy*9,sx*9+8,sy*9+8,10);rectfill(b,ex*9,ey*9,ex*9+8,ey*9+8,1);for |
| 26 | |
| 27 | (i=0;i<10;i++)for(j=0;j<10;j++)rect(b,i*9,j*9,i*9+9,j*9+9,3);rect(b,x*9,y*9,x*9+ |
| 28 | 9,y*9+9,4);draw_sprite(b,cur,x*9,y*9);for(i=0;i<10;i++)for(j=0;j<10;j++) |
| 29 | draw_sprite(b,g<i>[j],i*9,j*9);putpixel(b2,wx,wy,1);draw_sprite(b,b2,0,0); |
| 30 | textprintf_right(b3,font,w-20,0,14,"Lvl:%d Score:%d Next:",l+1,sc);draw_sprite( |
| 31 | b3,n,w-15,0);stretch_blit(b,b3,0,0,bs,bs,0,10,w,h);blit(b3,screen,0,0,0,0,w,h+10 |
| 32 | );}}db(b);db(b2);db(b3);for(i=0;i<5;i++)db(p<i>);}END_OF_MAIN() |
The full size game I am writing is 11,000 lines of code now.
Seeing all of these 80x25 games makes my code feel bloated now hehe.
If I ever need to conserve space in future work, I will have to reference some of these small games.
The coding reminds me of optimized sorting routines and how they do so much with so little.
Updated version. This is likely the last version of my Tetris and the end of my activity in this ScreenHack since I won't be writing another entry.
I did't feel like rewriting all the underlying code so I just patched it to have different colors, even though I'm not actually sure if it looks better or worse. Maybe it's just that I couldn't find a color combination that can be created on-the-fly and yet look good. Luckily I can revert it to the previous look by simply changing a few parameters.
Now the pieces fall slower and the velocity increases every 10 lines.
Also, hopefully all gameplay issues must be fixed now.
| 1 | #include <allegro.h> // SHTetris.c v.4 |
| 2 | #define f(i,x) for(i=0;i<x;i++) ///////////////// |
| 3 | #define B(x) PS(); stretch_blit(O,screen,0,0,128,200,0,0,256,400); rest(x) |
| 4 | #define RF(a,b,c,w) rectfill(O,xo+a+j*8,yo+b+y*8,xo+a+j*8+w,yo+b+y*8+w,c) |
| 5 | char ps[8][16]={0,15,0,0,2,2,2,2,0,15,0,0,2,2,2,2,2,2,3,0,7,4,0,0,3,1,1,0,1,7,0 |
| 6 | ,0,1,1,3,0,4,7,0,0,3,2,2,0,7,1,0,0,1,3,2,0,6,3,0,0,1,3,2,0,6,3,0,0,2,3,1,0,3,6, |
| 7 | 0,0,2,3,1,0,3,6,0,0,7,2,0,0,1,3,1,0,2,7,0,0,2,3,2,0,3,3,0,0,3,3,0,0,3,3,0,0,3,3 |
| 8 | ,0,0};int F[25],cF[25][16],i,j,p,q=0,m[4],x,y,w,a=0,o,no=0,d,e,g,k,rt,rtl,cf,h, |
| 9 | cm,rp,cr,xo,yo,l,S=0,tl=0,ll=0,P1=0xFFFF,is=64,cc=0x1FF4d4;BITMAP *O;UF(){l=0;f |
| 10 | (i,24)if(F<i>==P1){l++;DL(i,-1,-1,-1,0);B(50);DL(i,-1,cc<<4,-1,0);B(50);}f(i,24 |
| 11 | )if(F<i>==P1)f(j,i){F[i-j]=F[i-j-1];memcpy(cF[i-j],cF[i-j-1],64);}S+=pow(l,2)* |
| 12 | 1024;tl+=l;if((ll+=l)>=10){is-=is>6?4:0;ll-=10;}}DL(y,d,c,sc,u){f(j,16){if(u)cr |
| 13 | =cF[y][j]?cF[y][j]:c;else cr=c;if(d&1<<j){RF(1,1,cr,6);RF(3,3,cr<<1,4);}else if |
| 14 | (sc)RF(0,0,sc,8);}}PS(){textprintf_ex(O,font,17,0,-1,-1,"%10d",S);}C(h){cm=0;f( |
| 15 | i,4){m<i>=ps[p][o*4+i];cm=cm||F[h-i]&m<i><<x;}return cm;}main(){allegro_init(); |
| 16 | F[24]=-1;set_color_depth(24);set_gfx_mode(2,256,400,0,0);install_keyboard();f(i |
| 17 | ,24)F<i>=0xF003;O=create_bitmap(128,200);srand(time(NULL));while(!key[59]){if(! |
| 18 | a){p=q;q=rand()%7;o=no;no=rand()%4;d=0;a=1;x=6;y=3;w=6;}if(h>e){y++;h=0;}w=x;rp |
| 19 | =o;if(key[83]||key[82]){rt++;if(rt>rtl){rt=0;x+=key[83]?1:-1;rtl/=2;if(rtl<4) |
| 20 | rtl=4;}}if(key[75]&&!k){o=(o+1)%4;k=1;}if(!key[75])k=0;if(!key[83]&&!key[82]){ |
| 21 | rtl=32;rt=31;}if(key[85]){e=1;g=1;if(!d)S++;}if(!key[85]){e=is;g=48;}if(C(y-1)) |
| 22 | {x=w;o=rp;}if(!C(y)){d=0;h++;}else d++;if(d>g){f(i,4){F[y-i-1]|=m<i><<x;f(j,16) |
| 23 | if(m<i><<x&1<<j)cF[y-i-1][j]=(p+1)*cc;}a=0;if(y<4){rest(999);exit(0);}}xo=yo=0; |
| 24 | f(i,25)DL(i,F<i>,0xFFA6A6,99,1);f(i,4)DL(y-1-i,m<i><<x,(p+1)*cc,0,0);UF();xo=96 |
| 25 | ;yo=72;f(i,4){m<i>=ps[q][no*4+i];DL(5-i,m<i>,0xFFF,0,0);}B(15);}}END_OF_MAIN() |
{"name":"595251","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/6\/46723a2f8e48800a1482a96668210f46.gif","w":264,"h":433,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/6\/46723a2f8e48800a1482a96668210f46"}
Very simple pong clone, in only 16 lines of code, but with collision bug:
| 1 | #include <allegro.h> // Ultra simple pong clone in only 16 lines of code - v1 // |
| 2 | int rd(){return(rand()%2)?1:-1;}BITMAP*s;struct P{int x,y;static const int W=8,H |
| 3 | =64;P(int _x):x(_x),y((480-H)/2){}void m(int d){y+=d;y=MAX(0,y);y=MIN(y,480-H-1) |
| 4 | ;}void d(){rect(s,x,y,x+W-1,y+H-1,4);}};struct B{int x,y,dx,dy;static const int |
| 5 | S=8;B():x((640-S)/2),y((480-S)/2),dx(rd()),dy(rd()){}void idx(){dx*=-1;}void idy |
| 6 | (){dy*=-1;}void m(){x+=dx;y+=dy;}void d(){rect(s,x,y,x+S-1,y+S-1,14);}};int cc(B |
| 7 | &b,P&p){return!(b.y>p.y+P::H-1||b.y+B::S<p.y||b.x>p.x+P::W-1||b.x+B::S-1<p.x);} |
| 8 | int e=1;void cbh(){e=0;}int main(){srand(time(0));allegro_init();install_keyboa\ |
| 9 | rd();set_close_button_callback(cbh);set_gfx_mode(2,640,480,0,0);s=create_bitmap( |
| 10 | 640,480);int ls=0,rs=0;P pl(0),pr(640-P::W);B b;while(e){if(key[59])e=0;if(key[ |
| 11 | 23])pl.m(-1);if(key[19])pl.m(1);if(key[84])pr.m(-1);if(key[85])pr.m(1);b.m();if( |
| 12 | b.x<=0||b.x+B::S-1>=640){b.idx();if(b.x<=0)rs++;else if(b.x+B::S-1>=640)ls++;}if |
| 13 | (b.y<=0||b.y+B::S-1>=480)b.idy();if(cc(b,pl)||cc(b, pr))b.idx();clear_bitmap(s); |
| 14 | pl.d();pr.d();b.d();textprintf_centre_ex(s,font,640/2,1,7,-1,"%5i : %-5i",ls,rs) |
| 15 | ;blit(s,screen,0,0,0,0,640,480);rest(10);}destroy_bitmap(s);return 0;}END_OF_MA\ |
| 16 | IN();/////////////////////////////////////////////////////////////////////////// |
| 17 | //////////////////////////////////////////////////////////////////////////////// |
| 18 | /////////W A S T E D///S P A C E//////////////////////////////////////////////// |
| 19 | //////////////////////////////////////////////////////////////////////////////// |
| 20 | ////////////W A I T N G///T O///B E///////////////////////////////////////////// |
| 21 | //////////////////////////////////////////////////////////////////////////////// |
| 22 | ////////////////U S E D///////////////////////////////////////////////////////// |
| 23 | //////////////////////////////////////////////////////////////////////////////// |
| 24 | //////////////////////////////////////////////////////////////////////////////// |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
http://www.allegro.cc/files/attachment/595253
Awesome entries, everyone. Especially the tetris games blow my mind
Tomasz Grajewski, the pads are barely visible and it takes a bit too long for the ball to go from one side to the other at that speed.
Posting updated version:
faster gameplay (but still slow and easy)
pads and ball are better visible (I hope)
less code 
probably even more bugs to discover
| 1 | #include <allegro.h> // Ultra simple pong clone in only 14 lines of code - v2 // |
| 2 | BITMAP*s;int e,ls,rs;struct P{int x,y;static const int W=8,H=64;P(int z){x=z;y=( |
| 3 | 480-H)/2;}void m(int d){y+=d;y=0>?y;y=y<?480-H-1;}void d(){rectfill(s,x,y,x+W-1, |
| 4 | y+H-1,9);}}pl(0),pr(640-P::W);struct B{int x,y,dx,dy;static const int S=8;B(){x= |
| 5 | (640-S)/2;y=(480-S)/2;dx=dy=2;}void idx(){dx*=-1;}void idy(){dy*=-1;}void m(){x |
| 6 | +=dx;y+=dy;}void d(){rectfill(s,x,y,x+S-1,y+S-1,14);}}b;int cc(B&b,P&p){return!( |
| 7 | b.y>p.y+P::H-1||b.y+B::S<p.y||b.x>p.x+P::W-1||b.x+B::S-1<p.x);}int main(){alleg\ |
| 8 | ro_init();install_keyboard();set_gfx_mode(1,640,480,0,0);s=create_bitmap(640,480 |
| 9 | );while(!e){if(key[59])e=1;if(key[23])pl.m(-2);if(key[19])pl.m(2);if(key[84])pr. |
| 10 | m(-2);if(key[85])pr.m(2);b.m();if(b.x<1||b.x+B::S>640){b.idx();if(b.x<1)rs++;el\ |
| 11 | se if(b.x+B::S>640)ls++;}if(b.y<1||b.y+B::S>480)b.idy();if(cc(b,pl)||cc(b,pr))b. |
| 12 | idx();clear_bitmap(s);rect(s,0,0,639,479,8);pl.d();pr.d();b.d();textprintf_cent\ |
| 13 | re_ex(s,font,319,4,7,-1,"%5i : %-5i",ls,rs);blit(s,screen,0,0,0,0,640,480);rest( |
| 14 | 10);}destroy_bitmap(s);return 0;}END_OF_MAIN();///////////////////////////////// |
| 15 | //////////////////////////////////////////////////////////////////////////////// |
| 16 | /////////W A S T E D///S P A C E//////////////////////////////////////////////// |
| 17 | //////////////////////////////////////////////////////////////////////////////// |
| 18 | ////////////W A I T I N G///T O///B E/////////////////////////////////////////// |
| 19 | //////////////////////////////////////////////////////////////////////////////// |
| 20 | ////////////////U S E D///////////////////////////////////////////////////////// |
| 21 | //////////////////////////////////////////////////////////////////////////////// |
| 22 | ///////////////////B U T///I///H A V E///N O///I D E A S//////////////////////// |
| 23 | //////////////////////////////////////////////////////////////////////////////// |
| 24 | ///////////////////////T O///I M P L E M E N T////////////////////////////////// |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
That's better indeed Tomasz Grajewski. Well done.
Onewing, it seems you can't move after pressing 'N' to start a new game.
Oh, what the hell? This thread is made of total awesomeness and win!
I totally suck in doing things like this, the compo really ain't for me but hell, it seems so much fun that I have to give it a shot
No matter is it still on or not, this is just ... great! 
There should be more stuff like this.
I already got this idea in my head for an game snicker
It's disturbing how awesome entries there are already. Doing fine guys, keep at it.
Thread is still open, so this compo is still open. basically, if the thread gets closed, it's over, and voting will begin (I guess)
Keep it open! I want to do something for this, just haven't had time yet.
I already started mine yesterday at work, I just couldn't resist... 
Got the basic idea going. Don't have the time to do this on saturday(today) but I'll wrap it up tomorrow(sunday) I guess...
Nomnom! Everybody polishing their entries or lost their interest already?
Everybody polishing their entries or lost their interest already?
The second.
I actually sat down and worked on my old entry. I found a lot of places where I could save more space and I did add some basic syntax highlighting, but it wasn't really adding anything to the game
Everybody polishing their entries or lost their interest already?
The second.
I'm thinking about another attempt.
Too bad, I ended up messing my entry blargh... Would require some serious redoing of things if I wanted to get it in shape for real.
things that are missing:
Death
Proper collision detection for the bullets. Now it's just THROWN together...
Logic locks (but I guess nobody really has these? just rest(x)?
)
Redoing of the code so it would qualify for the 80x25
I'm not entirely sure does this qualify for an compo entry but here goes.
screenshot of it:
http://users.evtek.fi/~mikaka/temp/bShot.png
and here is the code:
| 1 | #include <allegro.h>//bShot something blargh //////////////////////////////// |
| 2 | volatile int eT=0;void eL(){eT++;}END_OF_FUNCTION(eL)R(x){return rand()%x;}D(x) |
| 3 | {double y;if(x==0)y=makecol(250,0,0);if(x==1)y=makecol(0,250,0);if(x==2)/////// |
| 4 | y=makecol(0,0,250);return y;}main(){srand(time(0));BITMAP *B;double e[30][2]=// |
| 5 | {{0,0}},b[2]={-5,200},h1=96,h2=100,pC,bC,eC[30];int A[30]={0},t,n,C=0,S=0,u=0,L |
| 6 | =1,w1=25,w2=29;allegro_init();install_keyboard();install_timer();install_int(eL |
| 7 | ,100);set_color_depth(32);set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,200,0,0);B= |
| 8 | create_bitmap(640,200);LOCK_FUNCTION(eL);while(!key[KEY_ESC]){clear_bitmap(B); |
| 9 | rect(B,20,-1,34,201,makecol(255,255,0));if(key[KEY_A])C=0;if(key[KEY_S])C=1;if( |
| 10 | key[KEY_D])C=2;pC=D(C);if(b[0]>0||b[1]<200){b[0]-=2.5;b[1]+=2.5;circle(B,26,b[0 |
| 11 | ],1,bC);circle(B,26,b[1],1,bC);}else{if(key[KEY_F]){bC=pC;b[0]=h2-2;b[1]=h1+2;} |
| 12 | }if(key[KEY_M]&&h2<195)h1+=0.5;if(key[KEY_K]&&h1>0)h1-=0.5;if(S>300){w1=20;w2= |
| 13 | 34;h2=h1+14;L=6;if(eT>0){t=0;eT=0;u=1;}}if(S>200&&S<=300){w1=21;w2=33;h2=h1+12; |
| 14 | L=5;if(eT>=2){t=0;eT=0;u=1;}}if(S>150&&S<=200){w1=22;w2=32;h2=h1+10;L=4;if(eT>= |
| 15 | 4){t=0;eT=0;u=1;}}else if(S>100&&S<=150){w1=23;w2=31;h2=h1+8;L=3;if(eT>=6){t=0; |
| 16 | eT=0;u=1;}}else if(S>50&&S<=100){w1=24;w2=30;h2=h1+6;L=2;if(eT>=8){t=0;eT=0;u=1 |
| 17 | ;}}else if(S<=50){h2=h1+4;L=1;if(eT>=10){t=0;eT=0;u=1;}}if(u){for(n=0;n<30;n++) |
| 18 | {if(!A[n]&&!t){e[n][0]=645;e[n][1]=R(195);A[n]=1;t=1;eC[n]=D( R(3) );}}}for(n=0 |
| 19 | ;n<30;n++){if(((e[n][1]+2.5>b[0]&&e[n][1]+2.5<b[0]+5)||(e[n][1]+2.5>b[1]&&e[n][ |
| 20 | 1]+2.5<b[1]+5))&&(e[n][0]+2.5>25&&e[n][0]+2.5<30)){if(bC==eC[n]){A[n]=0;S+=5;}} |
| 21 | if(A[n]&&e[n][0]>-5){ e[n][0]--;rectfill(B,e[n][0],e[n][1],e[n][0]+5,e[n][1]+5, |
| 22 | eC[n]);}else A[n]=0;}textprintf_ex(B,font,560,5,makecol(250,250,0),-1,"Scr: %d" |
| 23 | ,S);textprintf_ex(B,font,560,185,makecol(250,250,0),-1,"Lvl: %d",L);rectfill(B, |
| 24 | w1,h1,w2,h2,pC);blit(B,screen,0,0,0,0,640,200);rest(10);}}END_OF_MAIN()//////// |
| 25 | ////WASTED SPACE... NOT ENOUGH ROOM FOR PLAYER DEATH :( /////////////////////// |
It really did seem much more awesome in my head than it ended up being. I guess it's a pathetic attempt at some sort of shoot em up/rhythm game without music. How do you like that? 
You can get the exe file from here with the alleg42.dll. Only for Win.
Controls:
A - change color to red
S - change color to green
D - change color to blue
F - fire up and down
K - move up
M - move down
Game idea:
Dodge enemies and shoot them with the same color bullets. Red bullets kill red enemies and so on... Since the player death is not implemented, you don't even have to dodge the enemies. Yahoo~. Endless ... "fun"... Ugh... As the game does on, you grow bigger and the enemies are supposed to grow bigger, the dodging gets harder, or atleast SHOULD get harder but really doesn't since you can't die 
I had my share of fun tinkering with this. Also, since it really doesn't have any sort of "logic locks", only the rest(10), the results are that the game performance will vary on different machines ALOT. On some it's playable and on some it's not. Too bad. Also since the bullet collision ain't that good, it might miss some of the hits you do 
Another thing I again noticed was that good variable names are awesome. I ended up doing alot of one letter/two letter variables to save up space and other even more stupid things to save up chars. Man this compo was nuts.
Oh yeah, final note: guys, your entries are awesome. I can't believe you came up with stuff like that. It really shows that in a small compo like this, the game idea is everything.
I've made another pong version, this time more playable and challenging:
http://www.allegro.cc/files/attachment/595282
| 1 | #include <math.h> |
| 2 | #include <allegro.h> // Ultra simple pong clone in 20 lines of code - v3 // |
| 3 | BITMAP*s;int e,ls,rs;struct B{int r,c;float x,y,vx,vy;B(){r=8;c=3;x=s->w/2;y=s-> |
| 4 | h/2;vx=(rand()%2)?-4:4;vy=0;}virtual void m(){x+=vx;y+=vy;if(x<r){x=r;vx*=-1;rs |
| 5 | ++;}if(x>s->w-r){x=s->w-r;vx*=-1;ls++;}if(y<r){y=r;vy*=-1;}if(y>s->h-r){y=s->h-r |
| 6 | ;vy*=-1;}}virtual void d(){circlefill(s,(int)x,(int)y,r,c);}};struct P:public B{ |
| 7 | P(int _x){r=96;c=5;x=_x;y=s->h/2;vx=vy=0;}virtual void m(){x+=vx;y+=vy;if(y<0)y= |
| 8 | 0;if(y>s->h-1)y=s->h-1;}};void cc(B&b,P&p){int cd=b.r+p.r;float ad=sqrt((p.x-b.x |
| 9 | )*(p.x-b.x)+(p.y-b.y)*(p.y-b.y));if(ad<cd){float na=atan2(p.y-b.y,p.x-b.x),md=cd |
| 10 | -ad;b.x+=md*cos(na+M_PI);b.y+=md*sin(na+M_PI);float nx=cos(na),ny=sin(na),a1=b. |
| 11 | vx*nx+b.vy*ny,a2=p.vx*nx+p.vy*ny,op=a1-a2;b.vx=-op*nx;b.vy=-op*ny;}}int main(){ |
| 12 | srand(time(0));allegro_init();install_keyboard();set_gfx_mode(1,640,480,0,0);s= |
| 13 | create_bitmap(SCREEN_W,SCREEN_H);B b;P pl(-96+15),pr(s->w+96-16);while(!e){if(k\ |
| 14 | ey[59])e=1;if(key[KEY_S])pl.vy=2;else if(key[KEY_W])pl.vy=-2;else pl.vy=0;if(key |
| 15 | [KEY_DOWN])pr.vy=2;else if(key[KEY_UP])pr.vy=-2;else pr.vy=0;b.m();pl.m();pr.m() |
| 16 | ;cc(b,pl);cc(b,pr);clear_to_color(s,15);vline(s,s->w/2-2,0,s->h-1,11);vline(s,s |
| 17 | ->w/2-1,0,s->h-1,11);b.d();pl.d();pr.d();textprintf_centre_ex(s,font,s->w/2-1,4, |
| 18 | 1,-1,"%3i : %-3i",ls,rs);blit(s,screen,0,0,0,0,s->w,s->h);rest(10);}destroy_bit\ |
| 19 | map(s);return 0;}END_OF_MAIN()////////////////////////////////////////////////// |
| 20 | //////////////////////////////////////////////////////////////////////////////// |
| 21 | //////////////////////////////////////////////////////////////////////////////// |
| 22 | //////////////////////////////////////////////////////////////////////////////// |
| 23 | //////////////////////////////////////////////////////////////////////////////// |
| 24 | //////////////////////////////////////////////////////////////////////////////// |
Structures, variables with more than one letter, ... you guys are wasteful!
Structures, variables with more than one letter, ... you guys are wasteful!
Yeah, I know, now I'm trying to minimalise mine code.
BTW, love C++ for things like that:
From
if(y<r) { y=r; vy*=-1; } if(y>s->h-r) { y=s->h-r; vy*=-1; }
to
y=y<r?vy*=-1,r:y>s->h-r?vy*=-1,s->h-r:y;
Four bytes saved
Does C++ explicitly define what happens in the case of:
int a = b ? c, d : e, f;
?
Does C++ explicitly define what happens in the case of:
int a = b ? c, d : e, f;
?
The comma separated values are evaluated from left to right, so d or f will be returned for a, if this is what you ask for. The ternary conditional is evaluated from right to left.
I was just wondering if the commas are well defined sequence points in that example, but I suppose they are. They aren't in function calls (e.g.: f(a(), b(), c())), but obviously that's a different situation.
Næssén and Ted, your game is awesome. 80x25 or not, that game is really good.
-edit-
I haven't played cursor*10 before, so part of the reason I like this game is that it seemed so original and innovative. Now that I've tried cursor*10 this game doesn't seem as original (obviously) but I still prefer this one.
Tasty update of not so tasty "game" ... 
| 1 | #include <allegro.h>//ScreenHack08 entry - bShot/////////////////////////////// |
| 2 | volatile int eT=0,gT=0,gU;eL(){eT++;gT++;gU=1;}END_OF_FUNCTION(eL)R(x){return |
| 3 | rand()%x;}D(x){double y;if(x==0)y=makecol(250,0,0);if(x==1)y=makecol(0,250,0); |
| 4 | if(x==2)y=makecol(0,0,250);return y;}main(){srand(time(0));BITMAP *B;double e[ |
| 5 | 60][2]={{0,0}},b[2]={-5,200},h1=96,h2=100,pC,bC,eC[60];int A[60]={0},t,n,C=0,S= |
| 6 | 0,u=0,L=1,w1=20,w2=34,eu=14,pA=1;allegro_init();install_keyboard();install_int( |
| 7 | eL,100);set_color_depth(32);B=create_bitmap(640,200);B=create_bitmap(640,200); |
| 8 | LOCK_FUNCTION(eL);LOCK_VARIABLE(eT);LOCK_VARIABLE(gT);LOCK_VARIABLE(gU); |
| 9 | set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,200,0,0);while(!key[KEY_ESC]){ |
| 10 | clear_bitmap(B);if(pA){rect(B,20,-1,34,201,makecol(255,255,0));if(key[KEY_A])C= |
| 11 | 0;if(key[KEY_S])C=1;if(key[KEY_D])C=2;pC=D(C);if(b[0]>0||b[1]<200){b[0]-=2.5;b[ |
| 12 | 1]+=2.5;circle(B,26,b[0],1,bC);circle(B,26,b[1],1,bC);}else{if(key[KEY_F]){bC= |
| 13 | pC;b[0]=h2-2;b[1]=h1+2;}}if(key[KEY_M]&&h2<195)h1+=0.5;if(key[KEY_K]&&h1>0)h1-= |
| 14 | 0.5;if(gT%100==0&&gU){if(eu-1>=1){eu-=1;gU=0;gT=0;L++;}}if(S>300)h2=h1+18;else |
| 15 | if(S>250)h2=h1+15;else if(S>200)h2=h1+13;else if(S>150)h2=h1+11;else if(S>100) |
| 16 | h2=h1+9;else if(S>50)h2=h1+7;else h2=h1+4;if(eT>=eu){t=0;eT=0;u=1;}if(u){for(n= |
| 17 | 0;n<60;n++){if(!A[n]&&!t){e[n][0]=645;e[n][1]=R(195);A[n]=1;t=1;eC[n]=D(R(3));} |
| 18 | }}for(n=0;n<60;n++){if(((e[n][1]+2.5>b[0]&&e[n][1]+2.5<b[0]+5)||(e[n][1]+2.5>b[ |
| 19 | 1]&&e[n][1]+2.5<b[1]+5))&&(e[n][0]+2.5>25&&e[n][0]+2.5<30)){if(bC==eC[n]){A[n]= |
| 20 | 0;S+=5;}}if(A[n]&&e[n][0]>-5){e[n][0]--;rectfill(B,e[n][0],e[n][1],e[n][0]+5,e[ |
| 21 | n][1]+5,eC[n]);}else A[n]=0;if(e[n][0]<w2&&e[n][0]>w1&&e[n][1]>h1&&e[n][1]<h2) |
| 22 | pA=0;if(e[n][0]<w2&&e[n][0]>w1&&e[n][1]+5>h1&&e[n][1]+5<h2)pA=0;}textprintf_ex( |
| 23 | B,font,200,190,makecol(250,250,0),-1,"Scr: %d/Lvl: %d",S,L);rectfill(B,w1,h1,w2 |
| 24 | ,h2,pC);}else{textprintf_ex(B,font,100,90,makecol(250,250,0),-1,"Scr:%d/Lvl:%d" |
| 25 | ,S,L);}blit(B,screen,0,0,0,0,640,200);rest(10);}}END_OF_MAIN() |
Compiling the code gives pile of warnings since I had to cut through loads of things to even get this much done... I know there is room for even more improvements but the current improvements popped into my head last night.
Suddenly during the night I just got these ideas how to replace stuff and how to do things a bit differently to save even more space.
Added features:
Death! Long waited player death. Now you can die. Start dodging 
After small period of time the enemy spawn rate starts getting higher and higher.
Scoring points grows your "vessel" bigger.
Things I should redo:
Proper collision for bullets, it's annoyingly hard to hit the enemies.
And change to different kind of bullets for easier hitting too.
I don't know, it's still not good but heyh.. got atleast something done. Might do even more updates on it. Don't know at the moment.
Oh yeah, and you feel like just playing it, you can get the updated exe + alleg42.dll from here.
Here's my second game. It's a side scroller shoot-em-up. You're the green triangle, and you must avoid the grey cavern walls and shoot as many yellow orbs as you can before you crash.
Control is with the mouse, left button to fire.
| 1 | #include <allegro.h>////////////Side scrolling shooter by HardTranceFan///////// |
| 2 | int i,j,h,w=200,d=-1,c=0,e=1,o=0,n=1,p=1,l=0,g=13158600,Y=13158400,mx,my,f=0,z,s |
| 3 | =0,oh,lh,dh,bx=-1,by=-1;BITMAP *b,*k;int q(int x,int y){return _getpixel32(k,x+o |
| 4 | ,y);}void m(int x,int y){if(q(x,y)==Y){_putpixel32(k,x+o,y,0);m(x,y+1);m(x,y-1); |
| 5 | m(x-1,y);m(x+1,y);}}void v(int x,int y){if((++c>(rand()%20)+15)||h<15||h>465-w){ |
| 6 | d*=-1;c=0;}h+=(rand()%10)*d;if(h>oh){triangle(k,x,oh,y,h,x,h,0);triangle(k,x,oh+ |
| 7 | w,y,oh+w,y,h+w,0);lh=h;dh=h-oh;}else{triangle(k,x,oh,y,oh,y,h,0);triangle(k,x,oh |
| 8 | +w,x,h+w,y,h+w,0);lh=oh;dh=oh-h;}rectfill(k,x,lh,y,lh+w-dh,0);oh=h;}int T(){//// |
| 9 | return q(mx-5,my-5)|q(mx+5,my)|q(mx-5,my+5);}int B(){if(bx<0)return 0;return q( |
| 10 | bx,by);}main(){allegro_init();install_keyboard();install_mouse();set_color_depth |
| 11 | (32);set_gfx_mode(2,640,480,0,0);scare_mouse();b=create_bitmap(640, 480);oh=h;h= |
| 12 | 140;k=create_bitmap(650, 480);clear_to_color(k,g);for(i=0;i<65;i++)v(i*10,i*10+ |
| 13 | 10);l=p;position_mouse(20,240);while(f!=2){if(!f){poll_mouse();mx=mouse_x;l--;my |
| 14 | =mouse_y;if(!l){l=p;o=(o+e);if(o>10){o=0;blit(k,b,10,0,0,0,640,480);blit(b,k,0,0 |
| 15 | ,0,0,640,480);rectfill(k,640,0,650,480,g);v(640,650);if(!(++n%30))w=(w-5<50)?49: |
| 16 | w-5;else if(!(n%200))e++;}if(!(rand()%100))circlefill(k,644,rand()%w+h,5,Y);}if( |
| 17 | bx<0){if(mouse_b&1){bx=mx+2;by=my;}}else bx+=2;if(bx>640)bx=-1;}blit(k,b,o,0,0,0 |
| 18 | ,640,480);if(T())f=1;if(B()){s+=10;m(bx,by);bx=-1;}if(key[KEY_ESC])f=2;triangle( |
| 19 | b,mx-5,my-5,mx+5,my,mx-5,my+5,51200);if(bx>0)circlefill(b,bx,by,2,200);///////// |
| 20 | textprintf_ex(b,font,10,460,0,-1,"Score: %d",s);if(f)textprintf_ex(b,font,280,// |
| 21 | 235,16711680,-1,"You crashed.");blit(b,screen,0,0,0,0,640,480);rest(1);}}/////// |
| 22 | END_OF_MAIN()/////////////////////////////////////////////////////////////////// |
| 23 | //////////////////////////////////////////////////////////////////////////////// |
| 24 | /////////////ZOMG!!!! Spare space!!! What a waste of [binary] trees!!/////////// |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
{"name":"595290","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/8\/b85d32d32803630ad64ce5068b61d9a6.jpg","w":645,"h":504,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/8\/b85d32d32803630ad64ce5068b61d9a6"}
ZOMG!!!! Spare space!!! What a waste of [binary] trees!!
ZOMG! What a waste of corn!
Attached is a little tool that takes some of the tedious work off your shoulders.
Reads a C or C++ source file, removes all unnecessary stuff (whitespace unless absolutely needed, comments), renames all identifiers that start with a dollar sign into the shortest possible set of identifiers (first a single underscore, then a through z, then A through Z, then combinations of these plus digits; most-often-used identifiers get the shortest names), replaces character literarals by their numeric counterparts (because for everything below 100 the latter are shorter, above 100 they're equally long), formats the whole thing to max. 80 characters wide, and sends it to stdout.
1. Unzip into a folder of your choice.
2. Compile using something like g++ *.cpp -o compress
3. In your code, prepend all of your own identifiers you want replaced (types, variables, functions) with a $. Identifiers without a $ will be left alone.
4. Assuming your original, unoptimized source file is foo.c, use something like this: compress foo.c > foo_compressed.c
For example, my attempt at (yet another) snake:
| 1 | #include <allegro.h> |
| 2 | #define c(cond, code) case cond: code; break; |
| 3 | |
| 4 | typedef int $var; |
| 5 | |
| 6 | $var $val1024 = 1024; |
| 7 | $var $val256 = 256; |
| 8 | $var $val32 = 32; |
| 9 | $var $backbuf; |
| 10 | $var $head = 1; |
| 11 | $var $tail = 0; |
| 12 | $var $tar_length = 2; |
| 13 | $var $cur_length = 2; |
| 14 | $var $dir = 1; |
| 15 | $var $part_xy[32] = { 527, 528, 0 }; |
| 16 | $var $coinpos=0; |
| 17 | $var $running=1; |
| 18 | $var $newhead; |
| 19 | $var $paused = 1; |
| 20 | $var $speed = 1; |
| 21 | $var $score = 0; |
| 22 | |
| 23 | $var $multifunc0($var $func) { |
| 24 | switch ($func) { |
| 25 | c(0, $coinpos = rand() % $val1024) |
| 26 | c(1, return readkey() % $val256) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | $var $drawpixel($var $xy, $var $color) { |
| 31 | putpixel($backbuf, $xy % $val32, $xy >> 5, $color); return $color; |
| 32 | } |
| 33 | |
| 34 | $var $readpixel($var $xy) { |
| 35 | return getpixel($backbuf, $xy % $val32, $xy >> 5); |
| 36 | } |
| 37 | |
| 38 | $var $set_head_or_tail($ht, $color) { |
| 39 | return |
| 40 | $drawpixel($part_xy[$ht], |
| 41 | $color); |
| 42 | } |
| 43 | |
| 44 | $var $display_number($var $label, $var $number, $var $pos, $var $color) { |
| 45 | textprintf_ex(screen, font, 0, $pos*9, $color, -1, "%s: %i", $label, $number); |
| 46 | } |
| 47 | |
| 48 | $var main() { |
| 49 | // $var $i; |
| 50 | |
| 51 | allegro_init(); |
| 52 | set_gfx_mode(2, $val256, $val256, 0, 0); |
| 53 | install_keyboard(); |
| 54 | $backbuf = create_bitmap($val32, $val32); |
| 55 | clear($backbuf); |
| 56 | |
| 57 | /* |
| 58 | for ($i = 0; $i < 16; ++$i) |
| 59 | rectfill(screen, $i * 8, 0, ($i + 1) * 8, 8, $i); |
| 60 | readkey(); |
| 61 | */ |
| 62 | |
| 63 | // $coinpos = rand() % $val1024; |
| 64 | $multifunc0(0); |
| 65 | |
| 66 | $set_head_or_tail($head, |
| 67 | $set_head_or_tail($tail, |
| 68 | 2)); |
| 69 | $drawpixel($coinpos, 6); |
| 70 | |
| 71 | while ($running) { |
| 72 | stretch_blit($backbuf, screen, 0, 0, $val32, $val32, 0, 0, $val256, $val256); |
| 73 | $display_number("Length", $cur_length, 0, 7); |
| 74 | $display_number("Speed", $speed, 1, 7); |
| 75 | $display_number("Score", $score, 2, 7); |
| 76 | if ($paused) |
| 77 | // { readkey(); $paused = 0; } |
| 78 | $paused = ($multifunc0(1)&$val1024); |
| 79 | |
| 80 | rest($val1024 / ($speed + 1)); |
| 81 | for (;keypressed();) { |
| 82 | // switch (readkey() % $val256) { |
| 83 | switch ($multifunc0(1)) { |
| 84 | c(27,$running=0) |
| 85 | c('a',$dir=-1) |
| 86 | c('d',$dir=1) |
| 87 | c('w',$dir=-$val32) |
| 88 | c('s',$dir=$val32) |
| 89 | } |
| 90 | } |
| 91 | $newhead = ($head + 1) % $val32; |
| 92 | $var $newpos = $part_xy[$newhead] = $part_xy[$head] + $dir; |
| 93 | switch ($readpixel($newpos)) { |
| 94 | case 6: |
| 95 | $score += $speed * $tar_length; |
| 96 | if (++$tar_length == $val32) { |
| 97 | clear($backbuf); |
| 98 | $head = |
| 99 | $dir = 1; |
| 100 | $tail = 0; |
| 101 | $tar_length = |
| 102 | $cur_length = 2; |
| 103 | $part_xy[0] = 527; |
| 104 | $part_xy[1] = 528; |
| 105 | $set_head_or_tail($head, |
| 106 | $set_head_or_tail($tail, |
| 107 | 2)); |
| 108 | ++$speed; |
| 109 | $paused = 1; |
| 110 | } |
| 111 | for ($multifunc0(0); $readpixel($coinpos); $multifunc0(0)) |
| 112 | ; |
| 113 | $drawpixel($coinpos, 6); |
| 114 | break; |
| 115 | c(0, ) // empty cell: pass through |
| 116 | default: $running=0; |
| 117 | } |
| 118 | $head = $newhead; |
| 119 | $set_head_or_tail($head, 2); |
| 120 | if ($cur_length < $tar_length) { |
| 121 | ++$cur_length == 32; |
| 122 | } |
| 123 | else { |
| 124 | $set_head_or_tail($tail, 0); |
| 125 | $tail = ($tail + 1) % $val32; |
| 126 | } |
| 127 | } |
| 128 | $display_number("Game Over. Score", $score, 9, 13); |
| 129 | |
| 130 | // readkey(); |
| 131 | $multifunc0(1); |
| 132 | } |
| 133 | END_OF_MAIN() |
...is converted into this cryptic mess:
| 1 | #include <allegro.h> |
| 2 | #define c(cond, code) case cond: code; break; |
| 3 | typedef int _;_ u=1024;_ k=256;_ a=32;_ d;_ b=1;_ g=0;_ p=2;_ n=2;_ f=1;_ l[32]= |
| 4 | {527,528,0};_ m=0;_ r=1;_ w;_ t=1;_ q=1;_ s=0;_ i(_ y){switch(y){c(0,m=rand()%u) |
| 5 | c(1,return readkey()%k)}}_ v(_ j,_ e){putpixel(d,j%a,j>>5,e);return e;}_ x(_ j){ |
| 6 | return getpixel(d,j%a,j>>5);}_ h(z,e){return v(l[z],e);}_ o(_ A,_ C,_ B,_ e){ |
| 7 | textprintf_ex(screen,font,0,B*9,e,-1,"%s: %i",A,C);}_ main(){allegro_init(); |
| 8 | set_gfx_mode(2,k,k,0,0);install_keyboard();d=create_bitmap(a,a);clear(d);i(0);h( |
| 9 | b,h(g,2));v(m,6);while(r){stretch_blit(d,screen,0,0,a,a,0,0,k,k);o("Length",n,0, |
| 10 | 7);o("Speed",q,1,7);o("Score",s,2,7);if(t)t=(i(1)&u);rest(u/(q+1));for(; |
| 11 | keypressed();){switch(i(1)){c(27,r=0)c(97,f=-1)c(100,f=1)c(119,f=-a)c(115,f=a)}} |
| 12 | w=(b+1)%a;_ D=l[w]=l<b>+f;switch(x(D)){case 6:s+=q*p;if(++p==a){clear(d);b=f=1;g |
| 13 | =0;p=n=2;l[0]=527;l[1]=528;h(b,h(g,2));++q;t=1;}for(i(0);x(m);i(0));v(m,6);break |
| 14 | ;c(0,)default:r=0;}b=w;h(b,2);if(n<p){++n==32;}else{h(g,0);g=(g+1)%a;}}o( |
| 15 | "Game Over. Score",s,9,13);i(1);}END_OF_MAIN() |
BTW, that game lacks some very basic features like edge collision (or proper wrapping), but hey, I still have 10 lines left.
-- edit --
Oh, and one more thing: C has this "nice" feature called "weak typing", which enables you to do things like this:
int b = create_bitmap(256, 256); int c = "Hello world!";
In other words, pretty much anything can be an int.
I just edit my code exactly as you see in the 80x25 box. 
I never use whitespace anyway since I end up just sitting there all day wondering if I should use tabs or spaces.
You mean like this?
No more entries? Some one needs to take us into the 3rd dimension with ray casting Castle Wolfenstein in 80x25....
Given this thread is still open, I may have a crack at another idea...
A raycaster in 80x25?
This weekend I tried to turn my Tetris into 3D, but the Allegro 3D functions have long names and you have to call plenty of them, plus the vertex definitions and it easily went beyond the 25 lines. Even blitting from the buffer to the screen was annoying and the result wasn't even that great, not to mention how much slower it became, so I did quit.
Screenshot:
{"name":"595366","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/1\/51bbe58adc86990fb5c2869534fd3d93.gif","w":263,"h":431,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/1\/51bbe58adc86990fb5c2869534fd3d93"}
No more entries? Some one needs to take us into the 3rd dimension with ray casting Castle Wolfenstein in 80x25....
Didn't Krzysztof Kluczek do exactly that for the previous ScreenHack?
I think he did 'ray tracing' in one line actually. For raycasting, you only need to draw a line... repeatedly.
Bit late, but here's what I've worked on today and yesterday:
| 1 | #include<allegro.h>/* Breakout for Allegro ScreenHack 2008 - By James Stanley */ |
| 2 | float x,y,m,X,Y;int b,w=100,B[24],i,l=1,L=3,c,C[5],d;r(){x=160;y=180;X=-.5,Y=-.5 |
| 3 | ;b=160;}R(int a, int e, int f,int g, unsigned int h){rectfill(c,a,e,f,g,h);}a(){ |
| 4 | return b-w/2;}e(){return b+w/2;}o(){return((i%4)*80)+5;}p(){return ((i/4)*20)+5; |
| 5 | }main(){allegro_init();install_keyboard();set_color_depth(16);set_gfx_mode(2,320 |
| 6 | ,200,320,200);c=create_bitmap(320,200);for(i=0;i<5;i++){C<i>=makecol((125-i*25)+ |
| 7 | 100,l*40,0);}for(i=0;i<24;i++){B<i>=i/5;}r();while(!key[59]){clear_to_color(c,0) |
| 8 | ;if(key[82])b-=2;if(key[83])b+=2;if(a()<0)b=w/2;if(e()>320) b=320-w/2;x+=X;y+=Y; |
| 9 | if(x<4){x=4;X=-X;}if(y<4){y=4;Y=-Y;}if(x>316){x=316;X=-X;}if(y>180){if(x>a()&&(x |
| 10 | <e())){Y=-Y;y=180;if(key[82])X-=.1;if(key[83])X+=.1;}else if(((x+4)>a())&&((x-4) |
| 11 | <e())){X=-X;Y=-Y;y=180;if(key[82])X-=.2;if(key[83])X+=.2;}}if(y>204){L--;r();}d= |
| 12 | 0;for(i=0;i<24;i++){if(B<i><5){d=1;if((x>(o()+70))&&((x-8)<(o()+70))){if((y<(p() |
| 13 | +10))&&(y>p())){X=-X;x=o()+78;B<i>++;}else if(((y+8)>p())&&(y<p())){m=X;X=Y;Y=m; |
| 14 | B<i>++;}else if(((y-8)<(p()+10))&&(y>(p()+10))){m=X;X=-Y;Y=-m;B<i>++;}}else if(( |
| 15 | x<o())&&((x+8)>o())){if((y<(p()+10))&&(y>p())){X=-X;x=o()-8;B<i>++;}else if(((y+ |
| 16 | 8)>p())&&(y<p())){m=X;X=-Y;Y=-m;B<i>++;}else if(((y-8)<(p()+10))&&(y>(p()+10))){ |
| 17 | m=X;X=Y;Y=m;B<i>++;}}else if(((y+8)>p())&&(y<p())){if((x>o())&&(x<(o()+70))){Y=- |
| 18 | Y;y=p()-8;B<i>++;}}else if(((y-8)<(p()+10))&&(y>(p()+10))) {if((x>o())&&(x<(o()+ |
| 19 | 70))){Y=-Y;y=p()+18;B<i>++;}}R(o(),p(),o()+70,p()+10,C[B<i>]);}}if(!d){l++;for(i |
| 20 | =0;i<5;i++){C<i>=makecol((i*25)+100,l*40,0);}for(i=0;i<24;i++){B<i>=i/5;}L++;r() |
| 21 | ;w*=.8;}textprintf_ex(c,font,4,120,-1,-1,"Lives: %d Level: %d",L,l);R(x-4,y-4 |
| 22 | ,x+4,y+4,-1); R(a(), 184, e(),196,-1); blit(c, screen, 0,0,0,0,320,200);if(L<0){ |
| 23 | textout_centre_ex(c,font,"G A M E O V E R",160,100,-1,-1);while(!key[59])blit( |
| 24 | c,screen,0,0,0,0,320,200);return 0;}}}END_OF_MAIN()/*Only 24 lines, never mind*/ |
Controls are Left and Right arrow. ESC to quit.
http://stanley.homelinux.org/images/80x25.png
EDIT:
Should've said: there is no timing.
Run a busy wait in the background if it's too fast
.
Hmm...I'll play!
I can't really work on it until I get home and don't have an idea anyway, but I have been thinking about some shortening tricks I haven't seen anyone do yet.
In particular, I haven't seen anyone do something like this:
#define I int I(*DS)(I,I,I,I)=draw_sprite;I(*CB)(I,I)=create_bitmap; /* and later on in your code */ BITMAP *b; b=CB(64,64);DS(screen,b,1,1);
If you have a lot of calls to draw_sprite() or create_bitmap(), this might be a pretty significant space savings. This should save people from burning the first few lines on #defines (a cautionary note: this WILL generate warnings, at least with gcc)
May entries assume that they'll be run from the same directory the source file is in? I have some asset generation stupidity in mind...
#define I int
A waste of space. You lose 80 characters to save 2 bytes. Are you going to use int over 40 times? All variables should be global and then you only need one int. And all functions default to returning int with int parameters.
Almost every #define is a waste.
I(*DS)(I,I,I,I)=draw_sprite;I(*CB)(I,I)=create_bitmap;
Again, how often do you really call those functions? I created a Tetris game with a single rectfill and a single blit.
I actually have something somewhat sprite-heavy in mind; there might be a lot of calls to draw_sprite() (although the same trick would work for any routine with a long name and argument list that gets called a lot). I'm not sure if this will save me space or not, although it seems like it might (and yes, I agree that #defines are a huge space waster). I didn't know that all functions defaulted to int arguments, though, that might come in handy.
I haven't quite cooked up a strategy for encoding stick figure animations yet; the game I'm envisioning might not even be possible...
Somoene needs to gather all the programs and put up a nice page on the wiki or some other site
Maybe for all of the screenhacks as well.