0% found this document useful (0 votes)
3 views

message (2)456

This document contains a C++ implementation of a simple snake game using console graphics. The game features a snake that moves around the screen, collects eggs to grow, and must avoid walls and itself. The game tracks the player's score and health, and ends when the player runs out of health.

Uploaded by

張振祐
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

message (2)456

This document contains a C++ implementation of a simple snake game using console graphics. The game features a snake that moves around the screen, collects eggs to grow, and must avoid walls and itself. The game tracks the player's score and health, and ends when the player runs out of health.

Uploaded by

張振祐
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream>

#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <queue>

#define DIRECTION 224


#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define HEIGHT 20
#define WIDTH 50
#define SLEEP_TIME 100

#define SNAKE 1
#define EGG 2
#define WALL 3

using namespace std;

void HideCursor();
void gotoxy(int, int);

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 GM;
class Snake;
class Object
{
private:
static char allmap[HEIGHT][WIDTH];
public:
void change(int a,int b,int number)
{
allmap[a][b]=number;
}
int call(int a,int b)
{
return allmap[a][b];
}
};

char Object::allmap[HEIGHT][WIDTH]={0};

class Wall:public Object


{
public:
Wall()
{
for (int i = 0; i < WIDTH; ++i) {
change(0,i,3);//allmap[0][i] = WALL; // Top wall
change(HEIGHT-1,i,3);//allmap[HEIGHT-1][i] = WALL; // Bottom wall
}
for (int i = 0; i < HEIGHT; ++i) {
change(i,0,3);//allmap[i][0] = WALL; // Left wall
change(i,WIDTH-1,3);//allmap[i][WIDTH-1] = WALL; // Right wall
}
}
void printmap()
{
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
if(call(i,j)==3)
{
cout<<'#';
}
else
{
cout<<' ';
}
}
cout << endl;
}
}
};
class Snake:public Object
{
private:
int len;
int x,y;
char shape;
int direction;
queue<int> bodyx;
queue<int> bodyy;
public:
Snake()
{
len=1;
x=y=1;
bodyx.push(x);
bodyy.push(y);
change(y,x,1);
direction = RIGHT;
shape='S';
gotoxy(x,y);
cout<<shape;
}

void Move(){
unsigned char key;
if(kbhit()){
key = getch();
switch(key)
{
case DIRECTION:
switch(key = getch())
{
case UP:
direction = UP;
break;
case DOWN:
direction = DOWN;
break;
case LEFT:
direction = LEFT;
break;
case RIGHT:
direction = RIGHT;
break;
}
}
}
Forward();
}
void Forward(){
switch(direction){
case UP:
if(y>0) y--;
break;
case DOWN:
if(y<HEIGHT-1) y++;
break;
case LEFT:
if(x>0) x--;
break;
case RIGHT:
if(x<WIDTH-1) x++;
break;
}
}
int check()
{
if(call(y,x)==SNAKE)
{
return 0;
}
else if(call(y,x)==EGG)
{
len++;
return 1;
}
else if(call(y,x)==WALL)
{
return 0;
}
return 2;
}
void bodymove()
{
if(call(y,x)!= EGG)
{
gotoxy(bodyx.front(), bodyy.front());
change(bodyy.front(), bodyx.front(), 0);
cout<<" ";
bodyx.pop();//刪除 queue 最早的元素。
bodyy.pop();
}
for (int i = 0; i < bodyx.size(); i++)
{
int tx = bodyx.front();
int ty = bodyy.front();
bodyx.pop();
bodyy.pop();

gotoxy(tx, ty);
cout << "O";
change(ty, tx, SNAKE);

bodyx.push(tx);
bodyy.push(ty);
}
bodyx.push(x);//新增元素至 queue 最後。
bodyy.push(y);

gotoxy(x,y);
cout<<shape;
}
int getscore()
{
return len-1;
}
~Snake()
{
for (int i = len; i >0; i--)
{
gotoxy(bodyx.front(), bodyy.front());
change(bodyy.front(), bodyx.front(), 0);
cout<<" ";
bodyx.pop();
bodyy.pop();
}
}
};
class Egg:public Object
{
private:
char shape;
int x,y;
public:
Egg()
{
do
{
x=rand() % (WIDTH - 2) + 1;
y=rand() % (HEIGHT - 2) + 1;
}
while(call(y,x) != 0);
shape='E';
gotoxy(x,y);
change(y,x,2);
cout<<shape;
}

};
class GM
{
private:
int HP ;
int best_score ;

public:
GM() : HP(3), best_score(0)
{
Wall wall;
wall.printmap();
Snake *snake = new Snake;
Egg *egg = new Egg;
gotoxy(WIDTH + 3, 2);
cout << "HP : * * * ";
gotoxy(WIDTH + 3, 5);
cout << "Score :";
while (1)
{
gotoxy(WIDTH + 3 + 8, 5);
cout << snake->getscore();
snake->Move();
int status = snake->check();
if (status == 0)
{
gotoxy(WIDTH + 5 + HP * 2, 2);
cout << " ";
HP--;
delete snake;
if (HP == 0)
{
gotoxy(WIDTH / 3, HEIGHT / 2);
cout << "Game Over!";
Sleep(SLEEP_TIME);
gotoxy(WIDTH / 3, HEIGHT / 2);
cout << " ";
Sleep(SLEEP_TIME);
gotoxy(WIDTH / 3, HEIGHT / 2 - 2);
cout << "Game Over!";
gotoxy(WIDTH / 3, HEIGHT / 2 + 2);
cout << "Best Score : " << best_score;
}
else
{
if (snake->getscore() > best_score)
{
best_score = snake->getscore();
}
snake = new Snake;
}
}
else if (status == 1)
{
delete egg;
egg = new Egg;
}
snake->bodymove();
Sleep(SLEEP_TIME);
}
}
};

int main()
{
HideCursor();
srand((unsigned)time(NULL));
GM *gm=new GM;
return 0;
}

You might also like