2018 US Projekt - Stack Attack (Grupa 9) Haris Salman Amir Muminović Glavni program.

Dependencies:   SPI_TFT_ILI9341 Sprites TFT_fonts mbed

Committer:
2018US_harissalman
Date:
Mon Jul 02 20:23:26 2018 +0000
Revision:
0:88e77eadd8e1
2018 US Projekt - Stack Attack; Haris Salman; Amir Muminovi?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
2018US_harissalman 0:88e77eadd8e1 1 #include "mbed.h"
2018US_harissalman 0:88e77eadd8e1 2 #include "stdio.h"
2018US_harissalman 0:88e77eadd8e1 3 #include "string"
2018US_harissalman 0:88e77eadd8e1 4 #include "vector"
2018US_harissalman 0:88e77eadd8e1 5
2018US_harissalman 0:88e77eadd8e1 6 #include "SPI_TFT_ILI9341.h"
2018US_harissalman 0:88e77eadd8e1 7 #include "GraphicsDisplay.h"
2018US_harissalman 0:88e77eadd8e1 8
2018US_harissalman 0:88e77eadd8e1 9 #include "Arial12x12.h"
2018US_harissalman 0:88e77eadd8e1 10 #include "Arial24x23.h"
2018US_harissalman 0:88e77eadd8e1 11 #include "Arial28x28.h"
2018US_harissalman 0:88e77eadd8e1 12 #include "font_big.h"
2018US_harissalman 0:88e77eadd8e1 13
2018US_harissalman 0:88e77eadd8e1 14 #include "Sprites.h"
2018US_harissalman 0:88e77eadd8e1 15
2018US_harissalman 0:88e77eadd8e1 16 // Konstante: Velicina kutije, padding sa lijeva
2018US_harissalman 0:88e77eadd8e1 17 #define BoxSize 30
2018US_harissalman 0:88e77eadd8e1 18 #define LPAD 10
2018US_harissalman 0:88e77eadd8e1 19
2018US_harissalman 0:88e77eadd8e1 20 using std::vector;
2018US_harissalman 0:88e77eadd8e1 21
2018US_harissalman 0:88e77eadd8e1 22 // MOSI, MISO, SCK, CS, RESET, DC, NAME
2018US_harissalman 0:88e77eadd8e1 23 SPI_TFT_ILI9341 display(PTD2, PTD3, PTD1, PTE2, PTA20, PTD5, "display");
2018US_harissalman 0:88e77eadd8e1 24 AnalogIn Jx(PTB1);
2018US_harissalman 0:88e77eadd8e1 25 AnalogIn Jy(PTB2);
2018US_harissalman 0:88e77eadd8e1 26 DigitalIn BTN(PTE5);
2018US_harissalman 0:88e77eadd8e1 27 Timer deb;
2018US_harissalman 0:88e77eadd8e1 28
2018US_harissalman 0:88e77eadd8e1 29 // Master ticker za igru
2018US_harissalman 0:88e77eadd8e1 30 Ticker gridTicker;
2018US_harissalman 0:88e77eadd8e1 31
2018US_harissalman 0:88e77eadd8e1 32 vector<vector<bool> > mapa;
2018US_harissalman 0:88e77eadd8e1 33
2018US_harissalman 0:88e77eadd8e1 34 double OffsetX, OffsetY;
2018US_harissalman 0:88e77eadd8e1 35 bool paused(false);
2018US_harissalman 0:88e77eadd8e1 36 bool gameover(false);
2018US_harissalman 0:88e77eadd8e1 37 bool toggleDraw(true);
2018US_harissalman 0:88e77eadd8e1 38
2018US_harissalman 0:88e77eadd8e1 39 int generateRandom();
2018US_harissalman 0:88e77eadd8e1 40 void gameOver();
2018US_harissalman 0:88e77eadd8e1 41
2018US_harissalman 0:88e77eadd8e1 42 class Claw {
2018US_harissalman 0:88e77eadd8e1 43 int position, target;
2018US_harissalman 0:88e77eadd8e1 44 bool hasBox;
2018US_harissalman 0:88e77eadd8e1 45 double moveTime; // Vrijeme neophodno da se ruka pomjeri sa jedne lokacije na drugu, u milisekundama
2018US_harissalman 0:88e77eadd8e1 46 public:
2018US_harissalman 0:88e77eadd8e1 47 Claw() : position(0), target(generateRandom()), hasBox(true), moveTime(1000) {}
2018US_harissalman 0:88e77eadd8e1 48
2018US_harissalman 0:88e77eadd8e1 49 void GiveBox() {
2018US_harissalman 0:88e77eadd8e1 50 if (position == 0 && !hasBox){
2018US_harissalman 0:88e77eadd8e1 51 hasBox = true;
2018US_harissalman 0:88e77eadd8e1 52 target = generateRandom();
2018US_harissalman 0:88e77eadd8e1 53 }
2018US_harissalman 0:88e77eadd8e1 54 }
2018US_harissalman 0:88e77eadd8e1 55
2018US_harissalman 0:88e77eadd8e1 56 void MoveClaw() {
2018US_harissalman 0:88e77eadd8e1 57 GiveBox();
2018US_harissalman 0:88e77eadd8e1 58 if (target > position) position++;
2018US_harissalman 0:88e77eadd8e1 59 else if (target < position) position--;
2018US_harissalman 0:88e77eadd8e1 60 else if (target == position) BaciBox();
2018US_harissalman 0:88e77eadd8e1 61 }
2018US_harissalman 0:88e77eadd8e1 62
2018US_harissalman 0:88e77eadd8e1 63 void BaciBox() {
2018US_harissalman 0:88e77eadd8e1 64 hasBox = false;
2018US_harissalman 0:88e77eadd8e1 65 if (mapa[5][position]){
2018US_harissalman 0:88e77eadd8e1 66 bool kraj(true);
2018US_harissalman 0:88e77eadd8e1 67 for(int i(0); i<5; i++) if(mapa[i][position]==0) kraj=false;
2018US_harissalman 0:88e77eadd8e1 68 if(kraj) gameOver();
2018US_harissalman 0:88e77eadd8e1 69 }
2018US_harissalman 0:88e77eadd8e1 70 mapa[5][position] = true; // Dodaj kutiju u
2018US_harissalman 0:88e77eadd8e1 71 target = 0; // Pokupi novu na pocetku
2018US_harissalman 0:88e77eadd8e1 72 }
2018US_harissalman 0:88e77eadd8e1 73
2018US_harissalman 0:88e77eadd8e1 74 void DrawClaw() {
2018US_harissalman 0:88e77eadd8e1 75 // x0 y0 sirina visina *bitmap
2018US_harissalman 0:88e77eadd8e1 76 if(!hasBox) display.Bitmap(position*BoxSize + LPAD, 0, BoxSize, BoxSize, claw);
2018US_harissalman 0:88e77eadd8e1 77 else display.Bitmap(position*BoxSize + LPAD, 0, BoxSize, BoxSize, clawBoxColor);
2018US_harissalman 0:88e77eadd8e1 78 }
2018US_harissalman 0:88e77eadd8e1 79
2018US_harissalman 0:88e77eadd8e1 80 void drawBlank(){
2018US_harissalman 0:88e77eadd8e1 81 display.fillrect(position*BoxSize + LPAD, 0, position*BoxSize + BoxSize + LPAD, BoxSize, White);
2018US_harissalman 0:88e77eadd8e1 82 }
2018US_harissalman 0:88e77eadd8e1 83
2018US_harissalman 0:88e77eadd8e1 84 void TickerFja(){
2018US_harissalman 0:88e77eadd8e1 85 drawBlank();
2018US_harissalman 0:88e77eadd8e1 86 MoveClaw();
2018US_harissalman 0:88e77eadd8e1 87 DrawClaw();
2018US_harissalman 0:88e77eadd8e1 88 }
2018US_harissalman 0:88e77eadd8e1 89 };
2018US_harissalman 0:88e77eadd8e1 90
2018US_harissalman 0:88e77eadd8e1 91 class Player{
2018US_harissalman 0:88e77eadd8e1 92 short int x, y;
2018US_harissalman 0:88e77eadd8e1 93 public:
2018US_harissalman 0:88e77eadd8e1 94 Player() : x(5),y(0){}
2018US_harissalman 0:88e77eadd8e1 95
2018US_harissalman 0:88e77eadd8e1 96 void PomjeriDesno(){
2018US_harissalman 0:88e77eadd8e1 97 if(x < 9){ //Ako moze jos ici desno
2018US_harissalman 0:88e77eadd8e1 98 if (mapa[y][x+1] == 0 && mapa[y+1][x+1] == 0){ //Ako desno nije kutija
2018US_harissalman 0:88e77eadd8e1 99 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 100 x++;
2018US_harissalman 0:88e77eadd8e1 101 }
2018US_harissalman 0:88e77eadd8e1 102 else{ // Ako jeste kutija
2018US_harissalman 0:88e77eadd8e1 103 if (x+1 < 9){ // Ako nije kutija na kraju
2018US_harissalman 0:88e77eadd8e1 104 if (mapa[y][x+2] == 0 && mapa[y+1][x+1] == 0){ // Ako ima mjesta za pomjeranje kutije
2018US_harissalman 0:88e77eadd8e1 105 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 106 x++; //Update lokacije
2018US_harissalman 0:88e77eadd8e1 107 mapa[y][x] = 0; //Brisi kutiju
2018US_harissalman 0:88e77eadd8e1 108 mapa[y][x+1] = 1; //Pomjeri kutiju na novu lokaciju
2018US_harissalman 0:88e77eadd8e1 109 }
2018US_harissalman 0:88e77eadd8e1 110 }
2018US_harissalman 0:88e77eadd8e1 111 }
2018US_harissalman 0:88e77eadd8e1 112 }
2018US_harissalman 0:88e77eadd8e1 113 }
2018US_harissalman 0:88e77eadd8e1 114
2018US_harissalman 0:88e77eadd8e1 115 void PomjeriLijevo(){
2018US_harissalman 0:88e77eadd8e1 116 if(x > 0){ //Ako moze jos ici lijevo
2018US_harissalman 0:88e77eadd8e1 117 if (mapa[y][x-1] == 0 && mapa[y+1][x-1] == 0){ // Ako lijevo nije kutija
2018US_harissalman 0:88e77eadd8e1 118 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 119 x--;
2018US_harissalman 0:88e77eadd8e1 120 } else { // Ako jeste kutija
2018US_harissalman 0:88e77eadd8e1 121 if (x-1 > 0){ //Ako nije kutija na kraju
2018US_harissalman 0:88e77eadd8e1 122 if (mapa[y][x-2] == 0 && mapa[y+1][x-1] == 0){ // Ako ima mjesta za pomjeranje kutije
2018US_harissalman 0:88e77eadd8e1 123 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 124 x--; //Update lokacije
2018US_harissalman 0:88e77eadd8e1 125 mapa[y][x] = 0; //Brisi kutiju
2018US_harissalman 0:88e77eadd8e1 126 mapa[y][x-1] = 1; //Pomjeri kutiju na novu lokaciju
2018US_harissalman 0:88e77eadd8e1 127 }
2018US_harissalman 0:88e77eadd8e1 128 }
2018US_harissalman 0:88e77eadd8e1 129 }
2018US_harissalman 0:88e77eadd8e1 130 }
2018US_harissalman 0:88e77eadd8e1 131 }
2018US_harissalman 0:88e77eadd8e1 132
2018US_harissalman 0:88e77eadd8e1 133 void PomjeriGore(){
2018US_harissalman 0:88e77eadd8e1 134 // Provjeri da ima od sta ispod sebe da odskoci, kao i ako je dosao do vrha
2018US_harissalman 0:88e77eadd8e1 135 if(y == 0 || (y < 6 && mapa[y-1][x] == 1)){
2018US_harissalman 0:88e77eadd8e1 136 drawBlank(y,x);
2018US_harissalman 0:88e77eadd8e1 137 y++;
2018US_harissalman 0:88e77eadd8e1 138 }
2018US_harissalman 0:88e77eadd8e1 139 }
2018US_harissalman 0:88e77eadd8e1 140
2018US_harissalman 0:88e77eadd8e1 141 void PomjeriGoreDesno(){
2018US_harissalman 0:88e77eadd8e1 142 if(y == 0 || (y < 6 && mapa[y-1][x] == 1)){
2018US_harissalman 0:88e77eadd8e1 143 if (x < 9 && y < 6){ //Ima prostora za skok
2018US_harissalman 0:88e77eadd8e1 144 if (mapa[y+1][x+1] == 0){ // Pomjeri igraca ako nema kutije
2018US_harissalman 0:88e77eadd8e1 145 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 146 x++;
2018US_harissalman 0:88e77eadd8e1 147 y++;
2018US_harissalman 0:88e77eadd8e1 148 }
2018US_harissalman 0:88e77eadd8e1 149 else { // Ako ima kutije, pomakni kutiju
2018US_harissalman 0:88e77eadd8e1 150 if (x < 8){ // Ako nije boundary
2018US_harissalman 0:88e77eadd8e1 151 if (mapa[y+1][x+2] == 0) {// Ako se moze pomaci desno
2018US_harissalman 0:88e77eadd8e1 152 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 153 x++;
2018US_harissalman 0:88e77eadd8e1 154 y++;
2018US_harissalman 0:88e77eadd8e1 155 mapa[y][x] = 0; // Nema tu kutije
2018US_harissalman 0:88e77eadd8e1 156 mapa[y][x+1] = 1; // Sad je pomaknuta desno
2018US_harissalman 0:88e77eadd8e1 157 }
2018US_harissalman 0:88e77eadd8e1 158 }
2018US_harissalman 0:88e77eadd8e1 159 }
2018US_harissalman 0:88e77eadd8e1 160 }
2018US_harissalman 0:88e77eadd8e1 161 }
2018US_harissalman 0:88e77eadd8e1 162 }
2018US_harissalman 0:88e77eadd8e1 163
2018US_harissalman 0:88e77eadd8e1 164 void PomjeriGoreLijevo(){
2018US_harissalman 0:88e77eadd8e1 165 if(y == 0 || (y < 6 && mapa[y-1][x] == 1)){
2018US_harissalman 0:88e77eadd8e1 166 if (x > 0 && y < 6){ //Ima prostora za skok
2018US_harissalman 0:88e77eadd8e1 167 if (mapa[y+1][x-1] == 0){ // Pomjeri igraca ako nema kutije
2018US_harissalman 0:88e77eadd8e1 168 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 169 x--;
2018US_harissalman 0:88e77eadd8e1 170 y++;
2018US_harissalman 0:88e77eadd8e1 171 }
2018US_harissalman 0:88e77eadd8e1 172 else { // Ako ima kutije, pomakni kutiju
2018US_harissalman 0:88e77eadd8e1 173 if (x - 2 > 0){ // Ako nije boundary
2018US_harissalman 0:88e77eadd8e1 174 if (mapa[y+1][x-2] == 0) {// Ako se moze pomaci desno
2018US_harissalman 0:88e77eadd8e1 175 drawBlank(y,x); drawBlank(y+1,x);
2018US_harissalman 0:88e77eadd8e1 176 x--;
2018US_harissalman 0:88e77eadd8e1 177 y++;
2018US_harissalman 0:88e77eadd8e1 178 mapa[y][x] = 0; // Nema tu kutije
2018US_harissalman 0:88e77eadd8e1 179 mapa[y][x-1] = 1; // Sad je pomaknuta desno
2018US_harissalman 0:88e77eadd8e1 180 }
2018US_harissalman 0:88e77eadd8e1 181 }
2018US_harissalman 0:88e77eadd8e1 182 }
2018US_harissalman 0:88e77eadd8e1 183 }
2018US_harissalman 0:88e77eadd8e1 184 }
2018US_harissalman 0:88e77eadd8e1 185 }
2018US_harissalman 0:88e77eadd8e1 186
2018US_harissalman 0:88e77eadd8e1 187 void DaLiJeZiv(){
2018US_harissalman 0:88e77eadd8e1 188 if (mapa[y+1][x]) gameOver();
2018US_harissalman 0:88e77eadd8e1 189 }
2018US_harissalman 0:88e77eadd8e1 190
2018US_harissalman 0:88e77eadd8e1 191 int GetX(){ return x; }
2018US_harissalman 0:88e77eadd8e1 192 int GetY(){ return y; }
2018US_harissalman 0:88e77eadd8e1 193 void SetX(int Ox){ x = Ox; }
2018US_harissalman 0:88e77eadd8e1 194 void SetY(int Oy){ y = Oy; }
2018US_harissalman 0:88e77eadd8e1 195
2018US_harissalman 0:88e77eadd8e1 196 void drawPlayer(){
2018US_harissalman 0:88e77eadd8e1 197 // x0 y0 širina visina *bitmap
2018US_harissalman 0:88e77eadd8e1 198 display.Bitmap(x*BoxSize + LPAD + 1, (5-y)*BoxSize, BoxSize, 2*BoxSize + 1, charColor);
2018US_harissalman 0:88e77eadd8e1 199 }
2018US_harissalman 0:88e77eadd8e1 200
2018US_harissalman 0:88e77eadd8e1 201 void drawBlank(int row, int column){
2018US_harissalman 0:88e77eadd8e1 202 display.fillrect(row*BoxSize + LPAD + 1, column*BoxSize, row*BoxSize + BoxSize + LPAD, column*BoxSize + BoxSize - 1, White);
2018US_harissalman 0:88e77eadd8e1 203 }
2018US_harissalman 0:88e77eadd8e1 204 };
2018US_harissalman 0:88e77eadd8e1 205
2018US_harissalman 0:88e77eadd8e1 206 // Vraca random broj od 0 do 99 jer FRDM nema sat za random seed
2018US_harissalman 0:88e77eadd8e1 207 int generateRandom(){
2018US_harissalman 0:88e77eadd8e1 208 int random(int((Jx+Jy+1) * (rand()%100))%10);
2018US_harissalman 0:88e77eadd8e1 209 if(random < 0) random *= -1;
2018US_harissalman 0:88e77eadd8e1 210 return random;
2018US_harissalman 0:88e77eadd8e1 211 }
2018US_harissalman 0:88e77eadd8e1 212
2018US_harissalman 0:88e77eadd8e1 213 class Grid{
2018US_harissalman 0:88e77eadd8e1 214 Player p;
2018US_harissalman 0:88e77eadd8e1 215 Claw c;
2018US_harissalman 0:88e77eadd8e1 216 int score;
2018US_harissalman 0:88e77eadd8e1 217 public:
2018US_harissalman 0:88e77eadd8e1 218 Grid(){ setupMap(); }
2018US_harissalman 0:88e77eadd8e1 219
2018US_harissalman 0:88e77eadd8e1 220 Player& Igrac(){ return p; }
2018US_harissalman 0:88e77eadd8e1 221
2018US_harissalman 0:88e77eadd8e1 222 void setupMap(){
2018US_harissalman 0:88e77eadd8e1 223 p.SetX(5);
2018US_harissalman 0:88e77eadd8e1 224 p.SetY(0);
2018US_harissalman 0:88e77eadd8e1 225 score = 0;
2018US_harissalman 0:88e77eadd8e1 226 mapa.resize(0);
2018US_harissalman 0:88e77eadd8e1 227 vector<bool> novi(10, false);
2018US_harissalman 0:88e77eadd8e1 228 for (int i(0); i < 6; i++) mapa.push_back(novi);
2018US_harissalman 0:88e77eadd8e1 229
2018US_harissalman 0:88e77eadd8e1 230 mapa[0][0] = mapa[0][1] = mapa[0][7] = true;
2018US_harissalman 0:88e77eadd8e1 231 }
2018US_harissalman 0:88e77eadd8e1 232
2018US_harissalman 0:88e77eadd8e1 233 int GetScore(){ return score; }
2018US_harissalman 0:88e77eadd8e1 234
2018US_harissalman 0:88e77eadd8e1 235 void gravity(){
2018US_harissalman 0:88e77eadd8e1 236 if(p.GetY()>0 && !mapa[p.GetY()-1][p.GetX()]) p.SetY(p.GetY()-1);
2018US_harissalman 0:88e77eadd8e1 237 }
2018US_harissalman 0:88e77eadd8e1 238
2018US_harissalman 0:88e77eadd8e1 239 void masterTicker(){
2018US_harissalman 0:88e77eadd8e1 240 /*if(BTN){
2018US_harissalman 0:88e77eadd8e1 241 gridTicker.detach();
2018US_harissalman 0:88e77eadd8e1 242 pause();
2018US_harissalman 0:88e77eadd8e1 243 gridTicker.attach(this, &Grid::masterTicker, 1);
2018US_harissalman 0:88e77eadd8e1 244 }*/
2018US_harissalman 0:88e77eadd8e1 245 c.TickerFja();
2018US_harissalman 0:88e77eadd8e1 246 readJoystick();
2018US_harissalman 0:88e77eadd8e1 247 p.drawPlayer();
2018US_harissalman 0:88e77eadd8e1 248 drawMap();
2018US_harissalman 0:88e77eadd8e1 249 }
2018US_harissalman 0:88e77eadd8e1 250
2018US_harissalman 0:88e77eadd8e1 251 void drawMap(){
2018US_harissalman 0:88e77eadd8e1 252 drawBorders();
2018US_harissalman 0:88e77eadd8e1 253 displayScore();
2018US_harissalman 0:88e77eadd8e1 254
2018US_harissalman 0:88e77eadd8e1 255 // Crtanje mape ovisno o matrici prisustva
2018US_harissalman 0:88e77eadd8e1 256 for(int i(0); i<6; i++){
2018US_harissalman 0:88e77eadd8e1 257 for(int j(0); j<10; j++){
2018US_harissalman 0:88e77eadd8e1 258 if(mapa[i][j]) drawBox(j, 6-i);
2018US_harissalman 0:88e77eadd8e1 259 else if((i==p.GetY() || i==p.GetY()+1) && j==p.GetX()) continue;
2018US_harissalman 0:88e77eadd8e1 260 else drawBlank(j, 6-i);
2018US_harissalman 0:88e77eadd8e1 261 }
2018US_harissalman 0:88e77eadd8e1 262 }
2018US_harissalman 0:88e77eadd8e1 263 calculatePoints();
2018US_harissalman 0:88e77eadd8e1 264 if(toggleDraw = !toggleDraw) refreshMapa();
2018US_harissalman 0:88e77eadd8e1 265 p.DaLiJeZiv();
2018US_harissalman 0:88e77eadd8e1 266 }
2018US_harissalman 0:88e77eadd8e1 267
2018US_harissalman 0:88e77eadd8e1 268 void refreshMapa(){
2018US_harissalman 0:88e77eadd8e1 269 // Kad padaju kutije, treba se pozivati svaku sekundu
2018US_harissalman 0:88e77eadd8e1 270 // Gravitacija igrača
2018US_harissalman 0:88e77eadd8e1 271 gravity();
2018US_harissalman 0:88e77eadd8e1 272 for(int i(1); i<6; i++){
2018US_harissalman 0:88e77eadd8e1 273 for(int j(0); j<10; j++){
2018US_harissalman 0:88e77eadd8e1 274 if (mapa[i][j] && !mapa[i-1][j]){
2018US_harissalman 0:88e77eadd8e1 275 mapa[i][j] = false;
2018US_harissalman 0:88e77eadd8e1 276 mapa[i-1][j] = true;
2018US_harissalman 0:88e77eadd8e1 277 }
2018US_harissalman 0:88e77eadd8e1 278 }
2018US_harissalman 0:88e77eadd8e1 279 }
2018US_harissalman 0:88e77eadd8e1 280 }
2018US_harissalman 0:88e77eadd8e1 281
2018US_harissalman 0:88e77eadd8e1 282 void calculatePoints(){
2018US_harissalman 0:88e77eadd8e1 283 bool rowFull(true);
2018US_harissalman 0:88e77eadd8e1 284 // Vidi kakvo je stanje
2018US_harissalman 0:88e77eadd8e1 285 for (int i(0); i < 10; i++){
2018US_harissalman 0:88e77eadd8e1 286 if (mapa[0][i] == 0) rowFull = false;
2018US_harissalman 0:88e77eadd8e1 287 }
2018US_harissalman 0:88e77eadd8e1 288 // Ako je red pun, briši sve i dodaj bodove
2018US_harissalman 0:88e77eadd8e1 289 if (rowFull){
2018US_harissalman 0:88e77eadd8e1 290 for (int i(0); i < 10; i++) mapa[0][i] = 0;
2018US_harissalman 0:88e77eadd8e1 291 score += 100;
2018US_harissalman 0:88e77eadd8e1 292 displayScore();
2018US_harissalman 0:88e77eadd8e1 293 }
2018US_harissalman 0:88e77eadd8e1 294 }
2018US_harissalman 0:88e77eadd8e1 295
2018US_harissalman 0:88e77eadd8e1 296 void drawBox(int row, int column){
2018US_harissalman 0:88e77eadd8e1 297 display.rect(row*BoxSize + LPAD, column*BoxSize, row*BoxSize + BoxSize + LPAD, column*BoxSize + BoxSize, Black);
2018US_harissalman 0:88e77eadd8e1 298 display.line(row*BoxSize + LPAD, column*BoxSize, row*BoxSize + BoxSize + LPAD, column*BoxSize + BoxSize, Black);
2018US_harissalman 0:88e77eadd8e1 299 display.line(row*BoxSize + BoxSize + LPAD, column*BoxSize, row*BoxSize + LPAD, column*BoxSize + BoxSize, Black);
2018US_harissalman 0:88e77eadd8e1 300 }
2018US_harissalman 0:88e77eadd8e1 301
2018US_harissalman 0:88e77eadd8e1 302 void drawBlank(int row, int column){
2018US_harissalman 0:88e77eadd8e1 303 display.fillrect(row*BoxSize + LPAD + 1, column*BoxSize, row*BoxSize + BoxSize + LPAD, column*BoxSize + BoxSize - 1, White);
2018US_harissalman 0:88e77eadd8e1 304 }
2018US_harissalman 0:88e77eadd8e1 305
2018US_harissalman 0:88e77eadd8e1 306 // Nacrtaj granice igre
2018US_harissalman 0:88e77eadd8e1 307 void drawBorders(){
2018US_harissalman 0:88e77eadd8e1 308 display.line(LPAD - 1, 0, LPAD - 1, 211, Black); // Lijeva granica
2018US_harissalman 0:88e77eadd8e1 309 display.line(311, 0, 311, 210, Black); // Desna granica
2018US_harissalman 0:88e77eadd8e1 310 display.line(LPAD - 1, 211, 311, 211, Black); // Donja granica
2018US_harissalman 0:88e77eadd8e1 311 }
2018US_harissalman 0:88e77eadd8e1 312
2018US_harissalman 0:88e77eadd8e1 313 void displayScore(){
2018US_harissalman 0:88e77eadd8e1 314 display.locate(166, 219);
2018US_harissalman 0:88e77eadd8e1 315 display.printf("SCORE:%6d", score);
2018US_harissalman 0:88e77eadd8e1 316 }
2018US_harissalman 0:88e77eadd8e1 317
2018US_harissalman 0:88e77eadd8e1 318 void readJoystick(){
2018US_harissalman 0:88e77eadd8e1 319 // Debouncing džojstika
2018US_harissalman 0:88e77eadd8e1 320 if(deb.read_ms()<0.4) return;
2018US_harissalman 0:88e77eadd8e1 321 deb.reset();
2018US_harissalman 0:88e77eadd8e1 322
2018US_harissalman 0:88e77eadd8e1 323 int x = Jx*3.3 - 0.4; // 0 = Lijevo, 1 = Ništa, 2 = Desno
2018US_harissalman 0:88e77eadd8e1 324 int y = Jy*3.3 - 0.4; // 0 = Dole, 1 = Ništa, 2 = Gore
2018US_harissalman 0:88e77eadd8e1 325
2018US_harissalman 0:88e77eadd8e1 326 if(x == 0){
2018US_harissalman 0:88e77eadd8e1 327 if(y == 0) p.PomjeriGoreLijevo();
2018US_harissalman 0:88e77eadd8e1 328 else p.PomjeriLijevo();
2018US_harissalman 0:88e77eadd8e1 329 } else if(x == 2){
2018US_harissalman 0:88e77eadd8e1 330 if(y == 0) p.PomjeriGoreDesno();
2018US_harissalman 0:88e77eadd8e1 331 else p.PomjeriDesno();
2018US_harissalman 0:88e77eadd8e1 332 } else if(y == 0) p.PomjeriGore();
2018US_harissalman 0:88e77eadd8e1 333 }
2018US_harissalman 0:88e77eadd8e1 334 };
2018US_harissalman 0:88e77eadd8e1 335
2018US_harissalman 0:88e77eadd8e1 336 Grid g;
2018US_harissalman 0:88e77eadd8e1 337
2018US_harissalman 0:88e77eadd8e1 338 // Iskljuceno radi problema sa InterruptIn
2018US_harissalman 0:88e77eadd8e1 339 void pause(){
2018US_harissalman 0:88e77eadd8e1 340 // Resetuje igru ako je trenutno kraj
2018US_harissalman 0:88e77eadd8e1 341 if(gameover){
2018US_harissalman 0:88e77eadd8e1 342 gameover = false;
2018US_harissalman 0:88e77eadd8e1 343 display.cls();
2018US_harissalman 0:88e77eadd8e1 344 g.setupMap();
2018US_harissalman 0:88e77eadd8e1 345 return;
2018US_harissalman 0:88e77eadd8e1 346 }
2018US_harissalman 0:88e77eadd8e1 347
2018US_harissalman 0:88e77eadd8e1 348 if(deb.read_ms()>1000){
2018US_harissalman 0:88e77eadd8e1 349 // Toggle trenutno stanje pauze
2018US_harissalman 0:88e77eadd8e1 350 paused = !paused;
2018US_harissalman 0:88e77eadd8e1 351
2018US_harissalman 0:88e77eadd8e1 352 if(paused){
2018US_harissalman 0:88e77eadd8e1 353 display.locate(LPAD, 219);
2018US_harissalman 0:88e77eadd8e1 354 display.printf("PAUZA");
2018US_harissalman 0:88e77eadd8e1 355 while(paused);
2018US_harissalman 0:88e77eadd8e1 356 } else {
2018US_harissalman 0:88e77eadd8e1 357 // Obriši tekst za pauzu, nastavi igru
2018US_harissalman 0:88e77eadd8e1 358 display.fillrect(LPAD, 219, 70, 231, White);
2018US_harissalman 0:88e77eadd8e1 359 }
2018US_harissalman 0:88e77eadd8e1 360 }
2018US_harissalman 0:88e77eadd8e1 361 deb.reset();
2018US_harissalman 0:88e77eadd8e1 362 }
2018US_harissalman 0:88e77eadd8e1 363
2018US_harissalman 0:88e77eadd8e1 364 void gameOver(){
2018US_harissalman 0:88e77eadd8e1 365 gridTicker.detach();
2018US_harissalman 0:88e77eadd8e1 366 gameover = true;
2018US_harissalman 0:88e77eadd8e1 367 display.cls();
2018US_harissalman 0:88e77eadd8e1 368
2018US_harissalman 0:88e77eadd8e1 369 display.locate(58, 100);
2018US_harissalman 0:88e77eadd8e1 370 display.set_font((unsigned char*) Arial28x28);
2018US_harissalman 0:88e77eadd8e1 371 display.printf("GAME OVER");
2018US_harissalman 0:88e77eadd8e1 372 display.set_font((unsigned char*) Arial12x12);
2018US_harissalman 0:88e77eadd8e1 373 display.locate(100, 140);
2018US_harissalman 0:88e77eadd8e1 374 display.printf("SCORE:%6d", g.GetScore());
2018US_harissalman 0:88e77eadd8e1 375 //display.locate(70, 180);
2018US_harissalman 0:88e77eadd8e1 376 //display.printf("Press Joystick to restart...");
2018US_harissalman 0:88e77eadd8e1 377
2018US_harissalman 0:88e77eadd8e1 378 while(1){
2018US_harissalman 0:88e77eadd8e1 379 if(gameover) wait(0.1);
2018US_harissalman 0:88e77eadd8e1 380 else break;
2018US_harissalman 0:88e77eadd8e1 381 }
2018US_harissalman 0:88e77eadd8e1 382 }
2018US_harissalman 0:88e77eadd8e1 383
2018US_harissalman 0:88e77eadd8e1 384 // -------------------------------------------------------------------------- //
2018US_harissalman 0:88e77eadd8e1 385
2018US_harissalman 0:88e77eadd8e1 386 int main() {
2018US_harissalman 0:88e77eadd8e1 387 display.claim(stdout);
2018US_harissalman 0:88e77eadd8e1 388 display.set_orientation(1);
2018US_harissalman 0:88e77eadd8e1 389 display.background(White);
2018US_harissalman 0:88e77eadd8e1 390 display.foreground(Black);
2018US_harissalman 0:88e77eadd8e1 391 display.cls();
2018US_harissalman 0:88e77eadd8e1 392 display.set_font((unsigned char*) Arial12x12);
2018US_harissalman 0:88e77eadd8e1 393
2018US_harissalman 0:88e77eadd8e1 394 deb.start();
2018US_harissalman 0:88e77eadd8e1 395 gridTicker.attach(&g, &Grid::masterTicker, 0.25);
2018US_harissalman 0:88e77eadd8e1 396
2018US_harissalman 0:88e77eadd8e1 397 while(1) {
2018US_harissalman 0:88e77eadd8e1 398 wait(1);
2018US_harissalman 0:88e77eadd8e1 399 }
2018US_harissalman 0:88e77eadd8e1 400 }