Skip to content

Instantly share code, notes, and snippets.

/SNAKE Secret

Created May 14, 2017 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/15f882cad030cd38f2dc140e8b271450 to your computer and use it in GitHub Desktop.
Save anonymous/15f882cad030cd38f2dc140e8b271450 to your computer and use it in GitHub Desktop.
SNAKE
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <ctime>
class OGON //wsp zebranych punktów
{
public:
int x = 60;
int y = 60;
};
int losuj(OGON tabl[], int *x, int *y) //funkcja losujaca wsp pojawienia sie "X"
{
*x = rand() % 49;
*y = rand() % 14;
for(int i = 0; i < 100; i++)
{
if( tabl[i].x != 60 && tabl[i].y != 60)
{
if( tabl[i].x == *x && tabl[i].y == *y )
*x = rand() % 49;
*y = rand() % 14;
}
else
{
break;
}
}
}
int main()
{
using namespace std;
int klawisz; //potrzebny do menu
char plansza[15][50];
int xx = 25, yy = 7; //wspólrzêdne glowy
int sx, sy; //OSTATNIE WSP G£OWY
int px, py; // wsp. wylosowanego punktu
int punkty = 0; // licznik punktów
int tryb = 56;
int ost = 56; // ostatni ustawiony tryb
OGON tabl[100];
/* TRYBY:
56 - góra
50 - dól
52 - lewo
54 - prawo
27 - esc - pauza
32 - spacja - wyjscie z menu
*/
srand( time(NULL));
losuj(tabl, &px, &py );
for(int y = 0; y <15 ; y++)
{
for(int x = 0; x<50 ; x++)
{
plansza[y][x] = ' ';
}
}
cout << "Witaj w Snake!\n"
"Sterowanie odbywa sie za pomoca strzlek na numpadzie.\n"
"Kliknij Spacje aby rozpoczac!\n"
"ESC to pauza ;)\n"
<< endl;
klawisz = getch();
while(1)
{
if(klawisz == 32)
break;
else
continue;
}
while(1) //GLÓWNA PETLA PROGRAMU
{
if(tryb == 27 )
{
goto jump;
}
{//WYLICZANIE WSP PUNKTÓW OGONA
if( punkty > 0 )
{
for( int i = punkty; i > 0; i-- )
{
if( (i - 1) == 0 )
{
tabl[ i - 1 ].x = sx;
tabl[ i - 1 ].y = sy;
break;
}
else
{
tabl[ i - 1 ].x = tabl[ i - 2 ].x;
tabl[ i - 1 ].y = tabl[ i - 2 ].y;
}
}
}
}
jump:
{ //WYPISUJEMY WSP.
cout << "GLOWA: \n x = " << xx << " y = " << yy << endl;
/*for( int i = 0; i < 100; i++)
{
if( tabl[i].x == 60 && tabl[i].y == 60) break;
cout << "OGON: \n" << i << ". x = " << tabl[i].x << ", y = " << tabl[i].y << endl;
}*/
}
{//RYSUJEMY PLANSZE
if(tryb == 27)
cout << "Punkty: " << punkty << " PAUZA" << endl;
else
cout << "Punkty: " << punkty << endl;
cout << "|--------------------------------------------------|" << endl << "|";
for(int y = 0; y <15 ; y++)
{
for(int x = 0; x<50 ; x++)
{
if( y == py && x == px)
{
if( y == yy && x == xx )
{
plansza[y][x] = 'S';
cout << plansza[y][x];
continue;
}
else
{
plansza[y][x] = 'X';
cout << plansza[y][x];
continue;
}
}
//OGOON
for(int i = 0; i < 100; i++)
{
if( tabl[i].x != 60 && tabl[i].y != 60 )
{
if( tabl[i].x == x && tabl[i].y == y )
{
plansza[y][x] = 'O';
cout << plansza[y][x];
continue;
}
}
}
//OGOON
if( y == yy && x == xx )
{
plansza[y][x] = 'S';
cout << plansza[y][x];
}
else
{
plansza[y][x] = ' ';
cout << plansza[y][x];
}
if(x == 49)
{
cout << "|" << endl << "|";
}
}
}
cout << "--------------------------------------------------|" << endl;
}//KONIEC RYSOWANIA
{ // Szybkosc lazenia snake'a
if(punkty <= 10)
Sleep(200);
else
Sleep(100);
if(punkty >= 25)
Sleep(50);
}
{//CZYTANIE Z KLAWIATURY
if(kbhit() == 1)
{
tryb = getch();
}
}
//ROBIMY OSTATNIE WSP DLA GLOWY
sx = xx;
sy = yy;
switch(tryb)
{
case 56:
{
if(yy >=0 )
{
yy -= 1;
ost = 56;
}
else
{
yy = 14;
ost = 56;
}
break;
}
case 50:
{
if(yy <=14 )
{
yy += 1;
ost = 50;
}
else
{
yy = 0;
ost = 50;
}
break;
}
case 52:
{
if(xx >=0 )
{
xx -= 1;
ost = 52;
}
else
{
xx = 49;
ost = 49;
}
break;
}
case 54:
{
if(xx <=49 )
{
xx += 1;
ost = 54;
}
else
{
xx = 0;
ost = 54;
}
break;
}
case 27:
{
break;
}
default:
{
tryb = ost;
}
}
if( xx == px && yy == py )
{
punkty += 1;
losuj(tabl, &px, &py );
}
system("cls");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment