Permalink
Browse files

Merge branch 'master' of github.com:c3d2/PentaLight

  • Loading branch information...
2 parents daf755a + bbb634e commit e2cbc5ff2ca92193a4ef7bb020c9120dfa4a0dfd twobit committed Oct 24, 2011
Showing with 474 additions and 4 deletions.
  1. +90 −0 firmware/apps/click.c
  2. +188 −0 firmware/apps/duel.c
  3. +1 −1 firmware/apps/snake.c
  4. +187 −0 firmware/apps/zombie.c
  5. +3 −0 firmware/sim/.gitignore
  6. +5 −3 firmware/sim/main.c
View
@@ -0,0 +1,90 @@
+#include <main.h>
+
+#include <stdlib.h>
+
+#define CYCLE(x,c) ((x + c) % c)
+#define MAX(a,b) ((a) < (b) ? (b) : (a))
+#define MIN(a,b) ((a) > (b) ? (b) : (a))
+#define ARRAY_SIZE(array) (sizeof(array) / sizeof(*array))
+
+#define PENALTY -1
+
+static void init(void) ATTRIBUTES;
+static uint8_t tick(void);
+static void key(key_type key, event_type event);
+static void draw_flash(uint8_t b);
+static void draw_winner1(void);
+static void draw_winner2(void);
+
+static int8_t left_bar = 0;
+static int8_t right_bar = 0;
+static uint8_t flash_light = 0;
+static uint8_t winner = 0;
+static uint8_t end = 0;
+static uint8_t state = 0;
+
+void init(void) {
+ registerApp(tick, key, 22);
+}
+
+uint8_t tick() {
+ if (state == 0) {
+ state = 1;
+ left_bar = 0;
+ right_bar = 0;
+ flash_light = 0;
+ winner = 0;
+ end = 0;
+ }
+ int8_t y;
+ for(y = 0; y < LED_HEIGHT; y++) {
+ setLedXY(0, y,
+ y < left_bar / 7 ? 7 :
+ y == left_bar / 7 ? left_bar % 7 : 0);
+ setLedXY(3, y,
+ y < right_bar / 7 ? 7 :
+ y == right_bar / 7 ? right_bar % 7 : 0);
+ }
+ draw_flash(flash_light ? 7 : 1);
+ if(rand()%100 < 50 - 20*flash_light) flash_light = !flash_light;
+ if(winner == 1) draw_winner1();
+ if(winner == 2) draw_winner2();
+ if(winner > 0 && --end == 0) {
+ state = 0;
+ return 1;
+ }
+ return 0;
+}
+
+void key(key_type key, event_type event) {
+ if(event == DOWN && end == 0) {
+ if(key == KEY_A) {
+ right_bar = MAX(right_bar + (flash_light > 0 ? 1 : PENALTY), 0);
+ if (right_bar >= LED_HEIGHT * 7) winner = 1;
+ } else {
+ left_bar = MAX(left_bar + (flash_light > 0 ? 1 : PENALTY), 0);
+ if (left_bar >= LED_HEIGHT * 7) winner = 2;
+ }
+ if(winner > 0) end = 10;
+ }
+}
+
+void draw_flash(uint8_t b) {
+ setLedXY(1, 3, b);
+ setLedXY(1, 4, b);
+ setLedXY(2, 3, b);
+ setLedXY(2, 4, b);
+}
+
+void draw_winner1(void) {
+ setLedXY(2, 1, 7);
+ setLedXY(1, 2, 7);
+ setLedXY(1, 0, 7);
+}
+
+void draw_winner2(void) {
+ setLedXY(1, 1, 7);
+ setLedXY(2, 2, 7);
+ setLedXY(2, 0, 7);
+}
+
View
@@ -0,0 +1,188 @@
+#include <main.h>
+
+#define MAX_SLOW 15
+#define MIN_SLOW 5
+#define SHOT_SLOW 10
+#define SPEEDUP_SLOW 2
+
+#define EXPL_LEN 20
+
+#define ABS(x) (x > 0 ? x : -x)
+
+struct player {
+ int8_t pos;
+ int8_t dir;
+ int8_t slow;
+ int8_t tick;
+ int8_t shot[2];
+};
+
+static void init(void) ATTRIBUTES;
+
+static uint8_t tick(void);
+static void key(key_type key, event_type event);
+
+static int8_t state = -1;
+static int8_t key_state;
+static struct player players[2];
+static int8_t lost;
+static int8_t shot_tick;
+static int8_t speedup_tick;
+
+void init(void) {
+ registerApp(tick, key, 5);
+}
+
+uint8_t tick(void) {
+ // INIT
+ if(state < 0) {
+ // no key active
+ key_state = 0;
+
+ // nobody lost, yet
+ lost = 0;
+
+ // players start slowly in their corner
+ for(uint8_t p = 0; p < 2; ++p) {
+ struct player *player = &players[p];
+
+ player->pos = p ? (LED_WIDTH - 1) : 0;
+ player->dir = p ? -1 : 1;
+ player->slow = MAX_SLOW;
+ player->tick = MAX_SLOW;
+
+ player->shot[0] = -1;
+ }
+
+ state = 0;
+ }
+
+ // RUN
+ if(state == 0) {
+ // player loop
+ for(uint8_t p = 0; p < 2; ++p) {
+ struct player *player = &players[p];
+ const int8_t vert = p ? (LED_HEIGHT - 1) : 0;
+
+ // player step
+ if(++player->tick > player->slow) {
+ // clear old position
+ setLedXY(player->pos, vert, 0);
+
+ // bounce?
+ if(player->pos == (p ? 0 : LED_WIDTH - 1)) {
+ // player->pos *= -1;
+ player->pos = p ? (LED_WIDTH) : -1;
+ }
+
+ // move
+ player->pos += player->dir;
+
+ // paint new position
+ setLedXY(player->pos, vert, 7);
+
+ // reset the state
+ player->tick = 0;
+ }
+
+ // shooting?
+ if(key_state & (p + 1)) {
+ if(player->shot[0] == -1) {
+ // place projectile
+ player->shot[1] = p ? (LED_HEIGHT - 1) : 0;
+ player->shot[0] = player->pos;
+ }
+
+ // slowdown
+ player->slow = MIN_SLOW;
+ }
+
+ // speedup
+ if(speedup_tick == 0 && player->slow < MAX_SLOW) {
+ ++player->slow;
+ }
+ }
+
+ if(++speedup_tick > SPEEDUP_SLOW) {
+ speedup_tick = 0;
+ }
+
+ // shot loop
+ for(uint8_t p = 0; p < 2; ++p) {
+ int8_t *shot = players[p].shot;
+
+ if(shot[0] >= 0) {
+ if(++shot_tick > SHOT_SLOW) {
+ // clear projectile?
+ if(shot[1]) {
+ setLedXY(shot[0], shot[1], 0);
+ }
+
+ // move projectile?
+ shot[1] += p ? -1 : 1;
+
+
+ if(shot[1] < 0 || shot[1] >= LED_HEIGHT) {
+ shot[0] = -1;
+ } else {
+ // paint projectile
+ setLedXY(shot[0], shot[1], 3);
+ }
+
+ // reset
+ shot_tick = 0;
+ }
+
+ // hit?
+ if(shot[1] == (p ? 0 : (LED_HEIGHT - 1)) && shot[0] == players[!p].pos) {
+ state = 1;
+ lost |= !p + 1;
+ }
+ }
+ }
+ }
+
+ // THE END
+ if(state > 0) {
+ int8_t expand = state < EXPL_LEN ? state : (2 * EXPL_LEN - state);
+
+ for(uint8_t p = 0; p < 2; ++p) {
+ if((p + 1) & lost) {
+ for(uint8_t x = 0; x < LED_WIDTH; ++x) {
+ for(uint8_t y = 0; y < LED_HEIGHT; ++y) {
+ const int16_t dx = x - players[p].pos;
+ const int16_t dy = y - (p ? LED_HEIGHT - 1 : 0);
+
+ int16_t bright = (7 - (dx * dx + dy * dy)) * expand / EXPL_LEN;
+
+ if(bright >= 0) {
+ setLedXY(x, y, bright);
+ }
+ }
+ }
+ }
+ }
+
+ for(uint8_t p = 0; p < 2; ++p) {
+ setLedXY(players[p].pos, p ? LED_HEIGHT - 1 : 0, 7);
+ }
+
+ if(state >= EXPL_LEN * 2) {
+ state = -1;
+ return 1;
+ } else {
+ state++;
+ }
+ }
+
+ return 0;
+}
+
+void key(key_type key, event_type event) {
+ if(event == DOWN) {
+ key_state |= key + 1;
+ } else {
+ key_state &= ~(key + 1);
+ }
+}
+
View
@@ -51,7 +51,7 @@ static void new_apple(void) {
}
void init_snake(void) {
- registerApp(tick_snake, key_snake, 22);
+ registerApp(tick_snake, key_snake, 17);
}
uint8_t tick_snake() {
Oops, something went wrong.

0 comments on commit e2cbc5f

Please sign in to comment.