Chris Dick
/
Gameduino_Frogger_game
Frogger game for the Gameduino
main.cpp@3:91b295944b9a, 2012-12-21 (annotated)
- Committer:
- TheChrisyd
- Date:
- Fri Dec 21 13:51:01 2012 +0000
- Revision:
- 3:91b295944b9a
- Parent:
- 2:39f014e52c1c
updated Gameduino library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TheChrisyd | 0:d8b6340b0cdf | 1 | #include "mbed.h" |
TheChrisyd | 0:d8b6340b0cdf | 2 | #include "GD.h" |
TheChrisyd | 0:d8b6340b0cdf | 3 | #include "frogger_background.h" |
TheChrisyd | 0:d8b6340b0cdf | 4 | #include "frogger_sprite.h" |
TheChrisyd | 0:d8b6340b0cdf | 5 | #include "shield.h" |
TheChrisyd | 0:d8b6340b0cdf | 6 | |
TheChrisyd | 0:d8b6340b0cdf | 7 | |
TheChrisyd | 0:d8b6340b0cdf | 8 | Serial debug(USBTX, USBRX); |
TheChrisyd | 0:d8b6340b0cdf | 9 | GDClass GD(ARD_MOSI, ARD_MISO, ARD_SCK, ARD_D9, USBTX, USBRX) ; |
TheChrisyd | 0:d8b6340b0cdf | 10 | |
TheChrisyd | 0:d8b6340b0cdf | 11 | #define CONTROL_LEFT 1 |
TheChrisyd | 0:d8b6340b0cdf | 12 | #define CONTROL_RIGHT 2 |
TheChrisyd | 0:d8b6340b0cdf | 13 | #define CONTROL_UP 4 |
TheChrisyd | 0:d8b6340b0cdf | 14 | #define CONTROL_DOWN 8 |
TheChrisyd | 0:d8b6340b0cdf | 15 | DigitalIn down(ARD_A2); |
TheChrisyd | 0:d8b6340b0cdf | 16 | DigitalIn up(ARD_A3); |
TheChrisyd | 0:d8b6340b0cdf | 17 | DigitalIn left(ARD_A1); |
TheChrisyd | 0:d8b6340b0cdf | 18 | DigitalIn right(ARD_A0); |
TheChrisyd | 0:d8b6340b0cdf | 19 | |
TheChrisyd | 0:d8b6340b0cdf | 20 | class Controller { |
TheChrisyd | 0:d8b6340b0cdf | 21 | public: |
TheChrisyd | 0:d8b6340b0cdf | 22 | |
TheChrisyd | 0:d8b6340b0cdf | 23 | void begin() { |
TheChrisyd | 1:768e3d3a3ba0 | 24 | down.mode(PullUp); |
TheChrisyd | 1:768e3d3a3ba0 | 25 | up.mode(PullUp); |
TheChrisyd | 1:768e3d3a3ba0 | 26 | left.mode(PullUp); |
TheChrisyd | 1:768e3d3a3ba0 | 27 | right.mode(PullUp); |
TheChrisyd | 0:d8b6340b0cdf | 28 | prev = 0; |
TheChrisyd | 0:d8b6340b0cdf | 29 | } |
TheChrisyd | 0:d8b6340b0cdf | 30 | |
TheChrisyd | 0:d8b6340b0cdf | 31 | byte read() { |
TheChrisyd | 0:d8b6340b0cdf | 32 | byte r = 0; |
TheChrisyd | 2:39f014e52c1c | 33 | if (!down) |
TheChrisyd | 0:d8b6340b0cdf | 34 | r |= CONTROL_DOWN; |
TheChrisyd | 2:39f014e52c1c | 35 | if (!up) |
TheChrisyd | 0:d8b6340b0cdf | 36 | r |= CONTROL_UP; |
TheChrisyd | 2:39f014e52c1c | 37 | if (!left) |
TheChrisyd | 0:d8b6340b0cdf | 38 | r |= CONTROL_LEFT; |
TheChrisyd | 2:39f014e52c1c | 39 | if (!right) |
TheChrisyd | 0:d8b6340b0cdf | 40 | r |= CONTROL_RIGHT; |
TheChrisyd | 0:d8b6340b0cdf | 41 | byte edge = r & ~prev; |
TheChrisyd | 0:d8b6340b0cdf | 42 | prev = r; |
TheChrisyd | 0:d8b6340b0cdf | 43 | return edge; |
TheChrisyd | 0:d8b6340b0cdf | 44 | } |
TheChrisyd | 0:d8b6340b0cdf | 45 | private: |
TheChrisyd | 0:d8b6340b0cdf | 46 | byte prev; |
TheChrisyd | 0:d8b6340b0cdf | 47 | }; |
TheChrisyd | 0:d8b6340b0cdf | 48 | |
TheChrisyd | 0:d8b6340b0cdf | 49 | static Controller Control; |
TheChrisyd | 0:d8b6340b0cdf | 50 | |
TheChrisyd | 0:d8b6340b0cdf | 51 | #define BG_BLUE 0 |
TheChrisyd | 0:d8b6340b0cdf | 52 | #define BG_ZERO 12 |
TheChrisyd | 0:d8b6340b0cdf | 53 | #define BG_BLACK 34 |
TheChrisyd | 0:d8b6340b0cdf | 54 | #define BG_LIFE 35 |
TheChrisyd | 0:d8b6340b0cdf | 55 | |
TheChrisyd | 0:d8b6340b0cdf | 56 | static int atxy(byte x, byte y) { |
TheChrisyd | 0:d8b6340b0cdf | 57 | return RAM_PIC + 64 * y + (x + 11); |
TheChrisyd | 0:d8b6340b0cdf | 58 | } |
TheChrisyd | 0:d8b6340b0cdf | 59 | |
TheChrisyd | 0:d8b6340b0cdf | 60 | static void draw_score(uint16_t dst, long n) { |
TheChrisyd | 0:d8b6340b0cdf | 61 | GD.wr(dst + 0, BG_ZERO + (n / 10000) % 10); // ten-thousands |
TheChrisyd | 0:d8b6340b0cdf | 62 | GD.wr(dst + 1, BG_ZERO + (n / 1000) % 10); // thousands |
TheChrisyd | 0:d8b6340b0cdf | 63 | GD.wr(dst + 2, BG_ZERO + (n / 100) % 10); // hundreds |
TheChrisyd | 0:d8b6340b0cdf | 64 | GD.wr(dst + 3, BG_ZERO + (n / 10) % 10); // tens |
TheChrisyd | 0:d8b6340b0cdf | 65 | GD.wr(dst + 4, BG_ZERO + n % 10); // ones |
TheChrisyd | 0:d8b6340b0cdf | 66 | } |
TheChrisyd | 0:d8b6340b0cdf | 67 | |
TheChrisyd | 0:d8b6340b0cdf | 68 | // Game variables |
TheChrisyd | 0:d8b6340b0cdf | 69 | static unsigned int t; |
TheChrisyd | 0:d8b6340b0cdf | 70 | static int frogx, frogy; // screen position |
TheChrisyd | 0:d8b6340b0cdf | 71 | static int leaping; // 0 means not leaping, 1-8 animates the leap |
TheChrisyd | 0:d8b6340b0cdf | 72 | static int frogdir; // while leaping, which direction is the leap? |
TheChrisyd | 0:d8b6340b0cdf | 73 | static int frogface; // which way is the frog facing, as a sprite ROT field |
TheChrisyd | 0:d8b6340b0cdf | 74 | static int dying; // 0 means not dying, 1-64 animation counter |
TheChrisyd | 0:d8b6340b0cdf | 75 | static long score; |
TheChrisyd | 0:d8b6340b0cdf | 76 | static long hiscore; |
TheChrisyd | 0:d8b6340b0cdf | 77 | static byte lives; |
TheChrisyd | 0:d8b6340b0cdf | 78 | static byte done[5]; |
TheChrisyd | 0:d8b6340b0cdf | 79 | static byte homes[5] = { 24, 72, 120, 168, 216 }; |
TheChrisyd | 0:d8b6340b0cdf | 80 | |
TheChrisyd | 0:d8b6340b0cdf | 81 | void frog_start() { |
TheChrisyd | 0:d8b6340b0cdf | 82 | frogx = 120; |
TheChrisyd | 0:d8b6340b0cdf | 83 | frogy = 232; |
TheChrisyd | 0:d8b6340b0cdf | 84 | leaping = 0; |
TheChrisyd | 0:d8b6340b0cdf | 85 | frogdir = 0; |
TheChrisyd | 0:d8b6340b0cdf | 86 | frogface = 0; |
TheChrisyd | 0:d8b6340b0cdf | 87 | dying = 0; |
TheChrisyd | 0:d8b6340b0cdf | 88 | } |
TheChrisyd | 0:d8b6340b0cdf | 89 | |
TheChrisyd | 0:d8b6340b0cdf | 90 | void level_start() { |
TheChrisyd | 0:d8b6340b0cdf | 91 | for (byte i = 0; i < 5; i++) |
TheChrisyd | 0:d8b6340b0cdf | 92 | done[i] = 0; |
TheChrisyd | 0:d8b6340b0cdf | 93 | |
TheChrisyd | 0:d8b6340b0cdf | 94 | GD.begin(); |
TheChrisyd | 0:d8b6340b0cdf | 95 | //wait_ms(500); |
TheChrisyd | 0:d8b6340b0cdf | 96 | GD.copy(RAM_CHR, froggerbg_chr, sizeof(froggerbg_chr)); |
TheChrisyd | 0:d8b6340b0cdf | 97 | GD.copy(RAM_PAL, froggerbg_pal, sizeof(froggerbg_pal)); |
TheChrisyd | 0:d8b6340b0cdf | 98 | GD.fill(RAM_PIC, BG_BLACK, 4096); |
TheChrisyd | 0:d8b6340b0cdf | 99 | for (byte y = 0; y < 32; y++) |
TheChrisyd | 0:d8b6340b0cdf | 100 | GD.copy(atxy(0, y), froggerbg_pic + y * 28, 28); |
TheChrisyd | 0:d8b6340b0cdf | 101 | GD.fill(1 * 64 + 11, BG_BLUE, 28); |
TheChrisyd | 0:d8b6340b0cdf | 102 | |
TheChrisyd | 0:d8b6340b0cdf | 103 | byte i = 255; |
TheChrisyd | 0:d8b6340b0cdf | 104 | for (int y = 0; y < 256; y += 16) { |
TheChrisyd | 0:d8b6340b0cdf | 105 | GD.sprite(i--, 0, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 106 | GD.sprite(i--, 16, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 107 | GD.sprite(i--, 24, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 108 | GD.sprite(i--, 40, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 109 | GD.sprite(i--, 56, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 110 | GD.sprite(i--, 72, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 111 | GD.sprite(i--, 72 + 240, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 112 | GD.sprite(i--, 72 + 256, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 113 | GD.sprite(i--, 72 + 272, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 114 | GD.sprite(i--, 72 + 288, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 115 | GD.sprite(i--, 72 + 304, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 116 | GD.sprite(i--, 72 + 320, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 117 | GD.sprite(i--, 72 + 336, y, 63, 0); |
TheChrisyd | 0:d8b6340b0cdf | 118 | } |
TheChrisyd | 0:d8b6340b0cdf | 119 | |
TheChrisyd | 0:d8b6340b0cdf | 120 | GD.copy(PALETTE16A, sprite_sprpal, sizeof(sprite_sprpal)); |
TheChrisyd | 0:d8b6340b0cdf | 121 | GD.uncompress(RAM_SPRIMG, sprite_sprimg); |
TheChrisyd | 0:d8b6340b0cdf | 122 | } |
TheChrisyd | 0:d8b6340b0cdf | 123 | |
TheChrisyd | 0:d8b6340b0cdf | 124 | void game_start() { |
TheChrisyd | 0:d8b6340b0cdf | 125 | lives = 4; |
TheChrisyd | 0:d8b6340b0cdf | 126 | score = 0; |
TheChrisyd | 0:d8b6340b0cdf | 127 | } |
TheChrisyd | 0:d8b6340b0cdf | 128 | |
TheChrisyd | 0:d8b6340b0cdf | 129 | void setup() { |
TheChrisyd | 0:d8b6340b0cdf | 130 | Control.begin(); |
TheChrisyd | 0:d8b6340b0cdf | 131 | |
TheChrisyd | 0:d8b6340b0cdf | 132 | game_start(); |
TheChrisyd | 0:d8b6340b0cdf | 133 | level_start(); |
TheChrisyd | 0:d8b6340b0cdf | 134 | frog_start(); |
TheChrisyd | 0:d8b6340b0cdf | 135 | } |
TheChrisyd | 0:d8b6340b0cdf | 136 | |
TheChrisyd | 0:d8b6340b0cdf | 137 | static void sprite(byte x, byte y, byte anim, byte rot = 0) { |
TheChrisyd | 0:d8b6340b0cdf | 138 | draw_sprite(x + 80, y, anim, rot); |
TheChrisyd | 0:d8b6340b0cdf | 139 | } |
TheChrisyd | 0:d8b6340b0cdf | 140 | |
TheChrisyd | 0:d8b6340b0cdf | 141 | static void turtle3(byte x, byte y) { |
TheChrisyd | 0:d8b6340b0cdf | 142 | byte anim = 50 + ((t / 32) % 3); |
TheChrisyd | 0:d8b6340b0cdf | 143 | sprite(x, y, anim); |
TheChrisyd | 0:d8b6340b0cdf | 144 | sprite(x + 16, y, anim); |
TheChrisyd | 0:d8b6340b0cdf | 145 | sprite(x + 32, y, anim); |
TheChrisyd | 0:d8b6340b0cdf | 146 | } |
TheChrisyd | 0:d8b6340b0cdf | 147 | |
TheChrisyd | 0:d8b6340b0cdf | 148 | static void turtle2(byte x, byte y) { |
TheChrisyd | 0:d8b6340b0cdf | 149 | byte anim = 50 + ((t / 32) % 3); |
TheChrisyd | 0:d8b6340b0cdf | 150 | sprite(x, y, anim); |
TheChrisyd | 0:d8b6340b0cdf | 151 | sprite(x + 16, y, anim); |
TheChrisyd | 0:d8b6340b0cdf | 152 | } |
TheChrisyd | 0:d8b6340b0cdf | 153 | |
TheChrisyd | 0:d8b6340b0cdf | 154 | void log1(byte x, byte y) { |
TheChrisyd | 0:d8b6340b0cdf | 155 | sprite(x, y, 86); |
TheChrisyd | 0:d8b6340b0cdf | 156 | sprite(x + 16, y, 87); |
TheChrisyd | 0:d8b6340b0cdf | 157 | sprite(x + 32, y, 88); |
TheChrisyd | 0:d8b6340b0cdf | 158 | } |
TheChrisyd | 0:d8b6340b0cdf | 159 | |
TheChrisyd | 0:d8b6340b0cdf | 160 | void log(byte length, byte x, byte y) { |
TheChrisyd | 0:d8b6340b0cdf | 161 | sprite(x, y, 86); |
TheChrisyd | 0:d8b6340b0cdf | 162 | while (length--) { |
TheChrisyd | 0:d8b6340b0cdf | 163 | x += 16; |
TheChrisyd | 0:d8b6340b0cdf | 164 | sprite(x, y, 87); |
TheChrisyd | 0:d8b6340b0cdf | 165 | } |
TheChrisyd | 0:d8b6340b0cdf | 166 | sprite(x + 16, y, 88); |
TheChrisyd | 0:d8b6340b0cdf | 167 | } |
TheChrisyd | 0:d8b6340b0cdf | 168 | |
TheChrisyd | 0:d8b6340b0cdf | 169 | static int riverat(byte y, uint16_t tt) { |
TheChrisyd | 0:d8b6340b0cdf | 170 | switch (y) { |
TheChrisyd | 0:d8b6340b0cdf | 171 | case 120: |
TheChrisyd | 0:d8b6340b0cdf | 172 | return -tt; |
TheChrisyd | 0:d8b6340b0cdf | 173 | case 104: |
TheChrisyd | 0:d8b6340b0cdf | 174 | return tt; |
TheChrisyd | 0:d8b6340b0cdf | 175 | case 88: |
TheChrisyd | 0:d8b6340b0cdf | 176 | return 5 * tt / 4; |
TheChrisyd | 0:d8b6340b0cdf | 177 | case 72: |
TheChrisyd | 0:d8b6340b0cdf | 178 | return -tt / 2; |
TheChrisyd | 0:d8b6340b0cdf | 179 | case 56: |
TheChrisyd | 0:d8b6340b0cdf | 180 | return tt / 2; |
TheChrisyd | 0:d8b6340b0cdf | 181 | default: |
TheChrisyd | 0:d8b6340b0cdf | 182 | return 0; |
TheChrisyd | 0:d8b6340b0cdf | 183 | } |
TheChrisyd | 0:d8b6340b0cdf | 184 | } |
TheChrisyd | 0:d8b6340b0cdf | 185 | |
TheChrisyd | 0:d8b6340b0cdf | 186 | // midi frequency table |
TheChrisyd | 0:d8b6340b0cdf | 187 | static int midifreq[128] = { |
TheChrisyd | 0:d8b6340b0cdf | 188 | 32,34,36,38,41,43,46,48,51,55,58,61,65,69,73,77,82,87,92,97,103,110,116,123,130,138,146,155,164,174,184,195,207,220,233,246,261,277,293,311,329,349,369,391,415,440,466,493,523,554,587,622,659,698,739,783,830,880,932,987,1046,1108,1174,1244,1318,1396,1479,1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,8869,9397,9956,10548,11175,11839,12543,13289,14080,14917,15804,16744,17739,18794,19912,21096,22350,23679,25087,26579,28160,29834,31608,33488,35479,37589,39824,42192,44701,47359,50175 |
TheChrisyd | 0:d8b6340b0cdf | 189 | }; |
TheChrisyd | 0:d8b6340b0cdf | 190 | |
TheChrisyd | 0:d8b6340b0cdf | 191 | //#define MIDI(n) pgm_read_word(midifreq + (n)) |
TheChrisyd | 0:d8b6340b0cdf | 192 | |
TheChrisyd | 0:d8b6340b0cdf | 193 | static void squarewave(uint16_t freq, byte amp) { |
TheChrisyd | 0:d8b6340b0cdf | 194 | GD.voice(0, 0, freq, amp, amp); |
TheChrisyd | 0:d8b6340b0cdf | 195 | GD.voice(1, 0, 3 * freq, amp/3, amp/3); |
TheChrisyd | 0:d8b6340b0cdf | 196 | GD.voice(2, 0, 5 * freq, amp/5, amp/5); |
TheChrisyd | 0:d8b6340b0cdf | 197 | GD.voice(3, 0, 7 * freq, amp/7, amp/7); |
TheChrisyd | 0:d8b6340b0cdf | 198 | GD.voice(4, 0, 9 * freq, amp/9, amp/9); |
TheChrisyd | 0:d8b6340b0cdf | 199 | GD.voice(5, 0, 11 * freq, amp/11, amp/11); |
TheChrisyd | 0:d8b6340b0cdf | 200 | } |
TheChrisyd | 0:d8b6340b0cdf | 201 | |
TheChrisyd | 0:d8b6340b0cdf | 202 | static void sound() { |
TheChrisyd | 0:d8b6340b0cdf | 203 | byte note; |
TheChrisyd | 0:d8b6340b0cdf | 204 | |
TheChrisyd | 0:d8b6340b0cdf | 205 | if (dying) { |
TheChrisyd | 0:d8b6340b0cdf | 206 | note = 84 - (dying / 2); |
TheChrisyd | 0:d8b6340b0cdf | 207 | squarewave(midifreq[note], 100); |
TheChrisyd | 0:d8b6340b0cdf | 208 | } else if (leaping) { |
TheChrisyd | 0:d8b6340b0cdf | 209 | if (leaping & 1) |
TheChrisyd | 0:d8b6340b0cdf | 210 | note = 60 + leaping; |
TheChrisyd | 0:d8b6340b0cdf | 211 | else |
TheChrisyd | 0:d8b6340b0cdf | 212 | note = 72 + leaping; |
TheChrisyd | 0:d8b6340b0cdf | 213 | squarewave(midifreq[note], 100); |
TheChrisyd | 0:d8b6340b0cdf | 214 | } else { |
TheChrisyd | 0:d8b6340b0cdf | 215 | squarewave(0, 0); // silence |
TheChrisyd | 0:d8b6340b0cdf | 216 | } |
TheChrisyd | 0:d8b6340b0cdf | 217 | } |
TheChrisyd | 0:d8b6340b0cdf | 218 | |
TheChrisyd | 0:d8b6340b0cdf | 219 | void loop() { |
TheChrisyd | 0:d8b6340b0cdf | 220 | GD.__wstartspr(0); |
TheChrisyd | 0:d8b6340b0cdf | 221 | |
TheChrisyd | 0:d8b6340b0cdf | 222 | // Completed homes |
TheChrisyd | 0:d8b6340b0cdf | 223 | for (byte i = 0; i < 5; i++) { |
TheChrisyd | 0:d8b6340b0cdf | 224 | if (done[i]) |
TheChrisyd | 0:d8b6340b0cdf | 225 | sprite(homes[i], 40, 63); |
TheChrisyd | 0:d8b6340b0cdf | 226 | } |
TheChrisyd | 0:d8b6340b0cdf | 227 | |
TheChrisyd | 0:d8b6340b0cdf | 228 | // Yellow cars |
TheChrisyd | 0:d8b6340b0cdf | 229 | sprite(-t, 216, 3); |
TheChrisyd | 0:d8b6340b0cdf | 230 | sprite(-t + 128, 216, 3); |
TheChrisyd | 0:d8b6340b0cdf | 231 | |
TheChrisyd | 0:d8b6340b0cdf | 232 | // Dozers |
TheChrisyd | 0:d8b6340b0cdf | 233 | sprite(t, 200, 4); |
TheChrisyd | 0:d8b6340b0cdf | 234 | sprite(t + 50, 200, 4); |
TheChrisyd | 0:d8b6340b0cdf | 235 | sprite(t + 150, 200, 4); |
TheChrisyd | 0:d8b6340b0cdf | 236 | |
TheChrisyd | 0:d8b6340b0cdf | 237 | // Purple cars |
TheChrisyd | 0:d8b6340b0cdf | 238 | sprite(-t, 184, 7); |
TheChrisyd | 0:d8b6340b0cdf | 239 | sprite(-t + 75, 184, 7); |
TheChrisyd | 0:d8b6340b0cdf | 240 | sprite(-t + 150, 184, 7); |
TheChrisyd | 0:d8b6340b0cdf | 241 | |
TheChrisyd | 0:d8b6340b0cdf | 242 | // Green and white racecars |
TheChrisyd | 0:d8b6340b0cdf | 243 | sprite(2 * t, 168, 8); |
TheChrisyd | 0:d8b6340b0cdf | 244 | |
TheChrisyd | 0:d8b6340b0cdf | 245 | // Trucks |
TheChrisyd | 0:d8b6340b0cdf | 246 | sprite(-t/2, 152, 5); |
TheChrisyd | 0:d8b6340b0cdf | 247 | sprite(-t/2 + 16, 152, 6); |
TheChrisyd | 0:d8b6340b0cdf | 248 | sprite(-t/2 + 100, 152, 5); |
TheChrisyd | 0:d8b6340b0cdf | 249 | sprite(-t/2 + 116, 152, 6); |
TheChrisyd | 0:d8b6340b0cdf | 250 | |
TheChrisyd | 0:d8b6340b0cdf | 251 | // Turtles |
TheChrisyd | 0:d8b6340b0cdf | 252 | for (int i = 0; i < 256; i += 64) |
TheChrisyd | 0:d8b6340b0cdf | 253 | turtle3(riverat(120, t) + i, 120); |
TheChrisyd | 0:d8b6340b0cdf | 254 | |
TheChrisyd | 0:d8b6340b0cdf | 255 | // Short logs |
TheChrisyd | 0:d8b6340b0cdf | 256 | for (int i = 0; i < 240; i += 80) |
TheChrisyd | 0:d8b6340b0cdf | 257 | log(1, riverat(104, t) + i, 104); |
TheChrisyd | 0:d8b6340b0cdf | 258 | |
TheChrisyd | 0:d8b6340b0cdf | 259 | // Long logs |
TheChrisyd | 0:d8b6340b0cdf | 260 | for (int i = 0; i < 256; i += 128) |
TheChrisyd | 0:d8b6340b0cdf | 261 | log(5, riverat(88, t) + i, 88); |
TheChrisyd | 0:d8b6340b0cdf | 262 | |
TheChrisyd | 0:d8b6340b0cdf | 263 | // Turtles again, but slower |
TheChrisyd | 0:d8b6340b0cdf | 264 | for (int i = 0; i < 250; i += 50) |
TheChrisyd | 0:d8b6340b0cdf | 265 | turtle2(riverat(72, t) + i, 72); |
TheChrisyd | 0:d8b6340b0cdf | 266 | |
TheChrisyd | 0:d8b6340b0cdf | 267 | // Top logs |
TheChrisyd | 0:d8b6340b0cdf | 268 | for (int i = 0; i < 210; i += 70) |
TheChrisyd | 0:d8b6340b0cdf | 269 | log(2, riverat(56, t) + i, 56); |
TheChrisyd | 0:d8b6340b0cdf | 270 | |
TheChrisyd | 0:d8b6340b0cdf | 271 | // The frog himself, or his death animation |
TheChrisyd | 0:d8b6340b0cdf | 272 | byte frogspr = GD.spr; // record which sprite slot frog got, for collision check below |
TheChrisyd | 0:d8b6340b0cdf | 273 | |
TheChrisyd | 0:d8b6340b0cdf | 274 | if (!dying) { |
TheChrisyd | 0:d8b6340b0cdf | 275 | static byte frog_anim[] = {2, 1, 0, 0, 2}; |
TheChrisyd | 0:d8b6340b0cdf | 276 | sprite(frogx, frogy, frog_anim[leaping / 2], frogface); |
TheChrisyd | 0:d8b6340b0cdf | 277 | } else { |
TheChrisyd | 0:d8b6340b0cdf | 278 | static byte die_anim[] = {31, 32, 33, 30}; |
TheChrisyd | 0:d8b6340b0cdf | 279 | sprite(frogx, frogy, die_anim[dying / 16], frogface); |
TheChrisyd | 0:d8b6340b0cdf | 280 | } |
TheChrisyd | 0:d8b6340b0cdf | 281 | |
TheChrisyd | 0:d8b6340b0cdf | 282 | GD.__end(); |
TheChrisyd | 0:d8b6340b0cdf | 283 | t++; |
TheChrisyd | 0:d8b6340b0cdf | 284 | |
TheChrisyd | 0:d8b6340b0cdf | 285 | // player control. If button pressed, start the 'leaping' counter |
TheChrisyd | 0:d8b6340b0cdf | 286 | byte con = Control.read(); |
TheChrisyd | 0:d8b6340b0cdf | 287 | if (!dying && (leaping == 0) && con) { |
TheChrisyd | 0:d8b6340b0cdf | 288 | frogdir = con; |
TheChrisyd | 0:d8b6340b0cdf | 289 | leaping = 1; |
TheChrisyd | 0:d8b6340b0cdf | 290 | score += 10; |
TheChrisyd | 0:d8b6340b0cdf | 291 | } else if (leaping > 0) { |
TheChrisyd | 0:d8b6340b0cdf | 292 | if (leaping <= 8) { |
TheChrisyd | 0:d8b6340b0cdf | 293 | if (frogdir == CONTROL_LEFT) { |
TheChrisyd | 0:d8b6340b0cdf | 294 | frogx -= 2; |
TheChrisyd | 0:d8b6340b0cdf | 295 | frogface = 3; |
TheChrisyd | 0:d8b6340b0cdf | 296 | } |
TheChrisyd | 0:d8b6340b0cdf | 297 | if (frogdir == CONTROL_RIGHT) { |
TheChrisyd | 0:d8b6340b0cdf | 298 | frogx += 2; |
TheChrisyd | 0:d8b6340b0cdf | 299 | frogface = 5; |
TheChrisyd | 0:d8b6340b0cdf | 300 | } |
TheChrisyd | 0:d8b6340b0cdf | 301 | if (frogdir == CONTROL_UP) { |
TheChrisyd | 0:d8b6340b0cdf | 302 | frogy -= 2; |
TheChrisyd | 0:d8b6340b0cdf | 303 | frogface = 0; |
TheChrisyd | 0:d8b6340b0cdf | 304 | } |
TheChrisyd | 0:d8b6340b0cdf | 305 | if (frogdir == CONTROL_DOWN) { |
TheChrisyd | 0:d8b6340b0cdf | 306 | frogy += 2; |
TheChrisyd | 0:d8b6340b0cdf | 307 | frogface = 6; |
TheChrisyd | 0:d8b6340b0cdf | 308 | } |
TheChrisyd | 0:d8b6340b0cdf | 309 | leaping++; |
TheChrisyd | 0:d8b6340b0cdf | 310 | } else { |
TheChrisyd | 0:d8b6340b0cdf | 311 | leaping = 0; |
TheChrisyd | 0:d8b6340b0cdf | 312 | } |
TheChrisyd | 0:d8b6340b0cdf | 313 | } |
TheChrisyd | 0:d8b6340b0cdf | 314 | |
TheChrisyd | 0:d8b6340b0cdf | 315 | GD.waitvblank(); |
TheChrisyd | 0:d8b6340b0cdf | 316 | byte touching = (GD.rd(COLLISION + frogspr) != 0xff); |
TheChrisyd | 0:d8b6340b0cdf | 317 | |
TheChrisyd | 0:d8b6340b0cdf | 318 | if (dying) { |
TheChrisyd | 0:d8b6340b0cdf | 319 | if (++dying == 64) { |
TheChrisyd | 0:d8b6340b0cdf | 320 | if (--lives == 0) { |
TheChrisyd | 0:d8b6340b0cdf | 321 | game_start(); |
TheChrisyd | 0:d8b6340b0cdf | 322 | level_start(); |
TheChrisyd | 0:d8b6340b0cdf | 323 | } |
TheChrisyd | 0:d8b6340b0cdf | 324 | frog_start(); |
TheChrisyd | 0:d8b6340b0cdf | 325 | } |
TheChrisyd | 0:d8b6340b0cdf | 326 | } else if (frogx < 8 || frogx > 224) { |
TheChrisyd | 0:d8b6340b0cdf | 327 | dying = 1; |
TheChrisyd | 0:d8b6340b0cdf | 328 | } else if (frogy >= 136) { // road section |
TheChrisyd | 0:d8b6340b0cdf | 329 | // if touching something, frog dies |
TheChrisyd | 0:d8b6340b0cdf | 330 | if (touching) |
TheChrisyd | 0:d8b6340b0cdf | 331 | dying = 1; |
TheChrisyd | 0:d8b6340b0cdf | 332 | } else if (frogy > 40) { // river section |
TheChrisyd | 0:d8b6340b0cdf | 333 | if (!leaping) { |
TheChrisyd | 0:d8b6340b0cdf | 334 | // if touching something, frog is safe |
TheChrisyd | 0:d8b6340b0cdf | 335 | if (touching) { |
TheChrisyd | 0:d8b6340b0cdf | 336 | // move frog according to lane speed |
TheChrisyd | 0:d8b6340b0cdf | 337 | int oldx = riverat(frogy, t - 1); |
TheChrisyd | 0:d8b6340b0cdf | 338 | int newx = riverat(frogy, t); |
TheChrisyd | 0:d8b6340b0cdf | 339 | int river_velocity = newx - oldx; |
TheChrisyd | 0:d8b6340b0cdf | 340 | frogx += river_velocity; |
TheChrisyd | 0:d8b6340b0cdf | 341 | } else { |
TheChrisyd | 0:d8b6340b0cdf | 342 | dying = 1; |
TheChrisyd | 0:d8b6340b0cdf | 343 | } |
TheChrisyd | 0:d8b6340b0cdf | 344 | } |
TheChrisyd | 0:d8b6340b0cdf | 345 | } else { // riverbank section |
TheChrisyd | 0:d8b6340b0cdf | 346 | if (!leaping) { |
TheChrisyd | 0:d8b6340b0cdf | 347 | byte landed = 0; |
TheChrisyd | 0:d8b6340b0cdf | 348 | for (byte i = 0; i < 5; i ++) { |
TheChrisyd | 0:d8b6340b0cdf | 349 | if (!done[i] && abs(homes[i] - frogx) < 4) { |
TheChrisyd | 0:d8b6340b0cdf | 350 | done[i] = 1; |
TheChrisyd | 0:d8b6340b0cdf | 351 | landed = 1; |
TheChrisyd | 0:d8b6340b0cdf | 352 | score += 10; |
TheChrisyd | 0:d8b6340b0cdf | 353 | } |
TheChrisyd | 0:d8b6340b0cdf | 354 | } |
TheChrisyd | 0:d8b6340b0cdf | 355 | if (landed) { |
TheChrisyd | 0:d8b6340b0cdf | 356 | if (done[0] && done[1] && done[2] && done[3] && done[4]) |
TheChrisyd | 0:d8b6340b0cdf | 357 | level_start(); |
TheChrisyd | 0:d8b6340b0cdf | 358 | frog_start(); |
TheChrisyd | 0:d8b6340b0cdf | 359 | } else // if frog did not land in a home, die! |
TheChrisyd | 0:d8b6340b0cdf | 360 | dying = 1; |
TheChrisyd | 0:d8b6340b0cdf | 361 | } |
TheChrisyd | 0:d8b6340b0cdf | 362 | } |
TheChrisyd | 0:d8b6340b0cdf | 363 | sound(); |
TheChrisyd | 0:d8b6340b0cdf | 364 | if (score > hiscore) { |
TheChrisyd | 0:d8b6340b0cdf | 365 | hiscore = score; |
TheChrisyd | 0:d8b6340b0cdf | 366 | } |
TheChrisyd | 0:d8b6340b0cdf | 367 | draw_score(atxy(3, 1), score); |
TheChrisyd | 0:d8b6340b0cdf | 368 | draw_score(atxy(11, 1), hiscore); |
TheChrisyd | 0:d8b6340b0cdf | 369 | for (byte i = 0; i < 16; i++) |
TheChrisyd | 0:d8b6340b0cdf | 370 | GD.wr(atxy(i, 30), (i < lives) ? BG_LIFE : BG_BLACK); |
TheChrisyd | 0:d8b6340b0cdf | 371 | } |
TheChrisyd | 0:d8b6340b0cdf | 372 | |
TheChrisyd | 0:d8b6340b0cdf | 373 | |
TheChrisyd | 0:d8b6340b0cdf | 374 | int main() { |
TheChrisyd | 0:d8b6340b0cdf | 375 | setup(); |
TheChrisyd | 0:d8b6340b0cdf | 376 | while (1) { |
TheChrisyd | 0:d8b6340b0cdf | 377 | loop(); |
TheChrisyd | 0:d8b6340b0cdf | 378 | } |
TheChrisyd | 0:d8b6340b0cdf | 379 | } |
TheChrisyd | 0:d8b6340b0cdf | 380 |