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?
nonnus29
Member #2,606
August 2002
avatar

After reading this, I thought, hmmm, that sounds a like a neat idea! There's a screen shot there of a raycaster. So how many lines of code (semicolon delimited for all you cheaters) would it take to make a game in allegro?

#include<allegro.h>

void main(void)  {
   BITMAP *backbuffer=NULL;
   int playing=1;
   allegro_init();
   install_keyboard();
   install_timer();
   set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
   create_bitmap(SCREEN_W,SCREEN_H);
   while(playing)  { main loop in here}
   return;
}
END_OF_MAIN();

So whats that? 11 or 12? That leaves 8 entire lines to play with! Comeon who's up to the challenge?
8-)

23yrold3yrold
Member #1,134
March 2001
avatar

Semicolon delimiter? Eek. Lose the call to install_timer, and combine the buffer declaration with create_bitmap (what; you can't do that in C? Heh heh ;D). And check for key[KEY_ESC] instead of playing. And lose the newline right before END_OF_MAIN(), and the blank line after the #include. Brings it down to 9 lines, with 11 to play with.

create_bitmap() should return a variable to something, I presume ...

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

Bruce Perry
Member #270
April 2000

main() should return int.

Here's my offering:

1#include <allegro.h>
2int main(void) {
3 int x[10]={320,320}, y[10]={460,460,-1,-1,-1,-1,-1,-1,-1,-1}, c[10]={11-12,14-12}, lives=5, i;
4 allegro_init();
5 install_keyboard();
6 set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
7 while (lives > 0 && !key[KEY_ESC]) {
8 x[0] = (x[0] + 640 + (!!key[KEY_RIGHT] - !!key[KEY_LEFT]) * 4) % 640;
9 if ((y[1] -= 10) < 0) x[1]=x[0], y[1]=y[0];
10 for (i = 2; i < 10; i++) {
11 x<i> = y<i>++ >= 0 ? (x<i> + 639 + (rand()&2)) % 640 : rand() % 640;
12 if (MAX(ABS(x<i>-x[1]), ABS(y<i>-y[1])) < 20) y<i> = -1-rand()%64;
13 if (y<i> >= y[0]) y<i> = -1-rand()%64, lives--;
14 }
15 for (i = 0; i < 10; i++) if (y<i> >= 0) circlefill(screen, x<i>, y<i>, 8, c<i>+12);
16 rest(40);
17 for (i = 0; i < 10; i++) if (y<i> >= 0) circlefill(screen, x<i>, y<i>, 8, 0);
18 }
19 return 0;
20} END_OF_MAIN();

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

heh. cool... Its got problems though :) when the ball that is fired reaches the line of balls, all the balls reset (or so it seems).

--
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
"Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes." -- LordBob

Bruce Perry
Member #270
April 2000

Right - I was still working on it. You happened to get the first, severely broken version. Trust me to be overzealous :P

Finalised now :)

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

Karadoc ~~
Member #2,749
September 2002
avatar

Bruce Perry; that's some damn nice work!
To tell you the truth, I didn't think anyone would be able to do it (and have it still look like a game).

-----------

Thomas Fjellstrom
Member #476
June 2000
avatar

heh teh collision detection is a bit loose. but I like it :)

--
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
"Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes." -- LordBob

Evert
Member #794
November 2000
avatar

:o

And it's actually sortof fun too!

miran
Member #2,407
June 2002

:o:o

Do you want to see my "game" that does more or less the exact same thing? No less than 539 lines!

--
sig used to be here

spellcaster
Member #1,493
September 2001
avatar

Here's another 20 line game:

1#include <allegro.h>
2int main(int argc, char** argv) {
3 float speed_x = 0, speed_y = 0, gravity = 1.0, accel = 2.5, pos_x=320,pos_y=200, d;
4 int obj_x[10], obj_y[10],a, init = 1, ships = 4, score = 0, count = 0;
5 BITMAP *buffer;
6 allegro_init(), set_color_depth(16), set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0), install_keyboard(), install_timer(), clear(screen), buffer = create_bitmap(SCREEN_W, SCREEN_H), clear(buffer);
7 while (ships && !key[KEY_ESC]) {
8 clear(buffer), circlefill(buffer, (int)pos_x, (int) pos_y, 10, makecol(200,200,200));
9 if ((init = (count <= 0))) for (a=0; a < 10; a++) obj_x[a] = rand()%SCREEN_W, obj_y[a] = rand()%SCREEN_H, count = 5, textout_centre(buffer, font, "Get Ready", 320, 100, makecol(230,230,230));
10 pos_x = (pos_x > SCREEN_W && speed_x > 0.0) ? -10 : (pos_x < 10 && speed_x < 0.0) ? SCREEN_W : pos_x + speed_x;
11 pos_y = (pos_y > SCREEN_H && speed_y > 0.0) ? -10 : (pos_y < 10 && speed_y < 0.0) ? SCREEN_H : pos_y + speed_y;
12 speed_x = MID(-14.0, speed_x + (key[KEY_LEFT] ? (-accel) : (key[KEY_RIGHT] ? accel : 0)), 12.0);
13 speed_y = MID(-14.0, speed_y + (key[KEY_UP] ? (-accel) : (key[KEY_DOWN] ? accel : 0)) + gravity, 12.0);
14 for (a=0; a < 10; a++)
15 if ((d = obj_x[a] >= 0 ? ((pos_x - obj_x[a]) * (pos_x - obj_x[a]) + (pos_y - obj_y[a]) * (pos_y - obj_y[a])) : -10) >=0)
16 circlefill(buffer, obj_x[a], obj_y[a], 10, a >=5 ? makecol(200,0,0) : makecol(0,200,0)), ships -= (a>=5 && d < 400)? 1 : 0, count -= (a<5 && d < 400)? 1 : 0, score += (a<5 && d < 400)? 10 : 0,obj_x[a] = d < 400 && d >=0 ? -1 : obj_x[a];
17 rest(60), textprintf_centre(buffer, font, 320, 10, makecol(100,100,230), "Score:%04i Ships: %02i", score, ships), blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
18 if (init) init = 0, rest(1200);
19 } return 0;
20} END_OF_MAIN();

Collect all the green balls. Don't hit the red balls. You can hit up to 4 red balls before it's game over... unlimited number of dynamic generated levels.
:)

Oh, and if I only count the semicolons, I could get it down to 14 lines.

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

Bruce Perry
Member #270
April 2000

My own post said:

main() should return int.

Nice game though. Much less subtle abuse of Teh Almighty Comma ;D

miran: don't worry - supposing I wanted to extend my game, there's only so far I could go without a complete rewrite. Small hacks are like that. Your game is bound to be more extensible, and the code more readable :)

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

spellcaster
Member #1,493
September 2001
avatar

Quote:

Nice game though. Much less subtle abuse of Teh Almighty Comma

Hm.
Ok, if you have to nitpick... I changed my code, it's still 20 lines and uses now less (cheat) commata than yours :)

1#include <allegro.h>
2int main(int argc, char** argv) {
3 float speed_x = 0, speed_y = 0, gravity = 1.0, accel = 2.5, pos_x=320,pos_y=200, d;
4 int obj_x[10], obj_y[10],a, ships = 4, score = 0, count = 0;
5 BITMAP *buffer = allegro_init() ? NULL : install_keyboard() ? NULL : install_timer() ? NULL : set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) >= 0 ? create_bitmap(SCREEN_W, SCREEN_H) : NULL;
6 while (ships && !key[KEY_ESC]) { clear(buffer);
7 circlefill(buffer, (int)pos_x, (int) pos_y, 10, makecol(200,200,200));
8 if (count <= 0) textout_centre(buffer, font, "Get Ready", 320, 100, makecol(230,230,230));
9 pos_x = (pos_x > SCREEN_W && speed_x > 0.0) ? -10 : (pos_x < 10 && speed_x < 0.0) ? SCREEN_W : pos_x + speed_x;
10 pos_y = (pos_y > SCREEN_H && speed_y > 0.0) ? -10 : (pos_y < 10 && speed_y < 0.0) ? SCREEN_H : pos_y + speed_y;
11 speed_x = MID(-14.0, speed_x + (key[KEY_LEFT] ? (-accel) : (key[KEY_RIGHT] ? accel : 0)), 12.0);
12 speed_y = MID(-14.0, speed_y + (key[KEY_UP] ? (-accel) : (key[KEY_DOWN] ? accel : 0)) + gravity, 12.0);
13 for (a=0; a < 10; a++) { if ((d = obj_x[a] >= 0 ? ((pos_x - obj_x[a]) * (pos_x - obj_x[a]) + (pos_y - obj_y[a]) * (pos_y - obj_y[a])) : -10) >=0) { circlefill(buffer, (count <= 0) ? obj_x[a] = rand()%SCREEN_W : obj_x[a], (count <= 0) ? obj_y[a] = rand()%SCREEN_H : obj_y[a], 10, a >=5 ? makecol(200,0,0) : makecol(0,200,0));
14 ships -= (a>=5 && d < 400)? 1 : 0, count -= (a <5 && d < 400)? 1 : 0, score += (a <5 && d < 400)? 10 : 0,
15 obj_x[a] = d < 400 && d >=0 ? -1 : obj_x[a];}}
16 rest(60);
17 textprintf_centre(buffer, font, 320, 10, makecol(100,100,230), "Score:%04i Ships: %02i Remaining: %i", score, ships, count <= 0 ? count = 5 : count), blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
18 } return 0;
19} END_OF_MAIN();

And please, don't force me to change that code again :)

Quote:

and combine the buffer declaration with create_bitmap (what; you can't do that in C? Heh heh ;D).

You can combine the buffer decl with create_bitmap :)

Yeah.
Managed to cut one more line :)

