Haris Šoljić / Mbed 2 deprecated mbed_projekt_tetris

Dependencies:   SPI_TFT_ILI9341 TFT_fonts mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 # include "SPI_TFT_ILI9341.h"
00003 # include "Arial12x12.h"
00004 # define dp23 P0_0
00005 #define GORE 0
00006 #define DOLJE 1
00007 #define LIJEVO 2
00008 #define DESNO 3
00009 #define CENTAR 4
00010 SPI_TFT_ILI9341 TFT ( dp2 , dp1 , dp6 , dp24 , dp23 , dp25 ,"TFT");
00011 // mosi , miso , sclk , cs , reset , dc
00012 AnalogIn vRx(dp11), vRy(dp10);
00013 DigitalIn sw(dp9);
00014 Timer swDbnc;
00015 int ocitajDzojstik()
00016 {
00017     double XX = vRx, YY = vRy;
00018     if(XX < 0.333) return LIJEVO;
00019     else if(XX > 0.666) return DESNO;
00020     else if(YY < 0.333) return GORE;
00021     else if(YY > 0.666) return DOLJE;
00022     else return CENTAR;
00023 }
00024 int cosakX = 80, cosakY = 20, size = 14;
00025 int matrix[10][20] = {};
00026 float dilej = 0.5;
00027 bool kraj = false;
00028 int highScore = 0, score = 0, lines = 0;
00029 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};
00030 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};
00031 int nizBoja[] =   {Blue, Red, Orange, Yellow, Green, Purple, Maroon};
00032 void strcpy(short a[], short b[])
00033 {
00034     for(int i = 0; i < 4; i++)
00035         a[i] = b[i];
00036 }
00037 int sljedeca = 0;
00038 class Figura{
00039     public:
00040     int PX, PY;
00041     short X[4], Y[4];
00042     int Boja;
00043     Figura()
00044     {
00045         PX = 3;
00046         PY = 0;
00047         int rnd = sljedeca;
00048         strcpy(X, Xovi[rnd]);
00049         strcpy(Y, Yoni[rnd]);
00050         Boja = nizBoja[rnd];
00051         sljedeca = rand() % 7;
00052     }
00053     Figura(int n)
00054     {
00055         PX = 3;
00056         PY = 0;
00057         int rnd = n;
00058         strcpy(X, Xovi[rnd]);
00059         strcpy(Y, Yoni[rnd]);
00060         Boja = nizBoja[rnd];
00061     }
00062         
00063     
00064     Figura(const Figura& f)
00065     {
00066         PX = f.PX;
00067         PY = f.PY;
00068         for(int i = 0; i < 4; i++)
00069         { X[i] = f.X[i]; Y[i] = f.Y[i]; }
00070         Boja = f.Boja;
00071     }
00072     
00073 };
00074 Figura f;
00075 bool checkOut(int PX, int PY, short* xx, short* yy)
00076 {
00077     bool ok = true;
00078     for(int i = 0; i < 4; i++)
00079     {
00080         int x = PX + xx[i], y = PY + yy[i];
00081         if( x < 0 || x >=10 || y < 0 || y >= 20 || matrix[x][y] != 0)
00082         {
00083             ok = false;
00084         }
00085     }
00086     return ok;
00087 }
00088 bool checkEnd(int PX, int PY, short* xx, short* yy)
00089 {
00090      bool ok = true;
00091      for (int i = 0; i < 4; i++)
00092      {
00093           int x = PX + xx[i], y = PY + yy[i];
00094           if (y < 0 || y >= 20 || matrix[x][y] != 0)
00095           {
00096              ok = false;
00097           }
00098      }
00099      return ok;
00100 }
00101 bool rotiraj(short xr[], short yr[])
00102 {
00103     int px = f.X[0], py = f.Y[0];
00104     for (int i = 0; i < 4; i++)
00105     {
00106         int x2 = (f.Y[i] + px - py);
00107         int y2 = (px + py - f.X[i]);
00108         xr[i] = x2;
00109         yr[i] = y2;
00110     }
00111     if(checkOut(f.PX, f.PY, xr, yr) && checkEnd(f.PX, f.PY, xr, yr))
00112     {
00113        
00114         return true;
00115     }
00116     else return false;
00117 }
00118 void prikaziScore(int score){
00119     TFT.set_font((unsigned char*) Arial12x12);
00120     TFT.fillrect(0,160,79,210,DarkGrey);
00121     TFT.locate(15,170);
00122     TFT.printf("Score:");
00123     TFT.locate(15,190);
00124     TFT.printf("%d",score);
00125 }
00126 
00127 void prikaziHighScore(int highScore){
00128     TFT.set_font((unsigned char*) Arial12x12);
00129     TFT.fillrect(0,220,79,270,DarkGrey);
00130     TFT.locate(15,230);
00131     TFT.printf("High");
00132     TFT.locate(15, 250);
00133     TFT.printf("score:");
00134     TFT.locate(15,270);
00135     TFT.printf("%d",highScore);
00136 }
00137 
00138 void prikaziBrojLinija(int brojLinija) {
00139     TFT.set_font((unsigned char*) Arial12x12);
00140     TFT.fillrect(0,100,79,150,DarkGrey);
00141     TFT.locate(15,110);
00142     TFT.printf("Lines:");
00143     TFT.locate(15,130);
00144     TFT.printf("%d",brojLinija);
00145 }
00146 
00147 void prikaziSljedecuFiguru(Figura figura)
00148 {
00149     TFT.fillrect(15,20,65,60, White);
00150     for(int i = 0; i < 4; i++)
00151     {
00152         int x = 20 + figura.X[i] * 10;
00153         int y = 30 + figura.Y[i] * 10;
00154         TFT.fillrect(x,y,x+10,y+10,figura.Boja);
00155         TFT.rect(x,y,x+10,y+10,Black);
00156     }
00157 }
00158 void prikaziMeni(int odabrano, int level) {
00159     
00160     TFT.fillrect(0,0,239,319,DarkGrey);
00161     TFT.set_font((unsigned char*) Arial12x12);
00162     TFT.locate(95,130);
00163     TFT.printf("Play");
00164     TFT.locate(70,160);
00165     TFT.printf("Level: %d",level+1);
00166     
00167     if(odabrano==0) {
00168         TFT.locate(70,130);
00169         TFT.printf(">");
00170     }
00171     else {
00172         TFT.locate(45,160);
00173         TFT.printf(">");
00174     }
00175 }
00176 void shrink()
00177 {
00178   int puni = 0;
00179   bool ima = false;
00180   int cnt = 0;
00181  for (int i = 19; i >= 0; i--)
00182  {
00183      int sum = 0;
00184      for (int j = 9; j >= 0; j--)
00185      {
00186           if (matrix[j][i] != 0)
00187                 sum++;
00188      }
00189      if (sum != 10)
00190      {
00191            puni += (1 << i);
00192      }
00193      else { ima = true; cnt++; }
00194  }
00195     score += 100 * cnt * cnt;
00196     lines += cnt;
00197     if(score > highScore) highScore = score;
00198     prikaziScore(score);
00199     prikaziHighScore(highScore);
00200     prikaziBrojLinija(lines);
00201     prikaziSljedecuFiguru(Figura(sljedeca));
00202  
00203  int pocetak = 19;
00204  for(int i = 19; i >= 0; i--)
00205     if((puni & (1 << i)) == 0 ) 
00206         { pocetak = i; break; }
00207  for(int i = 19; i >= 0; i--)
00208  {
00209     int k = -1;
00210     for(int b = 19; b >= 0; b--)
00211     {
00212       if(((1 << b) & puni) > 0)
00213       {
00214          k = b;
00215          puni -= (1 << b);
00216          break;
00217       }
00218     }                
00219     if (k == -1)
00220        for (int j = 0; j < 10; j++)
00221             matrix[j][i] = 0;
00222     else
00223        for (int j = 0; j < 10; j++)
00224             matrix[j][i] = matrix[j][k];
00225  }
00226  if(ima)
00227  {
00228  for(int i = 0; i < 20; i++)
00229  {
00230      for(int j = 9; j >= 0; j--)
00231      {
00232          int xx = cosakX + j*size, yy = cosakY + i*size;
00233          TFT.fillrect(xx, yy, xx + size, yy + size, White);
00234          if(matrix[j][i])
00235          {
00236             TFT.fillrect(xx, yy, xx + size, yy + size, matrix[j][i]);
00237          }
00238      }
00239  }
00240  for(int i = 0; i < 20; i++)
00241  {
00242      for(int j = 9; j >= 0; j--)
00243      {
00244          int xx = cosakX + j*size, yy = cosakY + i*size;
00245          if(matrix[j][i])
00246          {
00247              TFT.rect(xx, yy, xx + size, yy + size, Black);    
00248          }
00249      }
00250  }
00251  }
00252 }
00253 void refresh(int prev, Figura ff)
00254 {
00255     for(int i = 0; i < 10; i++)
00256         for(int j = prev - 1; j >= 0; j--)
00257         {
00258             if(matrix[i][j] ) continue;
00259             bool ima = false;
00260             for(int i = 0; i < 4; i++)
00261             {
00262                 if(matrix[ff.PX + ff.X[i]][ff.PY + ff.Y[i]])
00263                     ima = true;
00264             }
00265             int xx = cosakX + i*size, yy = cosakY + j*size;
00266             TFT.fillrect(xx,yy,xx+size,yy+size,White);
00267         }
00268 }
00269 void obrisiFiguru(Figura ff)
00270 {
00271     for(int i = 0; i < 4; i++)
00272     {
00273         int x = cosakX + (ff.PX + ff.X[i]) * size, y = cosakY + (ff.PY + ff.Y[i]) * size;
00274         TFT.fillrect(x, y, x + size, y + size, White);
00275     }
00276 }
00277 void crtajFiguru(Figura ff)
00278 {
00279     for(int i = 0; i < 4; i++)
00280     {
00281         int x = cosakX + (ff.PX + ff.X[i]) * size, y = cosakY + (ff.PY + ff.Y[i]) * size;
00282         TFT.fillrect(x, y, x + size, y + size, ff.Boja);
00283         TFT.rect(x, y, x + size, y + size, Black);
00284     }
00285 }
00286 void pomjeriDesno()
00287 {
00288     if(checkOut(f.PX + 1, f.PY, f.X, f.Y))
00289     {
00290         obrisiFiguru(f);
00291         f.PX++; 
00292         crtajFiguru(f);
00293     }
00294 }
00295 void pomjeriLijevo()
00296 {
00297     if(checkOut(f.PX - 1, f.PY, f.X, f.Y))
00298     {
00299         obrisiFiguru(f);
00300         f.PX--; 
00301         crtajFiguru(f);
00302     }
00303 }
00304 void pomjeriGore()
00305 {
00306     short xr[4], yr[4];
00307     bool rotiro = rotiraj(xr, yr);
00308     if(rotiro)
00309     {
00310         obrisiFiguru(f);
00311         strcpy(f.X, xr);
00312         strcpy(f.Y, yr);
00313         crtajFiguru(f);
00314     }
00315 }
00316 enum stanje {S1, S2};
00317 stanje trenutno = S2;
00318 Ticker tickerDolje;
00319 bool ende = false;
00320 int opt = 0;
00321 int lvl = 0;
00322 int BROJLEVELA = 3;
00323 int ocitaj;
00324 int prev = CENTAR;
00325 float vejtovi[] = {0.8, 0.5, 0.3};
00326 Timer tasterDebounce;
00327 void pomjeriDolje()
00328 {
00329     if(checkOut(f.PX, f.PY + 1, f.X, f.Y))
00330     {
00331         obrisiFiguru(f);
00332         f.PY++;
00333         crtajFiguru(f);
00334     }
00335     else
00336     {
00337         for(int i = 0; i < 4; i++)
00338             matrix[f.PX + f.X[i]][f.PY + f.Y[i]] = f.Boja;
00339         shrink();
00340         f = Figura();
00341         score += 4;
00342         if(score > highScore) highScore = score;
00343         prikaziScore(score);
00344         prikaziHighScore(highScore);
00345         prikaziBrojLinija(lines);
00346         prikaziSljedecuFiguru(Figura(sljedeca));
00347         if(!checkOut(f.PX, f.PY, f.X, f.Y))
00348         {
00349             // kraj
00350             crtajFiguru(f);
00351             tickerDolje.detach();
00352             TFT.cls();
00353             TFT.locate(80, 150);
00354             TFT.printf("GAME OVER");
00355             wait(3);
00356             trenutno = S2;
00357                tickerDolje.detach();
00358              TFT.cls();
00359             opt = 0;
00360             lvl = 0;
00361              prikaziMeni(opt, lvl);
00362              prev = CENTAR;
00363           f = Figura();
00364         }
00365     }
00366 }
00367 
00368 void resetujMatricu()
00369 {
00370     for(int i = 0; i < 10; i++)
00371         for(int j = 0; j < 20; j++)
00372             matrix[i][j] = 0;
00373 }
00374 
00375 
00376 int ocitajSW()
00377 {
00378     if(tasterDebounce.read_ms() > 200)
00379     {
00380         tasterDebounce.reset();
00381         return sw;
00382     }
00383     else return 1;
00384 }
00385 
00386 void S1_entry()
00387 {
00388     score = 0;
00389     lines = 0;
00390     sljedeca = rand() % 7;
00391     TFT.cls();
00392     resetujMatricu();
00393     TFT.fillrect(cosakX, cosakY, cosakX + 140, cosakY + 280, White);
00394     
00395     dilej = vejtovi[lvl];
00396     prikaziScore(score);
00397     prikaziHighScore(highScore);
00398     prikaziBrojLinija(lines);
00399     prikaziSljedecuFiguru(Figura(sljedeca));
00400     tickerDolje.attach(&pomjeriDolje, dilej);  
00401 }
00402 
00403 void S1_do()
00404 {
00405         if(swDbnc.read_ms() > 40*(3-lvl)) 
00406             swDbnc.reset();
00407         else return;
00408            ocitaj = ocitajDzojstik();
00409         if(ocitaj == prev) return;
00410         if(ocitaj == LIJEVO)
00411             pomjeriLijevo();
00412         else if(ocitaj == DESNO)
00413             pomjeriDesno();
00414         else if(ocitaj == GORE)
00415             pomjeriGore();
00416         else if(ocitaj == DOLJE)
00417             pomjeriDolje();
00418         prev = ocitaj;
00419 }
00420 
00421 void S2_entry()
00422 {
00423    tickerDolje.detach();
00424    TFT.cls();
00425    opt = 0;
00426    lvl = 0;
00427    prikaziMeni(opt, lvl);
00428    prev = CENTAR;
00429    f = Figura();
00430 }
00431 
00432 void S2_do()
00433 {
00434        if(swDbnc.read_ms() > 40) 
00435             swDbnc.reset();
00436         else return;
00437        ocitaj = ocitajDzojstik();
00438        if(ocitaj == prev) return;
00439        if(ocitaj == DOLJE) opt = (opt + 1) % 2;
00440        else if(ocitaj == GORE) opt = (opt + 1) % 2;
00441        else if(ocitaj == DESNO && opt == 1)
00442             lvl = (lvl + 1) % BROJLEVELA;
00443        else if(ocitaj == LIJEVO && opt == 1)
00444             lvl = (lvl - 1 + BROJLEVELA) % BROJLEVELA;
00445        prikaziMeni(opt, lvl);
00446        prev = ocitaj;
00447 }
00448 
00449 void prelaz()
00450 {
00451     int o = ocitajSW();
00452     switch(trenutno)
00453     {
00454         case S1:
00455             switch(o)
00456             {
00457                 case 0:
00458                     trenutno = S2;
00459                     S2_entry();
00460                 break;
00461                 case 1:
00462                     S1_do();
00463                 break;
00464             }
00465         break;
00466         case S2:
00467             switch(o)
00468             {
00469                 case 0:
00470                     trenutno = S1;
00471                     S1_entry();
00472                 break;
00473                 case 1:
00474                     S2_do();
00475                 break;
00476             }
00477         break;
00478     }
00479 }
00480 
00481 void init()
00482 {
00483     sw.mode(PullUp);
00484     TFT.set_orientation(0);
00485     TFT.background(DarkGrey);
00486     TFT.foreground(White);
00487     srand(highScore);
00488     S2_entry();
00489 }
00490 
00491 int main() 
00492 {
00493     init();
00494     tasterDebounce.start();
00495     swDbnc.start();
00496     while(1) { prelaz(); }
00497 }