Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:a5f6ba8c378e, committed 2014-06-05
- Comitter:
- Vato
- Date:
- Thu Jun 05 17:37:09 2014 +0000
- Commit message:
- PAI Pong v1.0
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Loptica.cpp Thu Jun 05 17:37:09 2014 +0000 @@ -0,0 +1,61 @@ +#include "Loptica.h" + +void Loptica::setX(int broj) +{ + x = broj; +} + +void Loptica::setY(int broj) +{ + y = broj; +} + +int Loptica::dajX() +{ + return x; +} + +int Loptica::dajY() +{ + return y; +} + +int Loptica::dajKorakX() +{ + return x_korak; +} + +int Loptica::dajKorakY() +{ + return y_korak; +} + +void Loptica::postaviKorak(int xKorak, int yKorak) +{ + x_korak = xKorak; + y_korak = yKorak; +} + +void Loptica::crtajLopticu(N5110 &display) +{ + for(int i = -1; i < 2; i++) + for(int j = -1; j < 2; j++) + display.setPixel(x + j, y + i); + + display.refresh(); +} + +void Loptica::brisiLopticu(N5110 &display) +{ + for(int i = -1; i < 2; i++) + for(int j = -1; j < 2; j++) + display.clearPixel(x + j, y + i); + + display.refresh(); +} + +void Loptica::osvjeziPoziciju() +{ + x += x_korak; + y += y_korak; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Loptica.h Thu Jun 05 17:37:09 2014 +0000 @@ -0,0 +1,29 @@ +#ifndef LOPTICA_H +#define REKET_H + +#include "mbed.h" +#include "N5110.h" + +class Loptica { + int x, y; + int x_korak, y_korak; +public: + Loptica(int x1, int y1, int x1_korak, int y1_korak) : x(x1), y(y1), x_korak(x1_korak), y_korak(y1_korak) {} + + void setX(int broj); + void setY(int broj); + + int dajX(); + int dajY(); + + int dajKorakX(); + int dajKorakY(); + + void postaviKorak(int xKorak, int yKorak); + + void crtajLopticu(N5110 &display); + void brisiLopticu(N5110 &display); + void osvjeziPoziciju(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/N5110.lib Thu Jun 05 17:37:09 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/eencae/code/N5110/#adb79338d40f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Reket.cpp Thu Jun 05 17:37:09 2014 +0000 @@ -0,0 +1,67 @@ +#include "Reket.h" + +int Reket::dajX1() +{ + return x1; +} + +int Reket::dajX2() +{ + return x2; +} + +int Reket::dajY1() +{ + return y1; +} + +int Reket::dajY2() +{ + return y2; +} + +void Reket::crtajReket(N5110 &display) +{ + for(int i = y1; i <= y2; i++) + { + display.setPixel(x1, i); + display.setPixel(x2, i); + } + + display.refresh(); +} + +void Reket::brisiReket(N5110 &display) +{ + for(int i = y1; i <= y2; i++) + { + display.clearPixel(x1, i); + display.clearPixel(x2, i); + } + + display.refresh(); +} + +void Reket::osvjeziPoziciju(AnalogIn &p1) +{ + if (p1 < 1.0/3.0) + { + y1 -= 2; + y2 -= 2; + if(y1 < 0 or y2 < 7){ + y1 = 0; + y2 = 7; + } + } + + if (p1 > 2.0/3.0) + { + y1 += 2; + y2 += 2; + if(y2 > 47 or y1 > 40) + { + y2 = 47; + y1 = 40; + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Reket.h Thu Jun 05 17:37:09 2014 +0000 @@ -0,0 +1,22 @@ +#ifndef REKET_H +#define REKET_H + +#include "mbed.h" +#include "N5110.h" + +class Reket { + int x1, x2, y1, y2; +public: + Reket(int x_1, int x_2, int y_1, int y_2) : x1(x_1), x2(x_2), y1(y_1), y2(y_2) {} + + int dajX1(); + int dajX2(); + int dajY1(); + int dajY2(); + + void crtajReket(N5110 &display); + void brisiReket(N5110 &display); + void osvjeziPoziciju(AnalogIn &p1); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jun 05 17:37:09 2014 +0000 @@ -0,0 +1,332 @@ +#include "mbed.h" +#include "N5110.h" +#include "Reket.h" +#include "Loptica.h" + +Reket rLijevi (2, 3, 21, 28); +Reket rDesni (80, 81, 21, 28); +Loptica loptica (41, 12, 2, 1); + +int p1_skor = 0; +int p2_skor = 0; + +int zaustaviX, zaustaviY; + +N5110 display(dp4,dp24,dp23,dp25,dp2,dp6,dp18); +DigitalOut enable (dp14); + +AnalogIn P1(dp9); +AnalogIn P2(dp10); + +InterruptIn taster(dp1); +Ticker t; +Timer dbnc; +Timer dvostruki; + +bool pauza = true; +bool poen = false; +bool reset = false; + +void Lcifre(int cifra) +{ + if(cifra == 0) + { + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 1); + for(int i = 0; i < 5; i++) + display.setPixel(34, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 5); + for(int i = 0; i < 5; i++) + display.setPixel(31, 1 + i); + } + else if(cifra == 1) + { + for(int i = 0; i < 5; i++) + display.setPixel(34, 1 + i); + } + else if(cifra == 2) + { + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 1); + for(int i = 0; i < 3; i++) + display.setPixel(34, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 3); + for(int i = 0; i < 3; i++) + display.setPixel(31, 3 + i); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 5); + } + else if(cifra == 3) + { + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 1); + for(int i = 0; i < 5; i++) + display.setPixel(34, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 3); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 5); + } + else if(cifra == 4) + { + for(int i = 0; i < 3; i++) + display.setPixel(31, 1 + i); + for(int i = 0; i < 5; i++) + display.setPixel(34, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 3); + } + else if(cifra == 5) + { + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 1); + for(int i = 0; i < 3; i++) + display.setPixel(31, i + 1); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 3); + for(int i = 0; i < 3; i++) + display.setPixel(34, i + 3); + for(int i = 0; i < 4; i++) + display.setPixel(31 + i, 5); + } + display.refresh(); +} + +void Rcifre(int cifra) +{ + if(cifra == 0) + { + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 1); + for(int i = 0; i < 5; i++) + display.setPixel(48, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 5); + for(int i = 0; i < 5; i++) + display.setPixel(51, 1 + i); + } + else if(cifra == 1) + { + for(int i = 0; i < 5; i++) + display.setPixel(48, 1 + i); + } + else if(cifra == 2) + { + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 1); + for(int i = 0; i < 3; i++) + display.setPixel(51, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 3); + for(int i = 0; i < 3; i++) + display.setPixel(48, 3 + i); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 5); + } + else if(cifra == 3) + { + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 1); + for(int i = 0; i < 5; i++) + display.setPixel(51, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 3); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 5); + } + else if(cifra == 4) + { + for(int i = 0; i < 3; i++) + display.setPixel(48, 1 + i); + for(int i = 0; i < 5; i++) + display.setPixel(51, 1 + i); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 3); + } + else if(cifra == 5) + { + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 1); + for(int i = 0; i < 3; i++) + display.setPixel(48, i + 1); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 3); + for(int i = 0; i < 3; i++) + display.setPixel(51, i + 3); + for(int i = 0; i < 4; i++) + display.setPixel(48 + i, 5); + } + display.refresh(); +} + +void Lbrisi() +{ + for(int i = 0; i < 4; i++) + display.clearPixel(31 + i, 1); + for(int i = 0; i < 5; i++) + display.clearPixel(34, 1 + i); + for(int i = 0; i < 4; i++) + display.clearPixel(31 + i, 3); + for(int i = 0; i < 4; i++) + display.clearPixel(31 + i, 5); + for(int i = 0; i < 5; i++) + display.clearPixel(31, 1 + i); + display.refresh(); +} + +void Rbrisi() +{ + for(int i = 0; i < 4; i++) + display.clearPixel(48 + i, 1); + for(int i = 0; i < 5; i++) + display.clearPixel(51, 1 + i); + for(int i = 0; i < 4; i++) + display.clearPixel(48 + i, 3); + for(int i = 0; i < 4; i++) + display.clearPixel(48 + i, 5); + for(int i = 0; i < 5; i++) + display.clearPixel(48, 1 + i); + display.refresh(); +} + +void igraj() +{ + if(poen) return; + + for(int i = 0; i < 47; i++) + display.setPixel(41, i); + display.refresh(); + + rLijevi.brisiReket(display); + if(!pauza) + rLijevi.osvjeziPoziciju(P1); + rLijevi.crtajReket(display); + + rDesni.brisiReket(display); + if(!pauza) + rDesni.osvjeziPoziciju(P2); + rDesni.crtajReket(display); + + loptica.brisiLopticu(display); + if(!pauza) + loptica.osvjeziPoziciju(); + loptica.crtajLopticu(display); + + if(loptica.dajX() <= 5) + { + if(loptica.dajY() == rLijevi.dajY1() or loptica.dajY() == rLijevi.dajY1() + 1) + loptica.postaviKorak(2, -2); + else if (loptica.dajY() == rLijevi.dajY1() + 2 or loptica.dajY() == rLijevi.dajY1() + 3) + loptica.postaviKorak(2, -1); + else if (loptica.dajY() == rLijevi.dajY1() + 4) + loptica.postaviKorak(2, 0); + else if (loptica.dajY() == rLijevi.dajY1() + 5 or loptica.dajY() == rLijevi.dajY1() + 6) + loptica.postaviKorak(2, 1); + else if (loptica.dajY() == rLijevi.dajY1() + 7 or loptica.dajY() == rLijevi.dajY1() + 8) + loptica.postaviKorak(2, 2); + } + + if(loptica.dajX() >= 0 and loptica.dajX() < 2) + { + if((loptica.dajY() < rLijevi.dajY1() - 1) or (loptica.dajY() > rLijevi.dajY2() + 1)) + { + p2_skor++; + loptica.brisiLopticu(display); + loptica.setX(41); + loptica.setY(12); + loptica.postaviKorak(2, 1); + if(p2_skor == 5) + poen = true; + else return; + } + } + + if(loptica.dajX() >= 78) + { + if(loptica.dajY() == rDesni.dajY1() or loptica.dajY() == rDesni.dajY1() + 1) + loptica.postaviKorak(-2, -2); + else if(loptica.dajY() == rDesni.dajY1() + 2 or loptica.dajY() == rDesni.dajY1() + 3) + loptica.postaviKorak(-2, -1); + else if(loptica.dajY() == rDesni.dajY1() + 4) + loptica.postaviKorak(-2, 0); + else if(loptica.dajY() == rDesni.dajY1() + 5 or loptica.dajY() == rDesni.dajY1() + 6) + loptica.postaviKorak(-2, 1); + else if(loptica.dajY() == rDesni.dajY1() + 7 or loptica.dajY() == rDesni.dajY1() + 8) + loptica.postaviKorak(-2, 2); + } + + if(loptica.dajX() >= 81 and loptica.dajX() <= 83) + { + if((loptica.dajY() < rDesni.dajY1() - 1) or (loptica.dajY() > rDesni.dajY2() + 1)) + { + p1_skor++; + loptica.brisiLopticu(display); + loptica.setX(41); + loptica.setY(12); + loptica.postaviKorak(-2, 1); + if(p1_skor == 5) + poen = true; + else return; + } + } + + if(loptica.dajY() <= 1 or loptica.dajY() >= 46) + loptica.postaviKorak(loptica.dajKorakX(), loptica.dajKorakY() * (-1)); + + Lbrisi(); + Rbrisi(); + Lcifre(p1_skor); + Rcifre(p2_skor); + + if(poen) + { + loptica.setX(41); + loptica.setY(12); + loptica.postaviKorak(0, 0); + if(p1_skor == 5) display.printString("P1WINS", 2, 2); + if(p2_skor == 5) display.printString("P2 WINS", 43, 2); + } + + if(reset) + { + loptica.setX(41); + loptica.setY(12); + loptica.postaviKorak(-2, 1); + display.clear(); + reset = false; + poen = false; + p1_skor = 0; + p2_skor = 0; + } +} + +void pauzaStartRestart() +{ + if(dbnc.read_ms() > 200 and dvostruki.read_ms() > 800) + { + dbnc.reset(); + dvostruki.reset(); + pauza = !pauza; + } + + if(dvostruki.read_ms() < 800 and dbnc.read_ms() > 200) + { + reset = true; + poen = false; + dbnc.reset(); + dvostruki.reset(); + } +} + +int main() { + enable = 1; + display.init(); + display.setXYAddress(0, 0); + taster.rise(&pauzaStartRestart); + t.attach(&igraj, 0.08); + dbnc.start(); + dvostruki.start(); + while(1) {} +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jun 05 17:37:09 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776 \ No newline at end of file