Armin Klacar Josip Kvesic

Dependencies:   N5110 mbed

Committer:
tim004
Date:
Mon Jun 09 10:04:15 2014 +0000
Revision:
0:36537c240b95
PAI_ProjekatAsteroidi_Grupa6_Tim004

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tim004 0:36537c240b95 1 #include "mbed.h"
tim004 0:36537c240b95 2 #include "N5110.h"
tim004 0:36537c240b95 3 #include <stdlib.h>
tim004 0:36537c240b95 4 #include <stdio.h>
tim004 0:36537c240b95 5 #include <sstream>
tim004 0:36537c240b95 6
tim004 0:36537c240b95 7 //Deklaracija LCD objekta
tim004 0:36537c240b95 8 //N5110 lcd(VCC,SCE,RST,D/C,MOSI,SCLK,LED)
tim004 0:36537c240b95 9 N5110 lcd(dp4,dp24,dp23,dp25,dp2,dp6,dp18);
tim004 0:36537c240b95 10
tim004 0:36537c240b95 11 //Digitalni izlaz za deaktivaciju LED buffera
tim004 0:36537c240b95 12 DigitalOut enable(dp14);
tim004 0:36537c240b95 13
tim004 0:36537c240b95 14 //Analogni ulazi sa dzojstika
tim004 0:36537c240b95 15 AnalogIn VRx(dp11);
tim004 0:36537c240b95 16 AnalogIn VRy(dp10);
tim004 0:36537c240b95 17 //Taster sa dzojstika
tim004 0:36537c240b95 18 DigitalIn SW(dp9);
tim004 0:36537c240b95 19
tim004 0:36537c240b95 20
tim004 0:36537c240b95 21 Timer stoperica;
tim004 0:36537c240b95 22 Ticker t;
tim004 0:36537c240b95 23 Timer speedy;
tim004 0:36537c240b95 24 bool isAttached;
tim004 0:36537c240b95 25 int difficulty;
tim004 0:36537c240b95 26 int whatIsWorkingFlag; // Menu-0,Difficulty-1,HighScores-2,Game-3
tim004 0:36537c240b95 27 int highScores[3] = {0,0,0};
tim004 0:36537c240b95 28 int score;
tim004 0:36537c240b95 29 double speed;
tim004 0:36537c240b95 30 int asteroid1[3] = {0,0,0};
tim004 0:36537c240b95 31 int asteroid2[3] = {0,0,0};
tim004 0:36537c240b95 32 int asteroid3[3] = {0,0,0};
tim004 0:36537c240b95 33 int asteroid4[3] = {0,0,0};
tim004 0:36537c240b95 34 int asteroid5[3] = {0,0,0};
tim004 0:36537c240b95 35 int asteroid6[3] = {0,0,0};
tim004 0:36537c240b95 36 int player[3] = {0,0,0};
tim004 0:36537c240b95 37
tim004 0:36537c240b95 38
tim004 0:36537c240b95 39 void setAsteroids(void);
tim004 0:36537c240b95 40 void startAsteroids(void);
tim004 0:36537c240b95 41 void updateHighScores(void);
tim004 0:36537c240b95 42 void resetHighScores(void);
tim004 0:36537c240b95 43 void startGame(void);
tim004 0:36537c240b95 44 void paintDifficulty(void);
tim004 0:36537c240b95 45 void paintHighScores(void);
tim004 0:36537c240b95 46 void paintScore(void);
tim004 0:36537c240b95 47 void callPlayer(void);
tim004 0:36537c240b95 48
tim004 0:36537c240b95 49 char* convertToChar(int n)
tim004 0:36537c240b95 50 {
tim004 0:36537c240b95 51 static char k[10];
tim004 0:36537c240b95 52 int temp=n;
tim004 0:36537c240b95 53 int broj=0;
tim004 0:36537c240b95 54 while (temp)
tim004 0:36537c240b95 55 {
tim004 0:36537c240b95 56 temp/=10;
tim004 0:36537c240b95 57 broj++;
tim004 0:36537c240b95 58 }
tim004 0:36537c240b95 59 broj--;
tim004 0:36537c240b95 60 while (n)
tim004 0:36537c240b95 61 {
tim004 0:36537c240b95 62 temp=n%10;
tim004 0:36537c240b95 63 n/=10;
tim004 0:36537c240b95 64 k[broj]=temp+'0';
tim004 0:36537c240b95 65 broj--;
tim004 0:36537c240b95 66 }
tim004 0:36537c240b95 67 return k;
tim004 0:36537c240b95 68 }
tim004 0:36537c240b95 69
tim004 0:36537c240b95 70 void updateHighScores()
tim004 0:36537c240b95 71 {
tim004 0:36537c240b95 72 if (score>highScores[0])
tim004 0:36537c240b95 73 {
tim004 0:36537c240b95 74 double temp = highScores[0];
tim004 0:36537c240b95 75 highScores[2] = highScores[1];
tim004 0:36537c240b95 76 highScores[1]=temp;
tim004 0:36537c240b95 77 highScores[0]=score;
tim004 0:36537c240b95 78 }
tim004 0:36537c240b95 79 else if (score>highScores[1])
tim004 0:36537c240b95 80 {
tim004 0:36537c240b95 81 highScores[2] = highScores[1];
tim004 0:36537c240b95 82 highScores[1]=score;
tim004 0:36537c240b95 83 }
tim004 0:36537c240b95 84 else if (score>highScores[2]) highScores[2] = score;
tim004 0:36537c240b95 85 }
tim004 0:36537c240b95 86
tim004 0:36537c240b95 87 void resetHighScores()
tim004 0:36537c240b95 88 {
tim004 0:36537c240b95 89 highScores[0]=0;
tim004 0:36537c240b95 90 highScores[1]=0;
tim004 0:36537c240b95 91 highScores[2]=0;
tim004 0:36537c240b95 92 whatIsWorkingFlag = 0;
tim004 0:36537c240b95 93 }
tim004 0:36537c240b95 94
tim004 0:36537c240b95 95 void paintGameOverScore(){
tim004 0:36537c240b95 96 speedy.start();
tim004 0:36537c240b95 97 while(speedy.read()<1){
tim004 0:36537c240b95 98 char * c = convertToChar(score);
tim004 0:36537c240b95 99 lcd.printString("Game Over",0,0);
tim004 0:36537c240b95 100 lcd.printString("Score:",0,1);
tim004 0:36537c240b95 101 lcd.printString(c,0,2);
tim004 0:36537c240b95 102 }
tim004 0:36537c240b95 103 for(int i = 0; i <3 ; i++){
tim004 0:36537c240b95 104 asteroid1[i]=0;
tim004 0:36537c240b95 105 asteroid2[i]=0;
tim004 0:36537c240b95 106 asteroid3[i]=0;
tim004 0:36537c240b95 107 asteroid4[i]=0;
tim004 0:36537c240b95 108 asteroid5[i]=0;
tim004 0:36537c240b95 109 asteroid6[i]=0;
tim004 0:36537c240b95 110 }
tim004 0:36537c240b95 111 updateHighScores();score=0;
tim004 0:36537c240b95 112 }
tim004 0:36537c240b95 113 void gameOverTestA1(int ax,int ay)
tim004 0:36537c240b95 114 {
tim004 0:36537c240b95 115 int px = player[0];
tim004 0:36537c240b95 116 int py = player[1];
tim004 0:36537c240b95 117 int playax[13]= {px,px-1,px,px+1,px-2,px-1,px+1,px+2,px-2,px+2,-1,-1,-1};
tim004 0:36537c240b95 118 int playay[13]= {py,py+1,py+1,py+1,py+2,py+2,py+2,py+2,py+3,py+3,-1,-1,-1};
tim004 0:36537c240b95 119 int asterx[13]= {ax,ax-1,ax,ax+1,ax-1,ax,ax+1,ax-2,ax-1,ax,ax+1,ax-1,ax};
tim004 0:36537c240b95 120 int astery[13]= {ay,ay+1,ay+1,ay+1,ay+2,ay+2,ay+2,ay+3,ay+3,ay+3,ay+3,ay+4,ay+4};
tim004 0:36537c240b95 121
tim004 0:36537c240b95 122 for(int i=0; i<13; i++)
tim004 0:36537c240b95 123 {
tim004 0:36537c240b95 124 for(int j=0; j<13; j++)
tim004 0:36537c240b95 125 {
tim004 0:36537c240b95 126 if(playax[i]==asterx[j])
tim004 0:36537c240b95 127 {
tim004 0:36537c240b95 128 if(playay[i]==astery[j]){
tim004 0:36537c240b95 129 lcd.clear();
tim004 0:36537c240b95 130 whatIsWorkingFlag=0;
tim004 0:36537c240b95 131 paintGameOverScore();
tim004 0:36537c240b95 132 stoperica.reset();
tim004 0:36537c240b95 133 stoperica.stop();
tim004 0:36537c240b95 134 isAttached=false;
tim004 0:36537c240b95 135 break;}
tim004 0:36537c240b95 136 }
tim004 0:36537c240b95 137 }
tim004 0:36537c240b95 138 }
tim004 0:36537c240b95 139
tim004 0:36537c240b95 140
tim004 0:36537c240b95 141
tim004 0:36537c240b95 142 } //Implementacija Collision Testa
tim004 0:36537c240b95 143 void gameOverTestA2(int ax,int ay)
tim004 0:36537c240b95 144 {
tim004 0:36537c240b95 145 int px = player[0];
tim004 0:36537c240b95 146 int py = player[1];
tim004 0:36537c240b95 147 int playax[15]= {px,px-1,px,px+1,px-2,px-1,px+1,px+2,px-2,px+2,-1,-1,-1,-1,-1};
tim004 0:36537c240b95 148 int playay[15]= {py,py+1,py+1,py+1,py+2,py+2,py+2,py+2,py+3,py+3,-1,-1,-1,-1,-1};
tim004 0:36537c240b95 149 int asterx[15]= {ax-2,ax-1,ax,ax-1,ax,ax+1,ax-2,ax-1,ax,ax-1,ax,ax-2,ax-1,ax,ax+1};
tim004 0:36537c240b95 150 int astery[15]= {ay,ay,ay,ay+1,ay+1,ay+1,ay+2,ay+2,ay+2,ay+3,ay+3,ay+4,ay+4,ay+4,ay+4};
tim004 0:36537c240b95 151 for(int i=0; i<15; i++)
tim004 0:36537c240b95 152 {
tim004 0:36537c240b95 153 for(int j=0; j<15; j++)
tim004 0:36537c240b95 154 {
tim004 0:36537c240b95 155 if(playax[i]==asterx[j])
tim004 0:36537c240b95 156 {
tim004 0:36537c240b95 157 if(playay[i]==astery[j]){
tim004 0:36537c240b95 158 lcd.clear();
tim004 0:36537c240b95 159 whatIsWorkingFlag=0;
tim004 0:36537c240b95 160 paintGameOverScore();
tim004 0:36537c240b95 161 stoperica.reset();
tim004 0:36537c240b95 162 stoperica.stop();
tim004 0:36537c240b95 163 isAttached=false;
tim004 0:36537c240b95 164 break;}
tim004 0:36537c240b95 165 }
tim004 0:36537c240b95 166
tim004 0:36537c240b95 167 }
tim004 0:36537c240b95 168 }
tim004 0:36537c240b95 169
tim004 0:36537c240b95 170 } //Implementacija Collision Testa
tim004 0:36537c240b95 171 void gameOverTestA3(int ax,int ay)
tim004 0:36537c240b95 172 {
tim004 0:36537c240b95 173 int px = player[0];
tim004 0:36537c240b95 174 int py = player[1];
tim004 0:36537c240b95 175 int playax[18]= {px,px-1,px,px+1,px-2,px-1,px+1,px+2,px-2,px+2,-1,-1,-1,-1,-1,-1,-1,-1};
tim004 0:36537c240b95 176 int playay[18]= {py,py+1,py+1,py+1,py+2,py+2,py+2,py+2,py+3,py+3,-1,-1,-1,-1,-1,-1,-1,-1};
tim004 0:36537c240b95 177 int asterx[18]= {ax,ax+1,ax+2,ax+3,ax+4,ax+5,ax+6,ax+7,ax+1,ax+2,ax+3,ax+4,ax+5,ax+6,ax+7,ax+3,ax+4,ax+5};
tim004 0:36537c240b95 178 int astery[18]= {ay,ay,ay,ay,ay,ay,ay,ay,ay+1,ay+1,ay+1,ay+1,ay+1,ay+1,ay+1,ay+2,ay+2,ay+2};
tim004 0:36537c240b95 179
tim004 0:36537c240b95 180
tim004 0:36537c240b95 181 for(int i=0; i<18; i++)
tim004 0:36537c240b95 182 {
tim004 0:36537c240b95 183 for(int j=0; j<18; j++)
tim004 0:36537c240b95 184 {
tim004 0:36537c240b95 185 if(playax[i]==asterx[j])
tim004 0:36537c240b95 186 {
tim004 0:36537c240b95 187 if(playay[i]==astery[j]){
tim004 0:36537c240b95 188 lcd.clear();
tim004 0:36537c240b95 189 whatIsWorkingFlag=0;
tim004 0:36537c240b95 190 paintGameOverScore();
tim004 0:36537c240b95 191 stoperica.reset();
tim004 0:36537c240b95 192 stoperica.stop();
tim004 0:36537c240b95 193 isAttached=false;
tim004 0:36537c240b95 194 break;}
tim004 0:36537c240b95 195 }
tim004 0:36537c240b95 196
tim004 0:36537c240b95 197 }
tim004 0:36537c240b95 198 }
tim004 0:36537c240b95 199
tim004 0:36537c240b95 200 } //Implementacija Collision Testa
tim004 0:36537c240b95 201
tim004 0:36537c240b95 202
tim004 0:36537c240b95 203 void AsteroidBody1(int asteroid[])
tim004 0:36537c240b95 204 {
tim004 0:36537c240b95 205
tim004 0:36537c240b95 206
tim004 0:36537c240b95 207 int a=asteroid[0];
tim004 0:36537c240b95 208 int b=asteroid[1];
tim004 0:36537c240b95 209 int c=asteroid[2];
tim004 0:36537c240b95 210 for(int i=0; i<=4; i++)
tim004 0:36537c240b95 211 lcd.setPixel(a,b+i);
tim004 0:36537c240b95 212
tim004 0:36537c240b95 213 for(int i=1; i<=4; i++)
tim004 0:36537c240b95 214 lcd.setPixel(a-1,b+i);
tim004 0:36537c240b95 215
tim004 0:36537c240b95 216 for(int i=1; i<=3; i++)
tim004 0:36537c240b95 217 lcd.setPixel(a+1,b+i);
tim004 0:36537c240b95 218
tim004 0:36537c240b95 219 lcd.setPixel(a-2,b+3);
tim004 0:36537c240b95 220 lcd.refresh();
tim004 0:36537c240b95 221 asteroid[1]=b;
tim004 0:36537c240b95 222 asteroid[0]=a;
tim004 0:36537c240b95 223 asteroid[2]=c;
tim004 0:36537c240b95 224 gameOverTestA1(a,b);
tim004 0:36537c240b95 225
tim004 0:36537c240b95 226 }
tim004 0:36537c240b95 227
tim004 0:36537c240b95 228 void AsteroidBody2(int asteroid[])
tim004 0:36537c240b95 229 {
tim004 0:36537c240b95 230
tim004 0:36537c240b95 231 int a=asteroid[0];
tim004 0:36537c240b95 232 int b=asteroid[1];
tim004 0:36537c240b95 233 int c=asteroid[2];
tim004 0:36537c240b95 234 for(int i=0; i<=4; i++)
tim004 0:36537c240b95 235 lcd.setPixel(a,b+i);
tim004 0:36537c240b95 236
tim004 0:36537c240b95 237 for(int i=0; i<=4; i++)
tim004 0:36537c240b95 238 lcd.setPixel(a-1,b+i);
tim004 0:36537c240b95 239
tim004 0:36537c240b95 240 for(int i=0; i<=4; i=i+2)
tim004 0:36537c240b95 241 lcd.setPixel(a-2,b+i);
tim004 0:36537c240b95 242
tim004 0:36537c240b95 243 lcd.setPixel(a+1,b+1);
tim004 0:36537c240b95 244 lcd.setPixel(a+1,b+4);
tim004 0:36537c240b95 245 lcd.refresh();
tim004 0:36537c240b95 246 asteroid[1]=b;
tim004 0:36537c240b95 247 asteroid[0]=a;
tim004 0:36537c240b95 248 asteroid[2]=c;
tim004 0:36537c240b95 249 gameOverTestA2(a,b);
tim004 0:36537c240b95 250
tim004 0:36537c240b95 251 }
tim004 0:36537c240b95 252
tim004 0:36537c240b95 253 void AsteroidBody3(int asteroid[])
tim004 0:36537c240b95 254 {
tim004 0:36537c240b95 255
tim004 0:36537c240b95 256 int a=asteroid[0];
tim004 0:36537c240b95 257 int b=asteroid[1];
tim004 0:36537c240b95 258 int c=asteroid[2];
tim004 0:36537c240b95 259 lcd.setPixel(a,b);
tim004 0:36537c240b95 260 for(int i=1; i<=7; i++)
tim004 0:36537c240b95 261 {
tim004 0:36537c240b95 262 lcd.setPixel(a+i,b);
tim004 0:36537c240b95 263 lcd.setPixel(a+i,b+1);
tim004 0:36537c240b95 264 }
tim004 0:36537c240b95 265 for(int i=3; i<=5; i++)
tim004 0:36537c240b95 266 lcd.setPixel(a+i,b+2);
tim004 0:36537c240b95 267
tim004 0:36537c240b95 268 lcd.refresh();
tim004 0:36537c240b95 269 asteroid[1]=b;
tim004 0:36537c240b95 270 asteroid[0]=a;
tim004 0:36537c240b95 271 asteroid[2]=c;
tim004 0:36537c240b95 272 gameOverTestA3(a,b);
tim004 0:36537c240b95 273
tim004 0:36537c240b95 274 }
tim004 0:36537c240b95 275
tim004 0:36537c240b95 276 void paintMenu()
tim004 0:36537c240b95 277 {
tim004 0:36537c240b95 278 if(!whatIsWorkingFlag){
tim004 0:36537c240b95 279 int y = player[1];
tim004 0:36537c240b95 280 lcd.printString("Start Game",0,0);
tim004 0:36537c240b95 281 lcd.printString("Set Difficulty",0,1);
tim004 0:36537c240b95 282 lcd.printString("HighScores",0,2);
tim004 0:36537c240b95 283 lcd.printString("RstHighScores",0,3);
tim004 0:36537c240b95 284 if(SW==0&&y>=0&&y<=7)
tim004 0:36537c240b95 285 {
tim004 0:36537c240b95 286 whatIsWorkingFlag = 3;player[0]=42;player[1]=43;
tim004 0:36537c240b95 287 startGame();
tim004 0:36537c240b95 288 }
tim004 0:36537c240b95 289 else if(SW==0&&y>=8&&y<=15)
tim004 0:36537c240b95 290 {
tim004 0:36537c240b95 291 whatIsWorkingFlag=1;
tim004 0:36537c240b95 292 paintDifficulty();
tim004 0:36537c240b95 293 }
tim004 0:36537c240b95 294 else if(SW==0&&y>=16&&y<=23)
tim004 0:36537c240b95 295 {
tim004 0:36537c240b95 296 whatIsWorkingFlag=2;
tim004 0:36537c240b95 297 paintHighScores();
tim004 0:36537c240b95 298 }
tim004 0:36537c240b95 299 else if(SW==0&&y>=24&&y<=40)
tim004 0:36537c240b95 300 {
tim004 0:36537c240b95 301 whatIsWorkingFlag=0;
tim004 0:36537c240b95 302 resetHighScores();
tim004 0:36537c240b95 303 }
tim004 0:36537c240b95 304 }
tim004 0:36537c240b95 305 else{
tim004 0:36537c240b95 306 if(whatIsWorkingFlag==1){paintDifficulty();}
tim004 0:36537c240b95 307 else if(whatIsWorkingFlag==2){paintHighScores();}
tim004 0:36537c240b95 308 else if(whatIsWorkingFlag==3){startGame();}
tim004 0:36537c240b95 309 }
tim004 0:36537c240b95 310 }
tim004 0:36537c240b95 311
tim004 0:36537c240b95 312 void paintDifficulty()
tim004 0:36537c240b95 313 {
tim004 0:36537c240b95 314 int y= player[1];
tim004 0:36537c240b95 315 lcd.printString("Easy",0,0);
tim004 0:36537c240b95 316 lcd.printString("Normal",0,1);
tim004 0:36537c240b95 317 lcd.printString("Hard",0,2);
tim004 0:36537c240b95 318 if(!SW)
tim004 0:36537c240b95 319 {
tim004 0:36537c240b95 320 if(y>=0&&y<=7) {difficulty=0; speed=0.9;}
tim004 0:36537c240b95 321 else if(y>=8&&y<=15) {difficulty=1;speed=0.85;}
tim004 0:36537c240b95 322 else if(y>=16&&y<=23) {difficulty=2;speed=0.8;}
tim004 0:36537c240b95 323 whatIsWorkingFlag=0;
tim004 0:36537c240b95 324 paintMenu();
tim004 0:36537c240b95 325 }
tim004 0:36537c240b95 326 }
tim004 0:36537c240b95 327
tim004 0:36537c240b95 328 void paintHighScores()
tim004 0:36537c240b95 329 {
tim004 0:36537c240b95 330 wait(0.02);
tim004 0:36537c240b95 331 while(SW){
tim004 0:36537c240b95 332 char *s1;
tim004 0:36537c240b95 333 char *s2;
tim004 0:36537c240b95 334 char *s3;
tim004 0:36537c240b95 335 s1=convertToChar(highScores[0]);
tim004 0:36537c240b95 336 s2=convertToChar(highScores[1]);
tim004 0:36537c240b95 337 s3=convertToChar(highScores[2]);
tim004 0:36537c240b95 338
tim004 0:36537c240b95 339 lcd.printString("R1:",0,0);
tim004 0:36537c240b95 340 lcd.printString(s1,6,0);
tim004 0:36537c240b95 341 lcd.printString("R2:",0,1);
tim004 0:36537c240b95 342 lcd.printString(s2,6,1);
tim004 0:36537c240b95 343 lcd.printString("R3:",0,2);
tim004 0:36537c240b95 344 lcd.printString(s3,6,2);}
tim004 0:36537c240b95 345 whatIsWorkingFlag = 0;
tim004 0:36537c240b95 346 }
tim004 0:36537c240b95 347
tim004 0:36537c240b95 348 void paintScore()
tim004 0:36537c240b95 349 {
tim004 0:36537c240b95 350 int xyz;
tim004 0:36537c240b95 351 if (difficulty==0) xyz=2;
tim004 0:36537c240b95 352 else if(difficulty==1) xyz=4;
tim004 0:36537c240b95 353 else xyz=6;
tim004 0:36537c240b95 354 double speed1 = 1.0-speed;
tim004 0:36537c240b95 355 score = (int)((speed1*stoperica.read_ms()*xyz)/10);
tim004 0:36537c240b95 356 char * s= convertToChar(score);
tim004 0:36537c240b95 357 lcd.printString(s,0,5);
tim004 0:36537c240b95 358 }
tim004 0:36537c240b95 359
tim004 0:36537c240b95 360
tim004 0:36537c240b95 361
tim004 0:36537c240b95 362
tim004 0:36537c240b95 363
tim004 0:36537c240b95 364 void startGame()
tim004 0:36537c240b95 365 {
tim004 0:36537c240b95 366 if(!isAttached)
tim004 0:36537c240b95 367 {
tim004 0:36537c240b95 368 isAttached=!isAttached;
tim004 0:36537c240b95 369 stoperica.start();
tim004 0:36537c240b95 370 }
tim004 0:36537c240b95 371 int xyz=stoperica.read();
tim004 0:36537c240b95 372 if (xyz%3==0){
tim004 0:36537c240b95 373 if (speed>0.2)
tim004 0:36537c240b95 374 {if(difficulty==0)
tim004 0:36537c240b95 375
tim004 0:36537c240b95 376 { speed = speed - 0.02;}
tim004 0:36537c240b95 377 else if(difficulty == 1){speed = speed - 0.05;}
tim004 0:36537c240b95 378 else {speed = speed - 0.1;}
tim004 0:36537c240b95 379 }}
tim004 0:36537c240b95 380 setAsteroids();
tim004 0:36537c240b95 381
tim004 0:36537c240b95 382 }
tim004 0:36537c240b95 383
tim004 0:36537c240b95 384
tim004 0:36537c240b95 385 void PlayerBody(int a,int b)
tim004 0:36537c240b95 386 {
tim004 0:36537c240b95 387 if(a<3 || a>81 || b<3 || b>45) return;
tim004 0:36537c240b95 388 lcd.clear();
tim004 0:36537c240b95 389 lcd.setPixel(a,b);
tim004 0:36537c240b95 390 lcd.setPixel(a,b+1);
tim004 0:36537c240b95 391 lcd.setPixel(a-1,b+1);
tim004 0:36537c240b95 392 lcd.setPixel(a+1,b+1);
tim004 0:36537c240b95 393 lcd.setPixel(a-1,b+2);
tim004 0:36537c240b95 394 lcd.setPixel(a+1,b+2);
tim004 0:36537c240b95 395 lcd.setPixel(a-2,b+2);
tim004 0:36537c240b95 396 lcd.setPixel(a+2,b+2);
tim004 0:36537c240b95 397 lcd.setPixel(a-2,b+3);
tim004 0:36537c240b95 398 lcd.setPixel(a+2,b+3);
tim004 0:36537c240b95 399 lcd.refresh();
tim004 0:36537c240b95 400 player[0]=a;
tim004 0:36537c240b95 401 player[1]=b;
tim004 0:36537c240b95 402 player[2]=1;
tim004 0:36537c240b95 403
tim004 0:36537c240b95 404 }
tim004 0:36537c240b95 405 void callPlayer()
tim004 0:36537c240b95 406 {
tim004 0:36537c240b95 407 int x= player[0];
tim004 0:36537c240b95 408 int y = player[1];
tim004 0:36537c240b95 409 if (VRx<1.0/3.0)
tim004 0:36537c240b95 410 {
tim004 0:36537c240b95 411 x=x-1;
tim004 0:36537c240b95 412
tim004 0:36537c240b95 413 PlayerBody(x,y);
tim004 0:36537c240b95 414 }
tim004 0:36537c240b95 415 else if (VRx<2.0/3.0)
tim004 0:36537c240b95 416 {
tim004 0:36537c240b95 417 PlayerBody(x,y);
tim004 0:36537c240b95 418 }
tim004 0:36537c240b95 419 else
tim004 0:36537c240b95 420 {
tim004 0:36537c240b95 421 x=x+1;
tim004 0:36537c240b95 422 PlayerBody(x,y);
tim004 0:36537c240b95 423 }
tim004 0:36537c240b95 424
tim004 0:36537c240b95 425 if (VRy<1.0/3.0)
tim004 0:36537c240b95 426 {
tim004 0:36537c240b95 427 y=y-1;
tim004 0:36537c240b95 428
tim004 0:36537c240b95 429 PlayerBody(x,y);
tim004 0:36537c240b95 430 }
tim004 0:36537c240b95 431 else if (VRy<2.0/3.0)
tim004 0:36537c240b95 432 {
tim004 0:36537c240b95 433 PlayerBody(x,y);
tim004 0:36537c240b95 434 }
tim004 0:36537c240b95 435 else
tim004 0:36537c240b95 436 {
tim004 0:36537c240b95 437 y=y+1;
tim004 0:36537c240b95 438 PlayerBody(x,y);
tim004 0:36537c240b95 439 }
tim004 0:36537c240b95 440 }
tim004 0:36537c240b95 441
tim004 0:36537c240b95 442 int postojiLiAsteroid()
tim004 0:36537c240b95 443 {
tim004 0:36537c240b95 444 int n=0;
tim004 0:36537c240b95 445 if(asteroid6[2]) n++;
tim004 0:36537c240b95 446 if(asteroid5[2]) n++;
tim004 0:36537c240b95 447 if(asteroid4[2]) n++;
tim004 0:36537c240b95 448 if(asteroid3[2]) n++;
tim004 0:36537c240b95 449 if(asteroid2[2]) n++;
tim004 0:36537c240b95 450 if(asteroid1[2]) n++;
tim004 0:36537c240b95 451 return n;
tim004 0:36537c240b95 452 }
tim004 0:36537c240b95 453
tim004 0:36537c240b95 454 void setAsteroids()
tim004 0:36537c240b95 455 {
tim004 0:36537c240b95 456 int n=postojiLiAsteroid();
tim004 0:36537c240b95 457 if(!n)
tim004 0:36537c240b95 458 {
tim004 0:36537c240b95 459 int i = rand()%3+1;
tim004 0:36537c240b95 460 asteroid1[2]=i;
tim004 0:36537c240b95 461 goto nastavak;
tim004 0:36537c240b95 462 }
tim004 0:36537c240b95 463
tim004 0:36537c240b95 464 else
tim004 0:36537c240b95 465 {
tim004 0:36537c240b95 466 if(n==2&&difficulty==0) goto nastavak;
tim004 0:36537c240b95 467 if(n==4&&difficulty==1) goto nastavak;
tim004 0:36537c240b95 468 if(n==6&&difficulty==2) goto nastavak;
tim004 0:36537c240b95 469
tim004 0:36537c240b95 470 int i = rand()%3+1;
tim004 0:36537c240b95 471 int j = rand()%4+1;
tim004 0:36537c240b95 472 if(j>2){
tim004 0:36537c240b95 473 if (!asteroid1[2])asteroid1[2]=i;
tim004 0:36537c240b95 474 else if (!asteroid2[2]) asteroid2[2]=i;
tim004 0:36537c240b95 475 else if (!asteroid3[2]) asteroid3[2]=i;
tim004 0:36537c240b95 476 else if (!asteroid4[2])asteroid4[2]=i;
tim004 0:36537c240b95 477 else if (!asteroid5[2]) asteroid5[2]=i;
tim004 0:36537c240b95 478 else if (!asteroid6[2])asteroid6[2]=i;
tim004 0:36537c240b95 479 }
tim004 0:36537c240b95 480 goto nastavak;
tim004 0:36537c240b95 481 }
tim004 0:36537c240b95 482 nastavak:
tim004 0:36537c240b95 483 {
tim004 0:36537c240b95 484 startAsteroids();
tim004 0:36537c240b95 485 }
tim004 0:36537c240b95 486
tim004 0:36537c240b95 487 }
tim004 0:36537c240b95 488 void updateAsteroids(int asteroid[]){
tim004 0:36537c240b95 489 int b = asteroid[1];
tim004 0:36537c240b95 490 b++;
tim004 0:36537c240b95 491 if(b>48)
tim004 0:36537c240b95 492 {
tim004 0:36537c240b95 493 b=0;
tim004 0:36537c240b95 494 asteroid[0]=0;
tim004 0:36537c240b95 495 asteroid[2]=0;
tim004 0:36537c240b95 496 }
tim004 0:36537c240b95 497 asteroid[1]=b;
tim004 0:36537c240b95 498
tim004 0:36537c240b95 499 }
tim004 0:36537c240b95 500
tim004 0:36537c240b95 501 void updateAsteroidCall(){
tim004 0:36537c240b95 502 if(asteroid1[2])
tim004 0:36537c240b95 503 {
tim004 0:36537c240b95 504 updateAsteroids(asteroid1);
tim004 0:36537c240b95 505 }
tim004 0:36537c240b95 506 if(asteroid2[2])
tim004 0:36537c240b95 507 {
tim004 0:36537c240b95 508 updateAsteroids(asteroid2);
tim004 0:36537c240b95 509
tim004 0:36537c240b95 510 }
tim004 0:36537c240b95 511 if(asteroid3[2])
tim004 0:36537c240b95 512 {
tim004 0:36537c240b95 513 updateAsteroids(asteroid3);
tim004 0:36537c240b95 514 }
tim004 0:36537c240b95 515 if(asteroid4[2])
tim004 0:36537c240b95 516 {
tim004 0:36537c240b95 517 updateAsteroids(asteroid4);
tim004 0:36537c240b95 518 }
tim004 0:36537c240b95 519 if(asteroid5[2])
tim004 0:36537c240b95 520 {
tim004 0:36537c240b95 521 updateAsteroids(asteroid5);
tim004 0:36537c240b95 522 }
tim004 0:36537c240b95 523 if(asteroid6[2])
tim004 0:36537c240b95 524 {
tim004 0:36537c240b95 525 updateAsteroids(asteroid6);
tim004 0:36537c240b95 526 }
tim004 0:36537c240b95 527
tim004 0:36537c240b95 528 }
tim004 0:36537c240b95 529 void startAsteroids()
tim004 0:36537c240b95 530 {
tim004 0:36537c240b95 531 speedy.start();
tim004 0:36537c240b95 532 while(speedy.read()<speed){
tim004 0:36537c240b95 533 if(asteroid1[2])
tim004 0:36537c240b95 534 {
tim004 0:36537c240b95 535 if(asteroid1[0]==0&&asteroid1[1]==0)
tim004 0:36537c240b95 536 {
tim004 0:36537c240b95 537 int k = rand()%83;
tim004 0:36537c240b95 538 asteroid1[0]=k;
tim004 0:36537c240b95 539 }
tim004 0:36537c240b95 540 int i=asteroid1[2];
tim004 0:36537c240b95 541
tim004 0:36537c240b95 542 switch (i)
tim004 0:36537c240b95 543 {
tim004 0:36537c240b95 544 case 1:
tim004 0:36537c240b95 545 AsteroidBody1(asteroid1);
tim004 0:36537c240b95 546 break;
tim004 0:36537c240b95 547 case 2:
tim004 0:36537c240b95 548 AsteroidBody2(asteroid1);
tim004 0:36537c240b95 549 break;
tim004 0:36537c240b95 550 case 3:
tim004 0:36537c240b95 551 AsteroidBody3(asteroid1);
tim004 0:36537c240b95 552 break;
tim004 0:36537c240b95 553 }
tim004 0:36537c240b95 554 }
tim004 0:36537c240b95 555 if(asteroid2[2])
tim004 0:36537c240b95 556 {
tim004 0:36537c240b95 557 if(asteroid2[0]==0&&asteroid2[1]==0)
tim004 0:36537c240b95 558 {
tim004 0:36537c240b95 559 int k = rand()%83;
tim004 0:36537c240b95 560 asteroid2[0]=k;
tim004 0:36537c240b95 561 }
tim004 0:36537c240b95 562 int i=asteroid2[2];
tim004 0:36537c240b95 563
tim004 0:36537c240b95 564 switch (i)
tim004 0:36537c240b95 565 {
tim004 0:36537c240b95 566 case 1:
tim004 0:36537c240b95 567 AsteroidBody1(asteroid2);
tim004 0:36537c240b95 568 break;
tim004 0:36537c240b95 569 case 2:
tim004 0:36537c240b95 570 AsteroidBody2(asteroid2);
tim004 0:36537c240b95 571 break;
tim004 0:36537c240b95 572 case 3:
tim004 0:36537c240b95 573 AsteroidBody3(asteroid2);
tim004 0:36537c240b95 574 break;
tim004 0:36537c240b95 575 }
tim004 0:36537c240b95 576 }
tim004 0:36537c240b95 577 if(asteroid3[2])
tim004 0:36537c240b95 578 {
tim004 0:36537c240b95 579 if(asteroid3[0]==0&&asteroid3[1]==0)
tim004 0:36537c240b95 580 {
tim004 0:36537c240b95 581 int k = rand()%83;
tim004 0:36537c240b95 582 asteroid3[0]=k;
tim004 0:36537c240b95 583 }
tim004 0:36537c240b95 584 int i=asteroid3[2];
tim004 0:36537c240b95 585
tim004 0:36537c240b95 586 switch (i)
tim004 0:36537c240b95 587 {
tim004 0:36537c240b95 588 case 1:
tim004 0:36537c240b95 589 AsteroidBody1(asteroid3);
tim004 0:36537c240b95 590 break;
tim004 0:36537c240b95 591 case 2:
tim004 0:36537c240b95 592 AsteroidBody2(asteroid3);
tim004 0:36537c240b95 593 break;
tim004 0:36537c240b95 594 case 3:
tim004 0:36537c240b95 595 AsteroidBody3(asteroid3);
tim004 0:36537c240b95 596 break;
tim004 0:36537c240b95 597 }
tim004 0:36537c240b95 598 }
tim004 0:36537c240b95 599 if(asteroid4[2])
tim004 0:36537c240b95 600 {
tim004 0:36537c240b95 601 if(asteroid4[0]==0&&asteroid4[1]==0)
tim004 0:36537c240b95 602 {
tim004 0:36537c240b95 603 int k = rand()%83;
tim004 0:36537c240b95 604 asteroid4[0]=k;
tim004 0:36537c240b95 605 }
tim004 0:36537c240b95 606 int i=asteroid4[2];
tim004 0:36537c240b95 607
tim004 0:36537c240b95 608 switch (i)
tim004 0:36537c240b95 609 {
tim004 0:36537c240b95 610 case 1:
tim004 0:36537c240b95 611 AsteroidBody1(asteroid4);
tim004 0:36537c240b95 612 break;
tim004 0:36537c240b95 613 case 2:
tim004 0:36537c240b95 614 AsteroidBody2(asteroid4);
tim004 0:36537c240b95 615 break;
tim004 0:36537c240b95 616 case 3:
tim004 0:36537c240b95 617 AsteroidBody3(asteroid4);
tim004 0:36537c240b95 618 break;
tim004 0:36537c240b95 619 }
tim004 0:36537c240b95 620 }
tim004 0:36537c240b95 621 if(asteroid5[2])
tim004 0:36537c240b95 622 {
tim004 0:36537c240b95 623 if(asteroid5[0]==0&&asteroid5[1]==0)
tim004 0:36537c240b95 624 {
tim004 0:36537c240b95 625 int k = rand()%83;
tim004 0:36537c240b95 626 asteroid5[0]=k;
tim004 0:36537c240b95 627 }
tim004 0:36537c240b95 628 int i=asteroid5[2];
tim004 0:36537c240b95 629
tim004 0:36537c240b95 630 switch (i)
tim004 0:36537c240b95 631 {
tim004 0:36537c240b95 632 case 1:
tim004 0:36537c240b95 633 AsteroidBody1(asteroid5);
tim004 0:36537c240b95 634 break;
tim004 0:36537c240b95 635 case 2:
tim004 0:36537c240b95 636 AsteroidBody2(asteroid5);
tim004 0:36537c240b95 637 break;
tim004 0:36537c240b95 638 case 3:
tim004 0:36537c240b95 639 AsteroidBody3(asteroid5);
tim004 0:36537c240b95 640 break;
tim004 0:36537c240b95 641 }
tim004 0:36537c240b95 642 }
tim004 0:36537c240b95 643 if(asteroid6[2])
tim004 0:36537c240b95 644 {
tim004 0:36537c240b95 645 if(asteroid6[0]==0&&asteroid6[1]==0)
tim004 0:36537c240b95 646 {
tim004 0:36537c240b95 647 int k = rand()%83;
tim004 0:36537c240b95 648 asteroid6[0]=k;
tim004 0:36537c240b95 649 }
tim004 0:36537c240b95 650 int i=asteroid6[2];
tim004 0:36537c240b95 651
tim004 0:36537c240b95 652 switch (i)
tim004 0:36537c240b95 653 {
tim004 0:36537c240b95 654 case 1:
tim004 0:36537c240b95 655 AsteroidBody1(asteroid6);
tim004 0:36537c240b95 656 break;
tim004 0:36537c240b95 657 case 2:
tim004 0:36537c240b95 658 AsteroidBody2(asteroid6);
tim004 0:36537c240b95 659 break;
tim004 0:36537c240b95 660 case 3:
tim004 0:36537c240b95 661 AsteroidBody3(asteroid6);
tim004 0:36537c240b95 662 break;
tim004 0:36537c240b95 663 }
tim004 0:36537c240b95 664 }
tim004 0:36537c240b95 665 paintScore();
tim004 0:36537c240b95 666 }
tim004 0:36537c240b95 667 speedy.reset();speedy.stop();
tim004 0:36537c240b95 668 updateAsteroidCall();
tim004 0:36537c240b95 669 }
tim004 0:36537c240b95 670
tim004 0:36537c240b95 671 int main()
tim004 0:36537c240b95 672 {
tim004 0:36537c240b95 673 enable = 1;
tim004 0:36537c240b95 674 difficulty = 1;
tim004 0:36537c240b95 675 isAttached = false;
tim004 0:36537c240b95 676 whatIsWorkingFlag = 0;
tim004 0:36537c240b95 677 speed = 0.9;
tim004 0:36537c240b95 678 score = 0;
tim004 0:36537c240b95 679
tim004 0:36537c240b95 680 //Aktivacija pull-up otpornika
tim004 0:36537c240b95 681 SW.mode(PullUp);
tim004 0:36537c240b95 682 lcd.init();
tim004 0:36537c240b95 683 lcd.setXYAddress(0,0);
tim004 0:36537c240b95 684 PlayerBody(42,43);
tim004 0:36537c240b95 685 t.attach(&callPlayer,0.1);
tim004 0:36537c240b95 686 while(1)
tim004 0:36537c240b95 687 {
tim004 0:36537c240b95 688 switch(whatIsWorkingFlag)
tim004 0:36537c240b95 689 {
tim004 0:36537c240b95 690 case 0:
tim004 0:36537c240b95 691 paintMenu();
tim004 0:36537c240b95 692 break;
tim004 0:36537c240b95 693 case 1:
tim004 0:36537c240b95 694 paintDifficulty();
tim004 0:36537c240b95 695 break;
tim004 0:36537c240b95 696 case 2:
tim004 0:36537c240b95 697 paintHighScores();
tim004 0:36537c240b95 698 break;
tim004 0:36537c240b95 699 case 3:
tim004 0:36537c240b95 700 startGame();
tim004 0:36537c240b95 701 break;
tim004 0:36537c240b95 702 }
tim004 0:36537c240b95 703 }
tim004 0:36537c240b95 704 }