![]() |
|
An Allegro Game in 20 lines of code - Possible? |
Hard Rock
Member #1,547
September 2001
![]() |
Allright heres my entry, Nibbles/Snake 19 lines, or 20 if you count the comment on the top
I could probably cheat and drop a lot more lines, but it works fine as is... well maybe is should make the score a long.................. becuase it wont take long for the int to overfill. _________________________________________________ |
Goodbytes
Member #448
June 2000
![]() |
Well, after seeing that Hard Rock has done a nibbles/snake game, I don't know if my entry is going to be very impressive, but here goes. It's one of those 15-square puzzles. Enjoy!
It's 20 lines, and it's pretty much normal C-style indenting except for the fact that all of the lines are stuck together, and ignoring that little commanated 14th line that I hope no one notices [EDIT] By the way, are we going to do the whole 80x25 screen thing? I think it would be pretty damn cool. The rules are straightforward enough... let's say one file, must fit in 80x25 screen, absolutely no exceptions, no external files, and we post them in a thread in a week or so? What do you guys say? If whoever suggested the idea doesn't want to, I'll organize it |
Steve Terry
Member #1,989
March 2002
![]() |
goodbytes... quit is not initialized ___________________________________ |
Matthew Leverton
Supreme Loser
January 1999
![]() |
Quote: lines of code (semicolon delimited for all you cheaters) Ok, by strict interpretation of the rules, my game is 16 lines long. The name of the game is "20 Lines". Interesting fact (to me anyway) is that it only one variable is created in the process (and a second one use used for static data). NO cheating here. I would not stoop to the level of using commas! No warnings are generated during compilation using -W -Wall. And yes, I wrote it all by hand. Thank Bill Gates for VC.NET which easily lets me see which ( [ { pair with } ] )... The controls are a bit laggy, I never got around to updating that portion of the code. View it here: [url http://www.leverton.cc/misc/20lines.html] http://www.leverton.cc/pics/20lines.png |
Thomas Fjellstrom
Member #476
June 2000
![]() |
You my friend are officially insane. Welcome to the club. I ddin't think it would be possible to do a tetris clone.. But here you've proved me wrong. One thing though. The fact that it's windowed scores against it edit: Oh, so the controlls really are laggy? ok. I thought that was the fault of the driver. I know my pong is laggy in windowed mode... -- |
Matthew Leverton
Supreme Loser
January 1999
![]() |
Well, there's no reason it cannot be full screen. Just AUTODETECT_FULLSCREEN and set real screen size. Also, on the final buffer->screen blit, you wouldn't have to do a SCREEN_W,SCREEN_H as that would do a lot of extra black space. The main reason it's laggy is that I only allow the Key's to be pressed at certain times, that way they blocks don't just slide around like crazy. It works fine on my XP 1800 PC, but is a bit laggy on my C333 running FreeBSD. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
so your code draws it all centered or something? So setting a large mod wouldn't do anything weird? edit: Guess not. -- |
Matthew Leverton
Supreme Loser
January 1999
![]() |
Thomas Fjellstrom
Member #476
June 2000
![]() |
ja. that should work. You should think about submitting that to the ioccc one year. edit: w00t. and it only took an hour. /*1 */int main(void) { int i=0,x=320,y=rand()%480,dx=-1,dy=1,p1y=240,p2y=240,p1p=0,p2p=0,y1d=10,y2d=470, xs[] = {320, 10, 630}, *y1s[] = { &y1d, &p1y, &p2y }, *y2s[] = { &y2d, &p1y, &p2y }; /*2 */ BITMAP *buff = (allegro_init() == 0 && install_keyboard() == 0 && set_gfx_mode(GFX_AUTODETECT,640,480,0,0) == 0) ? create_bitmap(SCREEN_W, SCREEN_H) : NULL; /*3 */ while(buff && !key[KEY_ESC] && !key[KEY_Q]) { clear(buff); /*4 */ ( ((x += dx) | (y += dy)) && (y-2 <= 0 || y+2 >= 480) ) ? (dy = -dy) : ( ( (x-2 <= 10) && (p1y-12 <= y-2 && y+2 <= p1y+12)) ? (dx = -dx) : ( (x-2 <= 10) ? (++p2p, (x=320), (y=rand()%480)) : ( ( (x+2 >= 630) && (p2y-12 <= y-2 && y+2 <= p2y+12)) ? (dx = -dx) : ( (x+2 >= 630) ? (++p1p, (x=320), (y=rand()%480)) : 1)))); /*5 */ (key[KEY_UP] && p2y > 12) ? (( (key[KEY_DOWN] && p2y < 468) ? (p2y+=2) : 1) && (p2y-=2)) : ( (key[KEY_DOWN] && p2y < 468) ? (p2y+=2) : 1); /*6 */ (key[KEY_W] && p1y > 12) ? (( (key[KEY_S] && p1y < 468) ? (p1y+=2) : 1) && (p1y-=2)) : ( (key[KEY_S] && p1y < 468) ? (p1y+=2) : 1); /*7 */ textprintf_centre(buff, font, 320, 2, makecol(200,200,200), "%03i %03i", p1p, p2p); /*8 */ for(i=0; i < 3; ++i) vline(buff, xs<i>, (*y1s<i>)-10, (*y2s<i>)+10, makecol(255,255,255)); /*9 */ circlefill(buff, x-2, y-2, 2, makecol(255,255,255)); /*10*/ blit(buff, screen, 0, 0, 0, 0, 640, 480); /*11*/ } return 0; /*12*/}END_OF_MAIN(); notes: This new version should be equivelent to the last, including the same bugs, as its the same code, just reoganized a bit. -- |
nonnus29
Member #2,606
August 2002
![]() |
Bravo Matthew! Line 7 is the longest line of code I have ever seen. I was trying to think of how to do Tetris clone in that amount of space. You must have really been 'in the zone' for this one! EDIT: We've got a two player pong, a tetris, snakes, a couple of shooters, a bouncy ball game, a tile puzzle, some shooters... come on people, Who's going to take us into THE THIRD DIMENSION?? |
miran
Member #2,407
June 2002
|
Quote: THE THIRD DIMENSION
Funny you should mention that...
It's not exactly 20 lines, it's 22 but two of them are includes. Also it is in C++ not C and I cheated big time on this one with the commas and all that. It's impossible to make a raytracer in 20 lines whithout cheating. Oh yeah, it's a raytracer so it should be pretty slow on low end machines... -- |
Evert
Member #794
November 2000
![]() |
Very impressive so far! Too bad I couldn't run Miran's code... |
miran
Member #2,407
June 2002
|
Why not? Do you have a slow machine or does the code not work/compile? -- |
Evert
Member #794
November 2000
![]() |
Nothing you can fix: eglebbk@huygens: ~/Program/20>g++ -W -Wall miran.cc -o miran `allegro-config --libs` miran.cc: In function `void _mangled_main()': miran.cc:13: warning: unused variable `int c' eglebbk@huygens: ~/Program/20>./miran ld.so.1: ./miran: fatal: libstdc++.so.2.10.0: open failed: No such file or directory Killed
I'll try on my own Linux box later. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Worked for me. It was a bit slow, But it shul be fine in Full screen mode. Yup. the ESC key reacts alot quicker in full screen mode -- |
Goodbytes
Member #448
June 2000
![]() |
Steve Terry said: goodbytes... quit is not initialized set it to 0 otherwise sometimes it will just exit immediately like it did to me. No, it's... it's... damn. Blame that on untested code changes. p.s. Miran and Matthew, you are el337 h4XX0r5. |
Evert
Member #794
November 2000
![]() |
Anyone done TicTacToe already? No? Fine here's mine So who'se going to post the first RPG?
EDIT: fixed some minor bugs. |
miran
Member #2,407
June 2002
|
Doesn't work in Windows, at least not on my box. Had to change the window size to 120x120... -- |
Evert
Member #794
November 2000
![]() |
Ah, yes - Allegro has this limit that doesn't allow you to make a window of less than 100 pixels! |
Paul Pridham
Member #250
April 2000
![]() |
Yesterday I was a bit inspired by you guys, and so I made a little game. It's your standard "ship plummeting ever faster down the endless tunnel" game. Fun for a diversion at least. Get it here: http://www3.sympatico.ca/ppridham/misc/games/LongWayDown.zip Yes, it's all written in main(), ---- |
Evert
Member #794
November 2000
![]() |
Ok, an updated version of my TicTacToe:
And a game for two players: fox and geese. #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])), drawme |= !((key[KEY_S])?turn=0:1):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])), drawme |= !((key[KEY_A])?turn=0:1):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])), drawme |= !((key[KEY_Q])?turn=0:1)):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])), drawme |= !((key[KEY_W])?turn=0:1)):0 ):( sgoose=( sgoose + (key[KEY_TAB]?(drawme=1):0) )&3, (goose[sgoose]%8!=0 && goose[sgoose]>8) ? (goose[sgoose] += (goose[sgoose]-9==fox?0:9*key[KEY_LEFT]), drawme |= ((key[KEY_LEFT])?turn=1:0)):0, (goose[sgoose]%8!=7 && goose[sgoose]>8) ? (goose[sgoose] += (goose[sgoose]-7==fox?0:7*key[KEY_RIGHT]), drawme |= ((key[KEY_RIGHT])?turn=1:0)):0); if (drawme) for(c=0;c<64;c++){ scare_mouse(), DRAWTILE(c), unscare_mouse(), drawme = 0; } rest(50); done = (fox>56 || (goose[0]<8 && goose[1]<8 && goose[2]<8 && goose[3]<8) ) ? 1:0; } while(keypressed()) readkey(); done?textprintf_centre(screen, font, SCREEN_W/2, SCREEN_H/2, 2, (fox>56)?"Fox wins!":"Geese win!"), readkey():0; return 0; } END_OF_MAIN()
|
Zaphos
Member #1,468
August 2001
|
Matthew: Heh, I never knew MSVC had a 2048 character per line limit ... but now that I tried to copy paste your program into it, now I know! ... see what useful information I get from this site? Goodbytes: I'd love to have a 25x80 compo! Judging from the programs already written, I think we'd see some cool stuff ...
|
Goodbytes
Member #448
June 2000
![]() |
Quote: Goodbytes: I'd love to have a 25x80 compo! Judging from the programs already written, I think we'd see some cool stuff ... That sounds like an invitation! Well, then it's official... and one day, I will be the Contest Ruler of Allegro!!!!! hey... there's no evil smiley. That is unfortunate. Anyways... okay then. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Quote: there's no evil smiley. That is unfortunate.
well... hopefully http://strangesoft.net/kefka.gif will suffice! -- |
23yrold3yrold
Member #1,134
March 2001
![]() |
Quote: there's no evil smiley. Sure there is. It just doesn't work -- |
|
|