#include <iostream>
#include <conio.h>
using namespace std;
#include <winbgim.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <stdio.h>
#define MINW 20
#define MINH 20
#define MAXW 420
#define MAXH 220
#define BACKGROUND 15
void initialize ();
bool checkPoint ();
void drawPoint (int x,int y);
void moveSnake ();
void drawSnake ();
void drawFood ();
void drawGame ();
void mainLoop ();
void run ();
void changeDirecton (int x);
void showHighScore();
void getHighScore();
void checkHighScore(int score);
void stringToChar (string str,char c[]);
void initScore();
bool isEmpty();
struct Point {
int x,y,x0,y0;
};
struct HighScore {
int score;
char name[30];
};
int level;
bool endGame;
int snakeLength;
Point snake[100];
Point direction;
Point food;
const int DIRECTION = 10;
HighScore highscore[5];
char* str = new char[20];// Convert score to string
void initScore(){
if (isEmpty()){
for (int i = 0;i < 5;i++){
strcpy(highscore[i].name,"PLAYER");
highscore[i].score = 0;
}
getHighScore();
}else{
char arr[20];
int count = 0;
FILE *f;
f = fopen("highscore.txt", "r");
for (int i = 0;i < 5;){
while (!feof(f)){
count++;
fscanf(f, "%s", arr);
if (count%2 == 1){
strcpy(highscore[i].name,arr);
//printf ("%s\t",ch);
}
else{
highscore[i].score = atoi(arr);
//printf ("%d\n",atoi(arr));
i++;
}
}
}
}
}
bool isEmpty(){
FILE *fp;
long size;
fp = fopen("highscore.txt","r");
if(fp){
fseek (fp, 0, SEEK_END);
size = ftell(fp);
fclose(fp);
}
return(size == 0);
}
bool checkPoint (){
for (int i = 0;i < snakeLength;i++){
if (food.x == snake[i].x && food.y == snake[i].y)
return false;
}
return true;
}
void initGame(){
setbkcolor (15);
cleardevice ();
setwindowtitle ("SNAKE-Playing....");
endGame = false;
//Init highscore;
// Draw frame
setfillstyle (1,5);
bar (MINW,MINH,MAXW,MINH+5);
bar (MAXW,MINH,MAXW-5,MAXH);
bar (MAXW,MAXH,MINW,MAXH-5);
bar (MINW,MINH,MINW+5,MAXH);
//How to play
setcolor (0);
int bk = getbkcolor();
setfillstyle (SOLID_FILL,0);
int a[] = {15,350,30,350,30,345,50,355,30,365,30,360,15,360,15,350};
fillpoly (8,a);
settextstyle (4,0,3); outtextxy (70,340,"Right");
int b[] = {15,410,30,400,30,405,45,405,45,415,30,415,30,420,15,410};
fillpoly (8,b);outtextxy (70,400,"Left");
int c[] = {235,370,235,355,230,355,240,340,250,355,245,355,245,370,235,370};
fillpoly (8,c);outtextxy (265,340,"Up");
int d[] = {235,400,235,415,230,415,240,430,250,415,245,415,245,400,235,400};
fillpoly (8,d);outtextxy (265,400,"Down");
bar (50,450,170,480);setcolor (15);settextstyle(1,0,1);setbkcolor(0);outtextxy(60,455,"SPACE");setbkcolor(bk);
setcolor(0);settextstyle (4,0,3);outtextxy(200,450,"Pause/Resum");
bar (50,500,170,530);setcolor (15);settextstyle(1,0,1);setbkcolor(0);outtextxy(80,505,"ESC");setbkcolor(bk);
setcolor(0);settextstyle (4,0,3);outtextxy(200,500,"End Game");
// Init snake with 3 point
snake[0].x = 60;snake[0].y = 50;
snake[1].x = 50;snake[1].y = 50;
snake[2].x = 40;snake[2].y = 50;
snakeLength = 3;
setfillstyle (1,BACKGROUND);
bar (25,25,415,215);
// Direction
direction.x = 10;direction.y = 0; // Khoi tao di theo huong trai qua phai
setcolor (2);
srand ( time(NULL));
//Init food
do{
food.x = (rand() % (39) + 3)*10;
food.y = (rand() % (19) + 3)*10;
}while (checkPoint() == false);
}
void drawPoint (int x,int y){
circle (x,y,4);
floodfill (x,y,getcolor());
}
// Snake is moving
void moveSnake (){
for (int i = 0;i < snakeLength;i++){
if (i == 0){
snake[0].x0 = snake[0].x;snake[0].y0 = snake[0].y;
snake[0].x += direction.x;
snake[0].y += direction.y;
}
else {
snake[i].x0 = snake[i].x;snake[i].y0 = snake[i].y;
snake[i].x = snake[i-1].x0;snake[i].y = snake[i-1].y0;
}
if (snake[0].x < MINW+5 || snake[0].y < MINH+5 || snake[0].x > MAXW-5 || snake[0].y > MAXH - 5 )
endGame = true;
if (i != 0 && snake[0].x == snake[i].x && snake[0].y == snake[i].y) endGame = true;
}
if (snake[0].x == food.x && snake[0].y == food.y){
snake[snakeLength].x = snake[snakeLength-1].x0;snake[snakeLength].y = snake[snakeLength-1].y0;
snakeLength++;
//Random again food if sanke ate food
srand ( time(NULL));
do{
food.x = (rand() % (39) + 3)*10;
food.y = (rand() % (19) + 3)*10;
}while (checkPoint() == false);
}
}
void drawSnake (){
setfillstyle (1,10);
int count = 0;
for (int i = 0;count < snakeLength;i++){
drawPoint(snake[i].x, snake[i].y);
count++;
}
//delete point when snake move
int x = getcolor();
setfillstyle (1,BACKGROUND);
drawPoint(snake[snakeLength-1].x0,snake[snakeLength-1].y0);
setcolor (BACKGROUND);
circle (snake[snakeLength-1].x0,snake[snakeLength-1].y0,4);
setcolor(x);
setfillstyle (1,5);
}
void drawFood (){
int x = getcolor();
setcolor(5);
drawPoint(food.x, food.y);
setcolor(x);
}
//Ve toan bo giao dien game
void drawGame (){
drawSnake();
drawFood();
int x = getcolor ();
setcolor (2);
sprintf(str,"%d",snakeLength*level*10-level*30); //show score
settextstyle(4,0,3);
outtextxy(450,20,"SCORE:");
setcolor(12);
outtextxy(600,20,str);
setcolor (x);
}
void changeDirecton (int x){
switch(x){
case 72:
if (direction.y != DIRECTION) {
direction.y = -DIRECTION; direction.x = 0;
}
break;
case 80:
if (direction.y != -DIRECTION) {
direction.y = DIRECTION; direction.x = 0;
}
break;
case 77:
if (direction.x != -DIRECTION) {
direction.x = DIRECTION; direction.y = 0;
}
break;
case 75:
if (direction.x != DIRECTION) {
direction.x = -DIRECTION; direction.y = 0;
}
break;
case 27:
endGame = true;
break;
}
}
void mainLoop (){
int x = getcolor();
if (!kbhit()){
moveSnake();
}
else{
char ch;
ch = getch();
if (ch == -32) ch = getch();
if ( ch == 32){
setcolor(0);outtextxy (100,100,"Game Pause");setcolor(x);
ch = getch();
while (ch != 32){
ch = getch();
delay(0);
}
setcolor (BACKGROUND);
outtextxy (100,100,"Game Pause");
setcolor (x);
}
changeDirecton(ch);
mainLoop ();
}
}
void showHighScore(){
FILE *f;
f = fopen("highscore.txt", "r");
char ch[20];
settextstyle (1,0,5);
setcolor (15);
outtextxy (150,50,"HIGH SCORE");
settextstyle (1,0,4);
int y = 150,count = 0;
while (!feof(f)){
if (count == 10) break;
count++;
fscanf(f, "%s", ch);
if (count%2 == 1){
setcolor (10);
outtextxy (180,y,ch);
y+=50;
}else{
setcolor (12);
outtextxy (500,y-50,ch);
}
}
fclose(f);
}
void getHighScore (){
FILE *f;
f = fopen("highscore.txt", "w");
for (int i = 0;i < 5;i++){
fputs(highscore[i].name,f);
fputs(" ",f);
fprintf(f,"%d",highscore[i].score);
fputs("\n",f);
}
fclose(f);
}
void checkHighScore (int _score){
char _name[20]={""};
for (int i = 0;i < 5;i++){
if (_score > highscore[i].score){
//to do sth
settextstyle(1,0,3);
for (int j = 0;j < 50;j++){
if (j%2 == 0){
setcolor (14);
if (i == 0)
outtextxy(460,100,"BEST SCORE");
else
outtextxy(460,100,"HIGH SCORE");
delay(50);
}else{
setcolor (9);
if (i == 0)
outtextxy(460,100,"BEST SCORE");
else
outtextxy(460,100,"HIGH SCORE");
delay(50);
}
}
settextstyle(1,0,2);
setcolor (4);outtextxy(430,150,"Player:");
char ch1;
int x = 0;
char str[2];
str[1] = 0;
while (ch1 != 13 && x < 10){
do{
ch1 = getch();
}while (ch1 < 65 && ch1 >90 || ch1 < 97 && ch1 > 132);
x++;
str[0] = ch1;
strcat(_name,str);
outtextxy(540,150,_name);
}
for (int j = 4;j > i;j--){
strcpy(highscore[j].name,highscore[j-1].name);
highscore[j].score = highscore[j-1].score;
}
strcpy(highscore[i].name,_name);
highscore[i].score = _score;
break;
}
}
getHighScore();
}
void run (){
// cout<<"\nTHANK YOU FOR USE MY GAME!";
// Sleep (500);
// cout<<"\n\nHOW TO PLAY THIS GAME:";
// Sleep (500);
// cout<<"\n\nPRESS KEY UP/DOWN/RIGHT/LEFT TO CONTROL SNAKE\nEAT A LOT OF FOOD AND GET SCORE!";
// Sleep (500);
// cout<<"\n\nNOW, PRESS ANY KEY TO START GAME ....";
// Sleep (500);
initwindow (800,600);
initScore();
int x = 0;
char *s = new char[5];
while (x <= 100){
cleardevice();
setcolor (10);settextstyle(4,0,5);outtextxy (60,200,"Game Loading...");
sprintf (s,"%d",x);
setcolor (4);
outtextxy (550,200,strcat(s,"%"));
if (x == 100)
delay (500);
delay (20);
x++;
}
while (true){
x1:
cleardevice();
setbkcolor(0);
cleardevice();
settextstyle(1,0,5);
setcolor (12);
outtextxy (150,100,"GAME MENU");
settextstyle(1,0,4);
setcolor (2);
outtextxy (200,200,"START");
outtextxy (200,250,"HIGH SCORE");
outtextxy (200,300,"QUIT GAME");
outtextxy (200,350,"ABOUT");
settextstyle (4,0,2);
setcolor (15);
outtextxy(200,500,"Choose one by key number!");
char c;
do{
c = getch ();
}while (c!= 49 && c!= 50 && c!= 51 && c!=52);
if (c == 49){
x2:
setbkcolor(0);
cleardevice();
settextstyle (1,0,4);
setcolor (10);
outtextxy (10,100,"Choose level before play [1 - 5]");
char str1 = getch ();
if (str1 == 49){
level = 1;
}else if (str1 == 50){
level = 2;
}else if (str1 == 51){
level = 3;
}else if (str1 == 52){
level = 4;
}else if (str1 == 53){
level = 5;
}
initGame();
while (!endGame){
//Change delay folow level
delay (250 - (level-1)*40);
mainLoop();
drawGame();
}
settextstyle (1,0,4);
setcolor (0);
outtextxy (100,110,"GAME OVER!");
setcolor (8);
settextstyle (1,0,2);
outtextxy (100,160,"Your Score : ");
outtextxy (300,160,str);
//Here is check high score
checkHighScore(snakeLength*level*10-level*30);
setcolor(4);
outtextxy (100,200,"Play Again ? [y/n]");
char ch;
do{
ch = getch();
}while (ch != 'Y' && ch != 'y' && ch != 'n' && ch != 'N');
if (ch == 'Y' || ch == 'y'){
goto x2;
}else if (ch == 'n' || ch == 'N'){
goto x1;
}
}else if (c == 51){
// system("cls");
// cout<<"\nHOPE YOU COME BACK SOON :)";
exit(0);
}else if (c == 52){
cleardevice();
settextstyle (1,0,4);
setcolor (15);
outtextxy (200,100,"Made by me!");
settextstyle (1,0,2);
setcolor (4);
outtextxy(200,500,"Press any key to exit");
while (!kbhit()){
delay(0);
}
goto x1;
}else if (c == 50){
cleardevice();
showHighScore();
settextstyle (1,0,2);
setcolor (4);
outtextxy(200,500,"Press any key to exit");
while (!kbhit()){
delay(0);
}
goto x1;
}
}
}
int main (){
run();
//Giai phong bo nho con tro;
delete str;
getch();
closegraph ();
return 0;
}