Starwars Game
You are a x-wing shooting at the deathstar. I
Components and supplies
1
Solderless Breadboard Full Size
1
0.96 128x64 OLED display
1
Arduino Nano R3
1
Buzzer
1
Jumper wires (generic)
Apps and platforms
1
Arduino Web Editor
Project description
Code
STARWARS GAME
arduino
1 2#include <SPI.h> 3#include <Wire.h> 4#include <Adafruit_GFX.h> 5#include <Adafruit_SSD1306.h> 6#include <Fonts/FreeSans9pt7b.h> 7//#include <Fonts/FreeSans12pt7b.h> 8 9#define OLED_RESET 4 10Adafruit_SSD1306 display(OLED_RESET); 11 12 13 14 15 16 17 18const int c = 261; 19const int d = 294; 20const int e = 329; 21const int f = 349; 22const int g = 391; 23const int gS = 415; 24const int a = 440; 25const int aS = 455; 26const int b = 466; 27const int cH = 523; 28const int cSH = 554; 29const int dH = 587; 30const int dSH = 622; 31const int eH = 659; 32const int fH = 698; 33const int fSH = 740; 34const int gH = 784; 35const int gSH = 830; 36const int aH = 880; 37 38const unsigned char PROGMEM dioda16 [] = { 39 400x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x3F, 0xF0, 0x3C, 0x00, 0x3C, 0x00, 0xFF, 0x00, 0x7F, 0xFF, 410x7F, 0xFF, 0xFF, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x1F, 0xF0, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00 42 43}; 44 45const unsigned char PROGMEM storm [] = { 460x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 470x00, 0x00, 0x00, 0x00, 0x7F, 0xFE, 0x00, 0x00, 0x00, 0x07, 0x80, 0x01, 0xE0, 0x00, 0x00, 0x0C, 480x00, 0x00, 0x20, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00, 0x30, 0x00, 0x00, 0x04, 0x00, 490x00, 0x20, 0x00, 0x00, 0x04, 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x00, 0x00, 0x60, 0x00, 0x00, 500x02, 0x00, 0x00, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40, 510x00, 0x00, 0x01, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7F, 0xE0, 0x00, 0x01, 0x00, 520x00, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xD7, 0xFF, 0xFF, 530xE1, 0x00, 0x01, 0xBF, 0xFC, 0x1F, 0xFA, 0x80, 0x01, 0xBF, 0xF1, 0xCF, 0xFA, 0x80, 0x01, 0x3F, 540xC2, 0x37, 0xF7, 0x80, 0x01, 0xEF, 0x9C, 0x01, 0xE7, 0xC0, 0x01, 0xE0, 0x70, 0x06, 0x06, 0x80, 550x01, 0xE0, 0xC0, 0x03, 0x06, 0x80, 0x01, 0xFF, 0x80, 0x01, 0xFF, 0x80, 0x01, 0xF8, 0x00, 0x00, 560x1D, 0xC0, 0x03, 0x70, 0x00, 0x80, 0x0C, 0x60, 0x05, 0xB0, 0x07, 0xF0, 0x08, 0x90, 0x09, 0x10, 570x1F, 0xF8, 0x09, 0xD0, 0x0B, 0x90, 0x1F, 0x7C, 0x03, 0xF0, 0x0F, 0xC0, 0xFC, 0x0F, 0x07, 0x90, 580x0D, 0x43, 0xC0, 0x03, 0x07, 0x90, 0x05, 0x64, 0x00, 0x00, 0xCF, 0x10, 0x07, 0xFC, 0x00, 0x00, 590x26, 0x10, 0x01, 0x80, 0x00, 0x00, 0x10, 0x20, 0x01, 0x00, 0x00, 0x00, 0x0E, 0x40, 0x01, 0x80, 600x07, 0xF0, 0x01, 0x80, 0x00, 0x80, 0x07, 0xC8, 0x00, 0x80, 0x00, 0x80, 0x0B, 0xE8, 0x00, 0x80, 610x00, 0x87, 0x97, 0xE9, 0xE0, 0x80, 0x00, 0x87, 0xDF, 0xEF, 0xA0, 0x80, 0x00, 0x4B, 0xFF, 0xFF, 620xA0, 0x80, 0x00, 0x6B, 0xDF, 0xFB, 0xA3, 0x00, 0x00, 0x24, 0x97, 0xE8, 0x24, 0x00, 0x00, 0x1E, 630x1F, 0xC0, 0x2C, 0x00, 0x00, 0x07, 0xF8, 0x1F, 0xF0, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x00, 0x00 64}; 65 66 67 68void setup() { 69 70 71 pinMode(3,INPUT_PULLUP); 72 pinMode(12,INPUT_PULLUP); 73 pinMode(11,INPUT_PULLUP); 74 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 75 display.display(); 76 display.clearDisplay(); 77display.setTextSize(0); 78 display.drawBitmap(6, 11,storm, 48,48, 1); 79 display.setFont(&FreeSans9pt7b); 80 display.setTextColor(WHITE); 81 display.setCursor(65,14); 82 display.println("X-Wing"); 83 display.setFont(); 84 display.setCursor(65,17); 85 display.setTextSize(0); 86 display.println("vs"); 87 display.setCursor(0,0); 88 float voltaza=readVcc()/1000; 89 display.println(voltaza);; 90 display.setFont(&FreeSans9pt7b); 91 display.setCursor(65,39); 92 display.println("Death"); 93 display.setFont(); 94 display.setCursor(65,42); 95 display.println("star "); 96 display.setTextSize(0); 97 98 display.setCursor(65,55); 99 100 display.println("By Ian"); 101 102 display.setCursor(65,20); 103 104 display.display(); 105 106 107 108display.setFont(); 109 beep(a, 500); 110 beep(a, 500); 111 beep(a, 500); 112 beep(f, 350); 113 beep(cH, 150); 114 beep(a, 500); 115 beep(f, 350); 116 beep(cH, 150); 117 beep(a, 650); 118 119 delay(500); 120 121 122 123 delay(500); 124} 125int metx=0; 126int mety=0; 127int postoji=0; 128int nep=8; 129int smjer=0; 130int go=0; 131int rx=95; 132int ry=0; 133int rx2=95; 134int ry2=0; 135int rx3=95; 136int ry3=0; 137int bodovi=0; 138 139int brzina=3; //speed of bullet 140int bkugle=1; 141int najmanja=600; 142int najveca=1200; 143int promjer=10; 144 145int rx4=95; 146int ry4=0; 147int zivoti=5; 148int poc=0; 149int ispaljeno=0; 150int nivo=1; 151int centar=95; 152unsigned long pocetno=0; 153unsigned long odabrano=0; 154unsigned long trenutno=0; 155unsigned long nivovrije=0; 156int poz=30; 157void loop() { 158 159 if(go==0){ 160 display.clearDisplay(); 161 162display.drawPixel(50,30,1); 163display.drawPixel(30,17,1); 164display.drawPixel(60,18,1); 165display.drawPixel(55,16,1); 166display.drawPixel(25,43,1); 167 display.drawPixel(100,43,1); 168display.drawPixel(117,52,1); 169display.drawPixel(14,49,1); 170display.drawPixel(24,24,1); 171display.drawPixel(78,36,1); 172display.drawPixel(80,57,1); 173display.drawPixel(107,11,1); 174display.drawPixel(150,11,1); 175display.drawPixel(5,5,1); 176display.drawPixel(8,7,1); 177display.drawPixel(70,12,1); 178display.drawPixel(10,56,1); 179display.drawPixel(70,25,1); 180 181 182 183 if(poc==0){ 184 pocetno=millis(); 185 odabrano= random(400,1200); 186 poc=1; 187 } 188 trenutno=millis(); 189 190 191//nivoi 192 193if((trenutno-nivovrije)>50000) 194{ 195 nivovrije=trenutno; 196 nivo=nivo+1; 197 198 brzina=brzina+1; //brizna neprijateljevog metka 199 if ( nivo % 2 == 0 ) 200 { 201 bkugle=bkugle+1; 202 promjer=promjer-1; 203 } 204najmanja=najmanja-50; 205najveca=najveca-50; 206 207 } 208 209 210 211 if((odabrano+pocetno)<trenutno) 212 { 213 poc=0; 214 ispaljeno=ispaljeno+1; 215 if(ispaljeno==1) 216 {rx=95; 217 ry=nep;} 218 if(ispaljeno==2){ 219 rx2=95; 220 ry2=nep;} 221 if(ispaljeno==3) 222 { 223 rx3=95; 224 ry3=nep; 225 } 226 227 228 if(ispaljeno==4){ 229 rx4=95; 230 ry4=nep; 231 232 } 233 234 235 236 } 237 238 239 if(ispaljeno>0) 240 { 241 display.drawCircle(rx,ry,2,1); 242 rx=rx-brzina;} 243 244 if(ispaljeno>1) 245 { 246 display.drawCircle(rx2,ry2,1,1); 247 rx2=rx2-brzina;} 248 249 if(ispaljeno>2) 250 { 251 display.drawCircle(rx3,ry3,4,1); 252 rx3=rx3-brzina;} 253 254 if(ispaljeno>3) 255 { 256 display.drawCircle(rx4,ry4,2,1); 257 rx4=rx4-brzina;} 258 259 if(digitalRead(12)==0 && poz>=2){ 260 poz=poz-2;} 261 262 if(digitalRead(11)==0&& poz<=46){ 263 poz=poz+2;} 264 265 266 if(digitalRead(3)==0 && postoji==0) 267 { 268 postoji=1; 269 metx=6; 270 mety=poz+8; 271 tone(9,1200,20); 272 273 274 } 275 if(postoji==1) 276 277 { 278 metx=metx+8 ; 279 280 display.drawLine(metx,mety,metx+4,mety,1); 281 } 282 283 display.drawBitmap(4, poz,dioda16, 16,16, 1); 284 display.fillCircle(centar,nep,promjer,1); 285 display.fillCircle(centar+2,nep+3,promjer/3,0); 286 287 display.setTextSize(1); 288 display.setTextColor(WHITE); 289 display.setCursor(33,57); 290 display.println("SCORE:"); 291 display.setCursor(68,57); 292 display.println(bodovi); 293 294 display.setCursor(33,0); 295 display.println("HEALTH:"); 296 display.setCursor(68,0); 297 display.println(zivoti); 298 299 display.setCursor(110,0); 300 display.println("L:"); 301 302 display.setCursor(122,0); 303 display.println(nivo); 304 305 display.setCursor(108,57); 306 display.println(trenutno/1000); 307 display.display(); 308 309 if(metx>128)postoji=0; 310 311 312 313 if(smjer==0){ 314 nep=nep+bkugle;} 315 else 316 {nep=nep-bkugle;} 317 318 if(nep>=(64-promjer)) 319 smjer=1; 320 if(nep<=promjer) 321 smjer=0; 322 323 if(mety>=nep-promjer && mety<=nep+promjer) 324 if(metx>(centar-promjer)&&metx<(centar+promjer)) 325 { 326 metx=-20; 327 tone(9,500,20); 328 bodovi=bodovi+1; 329 postoji=0; 330 } 331 332int pozicija=poz+8; 333 if(ry>=pozicija-8 && ry<=pozicija+8) 334 if(rx<12&&rx>4) 335 { 336 rx=95; 337 ry=-50; 338 tone(9,100,100); 339 zivoti=zivoti-1; 340 } 341 342 if(ry2>=pozicija-8 && ry2<=pozicija+8) 343 if(rx2<12&&rx2>4) 344 { 345 rx2=-50; 346 ry2=-50; 347 tone(9,100,100); 348 zivoti=zivoti-1; 349 } 350 351 if(ry3>=pozicija-8 && ry3<=pozicija+8) 352 if(rx3<12&&rx3>4) 353 { 354 rx3=-50; 355 ry3=-50; 356 tone(9,100,100); 357 zivoti=zivoti-1; 358 } 359 360 if(ry4>=pozicija-8 && ry4<=pozicija+8) 361 if(rx4<12&&rx4>4) 362 { 363 rx4=200; 364 ry4=-50; 365 ispaljeno=0; 366 tone(9,100,100); 367 zivoti=zivoti-1; 368 } 369 370if(rx4<1){ 371 ispaljeno=0; 372 rx4=200;} 373 374 if(zivoti==0) 375 go=1; 376 } 377 378 if(go==1) 379 { 380 if(zivoti==0){ 381 tone(9,200,300); 382 delay(300); 383 tone(9,250,200); 384 delay(200); 385 tone(9,300,300); 386 delay(300); 387 zivoti=5; 388 } 389 display.clearDisplay(); 390 display.setFont(); 391 display.setTextSize(2); 392 display.setTextColor(WHITE); 393 display.setCursor(7,10); 394 display.println("GAME OVER!"); 395 display.setTextSize(1); 396 display.setCursor(7,30); 397 display.println("SCORE:"); 398 display.setCursor(44,30); 399 display.println(bodovi); 400 display.setCursor(7,40); 401 display.println("level:"); 402 display.setCursor(44,40); 403 display.println(nivo); 404 display.setCursor(7,50); 405 display.println("TIME"); 406 display.setCursor(60,50); 407 408 display.println(trenutno/1000); 409 display.display(); 410 411 if(digitalRead(3)==0) 412 { 413 tone(9,280,300); 414 delay(300); 415 tone(9,250,200); 416 delay(200); 417 tone(9,370,300); 418 delay(300); 419 ponovo(); 420 } 421 } 422} 423 424void ponovo() 425{ 426 metx=0; 427 mety=0; 428postoji=0; 429 nep=8; 430 smjer=0; 431 go=0; 432 rx=95; 433 ry=0; 434 rx2=95; 435 ry2=0; 436rx3=95; 437ry3=0; 438bodovi=0; 439 440 brzina=3; //brizna neprijateljevog metka 441bkugle=1; 442 najmanja=600; 443 najveca=1200; 444 promjer=12; 445 446 rx4=95; 447ry4=0; 448zivoti=5; 449poc=0; 450 ispaljeno=0; 451nivo=1; 452 pocetno=0; 453 odabrano=0; 454 trenutno=0; 455nivovrije=0; 456 457 458 459 460 } 461 462long readVcc() { 463 // Read 1.1V reference against AVcc 464 // set the reference to Vcc and the measurement to the internal 1.1V reference 465 #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 466 ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 467 #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) 468 ADMUX = _BV(MUX5) | _BV(MUX0); 469 #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) 470 ADMUX = _BV(MUX3) | _BV(MUX2); 471 #else 472 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 473 #endif 474 475 delay(2); // Wait for Vref to settle 476 ADCSRA |= _BV(ADSC); // Start conversion 477 while (bit_is_set(ADCSRA,ADSC)); // measuring 478 479 uint8_t low = ADCL; // must read ADCL first - it then locks ADCH 480 uint8_t high = ADCH; // unlocks both 481 482 long result = (high<<8) | low; 483 484 result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 485 return result; // Vcc in millivolts 486} 487 488 489void beep(int note, int duration) 490{ 491 //Play tone on buzzerPin 492 tone(9, note, duration); 493 494 495 496 delay(duration); 497 498 499 500 noTone(9); 501 502 delay(50); 503 504 505} 506
STARWARS GAME
arduino
1 2#include <SPI.h> 3#include <Wire.h> 4#include <Adafruit_GFX.h> 5#include <Adafruit_SSD1306.h> 6#include <Fonts/FreeSans9pt7b.h> 7//#include <Fonts/FreeSans12pt7b.h> 8 9#define OLED_RESET 4 10Adafruit_SSD1306 display(OLED_RESET); 11 12 13 14 15 16 17 18const int c = 261; 19const int d = 294; 20const int e = 329; 21const int f = 349; 22const int g = 391; 23const int gS = 415; 24const int a = 440; 25const int aS = 455; 26const int b = 466; 27const int cH = 523; 28const int cSH = 554; 29const int dH = 587; 30const int dSH = 622; 31const int eH = 659; 32const int fH = 698; 33const int fSH = 740; 34const int gH = 784; 35const int gSH = 830; 36const int aH = 880; 37 38const unsigned char PROGMEM dioda16 [] = { 39 400x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x3F, 0xF0, 0x3C, 0x00, 0x3C, 0x00, 0xFF, 0x00, 0x7F, 0xFF, 410x7F, 0xFF, 0xFF, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x1F, 0xF0, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00 42 43}; 44 45const unsigned char PROGMEM storm [] = { 460x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 470x00, 0x00, 0x00, 0x00, 0x7F, 0xFE, 0x00, 0x00, 0x00, 0x07, 0x80, 0x01, 0xE0, 0x00, 0x00, 0x0C, 480x00, 0x00, 0x20, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00, 0x30, 0x00, 0x00, 0x04, 0x00, 490x00, 0x20, 0x00, 0x00, 0x04, 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x00, 0x00, 0x60, 0x00, 0x00, 500x02, 0x00, 0x00, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40, 510x00, 0x00, 0x01, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7F, 0xE0, 0x00, 0x01, 0x00, 520x00, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xD7, 0xFF, 0xFF, 530xE1, 0x00, 0x01, 0xBF, 0xFC, 0x1F, 0xFA, 0x80, 0x01, 0xBF, 0xF1, 0xCF, 0xFA, 0x80, 0x01, 0x3F, 540xC2, 0x37, 0xF7, 0x80, 0x01, 0xEF, 0x9C, 0x01, 0xE7, 0xC0, 0x01, 0xE0, 0x70, 0x06, 0x06, 0x80, 550x01, 0xE0, 0xC0, 0x03, 0x06, 0x80, 0x01, 0xFF, 0x80, 0x01, 0xFF, 0x80, 0x01, 0xF8, 0x00, 0x00, 560x1D, 0xC0, 0x03, 0x70, 0x00, 0x80, 0x0C, 0x60, 0x05, 0xB0, 0x07, 0xF0, 0x08, 0x90, 0x09, 0x10, 570x1F, 0xF8, 0x09, 0xD0, 0x0B, 0x90, 0x1F, 0x7C, 0x03, 0xF0, 0x0F, 0xC0, 0xFC, 0x0F, 0x07, 0x90, 580x0D, 0x43, 0xC0, 0x03, 0x07, 0x90, 0x05, 0x64, 0x00, 0x00, 0xCF, 0x10, 0x07, 0xFC, 0x00, 0x00, 590x26, 0x10, 0x01, 0x80, 0x00, 0x00, 0x10, 0x20, 0x01, 0x00, 0x00, 0x00, 0x0E, 0x40, 0x01, 0x80, 600x07, 0xF0, 0x01, 0x80, 0x00, 0x80, 0x07, 0xC8, 0x00, 0x80, 0x00, 0x80, 0x0B, 0xE8, 0x00, 0x80, 610x00, 0x87, 0x97, 0xE9, 0xE0, 0x80, 0x00, 0x87, 0xDF, 0xEF, 0xA0, 0x80, 0x00, 0x4B, 0xFF, 0xFF, 620xA0, 0x80, 0x00, 0x6B, 0xDF, 0xFB, 0xA3, 0x00, 0x00, 0x24, 0x97, 0xE8, 0x24, 0x00, 0x00, 0x1E, 630x1F, 0xC0, 0x2C, 0x00, 0x00, 0x07, 0xF8, 0x1F, 0xF0, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x00, 0x00 64}; 65 66 67 68void setup() { 69 70 71 pinMode(3,INPUT_PULLUP); 72 pinMode(12,INPUT_PULLUP); 73 pinMode(11,INPUT_PULLUP); 74 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 75 display.display(); 76 display.clearDisplay(); 77display.setTextSize(0); 78 display.drawBitmap(6, 11,storm, 48,48, 1); 79 display.setFont(&FreeSans9pt7b); 80 display.setTextColor(WHITE); 81 display.setCursor(65,14); 82 display.println("X-Wing"); 83 display.setFont(); 84 display.setCursor(65,17); 85 display.setTextSize(0); 86 display.println("vs"); 87 display.setCursor(0,0); 88 float voltaza=readVcc()/1000; 89 display.println(voltaza);; 90 display.setFont(&FreeSans9pt7b); 91 display.setCursor(65,39); 92 display.println("Death"); 93 display.setFont(); 94 display.setCursor(65,42); 95 display.println("star "); 96 display.setTextSize(0); 97 98 display.setCursor(65,55); 99 100 display.println("By Ian"); 101 102 display.setCursor(65,20); 103 104 display.display(); 105 106 107 108display.setFont(); 109 beep(a, 500); 110 beep(a, 500); 111 beep(a, 500); 112 beep(f, 350); 113 beep(cH, 150); 114 beep(a, 500); 115 beep(f, 350); 116 beep(cH, 150); 117 beep(a, 650); 118 119 delay(500); 120 121 122 123 delay(500); 124} 125int metx=0; 126int mety=0; 127int postoji=0; 128int nep=8; 129int smjer=0; 130int go=0; 131int rx=95; 132int ry=0; 133int rx2=95; 134int ry2=0; 135int rx3=95; 136int ry3=0; 137int bodovi=0; 138 139int brzina=3; //speed of bullet 140int bkugle=1; 141int najmanja=600; 142int najveca=1200; 143int promjer=10; 144 145int rx4=95; 146int ry4=0; 147int zivoti=5; 148int poc=0; 149int ispaljeno=0; 150int nivo=1; 151int centar=95; 152unsigned long pocetno=0; 153unsigned long odabrano=0; 154unsigned long trenutno=0; 155unsigned long nivovrije=0; 156int poz=30; 157void loop() { 158 159 if(go==0){ 160 display.clearDisplay(); 161 162display.drawPixel(50,30,1); 163display.drawPixel(30,17,1); 164display.drawPixel(60,18,1); 165display.drawPixel(55,16,1); 166display.drawPixel(25,43,1); 167 display.drawPixel(100,43,1); 168display.drawPixel(117,52,1); 169display.drawPixel(14,49,1); 170display.drawPixel(24,24,1); 171display.drawPixel(78,36,1); 172display.drawPixel(80,57,1); 173display.drawPixel(107,11,1); 174display.drawPixel(150,11,1); 175display.drawPixel(5,5,1); 176display.drawPixel(8,7,1); 177display.drawPixel(70,12,1); 178display.drawPixel(10,56,1); 179display.drawPixel(70,25,1); 180 181 182 183 if(poc==0){ 184 pocetno=millis(); 185 odabrano= random(400,1200); 186 poc=1; 187 } 188 trenutno=millis(); 189 190 191//nivoi 192 193if((trenutno-nivovrije)>50000) 194{ 195 nivovrije=trenutno; 196 nivo=nivo+1; 197 198 brzina=brzina+1; //brizna neprijateljevog metka 199 if ( nivo % 2 == 0 ) 200 { 201 bkugle=bkugle+1; 202 promjer=promjer-1; 203 } 204najmanja=najmanja-50; 205najveca=najveca-50; 206 207 } 208 209 210 211 if((odabrano+pocetno)<trenutno) 212 { 213 poc=0; 214 ispaljeno=ispaljeno+1; 215 if(ispaljeno==1) 216 {rx=95; 217 ry=nep;} 218 if(ispaljeno==2){ 219 rx2=95; 220 ry2=nep;} 221 if(ispaljeno==3) 222 { 223 rx3=95; 224 ry3=nep; 225 } 226 227 228 if(ispaljeno==4){ 229 rx4=95; 230 ry4=nep; 231 232 } 233 234 235 236 } 237 238 239 if(ispaljeno>0) 240 { 241 display.drawCircle(rx,ry,2,1); 242 rx=rx-brzina;} 243 244 if(ispaljeno>1) 245 { 246 display.drawCircle(rx2,ry2,1,1); 247 rx2=rx2-brzina;} 248 249 if(ispaljeno>2) 250 { 251 display.drawCircle(rx3,ry3,4,1); 252 rx3=rx3-brzina;} 253 254 if(ispaljeno>3) 255 { 256 display.drawCircle(rx4,ry4,2,1); 257 rx4=rx4-brzina;} 258 259 if(digitalRead(12)==0 && poz>=2){ 260 poz=poz-2;} 261 262 if(digitalRead(11)==0&& poz<=46){ 263 poz=poz+2;} 264 265 266 if(digitalRead(3)==0 && postoji==0) 267 { 268 postoji=1; 269 metx=6; 270 mety=poz+8; 271 tone(9,1200,20); 272 273 274 } 275 if(postoji==1) 276 277 { 278 metx=metx+8 ; 279 280 display.drawLine(metx,mety,metx+4,mety,1); 281 } 282 283 display.drawBitmap(4, poz,dioda16, 16,16, 1); 284 display.fillCircle(centar,nep,promjer,1); 285 display.fillCircle(centar+2,nep+3,promjer/3,0); 286 287 display.setTextSize(1); 288 display.setTextColor(WHITE); 289 display.setCursor(33,57); 290 display.println("SCORE:"); 291 display.setCursor(68,57); 292 display.println(bodovi); 293 294 display.setCursor(33,0); 295 display.println("HEALTH:"); 296 display.setCursor(68,0); 297 display.println(zivoti); 298 299 display.setCursor(110,0); 300 display.println("L:"); 301 302 display.setCursor(122,0); 303 display.println(nivo); 304 305 display.setCursor(108,57); 306 display.println(trenutno/1000); 307 display.display(); 308 309 if(metx>128)postoji=0; 310 311 312 313 if(smjer==0){ 314 nep=nep+bkugle;} 315 else 316 {nep=nep-bkugle;} 317 318 if(nep>=(64-promjer)) 319 smjer=1; 320 if(nep<=promjer) 321 smjer=0; 322 323 if(mety>=nep-promjer && mety<=nep+promjer) 324 if(metx>(centar-promjer)&&metx<(centar+promjer)) 325 { 326 metx=-20; 327 tone(9,500,20); 328 bodovi=bodovi+1; 329 postoji=0; 330 } 331 332int pozicija=poz+8; 333 if(ry>=pozicija-8 && ry<=pozicija+8) 334 if(rx<12&&rx>4) 335 { 336 rx=95; 337 ry=-50; 338 tone(9,100,100); 339 zivoti=zivoti-1; 340 } 341 342 if(ry2>=pozicija-8 && ry2<=pozicija+8) 343 if(rx2<12&&rx2>4) 344 { 345 rx2=-50; 346 ry2=-50; 347 tone(9,100,100); 348 zivoti=zivoti-1; 349 } 350 351 if(ry3>=pozicija-8 && ry3<=pozicija+8) 352 if(rx3<12&&rx3>4) 353 { 354 rx3=-50; 355 ry3=-50; 356 tone(9,100,100); 357 zivoti=zivoti-1; 358 } 359 360 if(ry4>=pozicija-8 && ry4<=pozicija+8) 361 if(rx4<12&&rx4>4) 362 { 363 rx4=200; 364 ry4=-50; 365 ispaljeno=0; 366 tone(9,100,100); 367 zivoti=zivoti-1; 368 } 369 370if(rx4<1){ 371 ispaljeno=0; 372 rx4=200;} 373 374 if(zivoti==0) 375 go=1; 376 } 377 378 if(go==1) 379 { 380 if(zivoti==0){ 381 tone(9,200,300); 382 delay(300); 383 tone(9,250,200); 384 delay(200); 385 tone(9,300,300); 386 delay(300); 387 zivoti=5; 388 } 389 display.clearDisplay(); 390 display.setFont(); 391 display.setTextSize(2); 392 display.setTextColor(WHITE); 393 display.setCursor(7,10); 394 display.println("GAME OVER!"); 395 display.setTextSize(1); 396 display.setCursor(7,30); 397 display.println("SCORE:"); 398 display.setCursor(44,30); 399 display.println(bodovi); 400 display.setCursor(7,40); 401 display.println("level:"); 402 display.setCursor(44,40); 403 display.println(nivo); 404 display.setCursor(7,50); 405 display.println("TIME"); 406 display.setCursor(60,50); 407 408 display.println(trenutno/1000); 409 display.display(); 410 411 if(digitalRead(3)==0) 412 { 413 tone(9,280,300); 414 delay(300); 415 tone(9,250,200); 416 delay(200); 417 tone(9,370,300); 418 delay(300); 419 ponovo(); 420 } 421 } 422} 423 424void ponovo() 425{ 426 metx=0; 427 mety=0; 428postoji=0; 429 nep=8; 430 smjer=0; 431 go=0; 432 rx=95; 433 ry=0; 434 rx2=95; 435 ry2=0; 436rx3=95; 437ry3=0; 438bodovi=0; 439 440 brzina=3; //brizna neprijateljevog metka 441bkugle=1; 442 najmanja=600; 443 najveca=1200; 444 promjer=12; 445 446 rx4=95; 447ry4=0; 448zivoti=5; 449poc=0; 450 ispaljeno=0; 451nivo=1; 452 pocetno=0; 453 odabrano=0; 454 trenutno=0; 455nivovrije=0; 456 457 458 459 460 } 461 462long readVcc() { 463 // Read 1.1V reference against AVcc 464 // set the reference to Vcc and the measurement to the internal 1.1V reference 465 #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 466 ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 467 #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) 468 ADMUX = _BV(MUX5) | _BV(MUX0); 469 #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) 470 ADMUX = _BV(MUX3) | _BV(MUX2); 471 #else 472 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 473 #endif 474 475 delay(2); // Wait for Vref to settle 476 ADCSRA |= _BV(ADSC); // Start conversion 477 while (bit_is_set(ADCSRA,ADSC)); // measuring 478 479 uint8_t low = ADCL; // must read ADCL first - it then locks ADCH 480 uint8_t high = ADCH; // unlocks both 481 482 long result = (high<<8) | low; 483 484 result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 485 return result; // Vcc in millivolts 486} 487 488 489void beep(int note, int duration) 490{ 491 //Play tone on buzzerPin 492 tone(9, note, duration); 493 494 495 496 delay(duration); 497 498 499 500 noTone(9); 501 502 delay(50); 503 504 505} 506
Documentation
screen libraries
you download the files. go into libraries and click import and select one file at a time
screen libraries
Comments
Only logged in users can leave comments