Snake.cpp

 avataruser_6225994
plain_text
a month ago
1.2 kB
1
Indexable
Never
#include "Snake.h"
#pragma once


Snake::Snake(Map *p){
	_dir = RIGHT;
	_pmap = p;
	int intitHead = 20;
	_body.push_back(Point(2, 2, 'O'));
	_body.push_back(Point(2, 3, '*'));
	_body.push_back(Point(2, 4, '*'));
	_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 if (_dir == LEFT){
		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 = '.';
    }

	DisPlaySnake();
}