File: colecobullet.c - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

001: /*
002: Voici un code source ColecoVision qui fait je pense ce que tu cherches.
003: Ca utilise un algo de bresenham modifi�. En gros, il y � un vaisseau au milieu de l'�cran et
004:  toi du tourne autour, et lui tire vers toi quelquesoit ta position dans l'�cran. J'esp�re que �a peut t'aider ...
005: */
006: 
007: #include <string.h>
008: #include <coleco.h>
009: #include <getput1.h>
010: 
011: #define MAXBULLET 8
012: struct Bullet {
013:     byte flags;
014:     byte sprite;
015:     byte dx,dy;
016:     int err;
017: } bullet[MAXBULLET];
018: #define BUL_ACTIVE 0x80
019: #define BUL_UP 0x1
020: #define BUL_LEFT 0x2
021: 
022: void updatebullet(int id)
023: {
024:    byte s=bullet[id].sprite;
025:    byte dx=bullet[id].dx;
026:    byte dy=bullet[id].dy;
027:    int e2;
028: 
029:    if((bullet[id].flags&BUL_ACTIVE)==0) return; // not active.
030:    
031:    if(sprites[s].x==255 || sprites[s].x==0 || sprites[s].y==0 || sprites[s].y==191) {
032:       bullet[id].flags=0; // sprite inactive.
033:       sprites[s].y=192; // off screen.
034:    }
035:    e2=bullet[id].err<<1;
036:    if( e2 > -dy) {
037:       bullet[id].err-=dy;
038:       sprites[s].x+=(bullet[id].flags&BUL_LEFT)?-1:1;
039:    }
040:    if( e2 < dx ) {
041:       bullet[id].err+=dx;
042:       sprites[s].y+=(bullet[id].flags&BUL_UP)?-1:1;
043:    }
044: }
045: 
046: int newsprite(int id,int x0,int y0)
047: {
048:    const int reserved=2;
049:    sprites[id+reserved].x=x0;   // reserve first two sprites for player and enemy.
050:    sprites[id+reserved].y=y0;
051:    sprites[id+reserved].pattern=8;  // Our bullet pattern
052:    sprites[id+reserved].colour=15;   // VDP_WHITE
053:    return id+reserved;
054: }
055: 
056: int newbullet(int x0,int y0,int x1,int y1)
057: {
058:    int i;
059:    for(i=0;i<MAXBULLET;i++) {
060:       if(bullet[i].flags&BUL_ACTIVE) continue;
061:       /* found an unused bullet, so let's set it up.*/
062:       bullet[i].sprite=newsprite(i,x0,y0);
063:       bullet[i].flags=BUL_ACTIVE;
064:       bullet[i].flags|=(x0<x1)?0:BUL_LEFT;
065:       bullet[i].flags|=(y0<y1)?0:BUL_UP;
066:       bullet[i].dx=(x0<x1)?x1-x0:x0-x1;
067:       bullet[i].dy=(y0<y1)?y1-y0:y0-y1;
068:       bullet[i].err=bullet[i].dx-bullet[i].dy;
069:       return i;
070:    }
071:    return 0;   // all bullets still active.
072: }
073: 
074: const byte shipsPat[]={
075:    0x0f,0x1f,0x3f,0xff,0xff,0x3f,0x1f,0x0f,
076:    0x0f,0x1f,0x3f,0xff,0xff,0x3f,0x1f,0x0f,
077:    0xf0,0xf8,0xfc,0xff,0xff,0xfc,0xf8,0xf0,
078:    0xf0,0xf8,0xfc,0xff,0xff,0xfc,0xf8,0xf0,
079: 
080:    0x10,0x30,0xc3,0xcf,0xcf,0x3c,0x30,0x10,
081:    0x10,0x30,0xc3,0xcf,0xcf,0x3c,0x30,0x10,
082:    0x08,0x0c,0xc3,0xf3,0xf3,0x3c,0x0c,0x08,
083:    0x08,0x0c,0xc3,0xf3,0xf3,0x3c,0x0c,0x08,
084: 
085:    0x60,0xd0,0xf0,0x60,0,0,0,0,
086:    0,0,0,0,0,0,0,0,
087:    0,0,0,0,0,0,0,0,
088:    0,0,0,0,0,0,0,0,
089: };
090: 
091: void main()
092: {
093:    int i;
094:    int frame=0;
095: 
096:    screen_mode_1_text();
097:    sprites_16x16();
098:    change_spattern(0,shipsPat,3*4);
099:    sprites[0].x=128;
100:    sprites[0].y=96;
101:    sprites[0].pattern=0;
102:    sprites[0].colour=11;
103:    sprites[1].x=192;
104:    sprites[1].y=64;
105:    sprites[1].pattern=4;
106:    sprites[1].colour=13;
107:    for(i=2;i<32;i++) sprites[i].y=0xd0;
108:    updatesprites(0,12);
109:    fill_color(0,0x61,255);
110:    load_ascii();
111:    cls();
112:    screen_on();
113:    print_at(2,0,"Stop, or I'll say stop again!");
114: 
115:    for(;;) {
116:      
117:       delay(1);
118:       for(i=0;i<MAXBULLET;i++) {
119:          updatebullet(i);
120:          updatebullet(i);   // 2 pixels per 60th.
121:       }
122:       if((frame&15)==0) newbullet(sprites[0].x+8,sprites[0].y+8,sprites[1].x+8,sprites[1].y+8);
123:       frame++;
124:       // user to dodge the bullets.
125:       disable_nmi();
126:       if(joypad_1&LEFT) sprites[1].x--;
127:       if(joypad_1&RIGHT) sprites[1].x++;
128:       if(joypad_1&UP) sprites[1].y--;
129:       if(joypad_1&DOWN) sprites[1].y++;
130:       enable_nmi();
131:      
132: 
133: 
134:       // draw the effects.
135:       updatesprites(0,12);
136:    }
137: }
138: 
139: void nmi()
140: {
141:    ;
142: }
143: