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

message (4)123

This document contains a C++ implementation of a simple console-based Snake game. The game features a snake that moves around a grid, collects eggs to increase its score, and has boundaries that end the game if crossed. The code includes classes for the snake, egg, and game map, along with functions to handle 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)
9 views5 pages

message (4)123

This document contains a C++ implementation of a simple console-based Snake game. The game features a snake that moves around a grid, collects eggs to increase its score, and has boundaries that end the game if crossed. The code includes classes for the snake, egg, and game map, along with functions to handle 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;
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;
}
};

class Map : public Snake {


private:
int where[WIDTH][HEIGHT+1];
Egg* egg;

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 << '*';
}
}
}
}

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 {


public:
GM(){
gamestar();
}
};

int main(){
HideCursor();
srand(time(NULL));
{
GM gamestar;
} // 在這裡加入一個範圍,以確保 gamestar 物件會在範圍結束時被銷毀,從而釋放記憶體
gotoxy(0, 20);
return 0;
}

You might also like