Allegro.cc - Online Community

Allegro.cc Forums » The Depot » 2007 Allegro ScreenHack, 80x25 compo

This thread is locked; no one can reply to it. rss feed Print
2007 Allegro ScreenHack, 80x25 compo
Kikaru
Member #7,616
August 2006
avatar

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)/////////////////////////////////////////////////
4BITMAP*b;int u=KEY_UP,r=KEY_RIGHT,l=KEY_LEFT,sx=4,sy=0,bx[99],by[99],nb=0,hs=0,
5i=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<
6nb;i++)return((x>bx<i>)&&(y>by<i>)&&(x<bx<i>+16)&&(y<by<i>+16));}void T(){t++;}
7void 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(){////////
9allegro_init();set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);//////////////
10install_keyboard();install_timer();install_int_ex(T,MSEC_TO_TIMER(1));srand(///
11time(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[
14i],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,
15x+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){/////////
18textprintf_centre_ex(b,font,320,240,W,-1,"game over");}blit(b,screen,0,0,0,0,//
19640,480);clear_to_color(b,0);while(t<lt+16)rest(1);ln++;}destroy_bitmap(b);////
20remove_int(T);}END_OF_MAIN()///////////////////////////////////////////////////

Neil Black
Member #7,867
October 2006
avatar

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));///////////////////////////////
6int Square(int a){return a*a;}int main(){allegro_init();install_keyboard();/////
7set_color_depth(16);set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);//////////
8BITMAP* buffer=create_bitmap(640,480);double ex[10];double ey[10];bool ea[10];//
9srand(time(0));double MA=2*3.14;double A2=MA/4;double a=4.78;double x=320.0;////
10double y=440.0;double x2=0.0;double y2=0.0;double bx=0.0;double by=0.0;/////////
11double bs=0.0;double ba=0.0;bool bal=false;int bt=0;int c=makecol(255,255,255);
12int Score=0;while(!key[KEY_ESC]){CC;F{if(ea<i>){if(ex<i><x)ex<i>+=rand()%3+2;///
13if(ex<i>>x)ex<i>-=rand()%3+2;if(ey<i><y)ey<i>+=rand()%3+2;//////////////////////
14if(ey<i>>y)ey<i>-=rand()%3+2;if(sqrt(Square(x-ex<i>)+Square(y-ey<i>))<11){CC;T;
15readkey();break;}if(sqrt(Square(bx-ex<i>)+Square(by-ey<i>))<11){ea<i>=false;////
16bt=0;bal=false;Score+=100;}circle(buffer,ex<i>,ey<i>,10,c);}if(!ea<i>){/////////
17ex<i>=rand()%640;ey<i>=rand()%200;ea<i>=true;}}if(key[KEY_RIGHT])a+=.1;/////////
18if(key[KEY_LEFT])a-=.1;if(key[KEY_SPACE]&&bal==false){bx=x+7*cos(a);////////////
19by=y+7*sin(a);ba=a;bs=15;bal=true;bt=20;}if(bal){bx+=bs*cos(ba);by+=bs*sin(ba);
20bt--;if(bt==0)bal=false;circlefill(buffer,bx,by,2,c);}if(a>MA)a-=MA;////////////
21if(a<0)a+=MA;x2=x+7*cos(a);y2=y+7*sin(a);line(buffer,x,y,x2,y2,c);//////////////
22circlefill(buffer,x,y,3,c);textprintf_ex(buffer,font,2,2,c,-1,"Score: %d",//////
23Score);draw_sprite(screen,buffer,0,0);rest(50);}}END_OF_MAIN()//////////////////
24////////////////////////////////////////////////////////////////////////////////
25////////////////////////////////////////////////////////////////////////////////

kenmasters1976
Member #8,794
July 2007

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

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

Those of you making Tetris should figure out a way to use more color ;).

Mine uses all 16 colors from the classic CGA palette.

Quote:

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.

Arvidsson
Member #4,603
May 2004
avatar

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.

Matthew Leverton
Supreme Loser
January 1999
avatar

1#include <allegro.h> // Tetris by Matthew Leverton
2int j[]={0,1,3,5,7,8,9,1,4,4,4,2,2,2},i,q,r,y,s[]={0xcc00,0x46404e00,0x4c400e40,
30x44c00e20,0x64408e00,0xc4400e80,0x44602e00,0x8c406c00,0x4c80c600,0x44440f00},w[
424],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>=
9i>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=(
13d=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().

Arvidsson
Member #4,603
May 2004
avatar

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
2int 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,
12c){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(!
14key[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];
15if(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>[
16j]==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>[
17j]==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);
18if(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){
24if(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()////

595202

This will be my last entry. Life called.

Kikaru
Member #7,616
August 2006
avatar

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)/////////////////////////////////////////////////
5BITMAP*b;int u=KEY_UP,r=KEY_RIGHT,l=KEY_LEFT,sx=4,sy=0,bx[99],by[99],nb=0,hs=0,
6i=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<
7nb;i++)return((x>bx<i>)&&(y>by<i>)&&(x<bx<i>+24)&&(y<by<i>+24));}void T(){t++;}
8void 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(){////////
10allegro_init();set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);//////////////
11install_keyboard();install_timer();install_int_ex(T,MSEC_TO_TIMER(1));srand(///
12time(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=//
14rand()%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);
16if(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,
1820,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)
20rest(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.

Trezker
Member #1,739
December 2001
avatar

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

Kikaru
Member #7,616
August 2006
avatar

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.

Matthew Leverton
Supreme Loser
January 1999
avatar

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

Kikaru
Member #7,616
August 2006
avatar

I think almost all of my functions were int, but I'll use that for my void functions.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Quote:

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?

They all watch too much MSNBC... they get ideas.

Trezker
Member #1,739
December 2001
avatar

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
2int i,j,*b,a,e,k=0,h[100],t,d;float x[100],y[100],v[100],w[100],X,Y;T(){++t;}
3END_OF_FUNCTION(T)LOCK_VARIABLE(t);LOCK_FUNCTION(T);main(){srand(time(0));x[0]=
4400;y[0]=300;h[0]=99;for(i=1;i<100;++i)h<i>=0;allegro_init();install_keyboard();
5install_timer();install_mouse();install_int(T,50);set_gfx_mode(2,800,600,0,0);b=
6create_bitmap(800,600);while(!key[59]){rest(1);if(h[0]>0){if(!--t)continue;if(
7key[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>=(
10mouse_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<
12100;++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||
14y<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,
15255));}}}circle(b, x[0],y[0],5,makecol(0, 255, 0));circle(b, mouse_x,mouse_y,5,
16makecol(255,255,0));textprintf(b,font,8,580,15,"Kills: %d Health: %d",k,h[0]);}
17else{textprintf(b,font,300,300,15,"Game over: %d kills",k);if(key[KEY_ENTER]){
18x[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,
19600);clear(b);}}END_OF_MAIN()

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

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.

Quote:

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.

CGamesPlay
Member #2,559
July 2002
avatar

int W=-1;;D

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Kikaru
Member #7,616
August 2006
avatar

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.

LennyLen
Member #5,313
December 2004
avatar

Quote:

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.

Mokkan
Member #4,355
February 2004
avatar

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)/////////////////////////////////////////////////
4volatile long s;void isc(){s++;}END_OF_FUNCTION(isc) int main(){allegro_init();
5install_keyboard();install_timer();set_color_depth(desktop_color_depth());/////
6set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);BITMAP * b=create_bitmap(640,
7480);srand(time(0));install_int_ex(isc,BPS_TO_TIMER(50)); r: int p1s=0,p2s=0,p;
8while(1){textout_centre_ex(screen,font,"(1)1 Player, (2)2 Players, (ESC) Quit",
9320,240,W,0);int v=readkey();if(v>>8==KEY_1){p=1;goto s;};if(v>>8==KEY_2) {p=2;
10goto s;}if(v>>8==KEY_ESC)exit(0);} s: float bx=310,by=230,pbx,pby,vbx,vby,pvbx,
11pvby,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>=
14610&&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;}
16if(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]&&
19p2y>1&&p==2)p2y-=6;if(key[KEY_S]&&p2y<409&&p==2)p2y+=6;if(vbx>0&&p==1) {if(p2y+
2035>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(
22screen ); goto r; } s--;} textprintf_ex (b,font,100,2,W,0,"P1 Score: %d", p1s);
23textprintf_ex(b,font,450,2,W,0,"P2 Score: %d",p2s); rectfill (b,p1x,p1y,p1x+20,
24p1y+70,W);rectfill(b,p2x,p2y,p2x+20,p2y+70,W); rectfill(b,bx,by,bx+20,by+20,W);
25blit(b,screen,0,0,0,0,640,480);clear(b);}allegro_exit();return 0;}END_OF_MAIN()

