chatter protection

This commit is contained in:
Stefan Kinzel 2014-03-26 00:05:07 +01:00
parent 83f4b908a0
commit d1b377437e
1 changed files with 52 additions and 35 deletions

View File

@ -41,19 +41,23 @@ game_descriptor_t kart_game_descriptor __attribute__((section(".game_descriptors
}; };
#endif #endif
#define WAIT 75 #define WAIT 20
#define DRIVEDIV 3 #define DRIVEDIV 5
#define MOVEDIV 1 #define MOVEDIV 1
#define DECREASE_WIDTH_DIV 120 #define DECREASE_WIDTH_DIV 600
#define DIRECTION_DIV 20
#define KEY_IGNORE 10
#define CARCOLOR 3 #define CARCOLOR 3
#define BORDERCOLOR 2 #define BORDERCOLOR 2
#define LINECOLOR 1 #define LINECOLOR 1
#define CURVE_PROP 30 #define CURVE_PROP 50
uint8_t borders[NUM_ROWS][2]; uint8_t borders[NUM_ROWS][2];
uint8_t key_ignore[2];
void kart_game(){ void kart_game(){
@ -61,29 +65,23 @@ void kart_game(){
uint8_t carpos = NUM_COLS / 2; uint8_t carpos = NUM_COLS / 2;
uint32_t cycle = 0; uint32_t cycle = 0;
uint8_t draw_line = 1; uint8_t draw_line = 1;
uint8_t width = NUM_COLS -2; uint8_t width = NUM_COLS - 2;
uint8_t middle = NUM_COLS / 2; uint8_t middle = NUM_COLS / 2;
char game_over[100] = ""; char game_over[100] = "";
key_ignore[0] = 0;
key_ignore[1] = 0;
clear_screen(0); clear_screen(0);
// init border memory // init border memory
for(uint8_t row = 0; row < NUM_ROWS; row++){ for(uint8_t row = 0; row < NUM_ROWS; row++){
borders[row][0] = middle; borders[row][0] = middle;
borders[row][1] = width; borders[row][1] = NUM_COLS;
} }
setpixel((pixel){carpos, NUM_ROWS-1}, CARCOLOR); setpixel((pixel){carpos, NUM_ROWS-1}, CARCOLOR);
// Wait some seconds to start...
while(1){
cycle++;
wait(WAIT);
if(cycle >= DRIVEDIV*0){
break;
}
}
// main loop // main loop
while(1){ while(1){
@ -92,24 +90,31 @@ void kart_game(){
width--; width--;
} }
// MOVE-STEP // MOVE
if(cycle % MOVEDIV == 0){ if (JOYISLEFT && key_ignore[0] <= 0){
if (JOYISLEFT){ setpixel((pixel){carpos, NUM_ROWS-1}, 0);
carpos++; carpos++;
setpixel((pixel){carpos, NUM_ROWS-1}, 0);
}else if (JOYISRIGHT){
carpos--;
setpixel((pixel){carpos, NUM_ROWS-1}, 0);
}
if(check_collision(carpos)){ key_ignore[0] = KEY_IGNORE;
break; key_ignore[1] = 0;
} }else if (JOYISRIGHT && key_ignore[1] <= 0){
setpixel((pixel){carpos, NUM_ROWS-1}, 0);
carpos--;
key_ignore[1] = KEY_IGNORE;
key_ignore[0] = 0;
}else if(!(JOYISRIGHT || JOYISLEFT)){
key_ignore[1] = 0;
key_ignore[0] = 0;
} }
// DRIVE-STEP if(check_collision(carpos)){
if(cycle % DRIVEDIV == 0){ break;
}
// DIRECTION-STEP
if(cycle % DIRECTION_DIV == 0){
// generate a route // generate a route
int rnd = random8(); int rnd = random8();
if(rnd < CURVE_PROP && middle-(width/2) > 1){ if(rnd < CURVE_PROP && middle-(width/2) > 1){
@ -117,7 +122,10 @@ void kart_game(){
}else if(rnd > 256-CURVE_PROP && middle+(width/2) < NUM_COLS-1){ }else if(rnd > 256-CURVE_PROP && middle+(width/2) < NUM_COLS-1){
middle++; middle++;
} }
}
// DRIVE-STEP
if(cycle % DRIVEDIV == 0){
// shift pixmap down // shift pixmap down
drive(); drive();
@ -139,7 +147,7 @@ void kart_game(){
} }
// paint middle line // paint middle line
if(width > 4 && draw_line){ if(width > 6 && draw_line){
setpixel((pixel){middle, 0}, LINECOLOR); setpixel((pixel){middle, 0}, LINECOLOR);
} }
@ -147,6 +155,13 @@ void kart_game(){
setpixel((pixel){carpos, NUM_ROWS-1}, CARCOLOR); setpixel((pixel){carpos, NUM_ROWS-1}, CARCOLOR);
cycle++; cycle++;
if(key_ignore[0] > 0){
key_ignore[0]--;
}
if(key_ignore[1] > 0){
key_ignore[1]--;
}
wait(WAIT); wait(WAIT);
} }
@ -159,15 +174,17 @@ void kart_game(){
*/ */
void drive(void){ void drive(void){
unsigned char plane, row; unsigned char plane, row, byte;
for(plane=0; plane<NUMPLANE; plane++){ for(plane=0; plane<NUMPLANE; plane++){
for(row=NUM_ROWS-1;row>0; row--){ for(row=NUM_ROWS-1;row>0; row--){
pixmap[plane][row][0] = pixmap[plane][row-1][0]; for(byte=0; byte < LINEBYTES; byte++){
pixmap[plane][row][1] = pixmap[plane][row-1][1]; pixmap[plane][row][byte] = pixmap[plane][row-1][byte];
}
}
for(byte=0; byte < LINEBYTES; byte++){
pixmap[plane][row][byte] = 0x00;
} }
pixmap[plane][0][0] = 0x00;
pixmap[plane][0][1] = 0x00;
} }
} }