cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

dscan.c (21444B)


      1/********************************************************************
      2 * Color Deep Scan                                                  *
      3 *  by                                                              *
      4 * Mr. N.U. of TeamKNOx                                             *
      5 ********************************************************************/
      6
      7#include <gb/gb.h>
      8#include <gb/cgb.h>
      9#include <stdint.h>
     10#include <stdlib.h>
     11#include <rand.h>
     12
     13/* bitmaps */
     14#include "bkg.h"
     15#include "bkg.c"
     16#include "bkg_m.c"
     17#include "bkg_c.c"
     18#include "fore.h"
     19#include "fore.c"
     20
     21/* ************************************************************ */
     22
     23const uint16_t bkg_p[] =
     24{
     25  bkgCGBPal0c0,bkgCGBPal0c1,bkgCGBPal0c2,bkgCGBPal0c3,
     26  bkgCGBPal1c0,bkgCGBPal1c1,bkgCGBPal1c2,bkgCGBPal1c3,
     27  bkgCGBPal2c0,bkgCGBPal2c1,bkgCGBPal2c2,bkgCGBPal2c3,
     28  bkgCGBPal3c0,bkgCGBPal3c1,bkgCGBPal3c2,bkgCGBPal3c3,
     29  bkgCGBPal4c0,bkgCGBPal4c1,bkgCGBPal4c2,bkgCGBPal4c3,
     30  bkgCGBPal5c0,bkgCGBPal5c1,bkgCGBPal5c2,bkgCGBPal5c3,
     31  bkgCGBPal6c0,bkgCGBPal6c1,bkgCGBPal6c2,bkgCGBPal6c3,
     32  bkgCGBPal7c0,bkgCGBPal7c1,bkgCGBPal7c2,bkgCGBPal7c3
     33};
     34
     35const uint16_t obj_p[] =
     36{
     37  foreCGBPal0c0,foreCGBPal0c1,foreCGBPal0c2,foreCGBPal0c3,
     38  foreCGBPal1c0,foreCGBPal1c1,foreCGBPal1c2,foreCGBPal1c3,
     39  foreCGBPal2c0,foreCGBPal2c1,foreCGBPal2c2,foreCGBPal2c3,
     40  foreCGBPal3c0,foreCGBPal3c1,foreCGBPal3c2,foreCGBPal3c3,
     41  foreCGBPal4c0,foreCGBPal4c1,foreCGBPal4c2,foreCGBPal4c3,
     42  foreCGBPal5c0,foreCGBPal5c1,foreCGBPal5c2,foreCGBPal5c3,
     43  foreCGBPal6c0,foreCGBPal6c1,foreCGBPal6c2,foreCGBPal6c3,
     44  foreCGBPal7c0,foreCGBPal7c1,foreCGBPal7c2,foreCGBPal7c3
     45};
     46
     47/* screen size */
     48#define MIN_SX		5U		/* min x (char) */
     49#define MAX_SX		(20U-MIN_SX)    /* max x (char) */
     50#define MIN_SY		5U		/* min y (char) */
     51#define MAX_SY		(MIN_SY+13U)	/* max y (char) */
     52
     53#define DEF_SP		30U		/* sprite null char code */	
     54
     55/* player */
     56#define MIN_PX		(MIN_SX*8U+8U)	/* min x (dot) */
     57#define MAX_PX		(MAX_SX*8U-8U)	/* max x (dot) */
     58#define DEF_PX		80U		/* ship x pos (dot) */
     59#define DEF_PY		(MIN_SY*8U)	/* ship y pos (dot) */
     60#define DEF_PC0		14U
     61#define DEF_PC1		15U
     62#define DEF_PF		8U
     63
     64/* bomb */
     65#define MAX_TT		6U		/* number */
     66#define DEF_TS		2U		/* sprite tile id */
     67#define DEF_TC		2U		/* sprite data id */
     68#define DEF_TX		(80U-6U)	/* bomb x pos (dot) */
     69#define DEF_TY		(DEF_PY-14U)	/* bomb y pos (dot) */
     70#define MAX_TY		(MAX_SY*8U)	/* max y pos (dot) */
     71
     72/* enemy */
     73#define MAX_ET		10U		/* number */
     74#define DEF_ES0		(DEF_TS+MAX_TT) /* sprite tile id */
     75#define DEF_ES1		(DEF_ES0+1U)
     76#define DEF_1EC0	32U
     77#define DEF_1EC1	48U
     78#define DEF_2EC0	64U
     79#define DEF_2EC1	80U
     80#define DEF_XEC0	96U		/* sprite data(X) id */
     81#define DEF_XEC1	112U		/* sprite data(X) id */
     82#define DEF_EY		(DEF_PY+12U)
     83#define DEF_EH		10U
     84#define SUB_EX0		20U
     85#define SUB_EX1		(SUB_EX0-8U)
     86#define MIN_EX		(SUB_EX0-16U)
     87#define MAX_EX		(SUB_EX0+180U)
     88#define SPEED_EY	(DEF_EY+DEF_EH*3U)
     89#define DEF_BC1		4U
     90#define DEF_BC2		5U
     91
     92/* kirai */
     93#define MAX_KT		12U
     94#define DEF_KS		(DEF_ES0+MAX_ET*2U)	/* sprite tile id */
     95#define DEF_KC		4U		/* sprite data id */
     96#define DEF_KX		0U		/* bomb x pos (default_dot) */
     97#define DEF_KY		0U		/* bomb y pos (default_dot) */
     98#define MIN_KY		(DEF_PY+1U)	/* min y pos (dot) */
     99
    100unsigned char msg_tile[64];
    101
    102const unsigned char * const msg_1up   = "1UP";
    103const unsigned char * const msg_lv    = "LV";
    104
    105const unsigned char * const msg_gover = "GAMEOVER";
    106const unsigned char * const msg_pause = " PAUSE! ";
    107const unsigned char * const msg_start = "        ";
    108
    109uint8_t pf, px, pp, pl;
    110uint16_t pw;
    111uint16_t ps;
    112uint8_t tf[MAX_TT];
    113uint8_t tx[MAX_TT], ty[MAX_TT];
    114uint8_t ef[MAX_ET], ex[MAX_ET], ey[MAX_ET];
    115uint8_t kf[MAX_KT], kx[MAX_KT], ky[MAX_KT];
    116uint8_t rnd_enemy, rnd_kirai;
    117uint8_t k_right, k_left;
    118
    119void set_sprite_attrb( uint8_t nb, uint8_t tile )
    120{
    121  if(DEVICE_SUPPORTS_COLOR) {
    122    set_sprite_prop( nb, tile );
    123  }
    124}
    125
    126
    127void set_bkg_attr( uint8_t x, uint8_t y, uint8_t sx, uint8_t sy, unsigned char *d )
    128{
    129  uint8_t xx, yy;
    130
    131  VBK_REG = 1;		/* select palette bank */
    132  for( yy=0; yy<sy; yy++ ) {
    133    for( xx=0; xx<sx; xx++ ) {
    134      msg_tile[xx] = bkgCGB[(unsigned int)*d];
    135      d++;
    136    }
    137    set_bkg_tiles( x, y+yy, sx, 1, msg_tile );
    138  }
    139  VBK_REG = 0;		/* select data bank */
    140}
    141
    142uint8_t make_rnd( uint8_t i )
    143{
    144  return( arand()%(i+1) );
    145}
    146
    147void show_score( uint16_t s )
    148{
    149  uint16_t m;
    150  uint8_t i, n, f;
    151  unsigned char score[6];
    152
    153  f = 0; m = 10000;
    154  for( i=0; i<5; i++ ) {
    155    n = s/m; s = s%m; m = m/10;
    156    if( (n==0)&&(f==0) ) {
    157      score[i] = 0x20;      /* ' ' */
    158    } else {
    159      f = 1;
    160      score[i] = 0x30+n;    /* '0' - '9' */
    161    }
    162  }
    163  score[5] = 0x30;      /* '0' */
    164  set_bkg_tiles( 4, 0, 6, 1, score );
    165}
    166
    167void set_level( uint8_t i )
    168{
    169  /* level */
    170  if( i < 9 ) {
    171    rnd_enemy = 100-(i*12); /*  1% - */
    172    rnd_kirai = 50-(i*6);   /*  2% - */
    173  } else {
    174    rnd_enemy = 0;      /* 100 % */
    175    rnd_kirai = 0;      /* 100 % */
    176  }
    177}
    178
    179void show_level( uint8_t i )
    180{
    181  unsigned char level[2];
    182
    183  if( i < 9 ) {
    184    level[0] = 0x31+i;
    185  } else {
    186    level[0] = 0x41+i-9;
    187  }
    188  set_bkg_tiles( 19, 0, 1, 1, level );
    189  set_level( i );
    190}
    191
    192void show_gover()
    193{
    194  set_bkg_tiles(  6, 9, 8, 1, (unsigned char *)msg_gover );
    195  pf = DEF_PF;
    196}
    197
    198void show_pause()
    199{
    200  set_bkg_tiles(  6, 9, 8, 1, (unsigned char *)msg_pause );
    201}
    202
    203void hide_msg()
    204{
    205  set_bkg_tiles(  6, 9, 8, 1, (unsigned char *)msg_start );
    206}
    207
    208void init_score()
    209{
    210  ps = 0;
    211  show_score( ps );
    212  pp = 0; pl = 0;
    213  show_level( pl );
    214}
    215
    216void init_screen()
    217{
    218  uint8_t n;
    219
    220  if(DEVICE_SUPPORTS_COLOR) {
    221    /* Transfer color palette */
    222    set_bkg_palette( 0, 1, &bkg_p[0] );
    223    set_bkg_palette( 1, 1, &bkg_p[4] );
    224    set_bkg_palette( 2, 1, &bkg_p[8] );
    225    set_bkg_palette( 3, 1, &bkg_p[12] );
    226    set_bkg_palette( 4, 1, &bkg_p[16] );
    227    set_bkg_palette( 5, 1, &bkg_p[20] );
    228    set_bkg_palette( 6, 1, &bkg_p[24] );
    229    set_bkg_palette( 7, 1, &bkg_p[28] );
    230    set_sprite_palette( 0, 1, &obj_p[0] );
    231    set_sprite_palette( 1, 1, &obj_p[4] );
    232    set_sprite_palette( 2, 1, &obj_p[8] );
    233    set_sprite_palette( 3, 1, &obj_p[12] );
    234    set_sprite_palette( 4, 1, &obj_p[16] );
    235    set_sprite_palette( 5, 1, &obj_p[20] );
    236    set_sprite_palette( 6, 1, &obj_p[24] );
    237    set_sprite_palette( 7, 1, &obj_p[28] );
    238
    239    /* set attributes */
    240    set_bkg_attr( 0, 0, 20, 18, bkg_c );
    241    set_bkg_tiles(  0, 0, 20, 18, bkg_c );
    242  } else {
    243    set_bkg_tiles(  0, 0, 20, 18, bkg_m );
    244  }
    245
    246  pw = 50;
    247  set_bkg_data(  0, 96, bkg );
    248  set_bkg_tiles(  0, 0, 3, 1, (unsigned char *)msg_1up );
    249  set_bkg_tiles( 16, 0, 2, 1, (unsigned char *)msg_lv );
    250  SHOW_BKG;
    251  SPRITES_8x8;
    252  set_sprite_data( 0, 128, fore );
    253  SHOW_SPRITES;
    254  for( n=0; n<40; n++ ) {
    255    set_sprite_tile( n, DEF_SP );
    256    move_sprite( n, 0, 0 );
    257  }
    258}
    259
    260void init_player()
    261{
    262  pf = 0; px = DEF_PX;
    263  set_sprite_tile( 0, 0 );
    264  set_sprite_attrb( 0, foreCGB[0] );
    265  move_sprite( 0, px, DEF_PY );
    266  set_sprite_tile( 1, 1 );
    267  set_sprite_attrb( 1, foreCGB[1] );
    268  move_sprite( 1, px+8, DEF_PY );
    269}
    270
    271void init_tama()
    272{
    273  uint8_t i;
    274
    275  for( i=0; i<MAX_TT; i++ ) {
    276    tf[i] = 0;
    277    tx[i] = i*4+DEF_TX;
    278    ty[i] = DEF_TY;
    279    set_sprite_tile( i+DEF_TS, tf[i]+DEF_TC );
    280    set_sprite_attrb( i+DEF_TS, foreCGB[tf[i]+DEF_TC] );
    281    move_sprite( i+DEF_TS, tx[i], ty[i] );
    282  }
    283}
    284
    285void init_enemy()
    286{
    287  uint8_t i;
    288
    289  for( i=0; i<MAX_ET; i++ ) {
    290    ef[i] = 0;
    291    ex[i] = 0;
    292    ey[i] = 0;
    293    set_sprite_tile( i*2+DEF_ES0, DEF_SP );
    294    set_sprite_tile( i*2+DEF_ES1, DEF_SP );
    295    move_sprite( i*2+DEF_ES0, ex[i], ey[i] );
    296    move_sprite( i*2+DEF_ES1, ex[i], ey[i] );
    297  }
    298}
    299
    300void init_kirai()
    301{
    302  uint8_t i;
    303
    304  for( i=0; i<MAX_KT; i++ ) {
    305    kf[i] = 0;
    306    kx[i] = DEF_KX;
    307    ky[i] = DEF_KY;
    308    set_sprite_tile( i+DEF_KS, DEF_SP );
    309    move_sprite( i+DEF_KS, kx[i], ky[i] );
    310  }
    311}
    312
    313/* player */
    314void player()
    315{
    316  uint8_t key;
    317  uint8_t i;
    318  uint16_t seed;
    319
    320  key = joypad();
    321  /* pause */
    322  if( key & J_START ) {
    323    if( pf == DEF_PF ) {
    324      /* Initialize the random number generator */
    325      seed = DIV_REG;
    326      waitpadup();
    327      seed |= ((uint16_t)DIV_REG << 8);
    328      initarand(seed);
    329      hide_msg();
    330      init_score();
    331      init_player();
    332      init_tama();
    333      init_enemy();
    334      init_kirai();
    335      delay( 500 );
    336    } else {
    337      show_pause();
    338      waitpadup();
    339      key = joypad();
    340      while( !(key & J_START) ) {
    341        key = joypad();
    342        if( key & J_DOWN ) {
    343          if(pl > 0)
    344            pl--;
    345          show_level( pl );
    346          waitpadup();
    347        } else if( key & J_UP ) {
    348          if(pl < 8)
    349            pl++;
    350          show_level( pl );
    351          waitpadup();
    352        } else if( key & J_LEFT ) {
    353          while( joypad() & J_LEFT ) {
    354            if(pw > 0)
    355              pw--;
    356            show_score( pw );
    357            delay( 250 );
    358          }
    359          show_score( ps );
    360        } else if( key & J_RIGHT ) {
    361          while( joypad() & J_RIGHT ) {
    362            if(pw < 99)
    363              pw++;
    364            show_score( pw );
    365            delay( 250 );
    366          }
    367          show_score( ps );
    368        } else if( key & J_SELECT ) {
    369          i = k_right;
    370          k_right = k_left;
    371          k_left  = i;
    372          waitpadup();
    373        }
    374      }
    375      waitpadup();
    376      hide_msg();
    377      delay( 500 );
    378    }
    379    return;
    380  }
    381
    382  /* dead */
    383  if( pf > 1 ) {
    384    if( pf < DEF_PF ) {
    385      set_sprite_tile( 0, pf*2+DEF_PC0 );
    386      set_sprite_attrb( 0, foreCGB[pf*2+DEF_PC0] );
    387      set_sprite_tile( 1, pf*2+DEF_PC1 );
    388      set_sprite_attrb( 1, foreCGB[pf*2+DEF_PC1] );
    389      pf++;
    390    } else {
    391      set_sprite_tile( 0, DEF_SP );
    392      set_sprite_tile( 1, DEF_SP );
    393      show_gover();
    394    }
    395    return;
    396  }
    397
    398  /* move */
    399  if( (key&J_LEFT)&&(px>MIN_PX) ) {
    400    px--;
    401    move_sprite( 0, px, DEF_PY ); move_sprite( 1, px+8, DEF_PY );
    402  } else if( (key&J_RIGHT)&&(px<MAX_PX) ) {
    403    px++;
    404    move_sprite( 0, px, DEF_PY ); move_sprite( 1, px+8, DEF_PY );
    405  }
    406  /* shot */
    407  if( key & k_left ) {  /* change J_B to J_A */
    408    if( pf == 0 ) {
    409      pf = 1;
    410      for( i=0; i<MAX_TT; i++ ) {
    411        if( tf[i] == 0 ) {
    412          tf[i] = 1; tx[i] = px-4; ty[i] = DEF_PY;
    413          break;
    414        }
    415      }
    416    }
    417  } else if( key & k_right ) {  /* change J_A to J_B */
    418    if( pf == 0 ) {
    419      pf = 1;
    420      for( i=0; i<MAX_TT; i++ ) {
    421        if( tf[i] == 0 ) {
    422          tf[i] = 1; tx[i] = px+12; ty[i] = DEF_PY;
    423          break;
    424        }
    425      }
    426    }
    427  } else if( pf == 1 ) {
    428    pf = 0;
    429  }
    430}
    431
    432/* bombs */
    433void bombs()
    434{
    435   uint8_t i;
    436
    437   for( i=0; i<MAX_TT; i++ ) {
    438    if( tf[i] != 0 ) {
    439      ty[i]++;
    440      if( ty[i] > MAX_TY ) {
    441        tf[i] = 0; tx[i] = i*4+DEF_TX; ty[i] = DEF_TY;
    442      } else {
    443        tf[i] = 3-tf[i];
    444      }
    445      set_sprite_tile( i+DEF_TS, tf[i]+DEF_TC );
    446      set_sprite_attrb( i+DEF_TS, foreCGB[tf[i]+DEF_TC] );
    447      move_sprite( i+DEF_TS, tx[i], ty[i] );
    448    }
    449  }
    450}
    451
    452/* enemys */
    453void enemys()
    454{
    455  uint8_t i, j;
    456
    457  for( i=0; i<MAX_ET; i++ ) {
    458    if( ef[i] == 1 ) {
    459      /* move right to left */
    460      ex[i]--;
    461      if( (pl>0)&&(ey[i]<SPEED_EY) )  ex[i]--;
    462      if( ex[i] <= MIN_EX ) {
    463        ef[i] = 0;
    464        set_sprite_tile( i*2+DEF_ES0, DEF_SP );
    465        set_sprite_tile( i*2+DEF_ES1, DEF_SP );
    466      } else {
    467        /* new */
    468        if( ex[i]<MIN_SX*8+13 ) {
    469          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0 );
    470          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1 );
    471        } else if( ex[i]<MIN_SX*8+20 ) {
    472          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0 );
    473          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1+(ex[i]-MIN_SX*8-13) );
    474        } else if( ex[i]<MIN_SX*8+28 ) {
    475          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0+(ex[i]-MIN_SX*8-20) );
    476          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1+8 );
    477        } else if( ex[i]<MAX_SX*8+13 ) {
    478          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0+8 );
    479          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1+8 );
    480        } else if( ex[i]<MAX_SX*8+20 ) {
    481          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0+8 );
    482          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1+(ex[i]-MAX_SX*8-12)+7 );
    483        } else if( ex[i]<MAX_SX*8+28 ) {
    484          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0+(ex[i]-MAX_SX*8-20)+8 );
    485          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1 );
    486        } else {
    487          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0 );
    488          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1 );
    489        }
    490        /* new */
    491        move_sprite( i*2+DEF_ES0, ex[i]-SUB_EX0, ey[i] );
    492        move_sprite( i*2+DEF_ES1, ex[i]-SUB_EX1, ey[i] );
    493        /* check bomb */
    494        for( j=0; j<MAX_TT; j++ ) {
    495          if( tf[j] != 0 ) {
    496            if( (ty[j]>ey[i]-2)&&(ty[j]<ey[i]+2) ) {
    497              if( (tx[j]>(ex[i]-SUB_EX0-5))&&(tx[j]<(ex[i]-SUB_EX1+5)) ) {
    498                /* hit */
    499                tf[j] = 0; tx[j] = j*4+DEF_TX; ty[j] = DEF_TY;
    500                set_sprite_tile( j+DEF_TS, tf[j]+DEF_TC );
    501                set_sprite_attrb( j+DEF_TS, foreCGB[tf[j]+DEF_TC] );
    502                move_sprite( j+DEF_TS, tx[j], ty[j] );
    503                ef[i] = 3;
    504                set_sprite_tile( i*2+DEF_ES0, ef[i]*2+DEF_BC1 );
    505                set_sprite_attrb( i*2+DEF_ES0, foreCGB[ef[i]*2+DEF_BC1] );
    506                set_sprite_tile( i*2+DEF_ES1, ef[i]*2+DEF_BC2 );
    507                set_sprite_attrb( i*2+DEF_ES1, foreCGB[ef[i]*2+DEF_BC2] );
    508              }
    509            }
    510          }
    511        }
    512        if( make_rnd(rnd_kirai) == 0 ) {
    513          if( ((ex[i]-SUB_EX0)>MIN_PX)&&((ex[i]-SUB_EX0)<MAX_PX) ) {
    514            if( kf[i] == 0 ) {
    515              /* shot kirai */
    516              kf[i] = 1;
    517              kx[i] = ex[i]-SUB_EX0+4;
    518              ky[i] = ey[i]-4;
    519            } else if( kf[i+1] == 0 ) {
    520              /* shot kirai */
    521              kf[i+1] = 1;
    522              kx[i+1] = ex[i]-SUB_EX0+4;
    523              ky[i+1] = ey[i]-4;
    524            } else if( kf[i+2] == 0 ) {
    525              /* shot kirai */
    526              kf[i+2] = 1;
    527              kx[i+2] = ex[i]-SUB_EX0+4;
    528              ky[i+2] = ey[i]-4;
    529            }
    530          }
    531        }
    532      }
    533    } else if( ef[i] == 2 ) {
    534      /* move left to right */
    535      ex[i]++;
    536      if( (pl>0)&&(ey[i]<SPEED_EY) )  ex[i]++;
    537      if( ex[i] >= MAX_EX ) {
    538        ef[i] = 0;
    539        set_sprite_tile( i*2+DEF_ES0, DEF_SP );
    540        set_sprite_tile( i*2+DEF_ES1, DEF_SP );
    541      } else {
    542        /* new */
    543        if( i==9 ) {
    544          if( ex[i]<MIN_SX*8+13 ) {
    545            set_sprite_tile( i*2+DEF_ES0, DEF_XEC0 );
    546            set_sprite_tile( i*2+DEF_ES1, DEF_XEC1 );
    547          } else if( ex[i]<MIN_SX*8+20 ) {
    548            set_sprite_tile( i*2+DEF_ES0, DEF_XEC0 );
    549            set_sprite_tile( i*2+DEF_ES1, DEF_XEC1+(ex[i]-MIN_SX*8-13) );
    550          } else if( ex[i]<MIN_SX*8+28 ) {
    551            set_sprite_tile( i*2+DEF_ES0, DEF_XEC0+(ex[i]-MIN_SX*8-20) );
    552            set_sprite_tile( i*2+DEF_ES1, DEF_XEC1+8 );
    553          } else if( ex[i]<MAX_SX*8+13 ) {
    554            set_sprite_tile( i*2+DEF_ES0, DEF_XEC0+8 );
    555            set_sprite_tile( i*2+DEF_ES1, DEF_XEC1+8 );
    556          } else if( ex[i]<MAX_SX*8+20 ) {
    557            set_sprite_tile( i*2+DEF_ES0, DEF_XEC0+8 );
    558            set_sprite_tile( i*2+DEF_ES1, DEF_XEC1+(ex[i]-MAX_SX*8-12)+7 );
    559          } else if( ex[i]<MAX_SX*8+28 ) {
    560            set_sprite_tile( i*2+DEF_ES0, DEF_XEC0+(ex[i]-MAX_SX*8-20)+8 );
    561            set_sprite_tile( i*2+DEF_ES1, DEF_XEC1 );
    562          } else {
    563            set_sprite_tile( i*2+DEF_ES0, DEF_XEC0 );
    564            set_sprite_tile( i*2+DEF_ES1, DEF_XEC1 );
    565          }
    566        } else {
    567          if( ex[i]<MIN_SX*8+13 ) {
    568            set_sprite_tile( i*2+DEF_ES0, DEF_2EC0 );
    569            set_sprite_tile( i*2+DEF_ES1, DEF_2EC1 );
    570          } else if( ex[i]<MIN_SX*8+20 ) {
    571            set_sprite_tile( i*2+DEF_ES0, DEF_2EC0 );
    572            set_sprite_tile( i*2+DEF_ES1, DEF_2EC1+(ex[i]-MIN_SX*8-13) );
    573          } else if( ex[i]<MIN_SX*8+28 ) {
    574            set_sprite_tile( i*2+DEF_ES0, DEF_2EC0+(ex[i]-MIN_SX*8-20) );
    575            set_sprite_tile( i*2+DEF_ES1, DEF_2EC1+8 );
    576          } else if( ex[i]<MAX_SX*8+13 ) {
    577            set_sprite_tile( i*2+DEF_ES0, DEF_2EC0+8 );
    578            set_sprite_tile( i*2+DEF_ES1, DEF_2EC1+8 );
    579          } else if( ex[i]<MAX_SX*8+20 ) {
    580            set_sprite_tile( i*2+DEF_ES0, DEF_2EC0+8 );
    581            set_sprite_tile( i*2+DEF_ES1, DEF_2EC1+(ex[i]-MAX_SX*8-12)+7 );
    582          } else if( ex[i]<MAX_SX*8+28 ) {
    583            set_sprite_tile( i*2+DEF_ES0, DEF_2EC0+(ex[i]-MAX_SX*8-20)+8 );
    584            set_sprite_tile( i*2+DEF_ES1, DEF_2EC1 );
    585          } else {
    586            set_sprite_tile( i*2+DEF_ES0, DEF_2EC0 );
    587            set_sprite_tile( i*2+DEF_ES1, DEF_2EC1 );
    588          }
    589        }
    590        /* new */
    591        move_sprite( i*2+DEF_ES0, ex[i]-SUB_EX0, ey[i] );
    592        move_sprite( i*2+DEF_ES1, ex[i]-SUB_EX1, ey[i] );
    593        /* check bomb */
    594        for( j=0; j<MAX_TT; j++ ) {
    595          if( tf[j] != 0 ) {
    596            if( (ty[j]>ey[i]-2)&&(ty[j]<ey[i]+2) ) {
    597              if( (tx[j]>(ex[i]-SUB_EX0-5))&&(tx[j]<(ex[i]-SUB_EX1+5)) ) {
    598                /* hit */
    599                tf[j] = 0; tx[j] = j*4+DEF_TX; ty[j] = DEF_TY;
    600                set_sprite_tile( j+DEF_TS, tf[j]+DEF_TC );
    601                set_sprite_attrb( j+DEF_TS, foreCGB[tf[j]+DEF_TC] );
    602                move_sprite( j+DEF_TS, tx[j], ty[j] );
    603                ef[i] = 3;
    604                set_sprite_tile( i*2+DEF_ES0, ef[i]*2+DEF_BC1 );
    605                set_sprite_attrb( i*2+DEF_ES0, foreCGB[ef[i]*2+DEF_BC1] );
    606                set_sprite_tile( i*2+DEF_ES1, ef[i]*2+DEF_BC2 );
    607                set_sprite_attrb( i*2+DEF_ES1, foreCGB[ef[i]*2+DEF_BC2] );
    608              }
    609            }
    610          }
    611        }
    612        if( make_rnd(rnd_kirai) == 0 ) {
    613          if( ((ex[i]-SUB_EX0)>MIN_PX)&&((ex[i]-SUB_EX0)<MAX_PX) ) {
    614            if( kf[i] == 0 ) {
    615              /* shot kirai */
    616              kf[i] = 1;
    617              kx[i] = ex[i]-SUB_EX0+4;
    618              ky[i] = ey[i]-4;
    619            } else if( kf[i+1] == 0 ) {
    620              /* shot kirai */
    621              kf[i+1] = 1;
    622              kx[i+1] = ex[i]-SUB_EX0+4;
    623              ky[i+1] = ey[i]-4;
    624            } else if( kf[i+2] == 0 ) {
    625              /* shot kirai */
    626              kf[i+2] = 1;
    627              kx[i+2] = ex[i]-SUB_EX0+4;
    628              ky[i+2] = ey[i]-4;
    629            }
    630          }
    631        }
    632      }
    633    } else if( ef[i] >= 3 ) {
    634      if( ef[i] > 4 ) {
    635        ef[i] = 0;
    636        set_sprite_tile( i*2+DEF_ES0, DEF_SP );
    637        set_sprite_tile( i*2+DEF_ES1, DEF_SP );
    638        if( i == 9 ) {
    639          ps += 100; show_score( ps ); pp++;
    640          set_level( pl-1 );
    641        } else {
    642          ps += (i+1); show_score( ps ); pp++;
    643        }
    644      } else {
    645        set_sprite_tile( i*2+DEF_ES0, ef[i]*2+DEF_BC1 );
    646        set_sprite_attrb( i*2+DEF_ES0, foreCGB[ef[i]*2+DEF_BC1] );
    647        set_sprite_tile( i*2+DEF_ES1, ef[i]*2+DEF_BC2 );
    648        set_sprite_attrb( i*2+DEF_ES1, foreCGB[ef[i]*2+DEF_BC2] );
    649        ef[i]++;
    650      }
    651    } else if( i == 9 ) {
    652      if( pp > 20 ) {
    653        pp = 0;
    654        pl++; show_level( pl );
    655        /* X */
    656        ey[i] = i*DEF_EH+DEF_EY;
    657        ef[i] = i%2+1;
    658        ex[i] = MIN_EX;
    659        set_sprite_tile( i*2+DEF_ES0, DEF_XEC0 );
    660        set_sprite_attrb( i*2+DEF_ES0, foreCGB[DEF_XEC0] );
    661        set_sprite_tile( i*2+DEF_ES1, DEF_XEC1 );
    662        set_sprite_attrb( i*2+DEF_ES1, foreCGB[DEF_XEC1] );
    663        move_sprite( i*2+DEF_ES0, ex[i]-SUB_EX0, ey[i] );
    664        move_sprite( i*2+DEF_ES1, ex[i]-SUB_EX1, ey[i] );
    665      }
    666    } else if( make_rnd(rnd_enemy) == 0 ) {
    667      if( !((pl<4)&&(i==0)) ) {
    668        /* create */
    669        ey[i] = i*DEF_EH+DEF_EY;
    670        ef[i] = i%2+1;
    671        if( ef[i] == 1 ) {
    672          ex[i] = MAX_EX;
    673          set_sprite_tile( i*2+DEF_ES0, DEF_1EC0 );
    674          set_sprite_attrb( i*2+DEF_ES0, foreCGB[DEF_1EC0] );
    675          set_sprite_tile( i*2+DEF_ES1, DEF_1EC1 );
    676          set_sprite_attrb( i*2+DEF_ES1, foreCGB[DEF_1EC1] );
    677        } else {
    678          ex[i] = MIN_EX;
    679          set_sprite_tile( i*2+DEF_ES0, DEF_2EC0 );
    680          set_sprite_attrb( i*2+DEF_ES0, foreCGB[DEF_2EC0] );
    681          set_sprite_tile( i*2+DEF_ES1, DEF_2EC1 );
    682          set_sprite_attrb( i*2+DEF_ES1, foreCGB[DEF_2EC1] );
    683        }
    684        move_sprite( i*2+DEF_ES0, ex[i]-SUB_EX0, ey[i] );
    685        move_sprite( i*2+DEF_ES1, ex[i]-SUB_EX1, ey[i] );
    686      }
    687    }
    688  }
    689}
    690
    691/* kirai */
    692void kirai()
    693{
    694  uint8_t i;
    695
    696  for( i=0; i<MAX_KT; i++ ) {
    697    if( kf[i] != 0 ) {
    698      ky[i]--;
    699      if( kf[i] >=3 ) {
    700        kf[i]++;
    701        if( (kx[i]>(px-5))&&(kx[i]<(px+12)) ) {
    702          if( pf < 2 ) {
    703            /* out!! */
    704            pf = 2;
    705          }
    706        }
    707        if( kf[i] >= 6 ) {
    708          kf[i] = 0; kx[i] = DEF_KX; ky[i] = DEF_KY;
    709        }
    710      } else if( ky[i] <= MIN_KY ) {
    711        kf[i] = 3;
    712      } else {
    713        kf[i] = 3-kf[i];
    714      }
    715      set_sprite_tile( i+DEF_KS, kf[i]+DEF_KC );
    716      set_sprite_attrb( i+DEF_KS, foreCGB[kf[i]+DEF_KC] );
    717      move_sprite( i+DEF_KS, kx[i], ky[i] );
    718    }
    719  }
    720}
    721
    722/*--------------------------------------------------------------------------*
    723 | main program                                                             |
    724 *--------------------------------------------------------------------------*/
    725void main()
    726{
    727  disable_interrupts();
    728  DISPLAY_OFF;
    729
    730  initarand(((uint16_t)DIV_REG << 8));
    731
    732  init_screen();
    733  init_score();
    734  init_player();
    735  init_tama();
    736  init_enemy();
    737  init_kirai();
    738  show_gover();
    739  k_right = J_A;
    740  k_left = J_B;
    741  DISPLAY_ON;
    742  enable_interrupts();
    743
    744  while(1) {
    745    delay( pw );
    746    player();
    747    bombs();
    748    enemys();
    749    kirai();
    750  }
    751}