Message (3) 78
Message (3) 78
#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
using namespace std;
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; //蛇頭的位置 x
int y; //蛇頭的位置 y
int direction; //方向
int direction2; //用於紀錄上一個方向 不想讓玩家往回走
int arr_x[HEIGHT*WIDTH];
int arr_y[HEIGHT*WIDTH];
int head = 1;
int tail = 0;
public:
Snake(){ //創建蛇 方法 1
x = y = 2;
direction = RIGHT;
direction2 = DOWN;
}
int 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) bonk();
if(y == 2) y=HEIGHT-1;
else if(y>1) y--;
direction2 = DOWN;
break;
case DOWN:
//if(y==HEIGHT) bonk();
if(y==HEIGHT) y=2;
else if(y<HEIGHT) y++;
direction2 = UP;
break;
case LEFT:
//if(x==1) bonk();
if(x==1) x=WIDTH-1;
else if(x>1) x--;
direction2 = RIGHT;
break;
case RIGHT:
//if(x==WIDTH) bonk();
if(x==WIDTH) x=1;
else if(x<WIDTH) x++;
direction2 = LEFT;
break;
}
gotoxy(x,y);
}
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++;
}
/*int noeat(){
if(check(x,y)) return 0;
arr_x[head%200] = x;
arr_y[head%200] = y;
arr_x[tail%200] = 0;
arr_y[tail%200] = 0;
head++;
tail++;
}
void eat(){
arr_x[head%200] = x;
arr_y[head%200] = y;
head++;
}*/
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=rand()%(WIDTH-1)+1;
int y=rand()%(HEIGHT-1)+2;
public:
Egg(){
}
int Getx(){ //回傳蛋的位置 x
return(x);
}
int Gety(){ //回傳蛋的位置 y
return(y);
}
};
void initialize(){//初始化
for(int i = 0;i<WIDTH;i++){
for(int j = 0;j<HEIGHT;j++){
where[i][j] = {0};
}
}
}
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<<'*';
}
}
}
}
}
}
};
public:
GM(){
};
int main(){
HideCursor();
srand(time(NULL));
GM gamestar;
gotoxy(0,20);
}