Game Tetris on LPC 1114ETF with Banggood display and Joystick

Dependencies:   SPI_TFT_ILI9341 TFT_fonts mbed

Committer:
2018US_HarisSoljic
Date:
Mon Jun 18 20:04:05 2018 +0000
Revision:
0:71c49df3f7cd
Added a project ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
2018US_HarisSoljic 0:71c49df3f7cd 1 #include "mbed.h"
2018US_HarisSoljic 0:71c49df3f7cd 2 # include "SPI_TFT_ILI9341.h"
2018US_HarisSoljic 0:71c49df3f7cd 3 # include "Arial12x12.h"
2018US_HarisSoljic 0:71c49df3f7cd 4 # define dp23 P0_0
2018US_HarisSoljic 0:71c49df3f7cd 5 #define GORE 0
2018US_HarisSoljic 0:71c49df3f7cd 6 #define DOLJE 1
2018US_HarisSoljic 0:71c49df3f7cd 7 #define LIJEVO 2
2018US_HarisSoljic 0:71c49df3f7cd 8 #define DESNO 3
2018US_HarisSoljic 0:71c49df3f7cd 9 #define CENTAR 4
2018US_HarisSoljic 0:71c49df3f7cd 10 SPI_TFT_ILI9341 TFT ( dp2 , dp1 , dp6 , dp24 , dp23 , dp25 ,"TFT");
2018US_HarisSoljic 0:71c49df3f7cd 11 // mosi , miso , sclk , cs , reset , dc
2018US_HarisSoljic 0:71c49df3f7cd 12 AnalogIn vRx(dp11), vRy(dp10);
2018US_HarisSoljic 0:71c49df3f7cd 13 DigitalIn sw(dp9);
2018US_HarisSoljic 0:71c49df3f7cd 14 Timer swDbnc;
2018US_HarisSoljic 0:71c49df3f7cd 15 int ocitajDzojstik()
2018US_HarisSoljic 0:71c49df3f7cd 16 {
2018US_HarisSoljic 0:71c49df3f7cd 17 double XX = vRx, YY = vRy;
2018US_HarisSoljic 0:71c49df3f7cd 18 if(XX < 0.333) return LIJEVO;
2018US_HarisSoljic 0:71c49df3f7cd 19 else if(XX > 0.666) return DESNO;
2018US_HarisSoljic 0:71c49df3f7cd 20 else if(YY < 0.333) return GORE;
2018US_HarisSoljic 0:71c49df3f7cd 21 else if(YY > 0.666) return DOLJE;
2018US_HarisSoljic 0:71c49df3f7cd 22 else return CENTAR;
2018US_HarisSoljic 0:71c49df3f7cd 23 }
2018US_HarisSoljic 0:71c49df3f7cd 24 int cosakX = 80, cosakY = 20, size = 14;
2018US_HarisSoljic 0:71c49df3f7cd 25 int matrix[10][20] = {};
2018US_HarisSoljic 0:71c49df3f7cd 26 float dilej = 0.5;
2018US_HarisSoljic 0:71c49df3f7cd 27 bool kraj = false;
2018US_HarisSoljic 0:71c49df3f7cd 28 int highScore = 0, score = 0, lines = 0;
2018US_HarisSoljic 0:71c49df3f7cd 29 short Xovi[7][4] = {2,1,0,3, 1,0,2,2, 1,0,2,0, 0,1,0,1, 1,0,2,1, 1,1,2,0, 1,0,1,2};
2018US_HarisSoljic 0:71c49df3f7cd 30 short Yoni[7][4] = {0,0,0,0, 0,0,0,1, 0,0,0,1, 0,0,1,1, 0,0,0,1, 1,0,0,1, 1,0,0,1};
2018US_HarisSoljic 0:71c49df3f7cd 31 int nizBoja[] = {Blue, Red, Orange, Yellow, Green, Purple, Maroon};
2018US_HarisSoljic 0:71c49df3f7cd 32 void strcpy(short a[], short b[])
2018US_HarisSoljic 0:71c49df3f7cd 33 {
2018US_HarisSoljic 0:71c49df3f7cd 34 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 35 a[i] = b[i];
2018US_HarisSoljic 0:71c49df3f7cd 36 }
2018US_HarisSoljic 0:71c49df3f7cd 37 int sljedeca = 0;
2018US_HarisSoljic 0:71c49df3f7cd 38 class Figura{
2018US_HarisSoljic 0:71c49df3f7cd 39 public:
2018US_HarisSoljic 0:71c49df3f7cd 40 int PX, PY;
2018US_HarisSoljic 0:71c49df3f7cd 41 short X[4], Y[4];
2018US_HarisSoljic 0:71c49df3f7cd 42 int Boja;
2018US_HarisSoljic 0:71c49df3f7cd 43 Figura()
2018US_HarisSoljic 0:71c49df3f7cd 44 {
2018US_HarisSoljic 0:71c49df3f7cd 45 PX = 3;
2018US_HarisSoljic 0:71c49df3f7cd 46 PY = 0;
2018US_HarisSoljic 0:71c49df3f7cd 47 int rnd = sljedeca;
2018US_HarisSoljic 0:71c49df3f7cd 48 strcpy(X, Xovi[rnd]);
2018US_HarisSoljic 0:71c49df3f7cd 49 strcpy(Y, Yoni[rnd]);
2018US_HarisSoljic 0:71c49df3f7cd 50 Boja = nizBoja[rnd];
2018US_HarisSoljic 0:71c49df3f7cd 51 sljedeca = rand() % 7;
2018US_HarisSoljic 0:71c49df3f7cd 52 }
2018US_HarisSoljic 0:71c49df3f7cd 53 Figura(int n)
2018US_HarisSoljic 0:71c49df3f7cd 54 {
2018US_HarisSoljic 0:71c49df3f7cd 55 PX = 3;
2018US_HarisSoljic 0:71c49df3f7cd 56 PY = 0;
2018US_HarisSoljic 0:71c49df3f7cd 57 int rnd = n;
2018US_HarisSoljic 0:71c49df3f7cd 58 strcpy(X, Xovi[rnd]);
2018US_HarisSoljic 0:71c49df3f7cd 59 strcpy(Y, Yoni[rnd]);
2018US_HarisSoljic 0:71c49df3f7cd 60 Boja = nizBoja[rnd];
2018US_HarisSoljic 0:71c49df3f7cd 61 }
2018US_HarisSoljic 0:71c49df3f7cd 62
2018US_HarisSoljic 0:71c49df3f7cd 63
2018US_HarisSoljic 0:71c49df3f7cd 64 Figura(const Figura& f)
2018US_HarisSoljic 0:71c49df3f7cd 65 {
2018US_HarisSoljic 0:71c49df3f7cd 66 PX = f.PX;
2018US_HarisSoljic 0:71c49df3f7cd 67 PY = f.PY;
2018US_HarisSoljic 0:71c49df3f7cd 68 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 69 { X[i] = f.X[i]; Y[i] = f.Y[i]; }
2018US_HarisSoljic 0:71c49df3f7cd 70 Boja = f.Boja;
2018US_HarisSoljic 0:71c49df3f7cd 71 }
2018US_HarisSoljic 0:71c49df3f7cd 72
2018US_HarisSoljic 0:71c49df3f7cd 73 };
2018US_HarisSoljic 0:71c49df3f7cd 74 Figura f;
2018US_HarisSoljic 0:71c49df3f7cd 75 bool checkOut(int PX, int PY, short* xx, short* yy)
2018US_HarisSoljic 0:71c49df3f7cd 76 {
2018US_HarisSoljic 0:71c49df3f7cd 77 bool ok = true;
2018US_HarisSoljic 0:71c49df3f7cd 78 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 79 {
2018US_HarisSoljic 0:71c49df3f7cd 80 int x = PX + xx[i], y = PY + yy[i];
2018US_HarisSoljic 0:71c49df3f7cd 81 if( x < 0 || x >=10 || y < 0 || y >= 20 || matrix[x][y] != 0)
2018US_HarisSoljic 0:71c49df3f7cd 82 {
2018US_HarisSoljic 0:71c49df3f7cd 83 ok = false;
2018US_HarisSoljic 0:71c49df3f7cd 84 }
2018US_HarisSoljic 0:71c49df3f7cd 85 }
2018US_HarisSoljic 0:71c49df3f7cd 86 return ok;
2018US_HarisSoljic 0:71c49df3f7cd 87 }
2018US_HarisSoljic 0:71c49df3f7cd 88 bool checkEnd(int PX, int PY, short* xx, short* yy)
2018US_HarisSoljic 0:71c49df3f7cd 89 {
2018US_HarisSoljic 0:71c49df3f7cd 90 bool ok = true;
2018US_HarisSoljic 0:71c49df3f7cd 91 for (int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 92 {
2018US_HarisSoljic 0:71c49df3f7cd 93 int x = PX + xx[i], y = PY + yy[i];
2018US_HarisSoljic 0:71c49df3f7cd 94 if (y < 0 || y >= 20 || matrix[x][y] != 0)
2018US_HarisSoljic 0:71c49df3f7cd 95 {
2018US_HarisSoljic 0:71c49df3f7cd 96 ok = false;
2018US_HarisSoljic 0:71c49df3f7cd 97 }
2018US_HarisSoljic 0:71c49df3f7cd 98 }
2018US_HarisSoljic 0:71c49df3f7cd 99 return ok;
2018US_HarisSoljic 0:71c49df3f7cd 100 }
2018US_HarisSoljic 0:71c49df3f7cd 101 bool rotiraj(short xr[], short yr[])
2018US_HarisSoljic 0:71c49df3f7cd 102 {
2018US_HarisSoljic 0:71c49df3f7cd 103 int px = f.X[0], py = f.Y[0];
2018US_HarisSoljic 0:71c49df3f7cd 104 for (int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 105 {
2018US_HarisSoljic 0:71c49df3f7cd 106 int x2 = (f.Y[i] + px - py);
2018US_HarisSoljic 0:71c49df3f7cd 107 int y2 = (px + py - f.X[i]);
2018US_HarisSoljic 0:71c49df3f7cd 108 xr[i] = x2;
2018US_HarisSoljic 0:71c49df3f7cd 109 yr[i] = y2;
2018US_HarisSoljic 0:71c49df3f7cd 110 }
2018US_HarisSoljic 0:71c49df3f7cd 111 if(checkOut(f.PX, f.PY, xr, yr) && checkEnd(f.PX, f.PY, xr, yr))
2018US_HarisSoljic 0:71c49df3f7cd 112 {
2018US_HarisSoljic 0:71c49df3f7cd 113
2018US_HarisSoljic 0:71c49df3f7cd 114 return true;
2018US_HarisSoljic 0:71c49df3f7cd 115 }
2018US_HarisSoljic 0:71c49df3f7cd 116 else return false;
2018US_HarisSoljic 0:71c49df3f7cd 117 }
2018US_HarisSoljic 0:71c49df3f7cd 118 void prikaziScore(int score){
2018US_HarisSoljic 0:71c49df3f7cd 119 TFT.set_font((unsigned char*) Arial12x12);
2018US_HarisSoljic 0:71c49df3f7cd 120 TFT.fillrect(0,160,79,210,DarkGrey);
2018US_HarisSoljic 0:71c49df3f7cd 121 TFT.locate(15,170);
2018US_HarisSoljic 0:71c49df3f7cd 122 TFT.printf("Score:");
2018US_HarisSoljic 0:71c49df3f7cd 123 TFT.locate(15,190);
2018US_HarisSoljic 0:71c49df3f7cd 124 TFT.printf("%d",score);
2018US_HarisSoljic 0:71c49df3f7cd 125 }
2018US_HarisSoljic 0:71c49df3f7cd 126
2018US_HarisSoljic 0:71c49df3f7cd 127 void prikaziHighScore(int highScore){
2018US_HarisSoljic 0:71c49df3f7cd 128 TFT.set_font((unsigned char*) Arial12x12);
2018US_HarisSoljic 0:71c49df3f7cd 129 TFT.fillrect(0,220,79,270,DarkGrey);
2018US_HarisSoljic 0:71c49df3f7cd 130 TFT.locate(15,230);
2018US_HarisSoljic 0:71c49df3f7cd 131 TFT.printf("High");
2018US_HarisSoljic 0:71c49df3f7cd 132 TFT.locate(15, 250);
2018US_HarisSoljic 0:71c49df3f7cd 133 TFT.printf("score:");
2018US_HarisSoljic 0:71c49df3f7cd 134 TFT.locate(15,270);
2018US_HarisSoljic 0:71c49df3f7cd 135 TFT.printf("%d",highScore);
2018US_HarisSoljic 0:71c49df3f7cd 136 }
2018US_HarisSoljic 0:71c49df3f7cd 137
2018US_HarisSoljic 0:71c49df3f7cd 138 void prikaziBrojLinija(int brojLinija) {
2018US_HarisSoljic 0:71c49df3f7cd 139 TFT.set_font((unsigned char*) Arial12x12);
2018US_HarisSoljic 0:71c49df3f7cd 140 TFT.fillrect(0,100,79,150,DarkGrey);
2018US_HarisSoljic 0:71c49df3f7cd 141 TFT.locate(15,110);
2018US_HarisSoljic 0:71c49df3f7cd 142 TFT.printf("Lines:");
2018US_HarisSoljic 0:71c49df3f7cd 143 TFT.locate(15,130);
2018US_HarisSoljic 0:71c49df3f7cd 144 TFT.printf("%d",brojLinija);
2018US_HarisSoljic 0:71c49df3f7cd 145 }
2018US_HarisSoljic 0:71c49df3f7cd 146
2018US_HarisSoljic 0:71c49df3f7cd 147 void prikaziSljedecuFiguru(Figura figura)
2018US_HarisSoljic 0:71c49df3f7cd 148 {
2018US_HarisSoljic 0:71c49df3f7cd 149 TFT.fillrect(15,20,65,60, White);
2018US_HarisSoljic 0:71c49df3f7cd 150 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 151 {
2018US_HarisSoljic 0:71c49df3f7cd 152 int x = 20 + figura.X[i] * 10;
2018US_HarisSoljic 0:71c49df3f7cd 153 int y = 30 + figura.Y[i] * 10;
2018US_HarisSoljic 0:71c49df3f7cd 154 TFT.fillrect(x,y,x+10,y+10,figura.Boja);
2018US_HarisSoljic 0:71c49df3f7cd 155 TFT.rect(x,y,x+10,y+10,Black);
2018US_HarisSoljic 0:71c49df3f7cd 156 }
2018US_HarisSoljic 0:71c49df3f7cd 157 }
2018US_HarisSoljic 0:71c49df3f7cd 158 void prikaziMeni(int odabrano, int level) {
2018US_HarisSoljic 0:71c49df3f7cd 159
2018US_HarisSoljic 0:71c49df3f7cd 160 TFT.fillrect(0,0,239,319,DarkGrey);
2018US_HarisSoljic 0:71c49df3f7cd 161 TFT.set_font((unsigned char*) Arial12x12);
2018US_HarisSoljic 0:71c49df3f7cd 162 TFT.locate(95,130);
2018US_HarisSoljic 0:71c49df3f7cd 163 TFT.printf("Play");
2018US_HarisSoljic 0:71c49df3f7cd 164 TFT.locate(70,160);
2018US_HarisSoljic 0:71c49df3f7cd 165 TFT.printf("Level: %d",level+1);
2018US_HarisSoljic 0:71c49df3f7cd 166
2018US_HarisSoljic 0:71c49df3f7cd 167 if(odabrano==0) {
2018US_HarisSoljic 0:71c49df3f7cd 168 TFT.locate(70,130);
2018US_HarisSoljic 0:71c49df3f7cd 169 TFT.printf(">");
2018US_HarisSoljic 0:71c49df3f7cd 170 }
2018US_HarisSoljic 0:71c49df3f7cd 171 else {
2018US_HarisSoljic 0:71c49df3f7cd 172 TFT.locate(45,160);
2018US_HarisSoljic 0:71c49df3f7cd 173 TFT.printf(">");
2018US_HarisSoljic 0:71c49df3f7cd 174 }
2018US_HarisSoljic 0:71c49df3f7cd 175 }
2018US_HarisSoljic 0:71c49df3f7cd 176 void shrink()
2018US_HarisSoljic 0:71c49df3f7cd 177 {
2018US_HarisSoljic 0:71c49df3f7cd 178 int puni = 0;
2018US_HarisSoljic 0:71c49df3f7cd 179 bool ima = false;
2018US_HarisSoljic 0:71c49df3f7cd 180 int cnt = 0;
2018US_HarisSoljic 0:71c49df3f7cd 181 for (int i = 19; i >= 0; i--)
2018US_HarisSoljic 0:71c49df3f7cd 182 {
2018US_HarisSoljic 0:71c49df3f7cd 183 int sum = 0;
2018US_HarisSoljic 0:71c49df3f7cd 184 for (int j = 9; j >= 0; j--)
2018US_HarisSoljic 0:71c49df3f7cd 185 {
2018US_HarisSoljic 0:71c49df3f7cd 186 if (matrix[j][i] != 0)
2018US_HarisSoljic 0:71c49df3f7cd 187 sum++;
2018US_HarisSoljic 0:71c49df3f7cd 188 }
2018US_HarisSoljic 0:71c49df3f7cd 189 if (sum != 10)
2018US_HarisSoljic 0:71c49df3f7cd 190 {
2018US_HarisSoljic 0:71c49df3f7cd 191 puni += (1 << i);
2018US_HarisSoljic 0:71c49df3f7cd 192 }
2018US_HarisSoljic 0:71c49df3f7cd 193 else { ima = true; cnt++; }
2018US_HarisSoljic 0:71c49df3f7cd 194 }
2018US_HarisSoljic 0:71c49df3f7cd 195 score += 100 * cnt * cnt;
2018US_HarisSoljic 0:71c49df3f7cd 196 lines += cnt;
2018US_HarisSoljic 0:71c49df3f7cd 197 if(score > highScore) highScore = score;
2018US_HarisSoljic 0:71c49df3f7cd 198 prikaziScore(score);
2018US_HarisSoljic 0:71c49df3f7cd 199 prikaziHighScore(highScore);
2018US_HarisSoljic 0:71c49df3f7cd 200 prikaziBrojLinija(lines);
2018US_HarisSoljic 0:71c49df3f7cd 201 prikaziSljedecuFiguru(Figura(sljedeca));
2018US_HarisSoljic 0:71c49df3f7cd 202
2018US_HarisSoljic 0:71c49df3f7cd 203 int pocetak = 19;
2018US_HarisSoljic 0:71c49df3f7cd 204 for(int i = 19; i >= 0; i--)
2018US_HarisSoljic 0:71c49df3f7cd 205 if((puni & (1 << i)) == 0 )
2018US_HarisSoljic 0:71c49df3f7cd 206 { pocetak = i; break; }
2018US_HarisSoljic 0:71c49df3f7cd 207 for(int i = 19; i >= 0; i--)
2018US_HarisSoljic 0:71c49df3f7cd 208 {
2018US_HarisSoljic 0:71c49df3f7cd 209 int k = -1;
2018US_HarisSoljic 0:71c49df3f7cd 210 for(int b = 19; b >= 0; b--)
2018US_HarisSoljic 0:71c49df3f7cd 211 {
2018US_HarisSoljic 0:71c49df3f7cd 212 if(((1 << b) & puni) > 0)
2018US_HarisSoljic 0:71c49df3f7cd 213 {
2018US_HarisSoljic 0:71c49df3f7cd 214 k = b;
2018US_HarisSoljic 0:71c49df3f7cd 215 puni -= (1 << b);
2018US_HarisSoljic 0:71c49df3f7cd 216 break;
2018US_HarisSoljic 0:71c49df3f7cd 217 }
2018US_HarisSoljic 0:71c49df3f7cd 218 }
2018US_HarisSoljic 0:71c49df3f7cd 219 if (k == -1)
2018US_HarisSoljic 0:71c49df3f7cd 220 for (int j = 0; j < 10; j++)
2018US_HarisSoljic 0:71c49df3f7cd 221 matrix[j][i] = 0;
2018US_HarisSoljic 0:71c49df3f7cd 222 else
2018US_HarisSoljic 0:71c49df3f7cd 223 for (int j = 0; j < 10; j++)
2018US_HarisSoljic 0:71c49df3f7cd 224 matrix[j][i] = matrix[j][k];
2018US_HarisSoljic 0:71c49df3f7cd 225 }
2018US_HarisSoljic 0:71c49df3f7cd 226 if(ima)
2018US_HarisSoljic 0:71c49df3f7cd 227 {
2018US_HarisSoljic 0:71c49df3f7cd 228 for(int i = 0; i < 20; i++)
2018US_HarisSoljic 0:71c49df3f7cd 229 {
2018US_HarisSoljic 0:71c49df3f7cd 230 for(int j = 9; j >= 0; j--)
2018US_HarisSoljic 0:71c49df3f7cd 231 {
2018US_HarisSoljic 0:71c49df3f7cd 232 int xx = cosakX + j*size, yy = cosakY + i*size;
2018US_HarisSoljic 0:71c49df3f7cd 233 TFT.fillrect(xx, yy, xx + size, yy + size, White);
2018US_HarisSoljic 0:71c49df3f7cd 234 if(matrix[j][i])
2018US_HarisSoljic 0:71c49df3f7cd 235 {
2018US_HarisSoljic 0:71c49df3f7cd 236 TFT.fillrect(xx, yy, xx + size, yy + size, matrix[j][i]);
2018US_HarisSoljic 0:71c49df3f7cd 237 }
2018US_HarisSoljic 0:71c49df3f7cd 238 }
2018US_HarisSoljic 0:71c49df3f7cd 239 }
2018US_HarisSoljic 0:71c49df3f7cd 240 for(int i = 0; i < 20; i++)
2018US_HarisSoljic 0:71c49df3f7cd 241 {
2018US_HarisSoljic 0:71c49df3f7cd 242 for(int j = 9; j >= 0; j--)
2018US_HarisSoljic 0:71c49df3f7cd 243 {
2018US_HarisSoljic 0:71c49df3f7cd 244 int xx = cosakX + j*size, yy = cosakY + i*size;
2018US_HarisSoljic 0:71c49df3f7cd 245 if(matrix[j][i])
2018US_HarisSoljic 0:71c49df3f7cd 246 {
2018US_HarisSoljic 0:71c49df3f7cd 247 TFT.rect(xx, yy, xx + size, yy + size, Black);
2018US_HarisSoljic 0:71c49df3f7cd 248 }
2018US_HarisSoljic 0:71c49df3f7cd 249 }
2018US_HarisSoljic 0:71c49df3f7cd 250 }
2018US_HarisSoljic 0:71c49df3f7cd 251 }
2018US_HarisSoljic 0:71c49df3f7cd 252 }
2018US_HarisSoljic 0:71c49df3f7cd 253 void refresh(int prev, Figura ff)
2018US_HarisSoljic 0:71c49df3f7cd 254 {
2018US_HarisSoljic 0:71c49df3f7cd 255 for(int i = 0; i < 10; i++)
2018US_HarisSoljic 0:71c49df3f7cd 256 for(int j = prev - 1; j >= 0; j--)
2018US_HarisSoljic 0:71c49df3f7cd 257 {
2018US_HarisSoljic 0:71c49df3f7cd 258 if(matrix[i][j] ) continue;
2018US_HarisSoljic 0:71c49df3f7cd 259 bool ima = false;
2018US_HarisSoljic 0:71c49df3f7cd 260 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 261 {
2018US_HarisSoljic 0:71c49df3f7cd 262 if(matrix[ff.PX + ff.X[i]][ff.PY + ff.Y[i]])
2018US_HarisSoljic 0:71c49df3f7cd 263 ima = true;
2018US_HarisSoljic 0:71c49df3f7cd 264 }
2018US_HarisSoljic 0:71c49df3f7cd 265 int xx = cosakX + i*size, yy = cosakY + j*size;
2018US_HarisSoljic 0:71c49df3f7cd 266 TFT.fillrect(xx,yy,xx+size,yy+size,White);
2018US_HarisSoljic 0:71c49df3f7cd 267 }
2018US_HarisSoljic 0:71c49df3f7cd 268 }
2018US_HarisSoljic 0:71c49df3f7cd 269 void obrisiFiguru(Figura ff)
2018US_HarisSoljic 0:71c49df3f7cd 270 {
2018US_HarisSoljic 0:71c49df3f7cd 271 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 272 {
2018US_HarisSoljic 0:71c49df3f7cd 273 int x = cosakX + (ff.PX + ff.X[i]) * size, y = cosakY + (ff.PY + ff.Y[i]) * size;
2018US_HarisSoljic 0:71c49df3f7cd 274 TFT.fillrect(x, y, x + size, y + size, White);
2018US_HarisSoljic 0:71c49df3f7cd 275 }
2018US_HarisSoljic 0:71c49df3f7cd 276 }
2018US_HarisSoljic 0:71c49df3f7cd 277 void crtajFiguru(Figura ff)
2018US_HarisSoljic 0:71c49df3f7cd 278 {
2018US_HarisSoljic 0:71c49df3f7cd 279 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 280 {
2018US_HarisSoljic 0:71c49df3f7cd 281 int x = cosakX + (ff.PX + ff.X[i]) * size, y = cosakY + (ff.PY + ff.Y[i]) * size;
2018US_HarisSoljic 0:71c49df3f7cd 282 TFT.fillrect(x, y, x + size, y + size, ff.Boja);
2018US_HarisSoljic 0:71c49df3f7cd 283 TFT.rect(x, y, x + size, y + size, Black);
2018US_HarisSoljic 0:71c49df3f7cd 284 }
2018US_HarisSoljic 0:71c49df3f7cd 285 }
2018US_HarisSoljic 0:71c49df3f7cd 286 void pomjeriDesno()
2018US_HarisSoljic 0:71c49df3f7cd 287 {
2018US_HarisSoljic 0:71c49df3f7cd 288 if(checkOut(f.PX + 1, f.PY, f.X, f.Y))
2018US_HarisSoljic 0:71c49df3f7cd 289 {
2018US_HarisSoljic 0:71c49df3f7cd 290 obrisiFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 291 f.PX++;
2018US_HarisSoljic 0:71c49df3f7cd 292 crtajFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 293 }
2018US_HarisSoljic 0:71c49df3f7cd 294 }
2018US_HarisSoljic 0:71c49df3f7cd 295 void pomjeriLijevo()
2018US_HarisSoljic 0:71c49df3f7cd 296 {
2018US_HarisSoljic 0:71c49df3f7cd 297 if(checkOut(f.PX - 1, f.PY, f.X, f.Y))
2018US_HarisSoljic 0:71c49df3f7cd 298 {
2018US_HarisSoljic 0:71c49df3f7cd 299 obrisiFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 300 f.PX--;
2018US_HarisSoljic 0:71c49df3f7cd 301 crtajFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 302 }
2018US_HarisSoljic 0:71c49df3f7cd 303 }
2018US_HarisSoljic 0:71c49df3f7cd 304 void pomjeriGore()
2018US_HarisSoljic 0:71c49df3f7cd 305 {
2018US_HarisSoljic 0:71c49df3f7cd 306 short xr[4], yr[4];
2018US_HarisSoljic 0:71c49df3f7cd 307 bool rotiro = rotiraj(xr, yr);
2018US_HarisSoljic 0:71c49df3f7cd 308 if(rotiro)
2018US_HarisSoljic 0:71c49df3f7cd 309 {
2018US_HarisSoljic 0:71c49df3f7cd 310 obrisiFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 311 strcpy(f.X, xr);
2018US_HarisSoljic 0:71c49df3f7cd 312 strcpy(f.Y, yr);
2018US_HarisSoljic 0:71c49df3f7cd 313 crtajFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 314 }
2018US_HarisSoljic 0:71c49df3f7cd 315 }
2018US_HarisSoljic 0:71c49df3f7cd 316 enum stanje {S1, S2};
2018US_HarisSoljic 0:71c49df3f7cd 317 stanje trenutno = S2;
2018US_HarisSoljic 0:71c49df3f7cd 318 Ticker tickerDolje;
2018US_HarisSoljic 0:71c49df3f7cd 319 bool ende = false;
2018US_HarisSoljic 0:71c49df3f7cd 320 int opt = 0;
2018US_HarisSoljic 0:71c49df3f7cd 321 int lvl = 0;
2018US_HarisSoljic 0:71c49df3f7cd 322 int BROJLEVELA = 3;
2018US_HarisSoljic 0:71c49df3f7cd 323 int ocitaj;
2018US_HarisSoljic 0:71c49df3f7cd 324 int prev = CENTAR;
2018US_HarisSoljic 0:71c49df3f7cd 325 float vejtovi[] = {0.8, 0.5, 0.3};
2018US_HarisSoljic 0:71c49df3f7cd 326 Timer tasterDebounce;
2018US_HarisSoljic 0:71c49df3f7cd 327 void pomjeriDolje()
2018US_HarisSoljic 0:71c49df3f7cd 328 {
2018US_HarisSoljic 0:71c49df3f7cd 329 if(checkOut(f.PX, f.PY + 1, f.X, f.Y))
2018US_HarisSoljic 0:71c49df3f7cd 330 {
2018US_HarisSoljic 0:71c49df3f7cd 331 obrisiFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 332 f.PY++;
2018US_HarisSoljic 0:71c49df3f7cd 333 crtajFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 334 }
2018US_HarisSoljic 0:71c49df3f7cd 335 else
2018US_HarisSoljic 0:71c49df3f7cd 336 {
2018US_HarisSoljic 0:71c49df3f7cd 337 for(int i = 0; i < 4; i++)
2018US_HarisSoljic 0:71c49df3f7cd 338 matrix[f.PX + f.X[i]][f.PY + f.Y[i]] = f.Boja;
2018US_HarisSoljic 0:71c49df3f7cd 339 shrink();
2018US_HarisSoljic 0:71c49df3f7cd 340 f = Figura();
2018US_HarisSoljic 0:71c49df3f7cd 341 score += 4;
2018US_HarisSoljic 0:71c49df3f7cd 342 if(score > highScore) highScore = score;
2018US_HarisSoljic 0:71c49df3f7cd 343 prikaziScore(score);
2018US_HarisSoljic 0:71c49df3f7cd 344 prikaziHighScore(highScore);
2018US_HarisSoljic 0:71c49df3f7cd 345 prikaziBrojLinija(lines);
2018US_HarisSoljic 0:71c49df3f7cd 346 prikaziSljedecuFiguru(Figura(sljedeca));
2018US_HarisSoljic 0:71c49df3f7cd 347 if(!checkOut(f.PX, f.PY, f.X, f.Y))
2018US_HarisSoljic 0:71c49df3f7cd 348 {
2018US_HarisSoljic 0:71c49df3f7cd 349 // kraj
2018US_HarisSoljic 0:71c49df3f7cd 350 crtajFiguru(f);
2018US_HarisSoljic 0:71c49df3f7cd 351 tickerDolje.detach();
2018US_HarisSoljic 0:71c49df3f7cd 352 TFT.cls();
2018US_HarisSoljic 0:71c49df3f7cd 353 TFT.locate(80, 150);
2018US_HarisSoljic 0:71c49df3f7cd 354 TFT.printf("GAME OVER");
2018US_HarisSoljic 0:71c49df3f7cd 355 wait(3);
2018US_HarisSoljic 0:71c49df3f7cd 356 trenutno = S2;
2018US_HarisSoljic 0:71c49df3f7cd 357 tickerDolje.detach();
2018US_HarisSoljic 0:71c49df3f7cd 358 TFT.cls();
2018US_HarisSoljic 0:71c49df3f7cd 359 opt = 0;
2018US_HarisSoljic 0:71c49df3f7cd 360 lvl = 0;
2018US_HarisSoljic 0:71c49df3f7cd 361 prikaziMeni(opt, lvl);
2018US_HarisSoljic 0:71c49df3f7cd 362 prev = CENTAR;
2018US_HarisSoljic 0:71c49df3f7cd 363 f = Figura();
2018US_HarisSoljic 0:71c49df3f7cd 364 }
2018US_HarisSoljic 0:71c49df3f7cd 365 }
2018US_HarisSoljic 0:71c49df3f7cd 366 }
2018US_HarisSoljic 0:71c49df3f7cd 367
2018US_HarisSoljic 0:71c49df3f7cd 368 void resetujMatricu()
2018US_HarisSoljic 0:71c49df3f7cd 369 {
2018US_HarisSoljic 0:71c49df3f7cd 370 for(int i = 0; i < 10; i++)
2018US_HarisSoljic 0:71c49df3f7cd 371 for(int j = 0; j < 20; j++)
2018US_HarisSoljic 0:71c49df3f7cd 372 matrix[i][j] = 0;
2018US_HarisSoljic 0:71c49df3f7cd 373 }
2018US_HarisSoljic 0:71c49df3f7cd 374
2018US_HarisSoljic 0:71c49df3f7cd 375
2018US_HarisSoljic 0:71c49df3f7cd 376 int ocitajSW()
2018US_HarisSoljic 0:71c49df3f7cd 377 {
2018US_HarisSoljic 0:71c49df3f7cd 378 if(tasterDebounce.read_ms() > 200)
2018US_HarisSoljic 0:71c49df3f7cd 379 {
2018US_HarisSoljic 0:71c49df3f7cd 380 tasterDebounce.reset();
2018US_HarisSoljic 0:71c49df3f7cd 381 return sw;
2018US_HarisSoljic 0:71c49df3f7cd 382 }
2018US_HarisSoljic 0:71c49df3f7cd 383 else return 1;
2018US_HarisSoljic 0:71c49df3f7cd 384 }
2018US_HarisSoljic 0:71c49df3f7cd 385
2018US_HarisSoljic 0:71c49df3f7cd 386 void S1_entry()
2018US_HarisSoljic 0:71c49df3f7cd 387 {
2018US_HarisSoljic 0:71c49df3f7cd 388 score = 0;
2018US_HarisSoljic 0:71c49df3f7cd 389 lines = 0;
2018US_HarisSoljic 0:71c49df3f7cd 390 sljedeca = rand() % 7;
2018US_HarisSoljic 0:71c49df3f7cd 391 TFT.cls();
2018US_HarisSoljic 0:71c49df3f7cd 392 resetujMatricu();
2018US_HarisSoljic 0:71c49df3f7cd 393 TFT.fillrect(cosakX, cosakY, cosakX + 140, cosakY + 280, White);
2018US_HarisSoljic 0:71c49df3f7cd 394
2018US_HarisSoljic 0:71c49df3f7cd 395 dilej = vejtovi[lvl];
2018US_HarisSoljic 0:71c49df3f7cd 396 prikaziScore(score);
2018US_HarisSoljic 0:71c49df3f7cd 397 prikaziHighScore(highScore);
2018US_HarisSoljic 0:71c49df3f7cd 398 prikaziBrojLinija(lines);
2018US_HarisSoljic 0:71c49df3f7cd 399 prikaziSljedecuFiguru(Figura(sljedeca));
2018US_HarisSoljic 0:71c49df3f7cd 400 tickerDolje.attach(&pomjeriDolje, dilej);
2018US_HarisSoljic 0:71c49df3f7cd 401 }
2018US_HarisSoljic 0:71c49df3f7cd 402
2018US_HarisSoljic 0:71c49df3f7cd 403 void S1_do()
2018US_HarisSoljic 0:71c49df3f7cd 404 {
2018US_HarisSoljic 0:71c49df3f7cd 405 if(swDbnc.read_ms() > 40*(3-lvl))
2018US_HarisSoljic 0:71c49df3f7cd 406 swDbnc.reset();
2018US_HarisSoljic 0:71c49df3f7cd 407 else return;
2018US_HarisSoljic 0:71c49df3f7cd 408 ocitaj = ocitajDzojstik();
2018US_HarisSoljic 0:71c49df3f7cd 409 if(ocitaj == prev) return;
2018US_HarisSoljic 0:71c49df3f7cd 410 if(ocitaj == LIJEVO)
2018US_HarisSoljic 0:71c49df3f7cd 411 pomjeriLijevo();
2018US_HarisSoljic 0:71c49df3f7cd 412 else if(ocitaj == DESNO)
2018US_HarisSoljic 0:71c49df3f7cd 413 pomjeriDesno();
2018US_HarisSoljic 0:71c49df3f7cd 414 else if(ocitaj == GORE)
2018US_HarisSoljic 0:71c49df3f7cd 415 pomjeriGore();
2018US_HarisSoljic 0:71c49df3f7cd 416 else if(ocitaj == DOLJE)
2018US_HarisSoljic 0:71c49df3f7cd 417 pomjeriDolje();
2018US_HarisSoljic 0:71c49df3f7cd 418 prev = ocitaj;
2018US_HarisSoljic 0:71c49df3f7cd 419 }
2018US_HarisSoljic 0:71c49df3f7cd 420
2018US_HarisSoljic 0:71c49df3f7cd 421 void S2_entry()
2018US_HarisSoljic 0:71c49df3f7cd 422 {
2018US_HarisSoljic 0:71c49df3f7cd 423 tickerDolje.detach();
2018US_HarisSoljic 0:71c49df3f7cd 424 TFT.cls();
2018US_HarisSoljic 0:71c49df3f7cd 425 opt = 0;
2018US_HarisSoljic 0:71c49df3f7cd 426 lvl = 0;
2018US_HarisSoljic 0:71c49df3f7cd 427 prikaziMeni(opt, lvl);
2018US_HarisSoljic 0:71c49df3f7cd 428 prev = CENTAR;
2018US_HarisSoljic 0:71c49df3f7cd 429 f = Figura();
2018US_HarisSoljic 0:71c49df3f7cd 430 }
2018US_HarisSoljic 0:71c49df3f7cd 431
2018US_HarisSoljic 0:71c49df3f7cd 432 void S2_do()
2018US_HarisSoljic 0:71c49df3f7cd 433 {
2018US_HarisSoljic 0:71c49df3f7cd 434 if(swDbnc.read_ms() > 40)
2018US_HarisSoljic 0:71c49df3f7cd 435 swDbnc.reset();
2018US_HarisSoljic 0:71c49df3f7cd 436 else return;
2018US_HarisSoljic 0:71c49df3f7cd 437 ocitaj = ocitajDzojstik();
2018US_HarisSoljic 0:71c49df3f7cd 438 if(ocitaj == prev) return;
2018US_HarisSoljic 0:71c49df3f7cd 439 if(ocitaj == DOLJE) opt = (opt + 1) % 2;
2018US_HarisSoljic 0:71c49df3f7cd 440 else if(ocitaj == GORE) opt = (opt + 1) % 2;
2018US_HarisSoljic 0:71c49df3f7cd 441 else if(ocitaj == DESNO && opt == 1)
2018US_HarisSoljic 0:71c49df3f7cd 442 lvl = (lvl + 1) % BROJLEVELA;
2018US_HarisSoljic 0:71c49df3f7cd 443 else if(ocitaj == LIJEVO && opt == 1)
2018US_HarisSoljic 0:71c49df3f7cd 444 lvl = (lvl - 1 + BROJLEVELA) % BROJLEVELA;
2018US_HarisSoljic 0:71c49df3f7cd 445 prikaziMeni(opt, lvl);
2018US_HarisSoljic 0:71c49df3f7cd 446 prev = ocitaj;
2018US_HarisSoljic 0:71c49df3f7cd 447 }
2018US_HarisSoljic 0:71c49df3f7cd 448
2018US_HarisSoljic 0:71c49df3f7cd 449 void prelaz()
2018US_HarisSoljic 0:71c49df3f7cd 450 {
2018US_HarisSoljic 0:71c49df3f7cd 451 int o = ocitajSW();
2018US_HarisSoljic 0:71c49df3f7cd 452 switch(trenutno)
2018US_HarisSoljic 0:71c49df3f7cd 453 {
2018US_HarisSoljic 0:71c49df3f7cd 454 case S1:
2018US_HarisSoljic 0:71c49df3f7cd 455 switch(o)
2018US_HarisSoljic 0:71c49df3f7cd 456 {
2018US_HarisSoljic 0:71c49df3f7cd 457 case 0:
2018US_HarisSoljic 0:71c49df3f7cd 458 trenutno = S2;
2018US_HarisSoljic 0:71c49df3f7cd 459 S2_entry();
2018US_HarisSoljic 0:71c49df3f7cd 460 break;
2018US_HarisSoljic 0:71c49df3f7cd 461 case 1:
2018US_HarisSoljic 0:71c49df3f7cd 462 S1_do();
2018US_HarisSoljic 0:71c49df3f7cd 463 break;
2018US_HarisSoljic 0:71c49df3f7cd 464 }
2018US_HarisSoljic 0:71c49df3f7cd 465 break;
2018US_HarisSoljic 0:71c49df3f7cd 466 case S2:
2018US_HarisSoljic 0:71c49df3f7cd 467 switch(o)
2018US_HarisSoljic 0:71c49df3f7cd 468 {
2018US_HarisSoljic 0:71c49df3f7cd 469 case 0:
2018US_HarisSoljic 0:71c49df3f7cd 470 trenutno = S1;
2018US_HarisSoljic 0:71c49df3f7cd 471 S1_entry();
2018US_HarisSoljic 0:71c49df3f7cd 472 break;
2018US_HarisSoljic 0:71c49df3f7cd 473 case 1:
2018US_HarisSoljic 0:71c49df3f7cd 474 S2_do();
2018US_HarisSoljic 0:71c49df3f7cd 475 break;
2018US_HarisSoljic 0:71c49df3f7cd 476 }
2018US_HarisSoljic 0:71c49df3f7cd 477 break;
2018US_HarisSoljic 0:71c49df3f7cd 478 }
2018US_HarisSoljic 0:71c49df3f7cd 479 }
2018US_HarisSoljic 0:71c49df3f7cd 480
2018US_HarisSoljic 0:71c49df3f7cd 481 void init()
2018US_HarisSoljic 0:71c49df3f7cd 482 {
2018US_HarisSoljic 0:71c49df3f7cd 483 sw.mode(PullUp);
2018US_HarisSoljic 0:71c49df3f7cd 484 TFT.set_orientation(0);
2018US_HarisSoljic 0:71c49df3f7cd 485 TFT.background(DarkGrey);
2018US_HarisSoljic 0:71c49df3f7cd 486 TFT.foreground(White);
2018US_HarisSoljic 0:71c49df3f7cd 487 srand(highScore);
2018US_HarisSoljic 0:71c49df3f7cd 488 S2_entry();
2018US_HarisSoljic 0:71c49df3f7cd 489 }
2018US_HarisSoljic 0:71c49df3f7cd 490
2018US_HarisSoljic 0:71c49df3f7cd 491 int main()
2018US_HarisSoljic 0:71c49df3f7cd 492 {
2018US_HarisSoljic 0:71c49df3f7cd 493 init();
2018US_HarisSoljic 0:71c49df3f7cd 494 tasterDebounce.start();
2018US_HarisSoljic 0:71c49df3f7cd 495 swDbnc.start();
2018US_HarisSoljic 0:71c49df3f7cd 496 while(1) { prelaz(); }
2018US_HarisSoljic 0:71c49df3f7cd 497 }