message (4)123
message (4)123
#include <conio.h>
#include <windows.h>
#include<stdio.h>
#include <time.h>
#define HEIGHT 10
#define WIDTH 20
#define DIRECTION 224
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define SLEEP_TIME 100
class GM;
void HideCursor();
void gotoxy(int, int);
int score = 0;
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
class Snake {
private:
int x;
int y;
int direction;
int direction2;
int arr_x[HEIGHT*WIDTH];
int arr_y[HEIGHT*WIDTH];
int head;
int tail;
public:
Snake(){
x = y = 2;
direction = RIGHT;
direction2 = DOWN;
head = 1;
tail = 0;
arr_x[0] = x;
arr_y[0] = y;
}
void Move(){
unsigned char key;
if(kbhit()){
key = getch();
switch(key){
case DIRECTION:
switch(key = getch()){
case UP:
if(key != direction2) direction = UP;
break;
case DOWN:
if(key != direction2) direction = DOWN;
break;
case LEFT:
if(key != direction2) direction = LEFT;
break;
case RIGHT:
if(key != direction2) direction = RIGHT;
break;
}
}
}
Forward();
_sleep(SLEEP_TIME);
}
void Forward(){
switch(direction){
case UP:
if(y == 2) y = HEIGHT - 1;
else y--;
direction2 = DOWN;
break;
case DOWN:
if(y == HEIGHT) y = 2;
else y++;
direction2 = UP;
break;
case LEFT:
if(x == 1) x = WIDTH - 1;
else x--;
direction2 = RIGHT;
break;
case RIGHT:
if(x == WIDTH-1) x = 1;
else x++;
direction2 = LEFT;
break;
}
void noeat(){
arr_x[head % (HEIGHT*WIDTH)] = x;
arr_y[head % (HEIGHT*WIDTH)] = y;
gotoxy(arr_x[tail % (HEIGHT*WIDTH)], arr_y[tail % (HEIGHT*WIDTH)]);
cout << ' ';
head++;
tail++;
}
void eat(){
arr_x[head % (HEIGHT*WIDTH)] = x;
arr_y[head % (HEIGHT*WIDTH)] = y;
head++;
score++;
gotoxy(0,0);
cout << "score: " << score;
}
int get_x(){
return x;
}
int get_y(){
return y;
}
int get_tailx(){
return arr_x[tail];
}
int get_taily(){
return arr_y[tail];
}
};
class Egg {
private:
int x;
int y;
public:
Egg(){
x = rand() % (WIDTH - 1) + 1;
y = rand() % (HEIGHT - 1) + 2;
gotoxy(x, y);
cout << "E";
}
int Getx(){
return x;
}
int Gety(){
return y;
}
};
public:
Map(){
initialize();
printmap();
egg = new Egg();
itis(egg->Getx(), egg->Gety(), 2);
}
~Map(){
delete egg;
}
void gamestar(){
while(1){
Move();
if(where[get_x()][get_y()] == 1 || get_x() == 0 || get_x() == WIDTH ||
get_y() == 1 || get_y() == HEIGHT + 1)
break; // 如果蛇碰到邊界或者自身,則結束遊戲
else if(where[get_x()][get_y()] == 0) {
where[get_x()][get_y()] = 1;
where[get_tailx()][get_taily()] = 0;
noeat();
}
else if(where[get_x()][get_y()] == 2) {
eat();
delete egg;
egg = new Egg();
itis(egg->Getx(), egg->Gety(), 2);
}
itis(get_x(), get_y(), 1);
}
}
void initialize(){
for(int i = 0; i < WIDTH; i++){
for(int j = 0; j < HEIGHT+1; j++){
where[i][j] = 0;
}
}
where[2][2] = 1;
}
void printmap(){
gotoxy(0, 0);
cout << "score: " << score;
for(int i = 0; i < WIDTH + 1; i++){
for(int j = 1; j < HEIGHT + 2; j++){
if(i == 0 || j == 1 || i == WIDTH || j == HEIGHT + 1){
gotoxy(i, j);
cout << '*';
}
}
}
}
int main(){
HideCursor();
srand(time(NULL));
{
GM gamestar;
} // 在這裡加入一個範圍,以確保 gamestar 物件會在範圍結束時被銷毀,從而釋放記憶體
gotoxy(0, 20);
return 0;
}