Constantin Fürst
8 months ago
3 changed files with 127 additions and 2 deletions
@ -0,0 +1,78 @@ |
|||
#include "Snake.h"
|
|||
|
|||
#include <iostream>
|
|||
|
|||
Snake::Snake(uint32_t headY, uint32_t headX) |
|||
:direction_{LEFT} |
|||
{ |
|||
snake_.push_back(Point{headY, headX, '>'}); |
|||
|
|||
for (uint32_t i = 1; i <= SNAKE_DEFAULT_SIZE; i++) { |
|||
snake_.push_back(Point{headY, headX + i, SNAKE_BODY_CHAR}); |
|||
} |
|||
} |
|||
|
|||
void Snake::updateHead() { |
|||
switch (direction_) { |
|||
case UP: |
|||
snake_.front().moveUp(); |
|||
break; |
|||
case DOWN: |
|||
snake_.front().moveDown(); |
|||
break; |
|||
case LEFT: |
|||
snake_.front().moveLeft(); |
|||
break; |
|||
case RIGHT: |
|||
snake_.front().moveRight(); |
|||
break; |
|||
default: |
|||
std::cerr << "[x] This direction does not exist" << std::endl; |
|||
// OOPS!
|
|||
} |
|||
} |
|||
|
|||
void Snake::printSnake() const { |
|||
// TODO: for each point in snake_ call point.print()
|
|||
|
|||
Graphics::get().refreshScreen(); |
|||
} |
|||
|
|||
void Snake::move() { |
|||
auto head = snake_.begin(); |
|||
auto second = std::next(snake_.begin()); |
|||
|
|||
// update the previous to head node
|
|||
// by copying from head and setting
|
|||
// the image to be body instead of head
|
|||
*second = *head; |
|||
second->setImg(SNAKE_BODY_CHAR); |
|||
|
|||
updateHead(); |
|||
|
|||
printSnake(); |
|||
} |
|||
|
|||
void Snake::moveUp(){ |
|||
snake_.front().setImg('v'); |
|||
direction_ = UP; |
|||
move(); |
|||
} |
|||
|
|||
void Snake::moveDown(){ |
|||
snake_.front().setImg('^'); |
|||
direction_ = DOWN; |
|||
move(); |
|||
} |
|||
|
|||
void Snake::moveLeft(){ |
|||
snake_.front().setImg('>'); |
|||
direction_ = LEFT; |
|||
move(); |
|||
} |
|||
|
|||
void Snake::moveRight(){ |
|||
snake_.front().setImg('<'); |
|||
direction_ = RIGHT; |
|||
move(); |
|||
} |
@ -0,0 +1,27 @@ |
|||
#pragma once |
|||
|
|||
#include <vector> |
|||
|
|||
#include "Point.h" |
|||
#include "Snack.h" |
|||
|
|||
static constexpr int SNAKE_BODY_CHAR = 'o'; |
|||
static constexpr uint32_t SNAKE_DEFAULT_SIZE = 4; |
|||
|
|||
class Snake{ |
|||
private: |
|||
std::vector<Point> snake_; |
|||
int direction_; |
|||
|
|||
void updateHead(); |
|||
void printSnake() const; |
|||
|
|||
public: |
|||
Snake(uint32_t headY = LINES/2, uint32_t headX = COLS/2); |
|||
|
|||
void moveUp(); |
|||
void moveDown(); |
|||
void moveLeft(); |
|||
void moveRight(); |
|||
void move(); |
|||
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue