Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » An Allegro Game in 20 lines of code - Possible?

This thread is locked; no one can reply to it. rss feed Print
An Allegro Game in 20 lines of code - Possible?
Matthew Leverton
Supreme Loser
January 1999
avatar

You can count headers and I'm still under 20. But that 80 character count per line is bogus.

Thomas Fjellstrom
Member #476
June 2000
avatar

uuugghhh... Stupid computer. Stupid cat... Just finished writing the previous version of this message when XP decides to blue screen. SOme crappy error about IRQL_NOT_LESS_OR_EQUAL_TO or something. Soo, as with last time I seen that I opened up my case to see whats up... And I tell ya, a half hout later, I was finaly finished putting it all back to gether after ripping everything out to clean. IT was not a pretty sight. I swear there was enough cat fur in there to create another cat. ;D

Quote:

*cough* maybe we should stop discussing DSP in this thread

email!

;)

Oh, that 'Linear interpolation' is for resamplign right? hmm, Alsa-lib seems to do that for me if my card doesn't support the exact htz. :) Ohhh.. I need to add a cd input plugin :) maybe a ac3 or a52 plugin as well :) just to test to see if my code can easily handle more than 2 channels. (It should be easy. :))

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Plucky
Member #1,346
May 2001
avatar

Well somebody was interested in more 3D stuff, so I stuffed a crude voxel terrain engine into ~16 lines of code (actually ~15 if you get rid of the constant string definition on line 2 or perhaps 14 if you also get rid of lmap_c[][] and just use the map bitmap). Not really a game, but perhaps of some interest; you still have ~5 lines left to make a game out of it.;)

Same usual stuff: Only one semicolon or bracket {} per line, discounting the semicolons inside a for() expression. No commas except for variable definition and lists of parameter arguments in functions.

The engine is set for 32 bit color, which you can change to 15/16/24 bit. Arrow keys to move around. You can also try changing the voxel resolution setting from 3 to 0 to 7. And no optimizations (mostly due to trying to stuff the code), so it may be a bit slow.

1#include <allegro.h>
2const char *mapfile = "map.bmp"; /* 512x512 grayscale height map */
3int main() {
4 int p_x = 128<<16, p_y=128<<16, heading = 0, vox_res = 3 /* 0-7 */, lmap_c[512][512], i, j, ht_fac[8] = {3200, 4000, 5336, 8000, 10000, 16000, 24000, 40000}, max_step[8] = {160, 200, 267, 400, 500, 800, 1200, 2000}, step_factor[8] = {6250, 5000, 3748, 2500, 2000, 1250, 833, 500}, sz=0, curr_col=0, curr_step=0, ray_angle=0, dray_angle=0, ray_angle2=0, tempscale=0, rz=0, drz=0, drz0=0, rx=0, ry=0, rx0=0, ry0=0, drx=0, dry=0, ddrx=0, ddry=0, col_ht=0, voxel_scale=0, voxel_scaler=0, altitude=0, ht=0;
5 BITMAP *buffer = NULL, *map = NULL;
6 set_color_depth(32);
7 if ((!allegro_init()) | (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) >=0) | (!install_keyboard()) && (buffer=create_bitmap(640, 480)) ) map = load_bmp(mapfile, NULL);
8 for (i=0; i<512; i++) for (j=0; j<512; j++) lmap_c[j]<i> = getr(getpixel(map, i, j))*16;
9 while (!key[KEY_ESC]) {
10 for ((p_x += !!key[KEY_UP] * fmul(32768, fixsin(heading))) | (p_y += !!key[KEY_UP] * fmul(32768, -fixcos(heading))) && (heading += ((4<<16) * !!key[KEY_LEFT] * -1) + ((4<<16) * !!key[KEY_RIGHT]) ) |(curr_col=0)|((altitude=(((fmul(((lmap_c[((p_y>>16)+1)&511][((p_x>>16)+1)&511]-lmap_c[((p_y>>16)+1)&511][((p_x>>16))&511])*(p_x&0xFFFF)+(lmap_c[((p_y>>16)+1)&511][((p_x>>16))&511]<<16))-((lmap_c[((p_y>>16))&511][((p_x>>16)+1)&511]-lmap_c[((p_y>>16))&511][((p_x>>16))&511])*(p_x&0xFFFF)+(lmap_c[((p_y>>16))&511][((p_x>>16))&511]<<16)),(p_y & 0xFFFF))+((lmap_c[((p_y>>16))&511][((p_x>>16)+1)&511]-lmap_c[((p_y>>16))&511][((p_x>>16))&511])*(p_x&0xFFFF)+(lmap_c[((p_y>>16))&511][((p_x>>16))&511]<<16)))>>16)+32)*ht_fac[vox_res])|(ray_angle=heading-(32<<16))|(ray_angle2=heading+(32<<16))|(dray_angle=(64<<16)/SCREEN_W)|(tempscale=fmul(92682,step_factor[vox_res])) ? ((drx = fmul(fixsin(ray_angle), tempscale)) |(rx = fmul(fixsin(ray_angle2), tempscale)) ? ((ddrx = (rx-drx)/SCREEN_W) |(rx0 = p_x) |(dry = fmul(fixcos(ray_angle), -tempscale))|(ry = fmul(fixcos(ray_angle2), -tempscale)) ? ((ddry = (ry - dry)/SCREEN_W)|(ry0 = p_y)|(drz0 = -16384) ? (voxel_scaler = -2 * drz0 / SCREEN_H) :0) :0) :0) : 0) ; curr_col < SCREEN_W; (curr_col++) | (drx += ddrx) | (dry += ddry)) for ((curr_step = 0) | (rx = rx0)|(rz = altitude)|(ry = ry0)|(drz = drz0)|(voxel_scale = 0)|(sz = SCREEN_H-1);curr_step < max_step[vox_res]; (curr_step++)|(rx+=drx)|(rz+=drz)|(ry+=dry)|(voxel_scale+=voxel_scaler)) for ((ht =((fmul(((lmap_c[((ry>>16)+1)&511][((rx>>16)+1)&511]-lmap_c[((ry>>16)+1)&511][((rx>>16))&511])*(rx&0xFFFF)+(lmap_c[((ry>>16)+1)&511][((rx>>16))&511]<<16))-((lmap_c[((ry>>16))&511][((rx>>16)+1)&511]-lmap_c[((ry>>16))&511][((rx>>16))&511])*(rx&0xFFFF)+(lmap_c[((ry>>16))&511][((rx>>16))&511]<<16)),(ry&0xFFFF))+((lmap_c[((ry>>16))&511][((rx>>16)+1)&511]-lmap_c[((ry>>16))&511][((rx>>16))&511])*(rx&0xFFFF)+(lmap_c[((ry>>16))&511][((rx>>16))&511]<<16)))>>16))|(col_ht=ht*ht_fac[vox_res]);col_ht>rz;(drz+=voxel_scaler)|(rz+=voxel_scale)|(sz--))(sz<0)?((curr_step=max_step[vox_res])? (void)0 :putpixel(buffer,curr_col,sz,makecol(ht/16,ht/16,ht/16))):putpixel(buffer,curr_col,sz,makecol(ht/16,ht/16,ht/16));
11 blit(buffer, screen, 0, 0, 0, 0, 640, 480);
12 clear_to_color(buffer, makecol(0,0,0));
13 }
14 return 0;
15} END_OF_MAIN()

