#include "myfunc.h"
#include "walls.h"
#include "snakes.h"
#include "balls.h"
#include <graphics.h>
walls wall(30,30,14);
snakes snake(11);
balls ball(30,30,11);
int score ,map ,speed ,choice_speed = 1,choice=1,new_choi=-1,map_choice = 1;
int max_speed = 8, max_map = 10;
bool ok = true;
bool pause = false;
void loadmap();
void play();
void help();
void gametype();
void choicespeed();
void writescore();
void menu();
//==============================================================================
void info(){
char str[10];
int len;
for (int i=31; i<=55;i++){
print_rectangle(i,1,11,11);
print_rectangle(i,30,11,11);
}
for (int i=0; i<30;i++){
print_rectangle(31,i,11,11);
print_rectangle(55,i,11,11);
}
setcolor(11);
settextstyle(0,0,16);
outtextxy(390,15,"SCORE");text(str,score);
len = strlen(str);
outtextxy(470-len*20,65,str);
outtextxy(390,115,"SPEED");text(str,speed);
outtextxy(450,165,str);
outtextxy(420,215,"MAP");text(str,map);
outtextxy(450,265,str);
}
//==============================================================================
void prepare(){
char str[10];
clearviewport( );
info();
help();
ok = true;
wall.set(1);
wall.print();
wall.set(map);
ball.set(snake,wall);
wall.refresh();
setcolor(YELLOW);
settextstyle(0,0,16);text(str,map);
outtextxy(120,120,"MAP");
outtextxy(150,150,str);delay(1000);
outtextxy(100,120,"WAIT!");
outtextxy(150,150,"1");delay(800);
outtextxy(150,150,"2");delay(800);
outtextxy(150,150,"3");delay(800);
outtextxy(90,120," ");
outtextxy(130,150,"GO!");delay(800);
wall.refresh();
snake.set(50,2);
snake.print();
ball.print();
wall.print();
while (kbhit()){
char ch = getch();
if (ch==0) ch = getch();
else if (ch==27) {
while (kbhit()) ch = getch();
menu();
}
else if (ch==13) pause = !pause;
}
}
//==============================================================================
void help(){
char str[2];
str[1] = 0;
setcolor(WHITE);
settextstyle(0,0,13);
outtextxy(100,355,"PRESS KEY ESC TO COME BACK MENU");
outtextxy(100,385,"PRESS KEY ENTER TO PAUSE");
outtextxy(100,415,"PRESS KEY UP TO MOVE UP");
outtextxy(100,445,"PRESS KEY RIGHT TO MOVE RIGHT");
outtextxy(100,475,"PRESS KEY DOWN TO MOVE DOWN");
outtextxy(100,505,"PRESS KEY LEFT TO MOVE LEFT");
outtextxy(100,535,"DESIGN BY CAO VAN VIET K51CA");
outtextxy(100,565,"HAVE FUN! ^^");
}
//==============================================================================
void eat(){
score += (3+speed+map);
setcolor(11);
char str[10];
text(str,score);
outtextxy(470 - strlen(str)*20,65,str);
if (snake.get_leng()==10) {
map++;
if (map==max_map+1){
speed ++;
if (speed==max_speed+1) speed = max_speed;
map = 1;
}
play();
}
else {
ball.set(snake,wall);
ball.print();
}
}
//==============================================================================
void go(){
char kt;
if(!kbhit()) snake.goahead();
else {
char ch = getch();
if (ch==27) menu();
if (ch==13) {
pause = !pause;
if (pause) char kt = getch();
cout << pause << " " << kt << endl;
}
else if (ch==0){
ch = getch();
if ((ch==72)&&(snake.get_huong()!=3)) snake.moveup();
else if ((ch==77)&&(snake.get_huong()!=4)) snake.moveright();
else if ((ch==80)&&(snake.get_huong()!=1)) snake.movedown();
else if ((ch==75)&&(snake.get_huong()!=2)) snake.moveleft();
else snake.goahead();
}
else go();
}
snake.print();
if (snake.die(wall)){
snake.print_die1();
ok = false;
}
if (snake.die()){
snake.print_die2();
ok = false;
}
}
//==============================================================================
void play(){
if (ok) prepare();
while (ok){
//if (pause) cin.ignore(1);
delay(200-20*speed);
ball.print();
nude b = ball.get();
if (snake.eat(b)) eat();
if (ok) go();
}
menu();
}
//==============================================================================
void show(walls w){
for (int i=1; i<=30; i++)
for (int j=1; j<=30; j++)
if (w.get(i,j))
print_rectangle(i+13,j+13,11,14);
}
//==============================================================================
void process_map_by_keyboard(){
char ch = getch(),str[2];
walls w(30,30,14);
if (ch==27) menu();
if (ch==13) {
map = map_choice;
play();
}
else if (ch==0){
ch = getch();
if (ch==77) map_choice++;
else if (ch==75) map_choice --;
if (map_choice==max_map+1) map_choice = 1;
if (map_choice==0) map_choice = max_map;
setcolor(BLACK);settextstyle(0,0,16);
outtextxy(350,100," ");
print_hcn(142,142,0,0,340);
setcolor(YELLOW);
w.set(map_choice);
show(w);
text(str,map_choice);
settextstyle(0,0,16);setcolor(YELLOW);
outtextxy(350,100,str);
}
}
//==============================================================================
void process_map_by_mouse(){
char str[2];
walls w(30,30,14);
if (ismouseclick(WM_LBUTTONDOWN)){
int x_click ,y_click ;
getmouseclick(WM_LBUTTONDOWN,x_click,y_click);
if ((125<=x_click)&&(x_click<=295)&&(490<=y_click)&&(y_click<=540)) menu() ;
else if ((315<=x_click)&&(x_click<=485)&&(490<=y_click)&&(y_click<=540)){
map = map_choice;
play();
}
else {
if ((510<=x_click)&&(x_click<=600)&&(270<=y_click)&&(y_click<=350))
map_choice++;
else if ((30<=x_click)&&(x_click<=120)&&(270<=y_click)&&(y_click<=350))
map_choice--;
if (map_choice==max_map+1) map_choice = 1;
if (map_choice==0) map_choice = max_map;
if ((270<=y_click)&&(y_click<=350))
if (((510<=x_click)&&(x_click<=600))||((30<=x_click)&&(x_click<=120))){
setcolor(BLACK);settextstyle(0,0,16);
outtextxy(350,100," ");
print_hcn(142,142,0,0,340);
setcolor(YELLOW);
w.set(map_choice);
show(w);
text(str,map_choice);
settextstyle(0,0,16);setcolor(YELLOW);
outtextxy(350,100,str);
}
}
}
}
//==============================================================================
void loadmap(){
clearviewport( );
map_choice = 1;
char str[2];
walls w(30,30,14);
w.set(map_choice);
show(w);
settextstyle(0,0,16);setcolor(YELLOW);
outtextxy(250,100,"MAP");outtextxy(350,100,"1");
outtextxy(510,290,"-->");
outtextxy(30,290,"<--");
settextstyle(0,0,15);
print_hcn(125,490,50,170,5);
outtextxy(150,503,"CANCEL");
print_hcn(315,490,50,170,5);
outtextxy(350,503,"AGREE");
settextstyle(0,0,13);
outtextxy(525,275,"NEXT");
outtextxy(30,275,"PREVIEW");
while (1){
if (!kbhit()) process_map_by_mouse();
else process_map_by_keyboard();
}
}
//==============================================================================
void gametype(){
}
//==============================================================================
void process_speed_by_keyboard(){
char ch = getch(),str[2];
if (ch==27) menu();
if (ch==13) {
speed = choice_speed;
menu();
}
else if (ch==0){
ch = getch();
if ((ch==77)&&(choice_speed<8)){
choice_speed++;
setcolor(YELLOW);
print_hcn(120+36*choice_speed,206,1,36,22);
}
else if ((ch==75)&&(choice_speed>1)){
choice_speed --;
setcolor(BLACK);
print_hcn(156+36*choice_speed,206,1,36,22);
}
setcolor(YELLOW);
text(str,choice_speed);settextstyle(0,0,16);
outtextxy(375,150,str);
}
}
//==============================================================================
void process_speed_by_mouse(){
char str[2];
if (ismouseclick(WM_LBUTTONDOWN)){
int x_click ,y_click ;
getmouseclick(WM_LBUTTONDOWN,x_click,y_click);
if ((70<=x_click)&&(x_click<=270)&&(300<=y_click)&&(y_click<=350)) menu() ;
else if ((320<=x_click)&&(x_click<=520)&&(300<=y_click)&&(y_click<=350)){
speed = choice_speed;
menu();
}
else if ((515<=x_click)&&(x_click<=540)&&(180<=y_click)&&