Uses every available character :).

BAF
Member #2,981
December 2002
avatar

Quote:

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

kenmasters1976
Member #8,794
July 2007

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)
5int 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,
61,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,
80,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,
9cm,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
13main(){F[24]=-1;allegro_init();set_color_depth(24);set_gfx_mode(2,256,400,0,0);
14install_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=
1610;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=
1932;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&&(
22F[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"}595211

netcat
Member #6,097
August 2005

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>/*********************************************************/
3int 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()%
6360*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);
8if(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[
9n].x,d[n].y,8,d[n].c);textprintf_ex(b,font,8,8,15,0,"Score: %i",h);}else texto\
10ut_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;
12w=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[
14n].y)<=64;}main(){allegro_init();install_keyboard();install_timer();install_in\
15t_ex(it,BPS_TO_TIMER(60));set_gfx_mode(2,320,240,0,0);srand(time(NULL));b=crea\
16te_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[
22n].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,
250,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"}595212

HardTranceFan
Member #7,317
June 2006
avatar

[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///
2int 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(
3x+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,
4d);}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]||
5x*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;}
6return 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;
8for(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);
9while(!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));
13else{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,
16c(255));blit(b,screen,0,0,0,0,640,480);if(key[KEY_SPACE])n+=10;while(
17key[KEY_SPACE]);f=(x==1&&y==0);rest(50);}if(f){clear(screen);textprintf_ex(
18screen,font,140,230,c(200),-1,"Well done, You escaped in %i moves out of %i.",(
19n+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

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Trezker
Member #1,739
December 2001
avatar

New version of my Crimson game. Color changes, weapon pickups (machine gun and laser), bullet speed no longer (mouse-player)/100.
Ah, and enemies come from all edges.
I think that's all I did. Hmm, enemy health higher too and they spawn at a fixed rate.
I still have some lines left, what should I do now...
#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()
Jeff Bernard
Member #6,698
December 2005
avatar

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++)///////////////////////
3int 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[
4i][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?
61:-15))/10])return j;return 0;}A(){F(1)if(m<i>[0]>-1)return 0;}main(){//////////
7allegro_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));
9F(0)v[i%64][i/64]=rand()%15/14;B=create_bitmap(640,480);while(!key[59]&x[5]){///
10clear_bitmap(B);W("Level:",c,6);W("Score:",x[2],467);rectfill(B,x[0],x[1],x[0]+
1110,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[
14i][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);
15if(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[
170]&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++;
18x[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]){
21v[x[3]/10][(x[4]-10)/10]=0;x[2]+=5;x[3]=-1;}rest(10);blit(B,screen,0,0,0,0,640,
22480);}if(!x[5]){c=(j=c)%15+1;W("Game Over! Score:",x[2],237);W("Press 'Spac\
23e' to restart. Level:",j,251);blit(B,screen,0,0,0,0,640,480);destroy_bitmap(B);
24while(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.

--
I thought I was wrong once, but I was mistaken.



Go to: