0% found this document useful (0 votes)
2 views5 pages

Message (3) 78

This document contains a C++ implementation of a simple snake game. The game features a snake that moves around a grid, eating eggs to increase its length and score while avoiding collisions with itself. The code includes classes for the snake, egg, and game map, along with functions for handling movement, rendering, and game logic.

Uploaded by

張振祐
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Message (3) 78

This document contains a C++ implementation of a simple snake game. The game features a snake that moves around a grid, eating eggs to increase its length and score while avoiding collisions with itself. The code includes classes for the snake, egg, and game map, along with functions for handling movement, rendering, and game logic.

Uploaded by

張振祐
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<iostream>

#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);
}

void gotoxy(int x, int y){


COORD loc;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
loc.X = x;
loc.Y = y;
SetConsoleCursorPosition(hOuput,loc);
}

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);
}

int check(int a,int b){


for(int i = tail; i<head;i++){
if(a == arr_x[i] || b == arr_y[i]) {
return 0;
}
}
return 1;
}

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);
}

};

class Map: public Snake {


private:
int where[WIDTH+1][HEIGHT+1];
public:
Map(){
where[2][2]=1;
initialize();
printmap();

int gamestar(int s){


Egg egg;
itis(egg.Getx(),egg.Gety(),2);
Move();
while(1){
Move();
if(where[get_x()][get_y()] == 1) break;
else if(where[get_x()][get_y()] == 0) {
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;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<<'*';
}

}
}
}

void itis(int x,int y,int a){ //跟改資料


where[x][y] = a;
gotoxy(x,y);
switch(a){
case 1:
cout<<"O";
break;
case 2:
cout<<"E";
break;

}
}

/*int what(int x,int y){//這裡裡甚麼


return where[x][y];
}*/

};

class GM : public Map{


private:

public:
GM(){

};

int main(){
HideCursor();
srand(time(NULL));
GM gamestar;
gotoxy(0,20);
}

You might also like