Untitled
plain_text
a month ago
6.1 kB
4
Indexable
Never
//----------------------------------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 "Apple.h" #include "Snake.h" #include "Map.h" class Game{ public: int _score; Map *_mapGame; Apple *_apple; Snake *_snake; //method Game(); void KeyboardHandle(); void Loop(); void CheckEat(); void DisplayScore(); }; //-------------------------------Map.h #pragma once #include "Point.h" 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 "util.h" #include "Point.h" #include "Map.h" #include <list> using namespace std; class Snake{ public: list<Point> _body; int _status; DIR _dir; Map *_pmap; int _isAlive; //method Snake(Map *p); void Eat(); void Move(); void Check(); void CheckStatus(); void DisplaySnake(); }; /-------------------------------util.h #pragma once enum DIR{UP, RIGHT, DOWN, LEFT}; void GoToxy(int x, int y); void Clearxy(int x, int y); //-----------------------------Apple.cpp #include "Apple.h" #include "Map.h" #include <time.h> #include <stdlib.h> #include "util.h" Apple::Apple(){ _pmap = 0; } Apple::Apple(Map* p){ _pmap = p; } void Apple::DisplayApple() { _pos.DrawPoint(); } void Apple::ClearApple() { Clearxy(_pos._x, _pos._y); _pmap -> _map[_pos._y][_pos._x] = ' '; } void Apple::GeneratePosition() { //ClearApple(); srand(time(NULL)); while (1){ int x = (rand() % (_pmap->_w - 2)) + 1; int y = (rand() % (_pmap->_h - 2)) + 1; //check if (_pmap -> _map[y][x] == ' '){ _pos = Point(x, y, '@'); break; } } DisplayApple(); } //Game.cpp #include "Game.h" #include <time.h> #include <conio.h> #include <Windows.h> #include <iostream> using namespace std; Game::Game(){ _mapGame =new Map(); _apple = new Apple(_mapGame); _snake = new Snake(_mapGame); _score = 0; } void Game::Loop(){ _snake->_status = 0; _mapGame->DisplayMap(); _apple->GeneratePosition(); DisplayScore(); while(1){ KeyboardHandle(); _snake->Move(); if (_snake->_status == -1){ break; } CheckEat(); _snake->DisplaySnake(); Sleep(50); } if (system("CLS")) system("clear"); cout << "Game Over" << endl; system("pause"); } void Game::KeyboardHandle(){ if (_kbhit()){ int kb = _getch(); if (kb == 75 && _snake->_dir != RIGHT){ _snake->_dir = LEFT; } else if (kb == 72 && _snake->_dir != DOWN){ _snake->_dir = UP; } else if (kb == 77 && _snake->_dir != LEFT){ _snake->_dir = RIGHT; } else if (kb == 80 && _snake->_dir != UP){ _snake->_dir = DOWN; } } } void Game::CheckEat(){ Point head = _snake->_body.front(); if (head._x == _apple->_pos._x && head._y == _apple->_pos._y){ _score ++; _snake->_status = 1; _apple->ClearApple(); _apple->GeneratePosition(); DisplayScore(); } else _snake->_status = 0; } void Game::DisplayScore(){ GoToxy(0, _mapGame->_h + 2); cout << "Your score: " << _score; } /------------------------------------main.cpp #include "Map.h" #include "Point.h" #include "Apple.h" #include "Snake.h" #include "Game.h" #include <iostream> #include <Windows.h> #include <conio.h> int main(){ Game game; game.Loop(); return 0; } //---------------------------------------map.cpp #include "Map.h" #include <iostream> using namespace std; Map::Map(){ _w = 50; _h = 25; char **p = new char*[_h]; for (int row = 0; row < _h; row ++){ p[row] = new char[_w]; for (int col = 0; col < _w; col ++){ if (row == 0 || row == _h - 1 || col == 0 || col == _w - 1) p[row][col] = '#'; else p[row][col] = ' '; } } _map = p; } void Map::DisplayMap(){ for (int row = 0; row < _h; row ++){ for (int col = 0; col < _w; col ++){ cout << _map[row][col]; } cout << endl; } } Map::~Map(){ for (int row = 0; row < _h; row ++){ delete[] _map[row]; } delete _map; _map = 0; _w = 0; _h = 0; } //--------------------------------point.cpp #include "Point.h" #include "util.h" #include <iostream> using namespace std; 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" #include "Point.h" #include "Map.h" Snake::Snake(Map* p){ _dir = RIGHT; _isAlive = 1; _body.push_back(Point(4, 1, '@')); _body.push_back(Point(3, 1, 'O')); _body.push_back(Point(2, 1, 'O')); _body.push_back(Point(1, 1, 'O')); _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, '@'); if (_pmap->_map[ny][nx] == '#' || _pmap->_map[ny][nx] == 'O'){ _status = -1; return; } _body.front()._c = 'O'; _body.push_front(newHead); if (_status == 0){ Clearxy(_body.back()._x, _body.back()._y); _pmap->_map[_body.back()._y][_body.back()._x] = ' '; _body.pop_back(); } } //--------------------------------------util.cpp #include <Windows.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 << ' '; }