BITMAP *buffer = allegro_init() ? NULL : install_keyboard() ? NULL : install_timer() ? NULL : set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) >= 0 ? create_bitmap(SCREEN_W, SCREEN_H) : NULL;

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

Evert
Member #794
November 2000
avatar

I'd call that ?: abuse, but hey, it works, right ;)

How about it? A competition in who makes the best game with 20 lines of code?

spellcaster
Member #1,493
September 2001
avatar

My "de-commat" version also has a bug during level increase, so I suggest you guys use my first version.

Quote:

I'd call that ?: abuse, but hey, it works, right ;)

You have to scramble some eggs :)
Oh, and with 40 lines of code, you might even be able to do a cool game without cheating (both Bruce and I cheated by using commata...
Using the trinary op is IMHO not really cheating. I mean, the allegro overhead itself is pretty high (if you don't use trinary ops, that is) :D

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

Rash
Member #2,374
May 2002
avatar

return 0; can be omitted in both C++ as well as C99.

kazzmir
Member #1,786
December 2001
avatar

I think that would be a neat contest, maybe if the rules were a little less strict like 100 lines maybe. This would force people to be creative and learn to write effecient code!

Zaphos
Member #1,468
August 2001

Efficient? I think you mean "condensed"! It's still a fun exercise ...

Interestingly enough, all the 20 liners written so far have actually been 19 lines; since they're semicolon delimeted the "int main(){" doesn't count as a line.

Well, neither does #include <allegro> but the need for a return after it implies that it should be counted as a line anyway, I guess ...

Johan Henriksson
Member #11
April 2000
avatar

20 lines... just enough to fit in my comment describing the purpose of the main function ^_^ Be happy we aren't competing in fattest documentation >:)

Zaphos
Member #1,468
August 2001

Just realized that by a literal interpretation of "semicolon delimited" a for (;;) statement requires 3 lines ... heh, but you can fit "squeeze" a program by fitting statements like "x = 2" into "if (x=2) the_next_line; ... so we can rewrite bruce's program like this:

1#include <allegro.h>
2int main(void) {
3 int x[10]={320,320}, y[10]={460,460,-1,-1,-1,-1,-1,-1,-1,-1}, c[10]={11-12,14-12}, lives=5, i;
4 if (allegro_init()>=0&&install_keyboard()>=0&&set_gfx_mode(GFX_AUTODETECT,640,480,0,0)>=0){
5 while (lives > 0 && !key[KEY_ESC]) { if (x[0] = (x[0] + 640 + (!!key[KEY_RIGHT] - !!key[KEY_LEFT]) * 4) % 640) if ((y[1] -= 10) < 0) if (x[1]=x[0], y[1]=y[0]) for (i = 2;
6 i < 10;
7 i++) if (x<i> = y<i>++ >= 0 ? (x<i> + 639 + (rand()&2)) % 640 : rand() % 640) if (MAX(ABS(x<i>-x[1]), ABS(y<i>-y[1])) < 20) if(y<i> = -1-rand()%64) if (y<i> >= y[0]) if (y<i> = -1-rand()%64, lives--);
8 for (i = 0;
9 i < 10;
10 i++) if (y<i> >= 0) circlefill(screen, x<i>, y<i>, 8, c<i>+12);
11 rest(40);
12 for (i = 0;
13 i < 10;
14 i++) if (y<i> >= 0) circlefill(screen, x<i>, y<i>, 8, 0); }}
15 return 0;
16} END_OF_MAIN();

Edit: My snippet is messed up, but hopefully you get the point.

Johan Henriksson
Member #11
April 2000
avatar

I think we rather change the contest to "what game can you squeeze in into 20 lines and 20 columns? :)

Zaphos
Member #1,468
August 2001

Oh yeah; that's definitely the coolest idea. You just get a grid of 20 by 20 characters to fill up, and try to make a game ... and there is no such thing as cheating. Perhaps it would be more interesting to go 100x15 or something, though.

Do we want to do a compo around this? It could be fun! 8-)

spellcaster
Member #1,493
September 2001
avatar

Heh. That idea is great :)
Why not 80x25? That would be one textmode screen full of code :)

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

kazzmir
Member #1,786
December 2001
avatar

It is a good idea,. but why limit it to certain dimensions if all that means is a certain number of characters? 100x15 means a total of 1500 characters. 80x25 means a total of 2000 characters. So why not make the contest a total character contest instead of a dimensional contest?

Zaphos
Member #1,468
August 2001

I dunno ... I thought the dimensional thing added a neat little cuteness / originality; it's essentially the same as a char-count but more restricting and encourages a bit more interesting results by forcing the coder to focus on the graphical appearance of the code itself ...

kazzmir
Member #1,786
December 2001
avatar

thats true, but to maximize your winning chances, you would have to write as much code on one line as possible. in the end, you will have 25 lines of completely filled code. it wouldnt be hard for me to write some code, take out all the tabs, make sure every line ends at exactly 80 characters, and only have 1 space between every token. the dimensions dont force me to do anythign except have a certain number of characters.



Go to: