From 0d6f94d9e4355dfd63f8ed11dd3c54d711156fe2 Mon Sep 17 00:00:00 2001 From: girst Date: Thu, 24 May 2018 16:57:58 +0200 Subject: [PATCH] BONUS item: snip (Proof of Concept stage) --- schemes.h | 11 ++++++++--- viiper.c | 17 ++++++++++++++--- viiper.h | 1 + 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/schemes.h b/schemes.h index 8d83cc2..19e01f3 100644 --- a/schemes.h +++ b/schemes.h @@ -17,7 +17,8 @@ struct scheme { char* snake[5][5]; /* [predecessor][successor] */ char* color[3]; /* 0=even, 1=odd, 2=head */ - char* item[NUM_FOODS]; + char* food[NUM_FOODS]; + char* boni[NUM_BONI]; /* for en-/disabling e.g. DEC charset: */ char* init_seq; @@ -70,11 +71,14 @@ struct scheme unic0de = { }, .color = {"32", "92", "92;1"}, - .item = { + .food = { [FOOD_5] = "🍐", [FOOD_10] = "🍎", [FOOD_20] = "🥑", }, + .boni = { + [BONUS_SNIP] = "✂️ ", + }, .cell_width = 2, .display_width = 2, @@ -97,7 +101,8 @@ struct scheme vt220_charset = { }, .color = {"0", "0", "1"}, - .item = { "$", "%", "&"}, + .food = { "$", "%", "&" }, + .boni = { "x" }, .init_seq = "\033(0\033*B\x0f" /* G0=Graphics, G2=ASCII, invoke G0 */ "\033[?3l", /* disable 132 column mode (DECCOLM) */ diff --git a/viiper.c b/viiper.c index 47f5354..524b24a 100644 --- a/viiper.c +++ b/viiper.c @@ -169,6 +169,7 @@ int viiper(void) { for(;;) { switch (getctrlseq()) { +case '#': spawn_item(BONUS, BONUS_SNIP, NULL); case CTRSEQ_CURSOR_LEFT: case 'h':append_movement(WEST); break; case CTRSEQ_CURSOR_DOWN: case 'j':append_movement(SOUTH); break; case CTRSEQ_CURSOR_UP: case 'k':append_movement(NORTH); break; @@ -208,8 +209,8 @@ void snake_advance (void) { /* detect food hit and spawn a new food */ for (i = g.i; i; i = i->next) { if (i->r == new_row && i->c == new_col) { + respawn = (i->t == FOOD); /* only respawn when we ate a FOOD; must respawn after advancing head */ consume_item (i); - respawn = 1; /* must respawn after advancing head */ break; } } @@ -283,6 +284,16 @@ void consume_item (struct item* i) { snake_append(&g.s, -1, -1); break; /* will be reused as the head before it is drawn */ case BONUS: + switch (i->v) { + case BONUS_SNIP: + for (int i = 10; i && g.s->next->next; i--) { /* must have at least 2 elements, otherwise segfault during snake drawing */ + struct snake* old_head = g.s; + g.s = g.s->next; + free (old_head); + } + show_playfield(); + break; + } //TODO: handle bonus break; } @@ -350,8 +361,8 @@ void draw_sprites (int erase_r, int erase_c) { /* print item queue */ for (struct item* i = g.i; i; i = i->next) { move_ph (i->r+LINE_OFFSET, i->c*CW+COL_OFFSET); - if (i->t == FOOD) print (op.sch->item[i->v]); - else if (i->t==BONUS) /* TODO: print bonus */; + if (i->t == FOOD) print(op.sch->food[i->v]); + else if (i->t == BONUS) print(op.sch->boni[i->v]); } /* print score */ diff --git a/viiper.h b/viiper.h index 28913d2..701402d 100644 --- a/viiper.h +++ b/viiper.h @@ -50,6 +50,7 @@ enum food_value { NUM_FOODS, }; enum bonus_value { + BONUS_SNIP, NUM_BONI, }; enum game_state { -- 2.39.3