Untitled
unknown
plain_text
2 years ago
5.4 kB
12
Indexable
#include"Point.h"
#include"util.h"
#include <iostream>
using namespace std;
Point:: Point(){}
Point::Point(int a, int b , char cc){
_x = a;
_y = b;
c = cc;
}
void Point::DrawPoint(){
gotoxy(_x, _y);
cout<< c;
}
#include"Apple.h"
#include"Map.h"
#include<time.h>
#include<stdlib.h>
#include"util.h"
#include<iostream>
using namespace std;
Apple::Apple(){
_pmap = 0;
}
Apple :: Apple(Map *p){
_pmap = p;
}
void Apple::DisPlayApple(){
_pos.DrawPoint();
_pmap->map[_pos._y][_pos._x] = '@';
}
void Apple:: ClearApple(){
Clearxy(_pos._x , _pos._y); // clear tren consol
_pmap->map[_pos._y][_pos._x] = ' '; // clear tren map
}
void Apple::generatePosition(){
// ClearApple();
int x, y;
do{
srand(time(NULL));
x = (rand() % (_pmap-> _w-2))+1;
y = (rand() % (_pmap-> _h-2))+1;
}
while(_pmap->map[y][x] != ' ');
// check
_pos = Point(x,y,'@');
DisPlayApple();
}
#include"Snake.h"
#include"util.h"
#include<conio.h>
#include"Map.h"
using namespace std;
snake :: snake(Map *p){
_dir = UP;
status = 0;
int start = 20;
_pmap = p;
_body.push_back(Point(6,start,'O'));
_body.push_back(Point(6, start +1,'*'));
_body.push_back(Point(6,start +2,'*'));
}
void snake :: DisPlay(){
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] = ' ' ;
_body.pop_back();
}
//body.back().c='.';
DisPlay();
}
#include "Map.h"
#include<iostream>
using namespace std;
Map::Map(){
_w = 50;
_h= 50;
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 || r == _h-1 || c ==0 || 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;
_h = 0;
_w = 0;
}
#include"Game.h"
#include<conio.h>
#include<Windows.h>
#include"Apple.h"
#include<iostream>
using namespace std;
Game::Game(){
_mapGame = new Map();
_mapGame->DisPlayMap();
_apple = new Apple(_mapGame);
_snake = new snake(_mapGame);
_snake->DisPlay();
_score = 0;
}
void Game:: Loop(){
_apple ->generatePosition();
_snake->Move();
while(1){
Checkey();
_snake->Move();
if(_snake->status == -1){
break;
}
if( CheckSnakeEatApple()){
DisPlayScore();
}
Sleep(300);
}
if(system("CLS")) system("clear");
gotoxy(0,0);
cout<< "Game over" << _score;
system ("pause");
}
void Game :: Checkey(){
if(_kbhit()){
int tmp = _getch();
if(tmp == (int)KEY_UP && _snake->_dir != DOWN){
_snake->_dir = UP;
}
else if(tmp == (int)KEY_DOWN && _snake->_dir != UP){
_snake->_dir = DOWN;
}
else if(tmp == (int)KEY_RIGHT && _snake->_dir != LEFT){
_snake->_dir = RIGHT;
}
else if(tmp == (int)KEY_LEFT && _snake->_dir != RIGHT){
_snake->_dir= LEFT;
}
}
}
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:: DisPlayScore(){
gotoxy(0, _mapGame -> _h +2);
cout << "SCORE : " << _score;
}
Game:: ~Game(){
delete _snake;
delete _mapGame;
delete _apple;
}
#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 << ' ';
}
Editor is loading...