Browse Source

implement lesson04

lesson04
Constantin Fürst 6 months ago
parent
commit
daf2fdf6ed
  1. 78
      components/Snake.cpp
  2. 27
      components/Snake.h
  3. 24
      main.cpp

78
components/Snake.cpp

@ -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();
}

27
components/Snake.h

@ -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();
};

24
main.cpp

@ -4,6 +4,7 @@
#include "components/Point.h" #include "components/Point.h"
#include "components/Snack.h" #include "components/Snack.h"
#include "components/Snake.h"
#include "input-output/Graphics.h" #include "input-output/Graphics.h"
void mainL01() { void mainL01() {
@ -35,7 +36,7 @@ void mainL02() {
} }
void mainL03() { void mainL03() {
Graphics::get().init("Learners Helper 02");
Graphics::get().init("Learners Helper 03");
Point p(10,10); Point p(10,10);
@ -65,7 +66,26 @@ void mainL03() {
std::cout << "Helper QUIT" << std::endl; std::cout << "Helper QUIT" << std::endl;
} }
void mainL04() {
Graphics::get().init("Learners Helper 04");
Snake snake;
for (uint32_t i = 0; i < 100; i++) {
snake.moveDown();
Graphics::get().refreshScreen();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
snake.moveRight();
Graphics::get().refreshScreen();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
Graphics::get().finalize();
std::cout << "Helper QUIT" << std::endl;
}
int main() { int main() {
mainL03();
mainL04();
return 0; return 0;
} }
Loading…
Cancel
Save