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?
Thomas Fjellstrom
Member #476
June 2000
avatar

I was working on atwo player pong last night :( too bad I was too tired to finish ;D

But its at like 22 lines :( (pretty much finished, just need to fix a bug...)

--
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

23yrold3yrold
Member #1,134
March 2001
avatar

Restricting to lines and columns might work if you discount headers, since #includes need their own lines, right? I like the character count idea better ... and Spellcaster's source code gives me a headache. Crafty use of ?: though :)

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

Thomas Fjellstrom
Member #476
June 2000
avatar

You should see mine... Lots of &&, || and () ;)

edit: well, It USED to have lots of &&, || and ()... But writing code on 2 hours (in 48) of sleep is a bad idea. :)

--
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

nonnus29
Member #2,606
August 2002
avatar

Nice work SC and BP; see if I can get mine done tonight... it won't be 20 lines though; maybe 30 :P

Thomas Fjellstrom
Member #476
June 2000
avatar

well.. Heres my entry :)

1#include <allegro.h>
2 
3int main(void) { int x=320,y=240,dx=-1,dy=1,p1y=240,p2y=240,p1p=0,p2p=-1;
4 BITMAP *buff = (BITMAP *)(NULL,srand(time(NULL)),allegro_init(),install_keyboard(),set_gfx_mode(GFX_AUTODETECT,640,480,0,0));
5 buff = create_bitmap(640, 480);
6 while(!key[KEY_ESC] && !key[KEY_Q]) { clear(buff);
7 (x-2 <= 0 || x+2 >= 640) ? dx = -dx : ((y-2 <= 0 || y+2 >= 480) ? dy = -dy : 0);
8 ((x += dx), (y += dy));
9 (0 <= x-2 && x-2 <= 10) ? ( (p1y-12 <= y+2 && y+2 <= p1y+12) ? (dx = -dx) : (++p2p, (x=320), (y=rand()%480)) ) : 0;
10 (630 <= x+2 && x+2 <= 640) ? ( (p2y-12 <= y+2 && y+2 <= p2y+12) ? (dx = -dx) : (++p1p, (x=320), (y=rand()%480)) ) : 0;
11 if(key[KEY_UP] && p2y > 12) p2y-=2;
12 if(key[KEY_DOWN] && p2y < 468) p2y+=2;
13 if(key[KEY_W] && p1y > 12) p1y-=2;
14 if(key[KEY_S] && p1y < 468) p1y+=2;
15 textprintf_centre(buff, font, 320, 2, makecol(200,200,200), "%03i %03i", p1p, p2p);
16 vline(buff, 320, 0, 480, makecol(255,255,255));
17 vline(buff, 10, p1y-10, p1y+10, makecol(255,255,255));
18 vline(buff, 630, p2y-10, p2y+10, makecol(255,255,255));
19 circlefill(buff, x-2, y-2, 2, makecol(255,255,255));
20 blit(buff, screen, 0, 0, 0, 0, 640, 480);
21 } return 0;
22} END_OF_MAIN();

It doesn't make the requirements (and is a bit buggy...). But I like it :)

[edit]new version :) (w00t. It still doesn't quite make it nonnus) oops.. that should be 20...

[edit2] hmmmm.. I can shorten those 3 vlines to a for and 1 vline... sould I ??? ;):D

--
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

nonnus29
Member #2,606
August 2002
avatar

EDIT: Here's my shooter; its 22 lines though, very light on the comma's (only one line uses comma's, the allegor line) No collision detection, but you can shoot your little gun :P

1#include <allegro.h>
2int main(void) { BITMAP *backbuffer=NULL;
3 int i=0,sprites[6][6]={ {150,180,0,30,1,0},{30,0,0,0,0,0},{100,10,10,0,1,10},{75,20,-10,0,1,10},{150,30,10,0,1,10},{210,40,-10,0,1,10}};
4 allegro_init(),install_keyboard(), set_gfx_mode(GFX_AUTODETECT,320,200,0,0);
5 backbuffer=create_bitmap(SCREEN_W,SCREEN_H);
6 while(!key[KEY_ESC]) { clear_bitmap(backbuffer);
7 if (key[KEY_A] && sprites[1][5]<=0) {sprites[1][0]=sprites[0][0];
8 sprites[1][5]=37;}
9 if (key[KEY_RIGHT] && sprites[0][0]<310) sprites[0][0]+=5;
10 if (key[KEY_LEFT] && sprites[0][0]>15) sprites[0][0]+=-5;
11 for(i=2;i<6;i++) { if(sprites<i>[2]>0 && sprites<i>[0]<300) sprites<i>[0]+=sprites<i>[2];
12 else if(sprites<i>[2]<0 && sprites<i>[0]>20) sprites<i>[0]+=sprites<i>[2];
13 else sprites<i>[2] *= -1;
14 circlefill(backbuffer,sprites<i>[0],sprites<i>[1],4,50);}
15 if(sprites[1][5] >0) { sprites[1][5]--;
16 sprites[1][1]=(sprites[1][5]* 5);
17 circlefill(backbuffer,sprites[1][0],sprites[1][1],4,50);}
18 circle(backbuffer,sprites[0][0],sprites[0][1],4,3);
19 blit(backbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
20 rest(30);}
21 return 0; }
22END_OF_MAIN();

Nice pong TF; cheat on the comma's (like everyone else) in your allegro init code and you can get it under 20 ;D

yoheeb
Member #917
January 2001

can anyone say FORTRAN? :D Ahhh, Flashback, Flashback.....columns 1-9 ahhhh....

Jay

Measure with a micrometer, mark it with chalk and cut it with an axe.

Thomas Fjellstrom
Member #476
June 2000
avatar

COMEON PEOPLE!!! We Wanna see more! You know you want to! ;D

--
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

Karadoc ~~
Member #2,749
September 2002
avatar

1#include <allegro.h>
2volatile int clicker = 0;
3void ClickerThing(void) {clicker++;} END_OF_FUNCTION(ClickerThing)
4void main(void)
5{
6 int x = 320, y = 240, x_speed = 0, y_speed = 0;
7 BITMAP *buff;
8 srand(time(NULL));
9 allegro_init(), install_keyboard(), install_timer(), set_color_depth(16), set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
10 buff = create_bitmap(640, 480);
11 LOCK_FUNCTION(ClickerThing) LOCK_VARIABLE(clicker) install_int(ClickerThing, 10);
12 while (!key[KEY_ESC]) { while((clicker = (clicker > 0)?-1:0) && !key[KEY_ESC]) {
13 clear(buff), textout_centre(buff, font, "Hello World", x, y, makecol(255-(x>>2), 0, 255-(y>>1)));
14 x_speed = (x_speed > 5 || x > 540)?x_speed-1 :(x_speed < -5 || x < 100)?x_speed+1 :x_speed+rand()%3-1;
15 y_speed = (y_speed > 5 || y > 460)?y_speed-1 :(y_speed < -5 || y < 20)?y_speed+1 :y_speed+rand()%3-1;
16 x+=x_speed;
17 y+=y_speed;
18 blit(buff, screen, 0, 0, 0, 0, 640, 480); } }
19 remove_int(ClickerThing), destroy_bitmap(buff), allegro_exit();
20} END_OF_MAIN()

:)
Mine doesn't have much in the way of gameplay...
BUT mine is the only one (so far) that has a cleanup routine and runs the same speed on all computers. ;D
(don't look at the while line, it's not nice...)

--- Edit ---
Ok ok, that's not a game at all, but now i've modifided it... it's now as obfuscated as all hell (unintentionally I might add), but now it's a game.
The faster you are moving the more points you get. Dodge the circles. :)

1#include <allegro.h>
2volatile int clicker = 0;
3void ClickerThing(void) {clicker++;} END_OF_FUNCTION(ClickerThing)
4void main(void) { int x = 320, y = 240, x_speed = 0, y_speed = 0, lives = 10, score = 1, i, e[8] = {0, 0, 0, 0, 0, 0, 0, 0};
5 BITMAP *buff;
6 srand(time(NULL)), allegro_init(), install_keyboard(), install_timer(), set_color_depth(16), set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
7 buff = create_bitmap(640, 480);
8 LOCK_FUNCTION(ClickerThing) LOCK_VARIABLE(clicker) install_int(ClickerThing, 5);
9 while (!key[KEY_ESC] && lives>0) { while(lives>0 && (clicker = (clicker > 0)?-1:0) && !key[KEY_ESC]) {
10 clear(buff), triangle(buff, x, y, x, y+10, x+16, y+5, makecol(255-(x>>2), 0, 255-(y>>1)));
11 x_speed = (x_speed > 5 || x > 600)?x_speed-1 :(x_speed < -5 || x < 20)?x_speed+1 :x_speed-key[KEY_RIGHT]+key[KEY_LEFT];
12 y_speed = (y_speed > 5 || y > 440)?y_speed-1 :(y_speed < -5 || y < 20)?y_speed+1 :y_speed-key[KEY_DOWN]+key[KEY_UP];
13 y = ((i=8) && (x=x+x_speed) && (score = (score+(abs(x_speed)+ABS(y_speed))/2)))?y+y_speed :0;
14 while(i-- > 0){ e<i> = (e<i> < 20)?600+rand()%40 :e<i>-2;
15 circle(buff, e<i>, (i%2)?(e<i>*3)%440+20:460-(e<i>*3)%440, 5, makecol(150, 150, 0));
16 e<i> = (e<i>-5 < x+16 && e<i>+3 > x && ((i%2)?(e<i>*3)%440+15:455-(e<i>*3)%440) < y+10 && ((i%2)?(e<i>*2)%440+25:465-(e<i>*3)%440) > y)?lives-- :e<i>; }
17 textprintf(buff, font, 5, 5, makecol(0, 180, 0), "Score: %6d Lives: %2d", score, lives);
18 blit(buff, screen, 0, 0, 0, 0, 640, 480); } }
19 remove_int(ClickerThing), destroy_bitmap(buff), allegro_exit();
20} END_OF_MAIN()

-----------

Matthew Leverton
Supreme Loser
January 1999
avatar

Well, my game is 19 lines long right now, if I tallied correctly. I've only a bit more left before it's playable. Unfortunately, it's bed time. :(

And I'll be lucky if I can remember how it works tomorrow though. :-X

The name of the game is "20 Lines", and it's a dandy. ;)

Thomas Fjellstrom
Member #476
June 2000
avatar

If I can think of something more original than Pong.. I'll post it :)

Ohhh.. maybe Plinko ;)

--
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

miran
Member #2,407
June 2002

Here's my 20 lines:

1#include <allegro.h>
2static volatile float timer = 0.0f, score = 0.0f, highscore = 0.0f, r = 15.0f, clickCounter = 0.0f, x=160.0f, y=195.0f, vx=0.0f, vy=0.0f;
3static void timer_f() { timer += 1.0f; } END_OF_STATIC_FUNCTION(timer_f);
4void main() {
5BITMAP *buffer;
6allegro_init(), install_keyboard(), install_timer(), install_mouse(), set_color_depth(16), set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320, 240, 0, 0), clear(screen), buffer = create_bitmap(SCREEN_W, SCREEN_H), clear(buffer), text_mode(-1);
7LOCK_VARIABLE(timer) LOCK_FUNCTION(timer_f) install_int_ex(timer_f, BPS_TO_TIMER(40));
8while (!key[KEY_ESC]) { while (timer = (timer>0.0f) ? -1.0f : 0.0f) { if ((clickCounter = (clickCounter == 0.0f) ? (mouse_b ? 10.0f : 0.0f) : (clickCounter-1.0f) == 10.0f) && mouse_b && ((x - mouse_x)*(x - mouse_x) + (y - mouse_y)*(y - mouse_y) <= r*r)) vy = 12.0f, vx = (x - mouse_x)/1.5f, score += 1.0f;
9x += (vx *= 0.995f);
10vx = (x < r || x > SCREEN_W) ? (-vx) : (vx);
11y -= ((vy >= 1.0f) ? (vy *= 0.85f) : ((vy <= -1.0f) ? (vy /= 0.88f) : ((vy > 0.0f) ? (vy = -1.0f) : ((vy < 0.0f) ? (vy = 1.0f) : (0.0f)))));
12(y > 195.0f) ? (y = 195.0f, vx *= 0.85f, (vy = (vy >= -1.0f) ? (0.0f) : (-0.7f*vy)), (highscore = (score > highscore) ? (score) : (highscore)), score = 0.0f) : (y); }
13clear_to_color(buffer, makecol(212,218,255));
14textprintf(buffer, font, 0, 0, makecol(0,0,0), "score = %03d, high score = %03d", (int)score, (int)highscore);
15circlefill(buffer, (int)x, (int)y, (int)r, makecol(255,255,255));
16rectfill(buffer, 0, 210, SCREEN_W-1, SCREEN_H-1, makecol(0,200,0));
17draw_sprite(buffer, mouse_sprite, mouse_x, mouse_y);
18blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); }
19remove_int(timer_f), destroy_bitmap(buffer);
20} END_OF_MAIN();

Keep the ball in the air for as long as you can...

EDIT: fixed a little bug...

--
sig used to be here

Thomas Fjellstrom
Member #476
June 2000
avatar

heh. The flash version is fun too... though I bet its more lines of code! ;)

--
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

Here's my first attempt:

1#include <allegro.h>
2int main() {
3 int p_y=240, x[25], y[25], size[25], i, delay=0, lives=10;
4 BITMAP *buffer=NULL;
5 if (!allegro_init() && set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) );
6 if (!install_keyboard() && (buffer=create_bitmap(640, 480))) srand(time(0));
7 for (i=0; i<20; i++)
8 ((x<i>=rand()%640+640)|(y<i>=rand()%480)|(size<i>=rand()%20+5));
9 while (!key[KEY_ESC]) {
10 key[KEY_UP]&&p_y>0 ? p_y=(p_y-2):(key[KEY_DOWN]&&p_y<480? p_y=(p_y+2):0);
11 for (i=0; i<20 && lives>0; i++) {
12 x<i> -= 13-size<i>/2+(delay++)/16384;
13 if ((x<i>-5)*(x<i>-5)+(y<i>-p_y)*(y<i>-p_y) <= (size<i>+5)*(size<i>+5))
14 ((x<i>=rand()%640+640) | (y<i>=rand()%480) | --lives);
15 if (x<i> < 0) ((x<i>=rand()%640+640) | (y<i>=rand()%480));
16 circlefill(buffer, x<i>, y<i>, size<i>, size<i>/2);
17 }
18 circlefill(buffer, 5, p_y, 4, 15);
19 textprintf(buffer, font, 200, 2, 15, "Lives: %d Score: %d", lives, delay);
20 blit(buffer, screen, 0, 0, 0, 0, 640, 480);
21 clear(buffer);
22 rest(10);
23 }
24 return 0;
25} END_OF_MAIN()

The code has the following features:
- It fits in 80x25
- No commas except for variable definitions
- Each line has only one semicolon or bracket {}, except for the semicolons inside the for () expression.
- An indentation scheme.
- And hopefully no warnings under -Wall

miran
Member #2,407
June 2002

There is no newline at the end of file! :P

EDIT:
Actually there are 3 warnings on lines 8, 14 and 15...

--
sig used to be here

Thomas Fjellstrom
Member #476
June 2000
avatar

heh. Mine's only got one warning with -W and -Wall on line 4 ;)

--
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

Now try using -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-declarations -Wno-system-headers. (Also try without -Wno-system-headers if you're a masochist.) ;D

--
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

ooohhhh... and 1 more on line 3... ouch. that hurt. ;)

edit: Quit editing while I'm posting. Its getting anoying ;D

edit2:

Quote:

tf.c:3: warning: no previous declaration for `_mangled_main'
tf.c: In function `_mangled_main':
tf.c:4: warning: left-hand operand of comma expression has no effect

still ouch. ;) and I can't do a bloody thing about _mangled_main.

edit3: Also tried removing -Wno-system-headers. Nothing changed.

--
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

Quote:

EDIT:
Actually there are 3 warnings on lines 8, 14 and 15...

Good thing I said "hopefully"!

Actually when I compile with MinGW 2.0, I get no warnings under -Wall, and a warning on line 5 with -W.

Bruce Perry
Member #270
April 2000

int main(void);
int main(void)
{
   ...

I found -Wno-system-headers was necessary, because one of the earlier flags made it warn for lots of stuff in allegro.h. I suppose it depends on the GCC version.

As for line 5, I always use { }. Personally I don't think they should have removed that warning - you might have written the following by accident:

while (...);
{
   ...
}

--
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.

FrankyR
Member #243
April 2000
avatar

Miran: Awesome game. That should ship with every copy of windows!
Here's my entry:

1#include<allegro.h>
2int main(void){
3 int posx[10]={65,120,180,240,305,360,420,480,540,600},posy[10]={-200,-200,-200,-200,-200,-200,-200,-200,-200,-200},colour[10],dy[10]={1,2,3,3,2,1,2,1,1,2},cx=320,cy=460,score=0,time=2000;
4 if(allegro_init()>=0 && install_keyboard()>=0 && install_timer()>=0 && install_mouse>=0 && set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0) >=0){
5 BITMAP *backgr=create_bitmap(SCREEN_W,SCREEN_H);
6 for(int n=0 ; n<10 ; n++) colour[n]=makecol(rand()%255,rand()%255,rand()%255);
7 while(!key[KEY_ESC]) { clear(backgr);
8 for(int n=0 ; n<10 ; n++, posy[n]+=dy[n]){ circlefill(backgr,posx[n],posy[n],2,colour[n]);
9 if(posy[n]>480){ posy[n]=rand()%100-300;
10 posx[n]=rand()%600+20; }
11 if(posx[n]>cx && posx[n]<cx+50 && posy[n]>cy && posy[n]<cy+20){ score+=dy[n];
12 posy[n]=-100; }}
13 cx+= key[KEY_LEFT]*10 - key[KEY_RIGHT]*10;
14 rectfill(backgr,cx,cy,cx+50,cy+20,makecol(255,200,100));
15 textprintf_centre(backgr,font,SCREEN_W/2,2,makecol(0,255,255),"Score: %d Time Left: %d",score,(time--)/10);
16 blit(backgr,screen,0,0,0,0,SCREEN_W,SCREEN_H);
17 while(!key[KEY_ESC] && time<0){};
18 rest(15); }}
19}END_OF_MAIN();

Just catch as many balls as you can.

nonnus29
Member #2,606
August 2002
avatar

Miran - that is a really cool game; good idea!

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

As for line 5, I always use { }.

Who is that directed at?

--
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

nonnus29
Member #2,606
August 2002
avatar

Quote:

Who is that directed at?

ROFLMAO Now that really made me laugh.

Now if it were line 25 we could narrow it down a little, but line 5 in this thread? No chance.

Thomas Fjellstrom
Member #476
June 2000
avatar

ja.. IIRC I have a while on line 5, but I also have brakets sooo... And whats weird, is Ben seems to think that all those extra -W's will change the messages i get... but neither 2.95.4 or 3.2 makes a difference sooo. heh.

What's weirder, is that if I set a DGA mode in X, my player two score starts off with 1, but If I set a Xwindow full screen mode, player 2's score starts off at 0... Pain in the royal A$$.

--
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



Go to: