*Header files:
- apple.h:
#pragma once
#include "point.h"
#include "map.h"
class Apple {
public:
Point _pos;
int _status;
Map* _pmap;
//method
Apple();
Apple(Map *p);
void GeneratePosition();
void CheckStatus();
void ClearApple();
void DisplayApple();
};
- Game.h:
#pragma once
#include "map.h"
#include "apple.h"
#include "snake.h"
#include "Util.h"
#include "point.h"
class Game{
public:
int _score;
Map *_mapGame;
Apple *_apple;
Snake *_snake;
//
Game();
~Game();
void CheckKey();
void loop();
bool CheckSnakeEatApple();
void DisplayScore();
};
- map.h:
#include "point.h"
#pragma once
class Map {
public:
int _w, _h;
char **_map;
//method
void DisplayMap();
Map();
Map(int w, int h);
Map(int w, int h, char **map);
~Map();
};
- point.h:
#pragma once
class Point {
public:
int _x, _y;
char _c;
//method
void DrawPoint();
Point();
Point(int, int, char);
};
- snake.h
#pragma once
#include "point.h"
#include <list>
#include "Util.h"
#include "map.h"
using namespace std;
class Snake {
public:
list<Point> _body;
int _status;
DIR _dir;
Map *_pmap;
//method
Snake(Map *p);
void Eat();
void Move();
void Check();
void CheckStatus();
void DisplaySnake();
};
- Util.h:
#pragma once
enum DIR {UP, RIGHT, DOWN, LEFT};
enum KEYPRESS {KEY_LEFT = 75, KEY_UP = 72, KEY_RIGHT = 77, KEY_DOWN = 80};
void Gotoxy(int x, int y);
void Clearxy(int x, int y);
*Source Files:
- Apple.cpp:
#include "apple.h"
#include <time.h>
#include <stdlib.h>
#include "Util.h"
Apple::Apple(){
_pmap = 0;
}
Apple::Apple(Map *p){
_pmap = p;
}
void Apple::ClearApple(){
Clearxy(_pos._x, _pos._y); //clear tren console
_pmap -> _map[_pos._y][_pos._x] = ' ';//clear tren map
}
void Apple::DisplayApple(){
_pos.DrawPoint();
_pmap -> _map[_pos._y][_pos._x] = '@';
}
void Apple::GeneratePosition(){
//check
int x,y;
do {
srand(time(NULL));
x = (rand() % (_pmap -> _w - 2)) + 1;
y = (rand() % (_pmap -> _h - 2)) + 1;
}
while (_pmap -> _map[y][x] != ' ');
_pos = Point(x, y, '@');
DisplayApple();
}
- Game.cpp:
#include "Game.h"
#include "Windows.h"
#include <conio.h>
#include <iostream>
using namespace std;
Game::Game(){
_mapGame = new Map();
_mapGame -> DisplayMap();
_apple = new Apple(_mapGame);
_snake = new Snake(_mapGame);
_snake -> DisplaySnake();
_score = 0;
}
void Game::loop(){
_apple -> GeneratePosition();
DisplayScore();
while (1){
CheckKey();
_snake -> Move();
if(_snake->_status == -1)
break;
if(CheckSnakeEatApple())
DisplayScore();
Sleep(150);
}
if (system("CLS")) system("clear");
Gotoxy(0,0);
cout <<"Game Over, Your Score: "<<_score<<endl;
system("pause");
}
bool Game::CheckSnakeEatApple(){
Point headSnake = _snake -> _body.front();
if(headSnake._x == _apple -> _pos._x && headSnake._y == _apple -> _pos._y){
_snake -> _status = 1;
_apple -> ClearApple();
_apple -> GeneratePosition();
_score++;
return true;
}
_snake -> _status = 0;
return false;
}
void Game::CheckKey(){
if (_kbhit()) {
int tmp = _getch();
if (tmp == (int)KEY_UP){
if(_snake -> _dir != DOWN)
_snake -> _dir = UP;
}
else if (tmp == (int)KEY_DOWN){
if(_snake -> _dir != UP)
_snake ->_dir = DOWN;
}
else if (tmp == (int)KEY_LEFT){
if(_snake ->_dir != RIGHT)
_snake ->_dir = LEFT;
}
else if (tmp == (int)KEY_RIGHT){
if(_snake ->_dir != LEFT)
_snake ->_dir = RIGHT;
}
}
}
void Game::DisplayScore(){
Gotoxy(0, _mapGame -> _h + 2);
cout << "Your score: " <<_score;
}
Game::~Game(){
delete _mapGame;
delete _snake;
delete _apple;
}
- Map.cpp:
#include "map.h"
#include <iostream>
using namespace std;
Map::Map() {
_w = 30;
_h = 20;
char **p = new char*[_h];
for (int r=0;r<_h;r++){
p[r] = new char[_w];
for(int c = 0;c < _w; c++){
if (r==0 || c==0 || r == _h -1 || c == _w -1)
p[r][c]='#';
else
p[r][c] = ' ';
}
}
_map = p;
}
void Map::DisplayMap(){
for (int r=0;r<_h;r++){
for(int c=0;c<_w;c++){
cout<<_map[r][c];
}
cout<<endl;
}
}
Map::~Map(){
for (int r=0;r<_h;r++){
delete [] _map[r];
}
delete _map;
_map = 0;
_w=0;
_h=0;
}
- Point.cpp:
#include <iostream>
#include "point.h"
#include "Util.h"
using namespace std;
Point::Point(){}
Point::Point(int x, int y, char c){
_x=x;
_y=y;
_c=c;
}
void Point::DrawPoint(){
Gotoxy(_x, _y);
cout<< _c;
}
- Snake.cpp:
#include "snake.h"
Snake::Snake(Map *p){
_dir = RIGHT;
_body.push_back(Point(3,1,'O'));
_body.push_back(Point(2,1,'*'));
_body.push_back(Point(1,1,'*'));
_pmap = p;
_status = 0;
}
void Snake::DisplaySnake(){
for(auto i = _body.begin(); i != _body.end(); i++){
(*i).DrawPoint();
_pmap -> _map[(*i)._y][(*i)._x] = (*i)._c;
}
}
void Snake::Move(){
int nx, ny;
if(_dir == UP) {
nx = _body.front()._x;
ny = _body.front()._y - 1;
}
else if (_dir == RIGHT) {
nx = _body.front()._x + 1;
ny = _body.front()._y ;
}
else if (_dir == DOWN) {
nx = _body.front()._x;
ny = _body.front()._y + 1;
}
else {
nx = _body.front()._x - 1;
ny = _body.front()._y ;
}
Point newHead = Point(nx,ny,'O');
if (_pmap->_map[ny][nx] == '#' || _pmap -> _map[ny][nx] == '*'){
_status = -1;
return;
}
_body.front()._c='*';
_body.push_front(newHead);
if(_status == 0){
Point tail = _body.back();
Clearxy(tail._x, tail._y);
_pmap -> _map[tail._y][tail._x] =' ';//clear thang cuoi trong map
_body.pop_back();
}
DisplaySnake();
}
- Util.cpp:
#include <Windows.h>
#include "Util.h"
#include <iostream>
using namespace std;
void Gotoxy(int x, int y) {
static HANDLE h = NULL;
if(!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = {x, y};
SetConsoleCursorPosition(h,c);
}
void Clearxy(int x, int y){
Gotoxy(x, y);
cout << ' ';
}
- Main.cpp:
#include "Game.h"
int main(){
Game game;
game.loop();
return 0;
}