Created
May 29, 2015 19:05
-
-
Save aambrozkiewicz/e3f2b8fc5b5059dfb463 to your computer and use it in GitHub Desktop.
C++ Snake game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
snejk, aleksander ambrozkiewicz. | |
*/ | |
#include <cstdlib> | |
#include <iostream> | |
#include <windows.h> | |
#include <conio.h> | |
#include "Snejk.h" | |
using namespace std; | |
int main(int argc, char *argv[]) | |
{ | |
char Znak; | |
CSnejk Snake; | |
dKierunek uKierunek = static_cast<dKierunek>(-1); | |
for (;;) | |
{ | |
Snake.Kolor(14); | |
cout << "\n Snejk"; | |
Snake.Kolor(7); | |
cout << "!\n\n"; | |
Snake.Ruch(uKierunek); | |
Snake.RysujPlansze(); | |
if (Snake.CzyGameOver()) | |
{ | |
cout << "Game Over, kup se rower." << endl; | |
cout << "Zdobyles " << Snake.WezWynik() << " punktow.\n\n"; | |
break; | |
} | |
cout << " Punkty: " << Snake.WezWynik() << endl; | |
if (kbhit() || uKierunek == -1) | |
Znak = getch(); | |
switch (Znak) | |
{ | |
case 'w': | |
if (uKierunek != kDol) uKierunek = kGora; break; | |
case 'd': | |
if (uKierunek != kLewo) uKierunek = kPrawo; break; | |
case 'a': | |
if (uKierunek != kPrawo) uKierunek = kLewo; break; | |
case 's': | |
if (uKierunek != kGora) uKierunek = kDol; break; | |
default: break; | |
} | |
Sleep(Snake.WezSpeed()); | |
system("cls"); | |
} | |
system("PAUSE"); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include <windows.h> | |
#include <time.h> | |
#include "Snejk.h" | |
using namespace std; | |
CSnejk::CSnejk() | |
{ | |
for (int i=0;i<SIZE_Y;i++) | |
{ | |
for (int j=0;j<SIZE_X;j++) | |
{ | |
sPlansza[j][i] = pPuste; | |
} | |
} | |
for (int i=0;i<SIZE_Y;i++) | |
{ | |
for (int j=0;j<SIZE_X;j++) | |
{ | |
Waz[j][i] = 0; | |
} | |
} | |
// zaczynaj od srodka | |
x = SIZE_X/2; | |
y = SIZE_Y/2; | |
// dlugosc poczatkowa weza | |
Dlugosc = 6; | |
// game over na false | |
GameOver = false; | |
Wynik = 0; Speed = 100; KrokPunktow = 69; Zebranych = 0; | |
UpuscOwoc(); | |
} | |
void CSnejk::RamkaX() | |
{ | |
cout << " +"; | |
for (int i=0;i<SIZE_X;i++) | |
cout << "-"; | |
cout << "+" << endl; | |
} | |
void CSnejk::RysujPlansze() | |
{ | |
RamkaX(); | |
for (int i=0;i<SIZE_Y;i++) | |
{ | |
cout << " |"; | |
for (int j=0;j<SIZE_X;j++) | |
{ | |
if (Waz[j][i] > 0) | |
Waz[j][i]--; | |
else if (sPlansza[j][i] != pOwoc) | |
sPlansza[j][i] = pPuste; | |
switch (sPlansza[j][i]) | |
{ | |
case pPuste: | |
cout << ' '; | |
break; | |
case pOwoc: | |
Kolor(10); | |
cout << '*'; | |
Kolor(7); | |
break; | |
case pOgon: | |
// nasza glowa ? | |
if (j == x && i == y) | |
{ | |
Kolor(14); | |
cout << char(206); | |
Kolor(7); | |
} | |
else | |
{ | |
Kolor(11); | |
cout << 'o'; | |
Kolor(7); | |
} | |
break; | |
default: break; | |
} | |
} | |
cout << "|" << endl; | |
} | |
RamkaX(); | |
} | |
void CSnejk::Ruch(dKierunek Kierunek) | |
{ | |
switch (Kierunek) | |
{ | |
case kGora: | |
if (--y > SIZE_Y-1) GameOver = true; break; | |
case kDol: | |
if (++y > SIZE_Y-1) GameOver = true; break; | |
case kLewo: | |
if (--x > SIZE_X-1) GameOver = true; break; | |
case kPrawo: | |
if (++x > SIZE_X-1) GameOver = true; break; | |
default: return; | |
} | |
if (sPlansza[x][y] == pOgon) | |
{ | |
GameOver = true; | |
return; | |
} | |
if (GameOver) | |
return; | |
if (sPlansza[x][y] == pOwoc) | |
{ | |
Zebranych++; | |
// co 4 owoce zwieksz weza | |
if (!(Zebranych % 4) && (Dlugosc < 25)) | |
Dlugosc += 2; | |
// krok punktow zmniejsz o 20% co 7 owocow.. | |
if (KrokPunktow < 20) | |
KrokPunktow = 69; | |
else if (!(Zebranych % 7)) | |
KrokPunktow *= 0.80; | |
// co 8 owocow przyspiesz o 20ms | |
if (!(Zebranych % 8) && (Speed > 20)) | |
Speed -= 20; | |
Wynik += KrokPunktow; | |
UpuscOwoc(); | |
} | |
sPlansza[x][y] = pOgon; | |
Waz[x][y] = Dlugosc; | |
} | |
void CSnejk::UpuscOwoc() | |
{ | |
unsigned tempX, tempY; | |
do | |
{ | |
srand (static_cast<unsigned>(time(NULL))); | |
tempX = rand()%SIZE_X-1; | |
tempY = rand()%SIZE_Y-1; | |
} while ((sPlansza[tempX][tempY] != pPuste) && (tempX < SIZE_X-1) && (tempY < SIZE_Y-1)); | |
sPlansza[tempX][tempY] = pOwoc; | |
} | |
void CSnejk::Kolor(int Nr) | |
{ | |
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hConsole, Nr); | |
} | |
bool CSnejk::CzyGameOver() | |
{ | |
return GameOver; | |
} | |
int CSnejk::WezWynik() | |
{ | |
return Wynik; | |
} | |
int CSnejk::WezSpeed() | |
{ | |
return Speed; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _SNEJK_H_ | |
#define _SNEJK_H_ | |
#include <iostream> | |
#define SIZE_X 75 | |
#define SIZE_Y 16 | |
enum dPlansza { pPuste, pOwoc, pOgon }; | |
enum dKierunek { kGora, kDol, kPrawo, kLewo }; | |
class CSnejk | |
{ | |
private: | |
unsigned int Dlugosc, Waz[SIZE_X][SIZE_Y], Wynik, Speed, KrokPunktow, Zebranych; | |
dPlansza sPlansza[SIZE_X][SIZE_Y]; | |
// wspolrzedne 'glowy' :) | |
unsigned x, y; | |
bool GameOver; | |
void RamkaX(); | |
void UpuscOwoc(); | |
public: | |
// konstruktor | |
CSnejk(); | |
// rysuje plansze przy uzyciu RamkaX() rowniez | |
void RysujPlansze(); | |
// ruch w Kierunku | |
void Ruch(dKierunek Kierunek); | |
// czy nadal pilka w grze ? | |
bool CzyGameOver(); | |
// zwraca aktualny wynik | |
int WezWynik(); | |
// speed wezyka | |
int WezSpeed(); | |
// kolorowo | |
void Kolor(int Nr); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment