updating stuff

Dependencies:   PWMOut Pokitto_

Fork of Pokittris by Pokitto Community Team

Committer:
spinal
Date:
Sun Apr 08 13:32:35 2018 +0000
Revision:
5:abd49397ce0d
Parent:
4:e861ad5ad42d
Pokittris - Falling block game for Pokitto

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mougino 0:f759a823d3ae 1 // music - 8bit archade4 from www.dl-sounds.com
mougino 0:f759a823d3ae 2
mougino 0:f759a823d3ae 3 #include "Pokitto.h"
mougino 0:f759a823d3ae 4 #include "tetris_gfx.h"
mougino 0:f759a823d3ae 5 #include "easing.h"
mougino 0:f759a823d3ae 6
mougino 0:f759a823d3ae 7 #define REPSPEED 12
mougino 0:f759a823d3ae 8
mougino 0:f759a823d3ae 9 Pokitto::Core game;
mougino 0:f759a823d3ae 10
mougino 0:f759a823d3ae 11 bool bgNum = 1;
mougino 0:f759a823d3ae 12 char musicName[] = "pokittris.raw";
mougino 0:f759a823d3ae 13 byte palNum = 0;
mougino 0:f759a823d3ae 14
mougino 0:f759a823d3ae 15 int topLine;
mougino 0:f759a823d3ae 16 byte unlockedPal=0;
mougino 0:f759a823d3ae 17 bool removeLine[19];
mougino 0:f759a823d3ae 18 byte linesToRemove=0;
mougino 0:f759a823d3ae 19 byte animCount;
mougino 0:f759a823d3ae 20 bool splodeOK=0;
mougino 0:f759a823d3ae 21 byte animSplode=0;
mougino 0:f759a823d3ae 22
spinal 4:e861ad5ad42d 23 int sX,sY,sX1,sY1;
spinal 4:e861ad5ad42d 24
spinal 1:827c9852286e 25
spinal 1:827c9852286e 26 /**************************************************************************/
spinal 1:827c9852286e 27 #define HELD 0
spinal 1:827c9852286e 28 #define NEW 1
spinal 1:827c9852286e 29 #define RELEASE 2
spinal 1:827c9852286e 30 byte CompletePad, ExPad, TempPad, myPad;
spinal 1:827c9852286e 31 bool _A[3], _B[3], _C[3], _Up[3], _Down[3], _Left[3], _Right[3];
spinal 1:827c9852286e 32
spinal 1:827c9852286e 33 DigitalIn _aPin(P1_9);
spinal 1:827c9852286e 34 DigitalIn _bPin(P1_4);
spinal 1:827c9852286e 35 DigitalIn _cPin(P1_10);
spinal 1:827c9852286e 36 DigitalIn _upPin(P1_13);
spinal 1:827c9852286e 37 DigitalIn _downPin(P1_3);
spinal 1:827c9852286e 38 DigitalIn _leftPin(P1_25);
spinal 1:827c9852286e 39 DigitalIn _rightPin(P1_7);
spinal 1:827c9852286e 40
mougino 0:f759a823d3ae 41 void UPDATEPAD(int pad, int var) {
mougino 0:f759a823d3ae 42 _C[pad] = (var >> 1)&1;
mougino 0:f759a823d3ae 43 _B[pad] = (var >> 2)&1;
mougino 0:f759a823d3ae 44 _A[pad] = (var >> 3)&1;
mougino 0:f759a823d3ae 45 _Down[pad] = (var >> 4)&1;
mougino 0:f759a823d3ae 46 _Left[pad] = (var >> 5)&1;
mougino 0:f759a823d3ae 47 _Right[pad] = (var >> 6)&1;
mougino 0:f759a823d3ae 48 _Up[pad] = (var >> 7)&1;
mougino 0:f759a823d3ae 49 }
mougino 0:f759a823d3ae 50
mougino 0:f759a823d3ae 51 void UpdatePad(int joy_code){
mougino 0:f759a823d3ae 52 ExPad = CompletePad;
mougino 0:f759a823d3ae 53 CompletePad = joy_code;
mougino 0:f759a823d3ae 54 UPDATEPAD(HELD, CompletePad); // held
mougino 0:f759a823d3ae 55 UPDATEPAD(RELEASE, (ExPad & (~CompletePad))); // released
mougino 0:f759a823d3ae 56 UPDATEPAD(NEW, (CompletePad & (~ExPad))); // newpress
mougino 0:f759a823d3ae 57 }
mougino 0:f759a823d3ae 58
mougino 0:f759a823d3ae 59 byte updateButtons(byte var){
mougino 0:f759a823d3ae 60 var = 0;
spinal 1:827c9852286e 61 if (_cPin) var |= (1<<1);
spinal 1:827c9852286e 62 if (_bPin) var |= (1<<2);
spinal 1:827c9852286e 63 if (_aPin) var |= (1<<3); // P1_9 = A
spinal 1:827c9852286e 64 if (_downPin) var |= (1<<4);
spinal 1:827c9852286e 65 if (_leftPin) var |= (1<<5);
spinal 1:827c9852286e 66 if (_rightPin) var |= (1<<6);
spinal 1:827c9852286e 67 if (_upPin) var |= (1<<7);
mougino 0:f759a823d3ae 68
mougino 0:f759a823d3ae 69 return var;
mougino 0:f759a823d3ae 70 }
mougino 0:f759a823d3ae 71
mougino 0:f759a823d3ae 72
mougino 0:f759a823d3ae 73
mougino 0:f759a823d3ae 74 // some globals...
mougino 0:f759a823d3ae 75 long int frameNumber = 0;
mougino 0:f759a823d3ae 76 long int tempTime;
mougino 0:f759a823d3ae 77 int dropTime = 0;
mougino 0:f759a823d3ae 78 int slideTime = 0;
mougino 0:f759a823d3ae 79 byte gameMode = 0;
mougino 0:f759a823d3ae 80 bool paused = 0;
mougino 0:f759a823d3ae 81 byte menuItem;
mougino 0:f759a823d3ae 82 bool bgmusic = 1;
mougino 0:f759a823d3ae 83
mougino 0:f759a823d3ae 84 byte px, py, ps, pr; // player x,y,shape,rotation
mougino 0:f759a823d3ae 85 byte nextTile;
mougino 0:f759a823d3ae 86 byte lineCount, level;
mougino 0:f759a823d3ae 87 int score, lines;
mougino 0:f759a823d3ae 88 bool okToContinue = 0;
mougino 0:f759a823d3ae 89 byte slideSpeed = 10;
mougino 0:f759a823d3ae 90
mougino 0:f759a823d3ae 91
mougino 0:f759a823d3ae 92 void loadPal(char num){
mougino 0:f759a823d3ae 93 unsigned short curPal[4];
mougino 0:f759a823d3ae 94 curPal[0] = pallet[(num*4)];
mougino 0:f759a823d3ae 95 curPal[1] = pallet[(num*4)+1];
mougino 0:f759a823d3ae 96 curPal[2] = pallet[(num*4)+2];
mougino 0:f759a823d3ae 97 curPal[3] = pallet[(num*4)+3];
mougino 0:f759a823d3ae 98 game.display.load565Palette(curPal);
mougino 0:f759a823d3ae 99 }
mougino 0:f759a823d3ae 100
spinal 4:e861ad5ad42d 101 void drawShape(byte x1, signed char y1, byte shape, byte frame, bool bg=0) {
mougino 0:f759a823d3ae 102 for (char y = 0; y < 4; y++) {
mougino 0:f759a823d3ae 103 if (y1 + y > 1) {
mougino 0:f759a823d3ae 104 for (char x = 0; x < 4; x++) {
mougino 0:f759a823d3ae 105 byte mt = pgm_read_byte(shapeMap + (x + 4 * y) + shape * 64 + (frame * 16));
spinal 4:e861ad5ad42d 106 if (mt > 1 || bg==1) {
mougino 0:f759a823d3ae 107 game.display.drawBitmap((x1 + x)*8, (y1 + y)*8, tile_gfx[(mt-1)+shape*6]);
mougino 0:f759a823d3ae 108 }
mougino 0:f759a823d3ae 109 }
mougino 0:f759a823d3ae 110 }
mougino 0:f759a823d3ae 111 }
mougino 0:f759a823d3ae 112 }
mougino 0:f759a823d3ae 113
mougino 0:f759a823d3ae 114 bool check(signed char x1, signed char y1, char rot) {
mougino 0:f759a823d3ae 115 byte ret=0;
mougino 0:f759a823d3ae 116 for (char y = 0; y < 4; y++) {
mougino 0:f759a823d3ae 117 if (y1 + y >= 0) {
mougino 0:f759a823d3ae 118 for (char x = 0; x < 4; x++) {
mougino 0:f759a823d3ae 119 byte mt = pgm_read_byte(shapeMap + (x + 4 * y) + ps * 64 + (rot * 16));
mougino 0:f759a823d3ae 120 if (mt > 1) {
mougino 0:f759a823d3ae 121 if ( playfield[(x1 + x) + 16 * (y1 + y)] != 0) {
mougino 0:f759a823d3ae 122 ret= 1;
mougino 0:f759a823d3ae 123 }
mougino 0:f759a823d3ae 124 }
mougino 0:f759a823d3ae 125 }
mougino 0:f759a823d3ae 126 }
mougino 0:f759a823d3ae 127 }
mougino 0:f759a823d3ae 128 return ret;
mougino 0:f759a823d3ae 129 }
mougino 0:f759a823d3ae 130
mougino 0:f759a823d3ae 131 void stamp(signed char x1, signed char y1, byte shape, byte rot) {
mougino 0:f759a823d3ae 132 for (char y = 0; y < 4; y++) {
mougino 0:f759a823d3ae 133 if (y1 + y >= 0) {
mougino 0:f759a823d3ae 134 for (char x = 0; x < 4; x++) {
mougino 0:f759a823d3ae 135 byte mt = pgm_read_byte(shapeMap + (x + 4 * y) + shape * 64 + (rot * 16));
mougino 0:f759a823d3ae 136 if (mt > 1) {
mougino 0:f759a823d3ae 137 playfield[(x1 + x) + 16 * (y1 + y)] = (mt-1)+shape*6;
mougino 0:f759a823d3ae 138 }
mougino 0:f759a823d3ae 139 }
mougino 0:f759a823d3ae 140 }
mougino 0:f759a823d3ae 141 }
mougino 0:f759a823d3ae 142 }
mougino 0:f759a823d3ae 143
mougino 0:f759a823d3ae 144
mougino 0:f759a823d3ae 145 void clearPlayfield() {
mougino 0:f759a823d3ae 146 for (char y = 0; y < 19; y++) {
mougino 0:f759a823d3ae 147 for (char x = 3; x < 13; x++) {
mougino 0:f759a823d3ae 148 playfield[x + 16 * y] = 0;
mougino 0:f759a823d3ae 149 }
mougino 0:f759a823d3ae 150 }
mougino 0:f759a823d3ae 151 }
mougino 0:f759a823d3ae 152
mougino 0:f759a823d3ae 153 void drawBackground(){
mougino 0:f759a823d3ae 154 for (char y = 0; y < 22; y++) {
mougino 0:f759a823d3ae 155 for (char x = 0; x < 28; x++) {
mougino 0:f759a823d3ae 156 byte mt = bg_map[x + 28 * y + (616*bgNum)];
mougino 0:f759a823d3ae 157 game.display.drawBitmap(x*8, y*8, bg_gfx[mt]);
mougino 0:f759a823d3ae 158 }
mougino 0:f759a823d3ae 159 }
mougino 0:f759a823d3ae 160 }
mougino 0:f759a823d3ae 161
mougino 0:f759a823d3ae 162 void drawPlayfield() {
spinal 4:e861ad5ad42d 163
spinal 4:e861ad5ad42d 164 if(frameNumber==1){
spinal 4:e861ad5ad42d 165 drawBackground();
spinal 4:e861ad5ad42d 166 }
spinal 4:e861ad5ad42d 167
spinal 4:e861ad5ad42d 168
spinal 4:e861ad5ad42d 169 for (char y = 2; y < 19; y++) {
spinal 4:e861ad5ad42d 170 for (char x = 8; x < 18; x++) {
spinal 4:e861ad5ad42d 171 byte mt = bg_map[x + 28 * y + (616*bgNum)];
spinal 4:e861ad5ad42d 172 game.display.drawBitmap(x*8, y*8, bg_gfx[mt]);
spinal 4:e861ad5ad42d 173 }
spinal 4:e861ad5ad42d 174 }
mougino 0:f759a823d3ae 175
mougino 0:f759a823d3ae 176 for (char y = 1; y < 19; y++) {
mougino 0:f759a823d3ae 177 for (char x = 3; x < 13; x++) {
mougino 0:f759a823d3ae 178 byte mt = playfield[x + 16 * y];
mougino 0:f759a823d3ae 179 game.display.drawBitmap((x + 5)*8, (y+1 )*8, tile_gfx[mt]);
mougino 0:f759a823d3ae 180 }
mougino 0:f759a823d3ae 181 }
mougino 0:f759a823d3ae 182
mougino 0:f759a823d3ae 183 if(animSplode==0){
mougino 0:f759a823d3ae 184 // current shape
mougino 0:f759a823d3ae 185 drawShape(px+5, py+1, ps, pr);
mougino 0:f759a823d3ae 186 }
mougino 0:f759a823d3ae 187 // next shape?
spinal 4:e861ad5ad42d 188 // drawShape(19, 14, nextTile, 0);
mougino 0:f759a823d3ae 189
mougino 0:f759a823d3ae 190 char text[] = " ";
mougino 0:f759a823d3ae 191 sprintf(text, "%05d", score);
mougino 0:f759a823d3ae 192 game.display.setCursor(152,56);
mougino 0:f759a823d3ae 193 game.display.color=3;
mougino 0:f759a823d3ae 194 game.display.print(text);
mougino 0:f759a823d3ae 195 sprintf(text, "%5d", level);
mougino 0:f759a823d3ae 196 game.display.setCursor(16,96);
mougino 0:f759a823d3ae 197 game.display.print(text);
mougino 0:f759a823d3ae 198 sprintf(text, "%5d", lines);
mougino 0:f759a823d3ae 199 game.display.setCursor(16,48);
mougino 0:f759a823d3ae 200 game.display.print(text);
mougino 0:f759a823d3ae 201
mougino 0:f759a823d3ae 202 }
mougino 0:f759a823d3ae 203
mougino 0:f759a823d3ae 204 void checkLine() {
mougino 0:f759a823d3ae 205
mougino 0:f759a823d3ae 206 for(char t=0; t<19; t++){
mougino 0:f759a823d3ae 207 removeLine[t]=0;
mougino 0:f759a823d3ae 208 }
mougino 0:f759a823d3ae 209
mougino 0:f759a823d3ae 210 if (py <= 0) {
mougino 0:f759a823d3ae 211 loadPal(1); // default green palette
mougino 0:f759a823d3ae 212 gameMode = 3; // gameOver if off top of screen
mougino 0:f759a823d3ae 213 frameNumber=0;
mougino 0:f759a823d3ae 214 return;
mougino 0:f759a823d3ae 215 }
mougino 0:f759a823d3ae 216
mougino 0:f759a823d3ae 217 score++; // increase score here as it's called whenever a tile drops
mougino 0:f759a823d3ae 218 topLine = 0;
mougino 0:f759a823d3ae 219
mougino 0:f759a823d3ae 220 for (char y = 0; y < 19; y++) {
mougino 0:f759a823d3ae 221 char line = 0;
mougino 0:f759a823d3ae 222 for (char x = 3; x < 13; x++) {
mougino 0:f759a823d3ae 223 line += playfield[x + 16 * y] != 0 ? 1 : 0;
mougino 0:f759a823d3ae 224 }
mougino 0:f759a823d3ae 225 if (line == 10) { // remove line
mougino 0:f759a823d3ae 226 removeLine[y]=1;
mougino 0:f759a823d3ae 227 linesToRemove++;
mougino 0:f759a823d3ae 228 if(linesToRemove==4){splodeOK=1;}
mougino 0:f759a823d3ae 229 lineCount++;
mougino 0:f759a823d3ae 230 if (lineCount == 10) {
mougino 0:f759a823d3ae 231 lineCount = 0;
mougino 0:f759a823d3ae 232 level++;
mougino 0:f759a823d3ae 233 }
mougino 0:f759a823d3ae 234 lines++;
mougino 0:f759a823d3ae 235 score += 10;
mougino 0:f759a823d3ae 236 }
mougino 0:f759a823d3ae 237 }
mougino 0:f759a823d3ae 238
mougino 0:f759a823d3ae 239 for (char y = 0; y < 10; y++) {
mougino 0:f759a823d3ae 240 for (char x = 3; x < 13; x++) {
mougino 0:f759a823d3ae 241 topLine += playfield[x + 16 * y] != 0 ? 1 : 0;
mougino 0:f759a823d3ae 242 }
mougino 0:f759a823d3ae 243 }
mougino 0:f759a823d3ae 244
mougino 0:f759a823d3ae 245
mougino 0:f759a823d3ae 246 // mess with the palette
spinal 4:e861ad5ad42d 247 // remove because it's incompatible with the new draw routines.
spinal 4:e861ad5ad42d 248 /*
mougino 0:f759a823d3ae 249 int percent = 100;
mougino 0:f759a823d3ae 250 int diff = topLine;
mougino 0:f759a823d3ae 251
mougino 0:f759a823d3ae 252 palNum = 0;//level & 31;
spinal 4:e861ad5ad42d 253 unsigned short *p=pallet+(palNum*4);
mougino 0:f759a823d3ae 254 unsigned short curPal[4];
mougino 0:f759a823d3ae 255
mougino 0:f759a823d3ae 256 curPal[0] = pallet[(palNum*4)];
mougino 0:f759a823d3ae 257 curPal[1] = pallet[(palNum*4)+1];
mougino 0:f759a823d3ae 258 curPal[2] = pallet[(palNum*4)+2];
mougino 0:f759a823d3ae 259 curPal[3] = pallet[(palNum*4)+3];
mougino 0:f759a823d3ae 260
mougino 0:f759a823d3ae 261 int greyPal[] = {0xF800,0xF8000,0xF800,0xF800}; // it's actually RED for danger!
mougino 0:f759a823d3ae 262
mougino 0:f759a823d3ae 263 unsigned short red[4], green[4], blue[4], red1[4], green1[4], blue1[4], red2[4], green2[4], blue2[4];
mougino 0:f759a823d3ae 264
mougino 0:f759a823d3ae 265 for(char t=0; t<4; t++){
mougino 0:f759a823d3ae 266 red1[t] = (curPal[t]>>11) & 31;
mougino 0:f759a823d3ae 267 red2[t] = (greyPal[t]>> 11) & 31;
mougino 0:f759a823d3ae 268 green1[t] = (curPal[t]>> 5) & 63;
mougino 0:f759a823d3ae 269 green2[t] = (greyPal[t]>> 5) & 63;
mougino 0:f759a823d3ae 270 blue1[t] = curPal[t] & 31;
mougino 0:f759a823d3ae 271 blue2[t] = greyPal[t] & 31;
mougino 0:f759a823d3ae 272
mougino 0:f759a823d3ae 273 red[t] = red1[t]+((red2[t]-red1[t])*diff/percent);
mougino 0:f759a823d3ae 274 green[t] = green1[t]+((green2[t]-green1[t])*diff/percent);
mougino 0:f759a823d3ae 275 blue[t] = blue1[t]+((blue2[t]-blue1[t])*diff/percent);
mougino 0:f759a823d3ae 276
mougino 0:f759a823d3ae 277 curPal[t] = (red[t]<<11)+(green[t]<<5)+blue[t];
mougino 0:f759a823d3ae 278
mougino 0:f759a823d3ae 279 }
mougino 0:f759a823d3ae 280 game.display.load565Palette(curPal);
spinal 4:e861ad5ad42d 281 */
mougino 0:f759a823d3ae 282 }
mougino 0:f759a823d3ae 283
mougino 0:f759a823d3ae 284
mougino 0:f759a823d3ae 285 // transparent 2bit bitmap with mask
mougino 0:f759a823d3ae 286 void drawMyBitmap(int16_t x, int16_t y, const uint8_t* bitmap, const uint8_t* mask)
mougino 0:f759a823d3ae 287 {
mougino 0:f759a823d3ae 288 int16_t w = *bitmap;
mougino 0:f759a823d3ae 289 int16_t h = *(bitmap + 1);
mougino 0:f759a823d3ae 290 bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
mougino 0:f759a823d3ae 291 /** visibility check */
mougino 0:f759a823d3ae 292 if (y<-h || y>game.display.height) return; //invisible
mougino 0:f759a823d3ae 293 if (x<-w || x>game.display.width) return; //invisible
mougino 0:f759a823d3ae 294
mougino 0:f759a823d3ae 295 /** 2 bpp mode */
mougino 0:f759a823d3ae 296 int16_t i, j, byteNum, bitNum, byteWidth = w >> 2;
mougino 0:f759a823d3ae 297 for (i = 0; i < w; i++) {
mougino 0:f759a823d3ae 298 byteNum = i / 4;
mougino 0:f759a823d3ae 299 bitNum = (i % 4)<<1;
mougino 0:f759a823d3ae 300 for (j = 0; j < h; j++) {
mougino 0:f759a823d3ae 301 uint8_t source = *(bitmap + j * byteWidth + byteNum);
mougino 0:f759a823d3ae 302 uint8_t source2 = *(mask + j * byteWidth + byteNum+2);
mougino 0:f759a823d3ae 303 uint8_t output = (source & (0xC0 >> bitNum));
mougino 0:f759a823d3ae 304 output >>= (6-bitNum);
mougino 0:f759a823d3ae 305
mougino 0:f759a823d3ae 306 uint8_t output2 = (source2 & (0xC0 >> bitNum));
mougino 0:f759a823d3ae 307 output2 >>= (6-bitNum);
mougino 0:f759a823d3ae 308
mougino 0:f759a823d3ae 309 if (output2 != 0) {
mougino 0:f759a823d3ae 310 game.display.setColor(output);
mougino 0:f759a823d3ae 311 game.display.drawPixel(x + i, y + j);
mougino 0:f759a823d3ae 312 }
mougino 0:f759a823d3ae 313 }
mougino 0:f759a823d3ae 314 }
mougino 0:f759a823d3ae 315 }
mougino 0:f759a823d3ae 316
mougino 0:f759a823d3ae 317
mougino 0:f759a823d3ae 318
mougino 0:f759a823d3ae 319 void titleScreen(){
mougino 0:f759a823d3ae 320
mougino 0:f759a823d3ae 321 // background
mougino 0:f759a823d3ae 322 for (char y = 0; y < 22; y++) {
mougino 0:f759a823d3ae 323 for (char x = 0; x < 28; x++) {
mougino 0:f759a823d3ae 324 byte mt = bg_map[x + 28 * y];
mougino 0:f759a823d3ae 325 game.display.drawBitmap(x*8, y*8, bg_gfx[mt]);
mougino 0:f759a823d3ae 326 }
mougino 0:f759a823d3ae 327 }
mougino 0:f759a823d3ae 328
spinal 4:e861ad5ad42d 329 float y=48;
spinal 4:e861ad5ad42d 330 if(frameNumber<=32){
mougino 0:f759a823d3ae 331 // time, start, distance, duration
spinal 4:e861ad5ad42d 332 y = easeOutElastic(frameNumber, -48, 48+48, 32);
mougino 0:f759a823d3ae 333 }
mougino 0:f759a823d3ae 334 drawMyBitmap(16, y, title_bitmap, title_mask);
mougino 0:f759a823d3ae 335
mougino 0:f759a823d3ae 336
mougino 0:f759a823d3ae 337 char text[] = " Press A to Start ";
mougino 0:f759a823d3ae 338 game.display.setCursor(40,120);
mougino 0:f759a823d3ae 339 game.display.color=3;
mougino 0:f759a823d3ae 340 game.display.print(text);
mougino 0:f759a823d3ae 341
mougino 0:f759a823d3ae 342 if(_A[NEW]){
mougino 0:f759a823d3ae 343 // make sure the playfield is clear!
mougino 0:f759a823d3ae 344 for (char y = 18; y > 0; y--) {
mougino 0:f759a823d3ae 345 for (char x = 3; x < 13; x++) {
mougino 0:f759a823d3ae 346 playfield[x + 16 * y] = 0;
mougino 0:f759a823d3ae 347 }
mougino 0:f759a823d3ae 348 }
mougino 0:f759a823d3ae 349
mougino 0:f759a823d3ae 350 loadPal(0); // default green palette
spinal 4:e861ad5ad42d 351 frameNumber=0;
mougino 0:f759a823d3ae 352 gameMode = 1;
mougino 0:f759a823d3ae 353 }
mougino 0:f759a823d3ae 354
mougino 0:f759a823d3ae 355 }
mougino 0:f759a823d3ae 356
mougino 0:f759a823d3ae 357 void gameOver(){
mougino 0:f759a823d3ae 358 // background
mougino 0:f759a823d3ae 359 for (char y = 0; y < 22; y++) {
mougino 0:f759a823d3ae 360 for (char x = 0; x < 28; x++) {
mougino 0:f759a823d3ae 361 byte mt = bg_map[x + 28 * y];
mougino 0:f759a823d3ae 362 game.display.drawBitmap(x*8, y*8, bg_gfx[mt]);
mougino 0:f759a823d3ae 363 }
mougino 0:f759a823d3ae 364 }
mougino 0:f759a823d3ae 365
spinal 4:e861ad5ad42d 366 float y=48;
spinal 4:e861ad5ad42d 367 if(frameNumber<=32){
mougino 0:f759a823d3ae 368 // time, start, distance, duration
spinal 4:e861ad5ad42d 369 y = easeOutElastic(frameNumber, -48, 48+48, 32);
mougino 0:f759a823d3ae 370 }
mougino 0:f759a823d3ae 371 drawMyBitmap(1, y, gameover_bitmap, gameover_mask);
mougino 0:f759a823d3ae 372
mougino 0:f759a823d3ae 373 char text[] = " Press A ";
mougino 0:f759a823d3ae 374 game.display.setCursor(62,120);
mougino 0:f759a823d3ae 375 game.display.color=3;
mougino 0:f759a823d3ae 376 game.display.print(text);
mougino 0:f759a823d3ae 377
mougino 0:f759a823d3ae 378 if(_A[NEW]){
mougino 0:f759a823d3ae 379 gameMode = 0;
mougino 0:f759a823d3ae 380 frameNumber = 0;
mougino 0:f759a823d3ae 381 score=0;
mougino 0:f759a823d3ae 382 lines=0;
mougino 0:f759a823d3ae 383 level=0;
mougino 0:f759a823d3ae 384 splodeOK=0;
mougino 0:f759a823d3ae 385 animSplode=0;
mougino 0:f759a823d3ae 386 }
mougino 0:f759a823d3ae 387
mougino 0:f759a823d3ae 388 }
mougino 0:f759a823d3ae 389
mougino 0:f759a823d3ae 390 void playGame(){
mougino 0:f759a823d3ae 391 #ifdef POK_SIM
mougino 0:f759a823d3ae 392 #define SLIDECOUNT 6
mougino 0:f759a823d3ae 393 #define DROPCOUNT 20
mougino 0:f759a823d3ae 394 #else
spinal 3:8540039d1eed 395 #define SLIDECOUNT 6
spinal 3:8540039d1eed 396 #define DROPCOUNT 20
mougino 0:f759a823d3ae 397 #endif
mougino 0:f759a823d3ae 398
mougino 0:f759a823d3ae 399 if(linesToRemove==0 && animSplode==0){
mougino 0:f759a823d3ae 400
mougino 0:f759a823d3ae 401 if (_Left[NEW]) {
mougino 0:f759a823d3ae 402 if (check(px - 1, py, pr) == 0) {
mougino 0:f759a823d3ae 403 px--;
mougino 0:f759a823d3ae 404 slideTime = 0;
mougino 0:f759a823d3ae 405 }
mougino 0:f759a823d3ae 406 }
mougino 0:f759a823d3ae 407 if (_Right[NEW]) {
mougino 0:f759a823d3ae 408 if (check(px + 1, py, pr) == 0) {
mougino 0:f759a823d3ae 409 px++;
mougino 0:f759a823d3ae 410 slideTime = 0;
mougino 0:f759a823d3ae 411 }
mougino 0:f759a823d3ae 412 }
mougino 0:f759a823d3ae 413 if (_Left[HELD] && slideTime++ > SLIDECOUNT) {
mougino 0:f759a823d3ae 414 if (check(px - 1, py, pr) == 0) {
mougino 0:f759a823d3ae 415 px--;
mougino 0:f759a823d3ae 416 slideTime = 12;
mougino 0:f759a823d3ae 417 }
mougino 0:f759a823d3ae 418 }
mougino 0:f759a823d3ae 419 if (_Right[HELD] && slideTime++ > SLIDECOUNT) {
mougino 0:f759a823d3ae 420 if (check(px + 1, py, pr) == 0) {
mougino 0:f759a823d3ae 421 px++;
mougino 0:f759a823d3ae 422 slideTime = 12;
mougino 0:f759a823d3ae 423 }
mougino 0:f759a823d3ae 424 }
mougino 0:f759a823d3ae 425
mougino 0:f759a823d3ae 426 if ((_Down[HELD]) || (dropTime++ > DROPCOUNT - (level * 2))) {
mougino 0:f759a823d3ae 427 dropTime = 0;
mougino 0:f759a823d3ae 428 if (check(px, py+1, pr) == 0) {
mougino 0:f759a823d3ae 429 py++;
mougino 0:f759a823d3ae 430 } else {
mougino 0:f759a823d3ae 431 // place shape and create new one
mougino 0:f759a823d3ae 432 stamp(px, py, ps, pr);
mougino 0:f759a823d3ae 433 checkLine();
mougino 0:f759a823d3ae 434 py = 0; px = 6; ps = nextTile; nextTile = random(6); pr = 0;
spinal 4:e861ad5ad42d 435 drawShape(19, 14, nextTile, 0,1);
spinal 4:e861ad5ad42d 436 }
mougino 0:f759a823d3ae 437 }
mougino 0:f759a823d3ae 438 if (_Up[NEW] && splodeOK==1) {
mougino 0:f759a823d3ae 439 splodeOK=0;
mougino 0:f759a823d3ae 440 animSplode=1;
mougino 0:f759a823d3ae 441 }
mougino 0:f759a823d3ae 442
spinal 1:827c9852286e 443 if (_B[NEW]) {
mougino 0:f759a823d3ae 444 if (check(px, py, (pr - 1) & 3) == 0) {
mougino 0:f759a823d3ae 445 pr--;
mougino 0:f759a823d3ae 446 } else if (check(px - 1, py, (pr - 1) & 3) == 0) {
mougino 0:f759a823d3ae 447 pr--; px--;
mougino 0:f759a823d3ae 448 } else if (check(px + 1, py, (pr - 1) & 3) == 0) {
mougino 0:f759a823d3ae 449 pr--; px++;
mougino 0:f759a823d3ae 450 } else if (check(px - 2, py, (pr - 1) & 3) == 0) {
mougino 0:f759a823d3ae 451 pr--; px -= 2;
mougino 0:f759a823d3ae 452 } else if (check(px + 2, py, (pr - 1) & 3) == 0) {
mougino 0:f759a823d3ae 453 pr--; px += 2;
mougino 0:f759a823d3ae 454 }
mougino 0:f759a823d3ae 455 pr &= 3;
mougino 0:f759a823d3ae 456 }
mougino 0:f759a823d3ae 457
spinal 1:827c9852286e 458 if (_A[NEW]) {
mougino 0:f759a823d3ae 459 if (check(px, py, (pr + 1) & 3) == 0) {
mougino 0:f759a823d3ae 460 pr++;
mougino 0:f759a823d3ae 461 } else if (check(px - 1, py, (pr + 1) & 3) == 0) {
mougino 0:f759a823d3ae 462 pr++; px--;
mougino 0:f759a823d3ae 463 } else if (check(px + 1, py, (pr + 1) & 3) == 0) {
mougino 0:f759a823d3ae 464 pr++; px++;
mougino 0:f759a823d3ae 465 } else if (check(px - 2, py, (pr + 1) & 3) == 0) {
mougino 0:f759a823d3ae 466 pr++; px -= 2;
mougino 0:f759a823d3ae 467 } else if (check(px + 2, py, (pr + 1) & 3) == 0) {
mougino 0:f759a823d3ae 468 pr++; px += 2;
mougino 0:f759a823d3ae 469 }
mougino 0:f759a823d3ae 470 pr &= 3;
mougino 0:f759a823d3ae 471 }
mougino 0:f759a823d3ae 472
mougino 0:f759a823d3ae 473 animCount=0;
spinal 4:e861ad5ad42d 474 sX=(px + 4)*8;
spinal 4:e861ad5ad42d 475 sY=py*8;
spinal 4:e861ad5ad42d 476 sX1=((px+4)*8)+48;
spinal 4:e861ad5ad42d 477 sY1=(py*8)+48;
spinal 4:e861ad5ad42d 478 if(sY1>176)sY1=176;
mougino 0:f759a823d3ae 479 }
mougino 0:f759a823d3ae 480
mougino 0:f759a823d3ae 481 if(linesToRemove!=0){
spinal 4:e861ad5ad42d 482 sX=0; sY=0; sX1=220; sY1=176;
mougino 0:f759a823d3ae 483 // remove some lines
mougino 0:f759a823d3ae 484 for(byte t=0; t<19; t++){
mougino 0:f759a823d3ae 485 if(removeLine[t]==1){
mougino 0:f759a823d3ae 486 if(animCount<5){
mougino 0:f759a823d3ae 487 for (char x = 3; x < 13; x++) {
mougino 0:f759a823d3ae 488 playfield[x + 16 * t] = 5+animCount*6;
mougino 0:f759a823d3ae 489 } // x
mougino 0:f759a823d3ae 490 }else{
mougino 0:f759a823d3ae 491 removeLine[t]=0;
mougino 0:f759a823d3ae 492 linesToRemove--;
mougino 0:f759a823d3ae 493 for (char y1 = t; y1 > 0; y1--) {
mougino 0:f759a823d3ae 494 for (char x = 3; x < 13; x++) {
mougino 0:f759a823d3ae 495 playfield[x + 16 * y1] = playfield[x + 16 * (y1 - 1)];
mougino 0:f759a823d3ae 496 }
mougino 0:f759a823d3ae 497 }
mougino 0:f759a823d3ae 498 }
mougino 0:f759a823d3ae 499 }
mougino 0:f759a823d3ae 500 }
mougino 0:f759a823d3ae 501 animCount++;
mougino 0:f759a823d3ae 502 }
mougino 0:f759a823d3ae 503
mougino 0:f759a823d3ae 504 if(animSplode!=0){
mougino 0:f759a823d3ae 505
mougino 0:f759a823d3ae 506 if(animSplode<6){
spinal 4:e861ad5ad42d 507 sX=0;
spinal 4:e861ad5ad42d 508 sY=0;
spinal 4:e861ad5ad42d 509 sX1=220;
spinal 4:e861ad5ad42d 510 sY1=176;
mougino 0:f759a823d3ae 511 for (char y = 0; y < 4; y++) {
mougino 0:f759a823d3ae 512 if (py + y >= 0) {
mougino 0:f759a823d3ae 513 for (char x = 0; x < 4; x++) {
mougino 0:f759a823d3ae 514 byte mt = pgm_read_byte(shapeMap + (x + 4 * y) + ps * 64 + (pr * 16));
mougino 0:f759a823d3ae 515 if (mt > 1) {
mougino 0:f759a823d3ae 516 playfield[(px + x) + 16 * (py + y)] = 5+animSplode*6;
mougino 0:f759a823d3ae 517 }
mougino 0:f759a823d3ae 518 }
mougino 0:f759a823d3ae 519 }
mougino 0:f759a823d3ae 520 }
mougino 0:f759a823d3ae 521 animSplode++;
mougino 0:f759a823d3ae 522 }else{
mougino 0:f759a823d3ae 523
mougino 0:f759a823d3ae 524 for (char y = 0; y < 4; y++) {
mougino 0:f759a823d3ae 525 if (py + y >= 0) {
mougino 0:f759a823d3ae 526 for (char x = 0; x < 4; x++) {
mougino 0:f759a823d3ae 527 byte mt = pgm_read_byte(shapeMap + (x + 4 * y) + ps * 64 + (pr * 16));
mougino 0:f759a823d3ae 528 if (mt > 1) {
mougino 0:f759a823d3ae 529 playfield[(px + x) + 16 * (py + y)] = 0;
mougino 0:f759a823d3ae 530 }
mougino 0:f759a823d3ae 531 }
mougino 0:f759a823d3ae 532 }
mougino 0:f759a823d3ae 533 }
mougino 0:f759a823d3ae 534 py = 0; px = 6; ps = nextTile;
mougino 0:f759a823d3ae 535 nextTile = random(6); pr = 0;
mougino 0:f759a823d3ae 536 animSplode=0;
spinal 4:e861ad5ad42d 537 sX=0;
spinal 4:e861ad5ad42d 538 sY=0;
spinal 4:e861ad5ad42d 539 sX1=220;
spinal 4:e861ad5ad42d 540 sY1=176;
mougino 0:f759a823d3ae 541 }
mougino 0:f759a823d3ae 542 }
mougino 0:f759a823d3ae 543
mougino 0:f759a823d3ae 544 // render screen
mougino 0:f759a823d3ae 545 drawPlayfield();
mougino 0:f759a823d3ae 546 }
mougino 0:f759a823d3ae 547
mougino 0:f759a823d3ae 548
mougino 0:f759a823d3ae 549 int main(){
mougino 0:f759a823d3ae 550
mougino 0:f759a823d3ae 551 game.begin();
mougino 0:f759a823d3ae 552 game.display.width = 220; // full size
mougino 0:f759a823d3ae 553 game.display.height = 176;
mougino 0:f759a823d3ae 554 game.display.setFont(fontC64);
mougino 0:f759a823d3ae 555 //game.display.charSpacingAdjust = 0; //needed for the non-proportional C64 font (normal value=1)
mougino 0:f759a823d3ae 556 game.display.fixedWidthFont = true;
mougino 0:f759a823d3ae 557
mougino 0:f759a823d3ae 558 loadPal(1); // default green palette
mougino 0:f759a823d3ae 559
mougino 0:f759a823d3ae 560 px=6;
mougino 0:f759a823d3ae 561 gameMode=0;
mougino 0:f759a823d3ae 562
mougino 0:f759a823d3ae 563 game.sound.playMusicStream(musicName);
mougino 0:f759a823d3ae 564
mougino 0:f759a823d3ae 565 frameNumber=0;
mougino 0:f759a823d3ae 566 animCount=0;
mougino 0:f759a823d3ae 567 splodeOK=0;
mougino 0:f759a823d3ae 568 animSplode=0;
mougino 0:f759a823d3ae 569
spinal 4:e861ad5ad42d 570 sX=0; sY=0;
spinal 4:e861ad5ad42d 571 sX1=220; sY1=176;
spinal 4:e861ad5ad42d 572
mougino 0:f759a823d3ae 573 while (game.isRunning()) {
mougino 0:f759a823d3ae 574
mougino 0:f759a823d3ae 575 // if it is time to update the screen
spinal 3:8540039d1eed 576 // if (game.update(1)){
mougino 0:f759a823d3ae 577 frameNumber++;
mougino 0:f759a823d3ae 578 game.sound.updateStream();
mougino 0:f759a823d3ae 579 myPad = updateButtons(myPad);
mougino 0:f759a823d3ae 580 UpdatePad(myPad);
mougino 0:f759a823d3ae 581
mougino 0:f759a823d3ae 582 switch (gameMode) {
mougino 0:f759a823d3ae 583 case 0:
mougino 0:f759a823d3ae 584 titleScreen();
mougino 0:f759a823d3ae 585 break;
mougino 0:f759a823d3ae 586 case 1:
mougino 0:f759a823d3ae 587 if (paused) {
mougino 0:f759a823d3ae 588 // pauseMenu();
mougino 0:f759a823d3ae 589 } else {
mougino 0:f759a823d3ae 590 playGame();
mougino 0:f759a823d3ae 591 }
mougino 0:f759a823d3ae 592 break;
mougino 0:f759a823d3ae 593 case 3:
mougino 0:f759a823d3ae 594 gameOver();
mougino 0:f759a823d3ae 595 break;
mougino 0:f759a823d3ae 596 }
spinal 4:e861ad5ad42d 597 if(frameNumber==1){
spinal 4:e861ad5ad42d 598 sX=0; sY=0;
spinal 4:e861ad5ad42d 599 sX1=220; sY1=176;
spinal 3:8540039d1eed 600 }
spinal 4:e861ad5ad42d 601
spinal 4:e861ad5ad42d 602 game.display.updateRegion(sX,sY,sX1,sY1);
spinal 4:e861ad5ad42d 603 game.display.updateRegion(152,112,184,144); // next tile
spinal 3:8540039d1eed 604 // }
mougino 0:f759a823d3ae 605 }
mougino 0:f759a823d3ae 606 return 0;
mougino 0:f759a823d3ae 607 }