Archive for July, 2008

Codeblocks

There was a cool coding challenge over on the Game Developer’s Refuge recently – create a functional game whose source fits into a standard DOS console – i.e. 80×24 characters. I liked it so much that I started a similar challenge at TIGSource!

I’ve also made two entries, one for both contests, I guess 😀 The first is a simple arena shooter, generated by this code:

#include <allegro.h> /* 80x24 Codeblock : Arena Shooter!       30th June '08 */
#include <math.h>    /* By Terry Cavanagh  |  http://www.distractionware.com */
BITMAP *b;class m{public:double x,y,d;int t;};class pc{public:void c(int i, int 
j, double d, int z){p[n].x=i;p[n].y=j;p[n].t=z;p[n].d=d;n++;}void cp(int i, int 
j){p[i].x=p[j].x;p[i].y=p[j].y;p[i].t=p[j].t;p[i].d=p[j].d;}void d(int t){if(
t==n-1)n--;else{for(int i=t;i<n;i++)cp(i,i+1);n--;}}m p[999];int n;};void r(int 
x, int y, int s, int c){rectfill(b, x, y, x+s, y+s, c);}int m(int r,int g,int b
){return makecol(r*63,g*63,b*63);}int main(){allegro_init();srand(time(0));int 
a,w=640,h=480;install_keyboard();double x=315,y=235,d=0,t,s,f,g=0,k=0,u=0,o=0;
b=create_bitmap(w,h);pc p; p.n=0;set_gfx_mode(2,w,h,0,0);while(!key[59]&&u==0){
clear_bitmap(b);for(int i=0;i<p.n;i++){t=p.p[i].x;s=p.p[i].y;o=p.p[i].t;if(o==0
)r(t,s,2,m(4,4,4));if(o==1)r(t,s,15,m(4,0,0));}circlefill(b,x,y,5,m(0,2,4));t=x
+(sin(d)*30);s=y+(cos(d)*30);hline(b,t-3,s,t+3,m(3,3,3));vline(b,t,s-3,s+3,m(3, 
3,3));blit(b,screen,0,0,0,0,w,h);vsync();if(key[84]){x+=sin(d)*4;y+=cos(d)*4;}
if(key[82])d+=0.125;if(key[85]){x-=sin(d)*4;y-=cos(d)*4;}if(key[83])d-=0.125;if
(key[26])f=1;else f=0;if(d<0)d+=6.24;if(d>7)d-=6.24;if(x<0)x=0;if(y<0)y=0;if(x>
w)x=w;if(y>h)y=h;if(g<0){if(f==1){g=10;p.c(x,y,d-0.1,0);p.c(x,y,d+0.1,0);p.c(x,
y,d,0);}}else g--;for(int i=0;i<p.n;i++){t=p.p[i].x; s=p.p[i].y;o=p.p[i].t;if(o
==0){for(int j=0;j<p.n;j++){if(p.p[j].t==1&&abs(p.p[j].x-t)<12&&abs(p.p[j].y-s)
<12)p.d(j);}p.p[i].x+=sin(p.p[i].d)*10;p.p[i].y+=cos(p.p[i].d)*10;if(t<-10||t>w
||s<-10||s>h)p.d(i);}if(o==1){if(t>x)t--;if(t<x)t++;if(s>y)s--;if(s<y)s++;
p.p[i].x=t; p.p[i].y=s;if(abs(t-x)<10&&abs(s-y)<10)u=1;}}if(k<0){k=10;t=rand()%
4;a=rand();if(t==0)p.c(a%w,0,0,1);if(t==1)p.c(a%w,w,0,1);if(t==2)p.c(0,a%h,0,
1);if(t==3)p.c(w,a%h,0,1);}else k--;}return 0;}END_OF_MAIN()

Here’s a downloadable version ‘case you don’t have Allegro to link it with: Download. And here’s a screenshot:

After that success, I decided to try something a bit more ambitious – I made a raycaster maze game. It’s generated by this code:

#include <allegro.h> /* 80x24 Codeblock:  Simple Raycaster Maze  1st Jul '08 */
#include <math.h>    /* Terry Cavanagh   |   http://www.distractionware.com  */
BITMAP *b;int xp,yp,n,m;int map[15][15]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0
,0,0,1,0,1,0,1,0,0,0,0,1},{1,0,2,1,0,0,0,0,0,0,0,0,2,0,1},{1,0,1,1,1,1,1,1,1,1,
1,0,0,0,1},{1,0,0,1,0,0,0,0,0,0,0,0,0,0,1},{1,1,0,1,0,2,2,0,2,2,0,1,1,0,1},{1,0
,0,1,0,2,0,0,0,2,0,1,0,0,1},{1,0,1,1,0,0,0,1,0,0,0,1,0,1,1},{1,0,0,1,0,2,0,0,0,
2,0,1,0,0,1},{1,1,0,1,0,2,2,0,2,2,0,1,1,0,1},{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1},{1
,0,0,0,0,1,1,1,1,1,1,1,1,0,1},{1,0,2,0,0,0,0,0,0,0,0,1,2,0,1},{1,0,0,0,0,1,0,1,
0,1,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};int z(int q,int d){if(q==3)
return makecol(255,0,0);d=128-d;if(d<0)d=0;if(q<2)return makecol(d,d,d);if(q==2
)return makecol(d,d,0);}void r(int x,int y,int s,int c){rectfill(b,x,y,x+10,y+s
,c);}void pl(){m=1;while(m>0){xp=rand()%15; yp=rand()%15;m=map[xp][yp];}map[xp]
[yp]=3;n++;}void re(){map[xp][yp]=m;pl();}int main(){allegro_init();srand(time(
0));install_keyboard();double u=0,x=100,y=100,d=0,c,t,s,q,f,g;int a,w=640,h=480
;set_color_depth(32);set_gfx_mode(2,w,h,0,0);b=create_bitmap(w,h);n=-1;pl();
while(!key[KEY_ESC]&&u==0){acquire_bitmap(b);clear_bitmap(b);rectfill(b,0,240,
640,480,makecol(32,32,32));for(int i=0;i<64;i++){c=0;t=x;s=y;while(map[int(t/
100)][int(s/100)]==0 && c<800){c++;q=d+(i*0.0174)-0.5585;t+=sin(q);s+=cos(q);}c
=(10000/c)/cos(i*0.014);r(i*10,240-c,2*c,z(map[int(t/100)][int(s/100)],2000/c))
;}for(int i=0;i<n;i++){r(5+(i*15),465,10,makecol(255,0,0));}blit(b,screen,0,0,0
,0,w,h);release_bitmap(b);vsync();f=x;g=y;if(key[84]){x+=sin(d)*10;y+=cos(d)*10
;}if(key[82])d-=0.125;if(key[85]){x-=sin(d)*10;y-=cos(d)*10;}if(key[83])d+=
0.125;c=map[int(x/100)][int(y/100)];if(c!=0){if(c==3)re();x=f;y=g;}if(n>=5)u=1;
}return 0;}END_OF_MAIN()

That actually ended up taking up a lot less space; I had to pad it out to 24 lines by creating a bigger map 😛 Here’s the download for that one, and the screenshot:

Check out the challenge post on TIGSource for other people’s entries – and if you can code in anything at all, give it a shot! It’s an interesting challenge 🙂

* Post a Comment

« Previous Page