You need to create a 512x512 grayscale bitmap (.bmp). Or you can copy the one below, and resave it as a bmp.

http://www.geocities.com/sichoy/map.jpg

[edit] I forgot to get rid of another line, so it's now 14 or 15 lines, depending on whether you keep the mapfile string.

Evert
Member #794
November 2000
avatar

There you go: fox&geese with bugfixes. Should run fine now.;D

Man, I spend too much time on these things, but it is fun.

If we do want a competition (even if we don't, I think someone should collect all the little gems posted here and upload them somewhere), the most sensible requirement IMHO would be to limit the size of the sourcefile to, say, 2kb, not counting whitespace and comments.

#include <allegro.h>
#define DRAWTILE(c) rectfill(screen, c/8*(SCREEN_W/8), c%8*(SCREEN_H/8),  c/8*(SCREEN_W/8)+(SCREEN_W/8), c%8*(SCREEN_H/8)+(SCREEN_H/8), ((c%8)&1)^((c/8)&1)?0:15), circlefill(screen, goose[0]%8*(SCREEN_W/8)+SCREEN_W/16, goose[0]/8*(SCREEN_H/8)+SCREEN_H/16, SCREEN_W/32, 4), circlefill(screen, goose[1]%8*(SCREEN_W/8)+SCREEN_W/16, goose[1]/8*(SCREEN_H/8)+SCREEN_H/16, SCREEN_W/32, 4), circlefill(screen, goose[2]%8*(SCREEN_W/8)+SCREEN_W/16, goose[2]/8*(SCREEN_H/8)+SCREEN_H/16, SCREEN_W/32, 4), circlefill(screen, goose[3]%8*(SCREEN_W/8)+SCREEN_W/16, goose[3]/8*(SCREEN_H/8)+SCREEN_H/16, SCREEN_W/32, 4), circlefill(screen, fox%8*(SCREEN_W/8)+SCREEN_W/16, fox/8*(SCREEN_H/8)+SCREEN_H/16, SCREEN_W/32, 1), rect(screen, goose[sgoose]%8*(SCREEN_W/8), goose[sgoose]/8*(SCREEN_H/8),  goose[sgoose]%8*(SCREEN_W/8)+(SCREEN_W/8)-1, goose[sgoose]/8*(SCREEN_H/8)+(SCREEN_H/8)-1, 14)
int main(void) { int goose[4] = {63,61,59,57}, sgoose = 0, fox = 4, turn = 0, done = 0, drawme = 1, c;
   allegro_init(), install_keyboard(), install_timer, install_mouse(),set_gfx_mode(GFX_AUTODETECT, 256,256,0,0), clear_to_color(screen, 15), text_mode(-1), show_mouse(screen);
   while (!key[KEY_ESC] && !done) {
      turn ? ( (fox%8!=7) ? fox-=((goose[0]==fox+9||goose[1]==fox+9||goose[2]==fox+9||goose[3]==fox+9)?0:(9*(key[KEY_S]?turn=0,drawme=1,-1:0))):0,(fox%8!=0) ? fox-=((goose[0]==fox+7||goose[1]==fox+7||goose[2]==fox+7||goose[3]==fox+7)?0:(7*(key[KEY_A]?turn=0,drawme=1,-1:0))):0,(fox%8!=0 && fox>8) ? (fox +=((goose[0]==fox-9||goose[1]==fox-9||goose[2]==fox-9||goose[3]==fox-9)?0:(9*(key[KEY_Q]?turn=0,drawme=1,-1:0)))):0, (fox%8!=7 && fox>8) ? (fox +=((goose[0]==fox-7||goose[1]==fox-7||goose[2]==fox-7||goose[3]==fox-7)?0:(7*(key[KEY_W]?turn=0,drawme=1,-1:0)))):0 ):( sgoose=( sgoose + (key[KEY_TAB]?(rest(100), drawme=1):0) )&3, (goose[sgoose]%8!=0 && goose[sgoose]>8) ? (goose[sgoose] += (goose[sgoose]-9==fox?0:9*(key[KEY_LEFT]?turn=1,drawme=1,-1:0))) :0, (goose[sgoose]%8!=7 && goose[sgoose]>8) ? (goose[sgoose] += (goose[sgoose]-7==fox?0:7*(key[KEY_RIGHT]?turn=1,drawme=1,-1:0))):0);
      if (drawme) for(c=0;c<64;c++){ scare_mouse(), DRAWTILE(c), unscare_mouse(), drawme = 0; }
      done = (fox>56 || (goose[0]<8 && goose[1]<8 && goose[2]<8 && goose[3]<8)) || ( (goose[0]+goose[1]+goose[2]+goose[3]==4*fox) && (abs(fox-goose[0])==7 || abs(fox-goose[0]==9)) ) ? 1:0;
   } while(keypressed()) readkey();
   return done?textprintf_centre(screen, font, SCREEN_W/2, SCREEN_H/2, 2, (fox>56)?"Fox wins!":"Geese win!"), readkey(), 0:0;
} END_OF_MAIN()

white_door
Member #202
April 2000
avatar

This is a little piece of a rpg.. the battle between the forces of good rabbits and evil rabbits. Who will win? (designed to be like a final fantasy like battle)

[edit added srand(NULL);]

#include <allegro.h>
#include <string.h>
#include <stdlib.h>
/*01*/BITMAP *draw_char(BITMAP *buffer,int x,int y,int half_w,int half_h,int facing) {int gen[32]={half_w,0,half_w/4,-half_h/4,half_w/8,-half_h/2,half_w/8,-half_h*3/4,half_w/4,-half_h*3/4,half_w/2,-half_h/4,half_w*3/4,-half_h/4,half_w*3/4,-half_h/2,half_w,-half_h/2,half_w,-half_h*3/4,half_w/4,half_h/4,half_w/2,half_h*3/4,half_w,half_h*3/4,half_w,half_h,half_w/4,half_h,0,half_h/2},side1[32]={x+gen[0],y+gen[1],x+gen[2],y+gen[3],x+gen[4],y+gen[5],x+gen[6],y+gen[7],x+gen[8],y+gen[9],x+gen[10],y+gen[11],x+gen[12],y+gen[13],x+gen[14],y+gen[15],x+gen[16],y+gen[17],x+gen[18],y+gen[19],x+gen[20],y+gen[21],x+gen[22],y+gen[23],x+gen[24],y+gen[25],x+gen[26],y+gen[27],x+gen[28],y+gen[29],x+gen[30],y+gen[31]},side2[32]={x-gen[0],y+gen[1],x-gen[2],y+gen[3],x-gen[4],y+gen[5],x-gen[6],y+gen[7],x-gen[8],y+gen[9],x-gen[10],y+gen[11],x-gen[12],y+gen[13],x-gen[14],y+gen[15],x-gen[16],y+gen[17],x-gen[18],y+gen[19],x-gen[20],y+gen[21],x-gen[22],y+gen[23],x-gen[24],y+gen[25],x-gen[26],y+gen[27],x-gen[28],y+gen[29],x-gen[30],y+gen[31]};
/*02*/(facing)?polygon(buffer,16,side1,makecol(255,0,0)):polygon(buffer,16,side2,makecol(0,0,255));
/*03*/return buffer;
/*04*/} void main() {BITMAP game_state={/*int 7*/0,(allegro_init()||install_timer()||install_keyboard()||set_gfx_mode(GFX_AUTODETECT_WINDOWED,320,200,0,0))?-1:50,text_mode(-1),40,100,80,100,/*pointers 4*/NULL,NULL,NULL,NULL,/*long int*/0,/*pointer*/NULL,/*int 3*/270,130,0},game_state2={/*int 7*/5,10,0,0,0,0,0,/*pointers 4*/NULL,NULL,NULL,NULL,/*long int*/0,/*pointer*/NULL,/*int 3*/0,0,0},*buffer=create_bitmap(320,200);
/*05*/srand(time(NULL));
/*06*/while((game_state.cr>0||game_state.ct>0)&&memset(buffer->dat,0,320*200)&&game_state.h>0&&!key[KEY_DOWN]|(game_state.w=0)) while(game_state.w++<12)(game_state.w==1) ? textprintf(buffer,font,10,10,makecol(0,255,0),"Push [left] for magic"):(game_state.w==2) ? textprintf(buffer,font,10,20,makecol(0,255,0),"Push [up] for attack"):(game_state.w==3)?textprintf(buffer,font,10,30,makecol(0,255,0),"Push [Down] to Run"):(game_state.w==4)?((key[KEY_UP]&&game_state.seg==0&&game_state.ct>0)? game_state.seg=30*(game_state.id=1) : (key[KEY_LEFT]&&game_state.seg==0&&game_state.cr>0)? game_state.seg=15*(game_state.id=2) : (game_state.seg>0)? rest(game_state.seg--): (game_state.id==4)? game_state.id=(game_state.h-=(rand())%20) : (game_state.id==1)?(game_state.id=((game_state2.w-=(1+rand()%3))>0)?0*(game_state.seg=25)+4:(game_state.ct=-30)):(game_state.id==2)?(game_state.id=((game_state2.h-=(1+rand()%3))>0)?(game_state.seg=25)*0+4:(game_state.cr=-30)):rest(1)):(game_state.w==5)?((game_state.id==1)? line(buffer,game_state.ct+game_state.seg,game_state.cb+game_state.seg,game_state.ct+game_state.seg+20,game_state.cb+game_state.seg+20,makecol(255,200,200)): rest(1)):(game_state.w==6)?((game_state.id==2)?circle(buffer,game_state.cl,game_state.cr,game_state.seg,makecol(255,255,255)):rest(1)):(game_state.w==7)?(void)draw_char(draw_char(draw_char(buffer,game_state.ct,game_state.cb,20,20,TRUE),game_state.cl,game_state.cr,20,20,TRUE),game_state.x_ofs,game_state.y_ofs,20,20,FALSE):(game_state.w==8)?line(buffer,game_state.x_ofs-33,game_state.y_ofs+23,game_state.x_ofs-33+game_state.h,game_state.y_ofs+23,makecol(0,255,0)):(game_state.w==9)?line(buffer,game_state.ct+13,game_state.cb+23,game_state.ct+13+game_state2.w,game_state.cb+23,makecol(0,255,0)):(game_state.w==10)?line(buffer,game_state.cl+13,game_state.cr+23,game_state.cl+13+game_state2.h,game_state.cr+23,makecol(0,255,0)):(game_state.w==11)?(game_state.id==4)?line(buffer,game_state.x_ofs-20+game_state.seg,game_state.y_ofs+game_state.seg,game_state.x_ofs+game_state.seg,game_state.y_ofs+game_state.seg+20,makecol(255,200,200)):rest(1):blit(buffer,screen,0,0,0,0,320,200); 
/*07*/while(!key[KEY_ESC])(game_state.cr<0&&game_state.ct<0)? (textprintf_centre(screen,font,160,100,makecol(0,255,0),"You win! Push [Esc] to quit")):(textprintf_centre(screen,font,160,100,makecol(0,255,0),"You lose! Push [Esc] to quit"));
} END_OF_MAIN()

Evert
Member #794
November 2000
avatar

Nice! But is it at all possible to win? I get trashed each time round...

white_door
Member #202
April 2000
avatar

hehe well it depends on your compiler..
since I forgot to use srand(time(NULL)); you will either win or lose depending on the sequence of number generated by rand()...

on my computer I win.. so.. hmm ;) anyway its easy enough to add a srand(time(NULL)); with out pushing it past the limit.

Oscar Giner
Member #2,207
April 2002
avatar

Here's my game. It's some kind of space shoter, but with circles. You're the yellow circle and enemies are the red ones. You move with the arrows and shot a blue circle with the rigth control. If an enemy collides with you... you lose!

Compiles without any warning with -Wall :)

1#include <allegro.h>
2 
3/* 1*/ volatile int counter;
4/* 2*/ void handler(void){
5/* 3*/ counter++;
6/* 4*/ } END_OF_FUNCTION(handler);
7
8/* 5*/ int main(int argc, char *argv[]){
9/* 6*/ int buffer, t=0, i, k, posx=10, posy=100, shot_posx=321, shot_posy=-1, enemy_posx[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, enemy_posy[10], enemies_destroyed=0;
10/* 7*/ if ((srand(time(NULL)),allegro_init()) || install_keyboard() || install_int_ex(handler, BPS_TO_TIMER(100)) || set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) || !(buffer=(int)create_bitmap(320, 200)) || (text_mode(-1),0) || (counter=0)) allegro_message("Error initializing\n"), exit(-1);
11/* 8*/ while (!key[KEY_ESC] && !(k=0) && (clear((BITMAP *)buffer),1)){
12/* 9*/ while (counter > 0 && !(i=0) && (((shot_posx = shot_posx>320&&key[KEY_RCONTROL]?shot_posy=posy,posx+2:shot_posx),(posx += posx>8&&key[KEY_LEFT]?-1:posx<310&&key[KEY_RIGHT]?1:0),(posy += posy>20&&key[KEY_UP]?-1:posy<180&&key[KEY_DOWN]?1:0),shot_posx+=3,t--,counter--)||1))
13/*10*/ while (i++<10){
14/*11*/ if (enemy_posx[i-1]>=0) (enemy_posx[i-1]-=2, enemy_posy[i-1] += (rand()%4) ? (-1+rand()%3) : enemy_posy[i-1]==posy?0:enemy_posy[i-1]<posy?1:-1);
15/*12*/ if (enemy_posx[i-1]>=0&&((enemy_posx[i-1]-posx)*(enemy_posx[i-1]-posx)+(enemy_posy[i-1]-posy)*(enemy_posy[i-1]-posy))<50) set_gfx_mode(GFX_TEXT, 0, 0, 0, 0), allegro_message("You have destroyed %d enemies\n", enemies_destroyed), exit(-1);
16/*13*/ if (enemy_posx[i-1]>=0&&((enemy_posx[i-1]-shot_posx)*(enemy_posx[i-1]-shot_posx)+(enemy_posy[i-1]-shot_posy)*(enemy_posy[i-1]-shot_posy))<30) enemy_posx[i-1]=-1, shot_posx=330, enemies_destroyed++;
17/*14*/ if (enemy_posx[i-1]<0 && t<=0) (enemy_posx[i-1]=320),(enemy_posy[i-1]=rand()%200),(t = 5+(rand()%15)),i=20;
18/*15*/ }
19/*16*/ while (k++<10) if (enemy_posx[k-1]>=0) circlefill((BITMAP *)buffer, enemy_posx[k-1], enemy_posy[k-1], 2, 12);
20/*17*/ textprintf((BITMAP *)buffer, font, 0, 0, 15, "Enemies destroyed: %d", enemies_destroyed),circlefill ((BITMAP *)buffer, posx, posy, 5, 14),circlefill((BITMAP *)buffer, shot_posx, shot_posy, 2, 11), blit ((BITMAP *)buffer, screen, 0, 0, 0, 0, 320, 200);
21/*18*/ }
22/*19*/ return 0;
23/*20*/ } END_OF_MAIN();

It was very funny to program, and I thing it's very funny to play :)

Note: 100% original. I didn't look at any of your programs until I've finished this.

miran
Member #2,407
June 2002

You do know what the word funny means, right? And that think is spelled with a k? :)

Sorry 'bout that but I just had to...

BTW, I thought that whole funny business was a German thing or something...

--
sig used to be here

Bruce Perry
Member #270
April 2000

Let's clarify...

"Funny" can mean either "causing laughter" or "strange".
"Fun" means "enjoyable".

Things that are funny can be fun, and vice versa, of course :)

... and you flame me for going on about "it's" ::) ...

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Peter Wang
Member #23
April 2000

Just to complete the collection of 20 line interpretations of classic games, here's Pacman:

#SelectExpand
1#include <allegro.h> 2/*01*/int main(int dots, char**bmp){int x[]={6*20,5*20,6*20,7*20,6*20},y[]={14*20,7*20,7*20,7*20,6*20},dx[]={1,0,0,0,0},dy[]={0,0,0,0,0},dir,wantdir=0,ddx[]={1,0,-1,0},ddy[]={0,-1,0,1},map[16][13]={{1,1,1,1,1,1,1,1,1,1,1,1,1},{1,2,2,2,2,2,2,2,2,2,2,2,1},{1,2,1,1,2,2,1,2,2,1,1,2,1},{1,2,1,2,2,1,1,1,2,2,1,2,1},{1,2,1,2,2,2,1,2,2,2,1,2,1},{1,2,2,2,2,2,2,2,2,2,2,2,1},{1,1,1,2,1,2,2,2,1,2,1,1,1},{0,0,1,2,1,2,2,2,1,2,1,0,0},{1,1,1,2,1,1,1,1,1,2,1,1,1},{1,2,2,2,2,2,2,2,2,2,2,2,1},{1,2,2,1,1,1,2,1,1,1,2,2,1},{1,2,2,2,1,2,2,2,1,2,2,2,1},{1,2,1,2,2,2,1,2,2,2,1,2,1},{1,2,1,1,2,1,1,1,2,1,1,2,1},{1,2,2,2,2,2,0,2,2,2,2,2,1},{1,1,1,1,1,1,1,1,1,1,1,1,1}},*tmp,i,j,c[5],pdir=0,gobble=0; 3/*02*/for((void)((!allegro_init()&&!install_keyboard()&&!install_timer()&&!set_gfx_mode(GFX_AUTODETECT,640,480,0,0)&&(((int(*)())srand)(time(0))*0+1)&&(bmp=(char**)create_bitmap(13*20,16*20))&&(dots=101)&&(c[0]=makecol(255,255,0))&&(c[1]=makecol(255,0,255))&&(c[2]=makecol(255,200,200))&&(c[3]=makecol(0,255,255))&&(c[4]=makecol(255,0,0)))||((int(*)())exit)(0));!key[KEY_ESC]&&dots>0&&((!dx[0]&&!dy[0])||(gobble=gobble+4>40?-39:gobble+4)|1);blit((BITMAP*)bmp,screen,((int(*)())rest)(10)*0,0,190,80,13*20,16*20)){for(i=0;i<5;i++)if(map[(y<i>+dy<i>+((dy<i>>0)?19:0))/20][(x<i>+dx<i>+((dx<i>>0)?19:0))/20]==1)dx<i>=dy<i>=0; 4/*03*/for(wantdir=key[KEY_UP]?1:key[KEY_DOWN]?3:key[KEY_LEFT]?2:key[KEY_RIGHT]?0:wantdir,i=0;i<5;i++){if((tmp=&map[((y<i>+=dy<i>)+10)/20][((x<i>+=dx<i>)+10)/20])&&!i)*tmp=(dots-=*tmp==2)*0; 5/*04*/if(!(x<i>%20)&&!(y<i>%20)&&(dir=!i?wantdir:((rand()%10)-6))>=0&&map[(y<i>+ddy[dir]+((ddy[dir]>0)?19:0))/20][(x<i>+ddx[dir]+((ddx[dir]>0)?19:0))/20]!=1)pdir=!i?dir:pdir,dx<i>=ddx[dir],dy<i>=ddy[dir];} 6/*05*/for(i=1;i<5;i++)if(fixhypot(itofix(x<i>-x[0]),itofix(y<i>-y[0]))<itofix(16))dots=-1; 7/*06*/for(j=0;j<16;j++)for(i=0;i<13;i++){rectfill((BITMAP*)bmp,i*20,j*20,i*20+19,j*20+19,makecol(0,map[j]<i>==1?255:0,0)); 8/*07*/if(map[j]<i>==2)circlefill((BITMAP*)bmp,i*20+10,j*20+10,2,makecol(255,255,200));} 9/*08*/arc((BITMAP*)bmp,x[0]+10,y[0]+10,itofix(pdir*64+ABS(gobble)),itofix(pdir*64+255-ABS(gobble)),8,c[0]); 10/*09*/line((BITMAP*)bmp,x[0]+10,y[0]+10,x[0]+10+fixtoi(fcos(itofix(pdir*64+ABS(gobble)))*8),y[0]+10-fixtoi(fsin(itofix(pdir*64+ABS(gobble)))*8),c[0]); 11/*10*/line((BITMAP*)bmp,x[0]+10,y[0]+10,x[0]+10+fixtoi(fcos(itofix(pdir*64+255-ABS(gobble)))*8),y[0]+10-fixtoi(fsin(itofix(pdir*64+255-ABS(gobble)))*8),c[0]); 12/*11*/floodfill((BITMAP*)bmp,x[0]+10+(!pdir?-5:5),y[0]+10,c[0]); 13/*12*/for(i=1;i<5;i++){circlefill((BITMAP*)bmp,x<i>+10,y<i>+10,8,c<i>); 14/*13*/circlefill((BITMAP*)bmp,x<i>+7,y<i>+7,3,makecol(255,255,255)); 15/*14*/circlefill((BITMAP*)bmp,x<i>+13,y<i>+7,3,makecol(255,255,255)); 16/*15*/circlefill((BITMAP*)bmp,x<i>+8,y<i>+7,0,0); 17/*16*/circlefill((BITMAP*)bmp,x<i>+12,y<i>+7,1,0);}} 18/*17*/if(dots<=0){textout_centre(screen,font,!dots?"You win!":"You lose!",320,220,makecol(255,255,255)); 19/*18*/rest(2000);} 20/*19*/destroy_bitmap((BITMAP*)bmp); 21/*20*/return 0;}END_OF_MAIN()

I think I followed the "rules". If not, fsck it.

Thomas Fjellstrom
Member #476
June 2000
avatar

You suck. ;) I thought about doing a pacman.. But oh well.

Though, the rules say semi-colon delimited... But it brobably wouldn't be hard to fix...

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Bruce Perry
Member #270
April 2000

Ooh, casting function pointers to get around the void return values... not portable ;) I almost used raise(SIGABRT), which returns int, in my game, but in the end I didn't need it.

One or two bugs(?) in the game, namely that the whole background goes yellow whenever his mouth is closed, and the thingy doesn't stop moving when I let go of the keys (I wish he would). Otherwise works well :)

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Thomas Fjellstrom
Member #476
June 2000
avatar

he's not supposed to stop moving.. When was the last time you played pacman?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

aileron42
Member #3,184
January 2003
avatar

Well, I just joined {allegro.cc} tonight. I've been reading the forums for a while and decided it was time I gave my two cents, or in this case, my twenty lines. here goes...

1#include <allegro.h>
2int main() {
3 if (!allegro_init()&&!install_keyboard()&&set_gfx_mode(GFX_AUTODETECT,640,480,0,0)) return 1;
4 int x=0,y=400,d=0,ngx=460,ngy=0,gc=0,t = -29,i=0;
5 for (;!key[KEY_ESC] && gc < 16 ;t++,rest(60)) {
6 circlefill(screen,x,y,8,0);
7 for (i=0;i<2;i++) circlefill(screen,180 + i *280 ,240,100,2);
8 for (i=0;i<15;i++) rectfill(screen,172 + (i%2) *8, 342+i*8,180+ (i%2) *8,342+i*8+8,15 );
9 d = (d+256 +(key[KEY_LEFT] ? -4: 0)+(key[KEY_RIGHT] ? 4: 0)+((((x-180)*(x-180)+(y-240)*(y-240))>108*108) ? 0: 25 ) + ((((x-460)*(x-460)+(y-240)*(y-240))>108*108) ? 0: -25 ) )%256;
10 y = ((y + fixtoi(fixsin(itofix(d))*6) < 480)&&(y + fixtoi(fixsin(itofix(d))*6) > 0)) ? (y + fixtoi(fixsin(itofix(d))*6)) : y ;
11 x = ((x + fixtoi(fixcos(itofix(d))*6) < 640)&&(x + fixtoi(fixcos(itofix(d))*6) > 0)) ? (x + fixtoi(fixcos(itofix(d))*6)) : x ;
12 if (((x-ngx)*(x-ngx)<(4*4))&& ((y-ngy)*(y-ngy)<(150*150))){
13 ngx = (ngx==180) ? ((ngy==0)? 180 : 460 ) : ((ngy==0)? 460: 180 );
14 ngy = (ngy==0) ? 480 : 0;
15 gc++;
16 } circlefill(screen,x,y,8,1);
17 textprintf(screen,font,0,0,15,"Time: %i Laps Left: %i ",(t < 0) ? 0 : t, 4-(gc/4));
18 } for(;!key[KEY_ESC];)textprintf_centre(screen,font,320,240,15,"Course Complete! Your time was: %i",t-1);
19 return 0;
20}

It is a top view car racing game. No opponent, just a time trial though. Figur eights around the two circles. I hope you enjoy ;D

--------------------------
Pillage first... then burn

Peter Wang
Member #23
April 2000

Bruce: I can't reproduce the yellow background problem. Maybe floodfill() got changed at some stage, although I tested both stable and unstable Allegros.

Michael Jensen
Member #2,870
October 2002
avatar

I couldnt get pacman to compile at all...
compiler says 3 times on line 4 too many arguments to function -- maybe i have an old version of allegro or gcc.....

This is the first thread that I've read on allegro.cc in about a week or two and I decided to try to do the challenge, It's not an rpg but its tile based...... I did some things that may be considered cheap and cheating but I kept my code under 20 lines (19 I think) and no line has more than one semi colon on it.... the actual .cpp file is just under 5kb....

#SelectExpand
1/*God is awesome*/ #include <allegro.h> 2#define set(a,b) {a=b;} 3#define im_a_cheater(x,y,s) {score+=s;set(grid[x][y],0)} 4#define cheat2(a,b,c,d,e,f,g) {textprintf(a,b,c,d,e,f,g);} // -- is this cheating? :-) 5int slowdown_variable=FALSE; //for fps 6void slowdown_function(void){set(slowdown_variable,TRUE)} END_OF_FUNCTION(slowdown_function) int main(void){LOCK_VARIABLE(slowdown_variable)LOCK_FUNCTION(slowdown_function) if ((allegro_init()==0) && (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0)==0) && (!install_keyboard()) && (!install_int(slowdown_function, 150))){srand((unsigned)time(NULL)); ///setup allegro randomize timer 7BITMAP * apage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,0,0,0,0,0,0,0,0,0,0,0,0}, px=rand()%40, py=rand()%24,alive=TRUE,score=0,move[]={-1,0,1},controls[]={KEY_HOME, KEY_UP, KEY_PGUP, KEY_LEFT, KEY_ESC, KEY_RIGHT, KEY_END, KEY_DOWN, KEY_PGDN},controls2[]={KEY_7_PAD, KEY_8_PAD, KEY_9_PAD, KEY_4_PAD, KEY_5_PAD, KEY_6_PAD, KEY_1_PAD, KEY_2_PAD, KEY_3_PAD}, movin=FALSE; //setup game & variables 10while(alive && !key[KEY_ESC]){int x=rand()%40, y=rand()%24, t=(rand()%2)+1; //main while loop 11if (grid[px][py]==1){im_a_cheater(px,py,1)} else if (grid[px][py]==2){set(alive,FALSE)} if (x!=px || y!=py){set(grid[x][y],t)} if (grid[x][y]==1){cheat2(apage, font,x*8, (y+1)*8, makecol(0,255,0), "$", px)} else {rectfill(apage, x*8, (y+1)*8, ((x+1)*8)-1, ((y+2)*8)-1, makecol(192,192,192));} //check for death/score then place new item & draw money and bricks only one semicolon 12cheat2(apage, font, 0, 0, makecol(255,255,255), "SCORE: %d", score) /*im_a_cheater(0,24,1)*/ circlefill(apage, (px*8)+3, ((py+1)*8)+3, 2, makecol(255,255,0)); 13blit(apage,screen, 0,0,0,0, SCREEN_W, SCREEN_H); 14rectfill(apage,px*8, (py+1)*8, ((px+1)*8)-1, ((py+2)*8)-1,makecol(0,0,0)); 15set(movin,FALSE) while(!slowdown_variable || !movin){for (int j=0; j<9; j++){if ((!movin) && (key[controls[j]] || key[controls2[j]]) && (py+move[j/3]<24) && (py+move[j/3]>-1) && ((px+move[j%3])<40) && (px+move[j%3]>-1)){//stuff inside the for ()s doesn't count right? 16py+=move[j/3]; 17px+=move[j%3]; if (!key[KEY_ESC]){while(key[controls[j]] || key[controls2[j]]){}} set(movin,TRUE)}} 18}set(slowdown_variable,FALSE)}cheat2(screen, font, 0, 0, makecol(255,0,0), "SCORE: %d (GAME OVER PRESS ENTER)", score) while (!key[KEY_ENTER]){} allegro_exit();} //shutting down & end of while, & then end of if 19return(0);}END_OF_MAIN()//exiting

if there is word wrap on here it might not be under 20 lines on here, but the .cpp file i have is under 20 with no more than 1 semicolon per line.....

anyways its a remake of an old game that I made (first game I ever made) on my b&w trs-80 model 4 computer with basic.....(it was in textmode)

Bricks & Money "fall", if you walk on the money -- you get a point and it disappears, if you walk on the bricks you die, esc quits, dont hold keys down -- tap them (sorry bout this, its hard to do good keyboard routines in a couple of lines -- for me anyways) It gets interesting once you're score is about 200 - 300, which is a while to wait...

Bruce Perry
Member #270
April 2000

Michael: compile Pacman as C, not C++. He uses some function pointers with "()" for the parameters; in C this means 'parameter number and types unknown', while in C++ it means 'no parameters'.

-Wstrict-prototypes is a good warning... ;)

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Michael Jensen
Member #2,870
October 2002
avatar

by the way I lied about my game -- the line with the for loop has two semicolons I think...

Quote:

the .cpp file i have is under 20 with no more than 1 semicolon per line.....

oh cool no warnings this time... thanks BP! -- but it's tilebased -- since when is pacman tilebased?
And no fruit and when you beat it -- nothing happens and only one life -- heh sorry I'm a big pac-man fan.....(nitpick nitpick nitpick) but it should almost go without saying -- great job!

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

since when is pacman tilebased?

Since when isn't it?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Michael Jensen
Member #2,870
October 2002
avatar

since forever! -- the walls are always thin and the pathways wide -- if you can call that tilebased, maybe it is, but I dont see how at this point....

spellcaster
Member #1,493
September 2001
avatar

Pacman is one of the traditional tile based examples..

--
There are no stupid questions, but there are a lot of inquisitive idiots.

23yrold3yrold
Member #1,134
March 2001
avatar

Pac-Man is soooo tile-based! Not terribly attractive tiles, but tiles all the same.

BTW, this thread is a good example of why we need pages in the forums. How's that coming, Matthew? ;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Michael Jensen
Member #2,870
October 2002
avatar

thats not the origonal pacman :P

just cause you can do it with tiles doesn't make it right -- if you look at it in the offical games it becomes very hard for me to divide it into tiles... maybe it is tilebased -- but I doub't it -- I have it on 2600 and 800XL (those are ataris) :)

edit: the walls arn't just tiles that have skinnier wall graphics, they're actually graphically offset as far as I can tell..... from what I've been looking at it would take more tiles than my atari had memory for :) if pacman is tile based them maybe some people are just better at hiding it than others cause it really doesnt seem like it is.

edit2: IMO you'd have to have 4 different tile sizes (on the same map) and a lot of tile graphics to do it right....

edit3: I'm starting to think maybe pacman is tilebased -- but I know that people have done it non tile based, and I know that this isn't the origonal pacman but it's very hard for me to see tiles in it for example, maybe you could show me how you would divide it into equal tiles...

http://www.geocities.com/michael_jensen419/level.gif

spellcaster
Member #1,493
September 2001
avatar

The original pacman:
http://roms2.mame.dk/png/puckman.png
Take a look at the rom, check the gfx set, and you'll see the individual tiles ;)

Edit:
You can see the individual tiles, if you take a look at the IBM PC character set :)
Edit2:
http://web-wise-wizard.com/web-lookup-lists/dem/ascii-ibm/ascii_12.gif

--
There are no stupid questions, but there are a lot of inquisitive idiots.



Go to: