2013-02-20 21:27
tabyla_rasa
Все еще пишу "земейку". Ниже пртиведу код. В нем закрыла функцию, которая после окончания игры и при условии что игрок хочет начать игру заново - запускает все заново. Ибо если ее открыть то при попадании змейки в состояние смерти (встретилась с барьером или попала на саму себя) игра уже не спрашивает хочет игрок продолжить или нет - тупо запускает все с начала.
Спасите мой мозг-)))
using namespace std;
#define DIR_RIGHT 1
#define DIR_LEFT -1
#define DIR_UP 2
#define DIR_DOWN -2
char a[50][50]; //razmer polya v kotorom igra
int co_x[50], co_y[50], foodx, foody; //koordinaty zmeiki i edy
const int height = 16, width = 46; // visota i shirina
int num = 2; //num - dlina zmeiki
int direction=1;//iznachalno dvizhenie nachitaetsa napravo
int score = 0;// podschet ochkov nachinaetsa s nulua
void gotoxy (int column, int line) //peremeschaem kursor na zadannyu pozitsiu
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void menu ()
{
char s;
gotoxy (19, 7);
cout <<"SNAKE is the best game!\n\n";
gotoxy (17, 8);
cout<<"FOR STARTING GAME PRESS 'ENTER'\n\t";
getch();
}
void init_field ()
{
// initsializiruem igrovoe pole
for (int i = 0; i <= height; ++i)
for (int j = 0; j <= width; ++j)
a[i][j] = (i == 0 || j == 0 || i == height || j == width ? '#' : ' '); //opredelyaem granitsy
// initsializiruem strukturu zmeiki
num = 2;
co_x[0] = 1;
co_y[0] = 1;
co_x[1] = 2;
co_y[1] = 1;
a[1][1] = '$';
a[1][2] = '$';
// dlya prodolzheniya igry, esli polzovatel vibral nachat zanovo, to osnovine dannie dolzhni nachatsa s pervih harakteristik
foodx=0;
foody=0;
direction =1;
score=0;
}
void generate_food () // generiruem edy
{
foody = (rand() % 15)+1;
foodx = (rand() % 45)+1;
gotoxy(foodx, foody);
cout << "*";
}
void draw_field () //vistraivaem polya
{
int i,j;
system("cls"); //clean screen
for (i = 0; i <= height; ++i)
{
for (j = 0; j <= width; ++j)
cout << a[i][j];
cout << "\n";
}
cout << "\n";
// generatsia edy
generate_food();
}
bool isFoodField ()// eda na igrovom pole
{
if (co_x[num]==foodx && co_y[num]==foody)
{
foodx=NULL;
foody=NULL;
score++;
return true;
}
return false;
}
bool dead () //pri kakih ysloviah zmeika mertva
{
int i;
// esli vrezalas v konets polya
if (co_x[num-1] == width)
return true;
if (co_x[num-1] == 0)
return true;
if (co_y[num-1] == height)
return true;
if (co_y[num-1] == 0)
return true;
// esli popala sama na sebya
for (i = 0; i < num-3; ++i)
if (co_x[i] == co_x[num-1] && co_y[i] == co_y[num-1])
return true;
return false;
}
void move () //jsnovnie operatsii pri dvizhenii
{
char input;
int temp_dir = direction;
int speed=300;
char ending[]="GAME OVER!";
do {
if (kbhit()) input = getch(); //proveryaem nazhatie klavishi na konsole
else input = 0;//v buffer otkladivaem znachenie nazhatoi klavishi
switch (input)//globalnoe znachenie napravleniya i ignor protivopolozhnogo
{
case 'P':
if (direction==DIR_UP) // esli daem dvizhenie naverh
break;// nichego ne poluchaetsa
temp_dir=DIR_DOWN; // idti tolko vniz
break;
case 'H':
if (direction==DIR_DOWN) break;
temp_dir=DIR_UP;
break;
case 'K':
if (direction==DIR_RIGHT) break;
temp_dir= DIR_LEFT;
break;
case 'M':
if (direction==DIR_LEFT) break;
temp_dir= DIR_RIGHT;
break;
default:
break;
}
direction=temp_dir;
switch (temp_dir) // dvizhenie ot golovi po ukazannomy napravleniu
{
case DIR_RIGHT:
co_x[num] = co_x[num-1]+1;
co_y[num] = co_y[num-1];
break;
case DIR_LEFT:
co_x[num] = co_x[num-1]-1;
co_y[num] = co_y[num-1];
break;
case DIR_UP:
co_y[num] = co_y[num-1]-1;
co_x[num] = co_x[num-1];
break;
case DIR_DOWN:
co_y[num] = co_y[num-1]+1;
co_x[num] = co_x[num-1];
break;
default:
break;
}
// esli zmeika sela edy, to razmer i skorost uvelochivautsa i schet menyaetsa - poyavlaetsa novaya eda
if (isFoodField())
{
gotoxy (co_x[num], co_y[num]);
cout << "$";
++num;// razmer
speed=speed-5;//schet
generate_food();//novaya eda
}
else
{
gotoxy (co_x[0],co_y[0]);
cout << ' ';
for (int i = 0; i < num; i++)
{
co_x[i] = co_x[i+1];
co_y[i] = co_y[i+1];
}
gotoxy (co_x[num], co_y[num]);
cout << "$";
}
// esli usloviya smerti ispolnilis to konets igre
if(dead())
{
int y=1;
for (int i=0; i<11; i++)
{
gotoxy (i+10, 7);
cout << ending[i];
}
cout <<"Start new game? y-1/n-other\n\n";
// вот это вот вызывает траблы
/* if (y == 1)
{
init_field ();
draw_field();
move();
}
else
{
ExitProcess(0);
}*/
break;
}
// oboznachenie scheta i koordinat edy na ekrane
gotoxy(1,18);
cout <<"Score : "<< score <<"\n\n";
gotoxy(0,22);
cout << foodx;
gotoxy(0,23);
cout << foody;
Sleep(speed);
} while (input!=27);
}
int main()
{
menu ();
init_field ();
draw_field();
move();
return 0;
}
Спасите мой мозг-)))
using namespace std;
#define DIR_RIGHT 1
#define DIR_LEFT -1
#define DIR_UP 2
#define DIR_DOWN -2
char a[50][50]; //razmer polya v kotorom igra
int co_x[50], co_y[50], foodx, foody; //koordinaty zmeiki i edy
const int height = 16, width = 46; // visota i shirina
int num = 2; //num - dlina zmeiki
int direction=1;//iznachalno dvizhenie nachitaetsa napravo
int score = 0;// podschet ochkov nachinaetsa s nulua
void gotoxy (int column, int line) //peremeschaem kursor na zadannyu pozitsiu
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void menu ()
{
char s;
gotoxy (19, 7);
cout <<"SNAKE is the best game!\n\n";
gotoxy (17, 8);
cout<<"FOR STARTING GAME PRESS 'ENTER'\n\t";
getch();
}
void init_field ()
{
// initsializiruem igrovoe pole
for (int i = 0; i <= height; ++i)
for (int j = 0; j <= width; ++j)
a[i][j] = (i == 0 || j == 0 || i == height || j == width ? '#' : ' '); //opredelyaem granitsy
// initsializiruem strukturu zmeiki
num = 2;
co_x[0] = 1;
co_y[0] = 1;
co_x[1] = 2;
co_y[1] = 1;
a[1][1] = '$';
a[1][2] = '$';
// dlya prodolzheniya igry, esli polzovatel vibral nachat zanovo, to osnovine dannie dolzhni nachatsa s pervih harakteristik
foodx=0;
foody=0;
direction =1;
score=0;
}
void generate_food () // generiruem edy
{
foody = (rand() % 15)+1;
foodx = (rand() % 45)+1;
gotoxy(foodx, foody);
cout << "*";
}
void draw_field () //vistraivaem polya
{
int i,j;
system("cls"); //clean screen
for (i = 0; i <= height; ++i)
{
for (j = 0; j <= width; ++j)
cout << a[i][j];
cout << "\n";
}
cout << "\n";
// generatsia edy
generate_food();
}
bool isFoodField ()// eda na igrovom pole
{
if (co_x[num]==foodx && co_y[num]==foody)
{
foodx=NULL;
foody=NULL;
score++;
return true;
}
return false;
}
bool dead () //pri kakih ysloviah zmeika mertva
{
int i;
// esli vrezalas v konets polya
if (co_x[num-1] == width)
return true;
if (co_x[num-1] == 0)
return true;
if (co_y[num-1] == height)
return true;
if (co_y[num-1] == 0)
return true;
// esli popala sama na sebya
for (i = 0; i < num-3; ++i)
if (co_x[i] == co_x[num-1] && co_y[i] == co_y[num-1])
return true;
return false;
}
void move () //jsnovnie operatsii pri dvizhenii
{
char input;
int temp_dir = direction;
int speed=300;
char ending[]="GAME OVER!";
do {
if (kbhit()) input = getch(); //proveryaem nazhatie klavishi na konsole
else input = 0;//v buffer otkladivaem znachenie nazhatoi klavishi
switch (input)//globalnoe znachenie napravleniya i ignor protivopolozhnogo
{
case 'P':
if (direction==DIR_UP) // esli daem dvizhenie naverh
break;// nichego ne poluchaetsa
temp_dir=DIR_DOWN; // idti tolko vniz
break;
case 'H':
if (direction==DIR_DOWN) break;
temp_dir=DIR_UP;
break;
case 'K':
if (direction==DIR_RIGHT) break;
temp_dir= DIR_LEFT;
break;
case 'M':
if (direction==DIR_LEFT) break;
temp_dir= DIR_RIGHT;
break;
default:
break;
}
direction=temp_dir;
switch (temp_dir) // dvizhenie ot golovi po ukazannomy napravleniu
{
case DIR_RIGHT:
co_x[num] = co_x[num-1]+1;
co_y[num] = co_y[num-1];
break;
case DIR_LEFT:
co_x[num] = co_x[num-1]-1;
co_y[num] = co_y[num-1];
break;
case DIR_UP:
co_y[num] = co_y[num-1]-1;
co_x[num] = co_x[num-1];
break;
case DIR_DOWN:
co_y[num] = co_y[num-1]+1;
co_x[num] = co_x[num-1];
break;
default:
break;
}
// esli zmeika sela edy, to razmer i skorost uvelochivautsa i schet menyaetsa - poyavlaetsa novaya eda
if (isFoodField())
{
gotoxy (co_x[num], co_y[num]);
cout << "$";
++num;// razmer
speed=speed-5;//schet
generate_food();//novaya eda
}
else
{
gotoxy (co_x[0],co_y[0]);
cout << ' ';
for (int i = 0; i < num; i++)
{
co_x[i] = co_x[i+1];
co_y[i] = co_y[i+1];
}
gotoxy (co_x[num], co_y[num]);
cout << "$";
}
// esli usloviya smerti ispolnilis to konets igre
if(dead())
{
int y=1;
for (int i=0; i<11; i++)
{
gotoxy (i+10, 7);
cout << ending[i];
}
cout <<"Start new game? y-1/n-other\n\n";
// вот это вот вызывает траблы
/* if (y == 1)
{
init_field ();
draw_field();
move();
}
else
{
ExitProcess(0);
}*/
break;
}
// oboznachenie scheta i koordinat edy na ekrane
gotoxy(1,18);
cout <<"Score : "<< score <<"\n\n";
gotoxy(0,22);
cout << foodx;
gotoxy(0,23);
cout << foody;
Sleep(speed);
} while (input!=27);
}
int main()
{
menu ();
init_field ();
draw_field();
move();
return 0;
}