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.
Dependencies: N5110_Double_Snake mbed
Diff: main.cpp
- Revision:
- 0:f518d387c29e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Jul 05 22:07:09 2018 +0000
@@ -0,0 +1,317 @@
+#include "mbed.h"
+#include "string"
+#include "mbed.h"
+#include "N5110.h"
+#define dp23 P0_0
+
+AnalogIn q(dp9);
+AnalogIn w(dp10);
+AnalogIn ax2(dp11);
+AnalogIn ay2(dp13);
+N5110 lcd(dp17,dp24,dp23,dp25,dp2,dp6,dp18);
+DigitalOut en(dp14);
+AnalogIn rand2(dp4);
+DigitalIn taster(dp1);
+
+using namespace std;
+bool gameOver,pomocna;
+double t;
+const int width = 84;
+const int height = 48;
+int x, y, x2, y2, voceX, voceY, score,voceX2,voceY2;
+int xp,yp;
+int repX[100], repY[100];
+int repX2[100], repY2[100];
+int n,m;
+enum smjer { STOP = 0, LIJEVO, DESNO, GORE, DOLE};
+smjer dir,dir2;
+bool prom=false;
+bool prelom=false;
+void Postavka()
+{
+
+ lcd.init();
+ lcd.normalMode();
+ lcd.setBrightness(0.5);
+ gameOver = false;
+ dir = STOP;
+ dir2=STOP;
+ x = width / 3;
+ y = height / 2;
+ x2= 2*width/3;
+ y2=y;
+ srand (time(NULL));
+ voceX = rand()%83;
+ voceY = rand()%38;
+ voceX2 = int(rand2*rand())%83;
+ voceY2 = int(rand2*rand())%38;
+ score = 0;
+ m=0;
+ n=0;
+ t=0;
+ pomocna=true;
+ lcd.printString("DOUBLE SNAKE", 8,1);
+ lcd.printString("Press button", 5,3);
+ lcd.printString("to start", 14,4);
+ while(!taster){}
+}
+void Ispis()
+{
+ lcd.clear();
+ if(score==100 and pomocna==true){ lcd.inverseMode(); pomocna=false;}
+ lcd.drawRect(0,0,83,38,0);
+
+
+ for (int i = 0; i < height; i++)
+ {
+ for (int j = 0; j < width; j++)
+ {
+
+ if (i == y && j == x)
+ lcd.drawCircle(x,y,3,1);
+ else if (i == voceY && j == voceX)
+ lcd.drawCircle(j,i,1,1);
+ else
+ {
+
+ for (int k = 0; k < n; k++)
+ {
+ if (repX[k] == j && repY[k] == i)
+ {
+ lcd.drawCircle(j,i,2,0);
+
+ }
+ }
+
+ }
+
+ }
+ }
+
+
+ for (int i = 0; i < height; i++)
+ {
+ for (int j = 0; j < width; j++)
+ {
+
+ if (i == y2 && j == x2)
+ lcd.drawCircle(x2,y2,3,0);
+ else if (i == voceY2 && j == voceX2)
+ lcd.drawCircle(j,i,1,0);
+ else
+ {
+
+ for (int k = 0; k < m; k++)
+ {
+ if (repX2[k] == j && repY2[k] == i)
+ {
+ lcd.drawCircle(j,i,2,1);
+
+ }
+ }
+
+ }
+
+ }
+ }
+
+
+
+ char s[11];
+ sprintf(s,"Score: %d",score);
+ lcd.printString(s,0 ,5);
+ if(t>-0.2)
+ wait(0.2+t);
+ else wait(0.05);
+ lcd.refresh();
+}
+void Ucitaj()
+{
+
+ if(q<1/3. and dir!=DESNO){
+ dir = LIJEVO;
+ }
+ else if(q>2/3. and dir!=LIJEVO){
+ dir = DESNO;
+ }
+ else if(w<1/3. and dir!=DOLE){
+ dir = GORE;
+ }
+ else if(w>2/3. and dir!=GORE){
+ dir = DOLE;
+ }
+
+ if(ax2<1/3. and dir2!=DESNO){
+ dir2 = LIJEVO;
+ }
+ else if(ax2>2/3. and dir2!=LIJEVO){
+ dir2 = DESNO;
+ }
+ else if(ay2<1/3. and dir2!=DOLE){
+ dir2 = GORE;
+ }
+ else if(ay2>2/3. and dir2!=GORE){
+ dir2 = DOLE;
+ }
+
+
+
+
+
+}
+void Igra()
+{
+ for (int i = 0; i < m; i++)
+ if ( (repX2[i] == x || repX2[i]==x-1 || repX2[i]==x+1 || repX2[i]==x-2 || repX2[i]==x+2) &&( repY2[i]==y || repY2[i]==y-1 || repY2[i]==y+1 || repY2[i]==y-2 || repY2[i]==y+2 ) )
+ gameOver = true;
+
+ for (int i = 0; i < n; i++)
+ if ( (repX[i] == x2 || repX[i]==x2-1 || repX[i]==x2+1 || repX[i]==x2-2 || repX[i]==x2+2) && ( repY[i]==y2 || repY[i]==y2-1 || repY[i]==y2+1 || repY[i]==y2-2 || repY[i]==y2+2 ) )
+ gameOver = true;
+ if ((x == x2 or x==x2-1 or x==x2+1 or x==x2+2 or x==x2-2) && (y==y2 or y==y2-1 or y==y2+1 or y==y2-2 or y==y2+2))
+ gameOver=true;
+ int prosliX = repX[0];
+ int prosliY = repY[0];
+ int prosliX2, prosliY2;
+ repX[0] = x;
+ repY[0] = y;
+
+ for (int i = 1; i < n; i++)
+ {
+ prosliX2 = repX[i];
+ prosliY2 = repY[i];
+
+ repX[i] = prosliX;
+ repY[i] = prosliY;
+
+ prosliX = prosliX2;
+ prosliY = prosliY2;
+ }
+ switch (dir)
+ {
+ case LIJEVO:
+ x+=3;
+ break;
+ case DESNO:
+ x-=3;
+ break;
+ case GORE:
+ y-=3;
+ break;
+ case DOLE:
+ y+=3;
+ break;
+ default:
+ break;
+ }
+
+ if (x >= 83) x = 0; else if (x < 0) x = 83 - 1;
+ if (y >= 38) y = 0; else if (y < 0) y = 38 - 1;
+
+ for (int i = 0; i < n; i++)
+ if (repX[i] == x && repY[i] == y)
+ gameOver = true;
+
+
+
+
+
+ if ((x == voceX or x==voceX-1 or x==voceX+1) && (y==voceY or y==voceY-1 or y==voceY+1))
+ {
+ score += 10;
+ if(score%50==0) t-=0.05;
+ voceX = rand() % 83;
+ voceY = rand() % 38;
+ n++;
+ }
+
+
+
+
+ prosliX = repX2[0];
+ prosliY = repY2[0];
+
+ repX2[0] = x2;
+ repY2[0] = y2;
+
+ for (int i = 1; i < m; i++)
+ {
+ prosliX2 = repX2[i];
+ prosliY2 = repY2[i];
+
+ repX2[i] = prosliX;
+ repY2[i] = prosliY;
+
+ prosliX = prosliX2;
+ prosliY = prosliY2;
+ }
+
+
+
+
+ switch (dir2)
+ {
+ case LIJEVO:
+ x2+=3;
+ break;
+ case DESNO:
+ x2-=3;
+ break;
+ case GORE:
+ y2-=3;
+ break;
+ case DOLE:
+ y2+=3;
+ break;
+ default:
+ break;
+ }
+
+ if (x2 >= 83) x2 = 0; else if (x2 < 0) x2 = 83 - 1;
+ if (y2 >= 38) y2 = 0; else if (y2 < 0) y2 = 38 - 1;
+
+ for (int i = 0; i < m; i++)
+ if (repX2[i] == x2 && repY2[i] == y2)
+ gameOver = true;
+
+
+ if ((x2 == voceX2 or x2==voceX2-1 or x2==voceX2+1) && (y2==voceY2 or y2==voceY2-1 or y2==voceY2+1))
+ {
+ score += 10;
+ if(score%50==0) t-=0.05;
+ voceX2 =int(rand2* rand()) % 83;
+ voceY2 = int(rand2* rand()) % 38;
+ m++;
+ }
+ if (score==140) lcd.normalMode();
+
+
+}
+int main()
+{
+ en=1;
+while(1){
+ Postavka();
+ while (!gameOver)
+ {
+ Ispis();
+
+ Ucitaj();
+ Igra();
+
+ }
+ lcd.clear();
+ lcd.refresh();
+ lcd.printString("Game over ", 15,0);
+ char s[11];
+ sprintf(s,"Score:%d",score);
+ lcd.printString("Press button", 8,4);
+ lcd.printString("to continue", 10,5);
+ lcd.printString(s,20 ,2);
+ while(!taster){};
+ lcd.clear();
+ lcd.refresh();
+ wait(0.5);
+ }
+ return 0;
+}
\ No newline at end of file