Browse Source

implement lesson06

lesson06
Constantin Fürst 6 months ago
parent
commit
34532d1ffc
  1. 24
      LESSONS.md
  2. 25
      components/Snake.cpp
  3. 4
      components/Snake.h
  4. 22
      main.cpp

24
LESSONS.md

@ -113,3 +113,27 @@ This one is quite difficult. Don't worry if it takes a lot of time and if you're
3) Try this out in main where you can still use the __mainL04__ from the previous task. 3) Try this out in main where you can still use the __mainL04__ from the previous task.
Once you're ready, you can _git merge lesson06_. Once you're ready, you can _git merge lesson06_.
---
## Lesson 06
#### Prequisites
None
#### Files
- _components/Snake.cpp_
- _components/Snake.h_
- _main.cpp_
#### Task
Our snake seems invincible for now and can't eat. Run into a wall - nothing will happen. Bite yourself - no injury. Try to eat the snack - you will just slither over it. Sad Snake :(
1) See the changes to _components/Snake.*_ and try to implement the test functions which we will use later on.
2) After implementing them, move to _main.cpp_ where __mainL06__ will allow you to test the newly written functions.
3) Figure out how you can test the bit-itself and bit-snack by yourself.
Once you're ready, you can _git merge lesson07_.
---

25
components/Snake.cpp

@ -81,3 +81,28 @@ void Snake::moveRight(){
direction_ = RIGHT; direction_ = RIGHT;
move(); move();
} }
bool Snake::isBitten() const {
const Point& head = snake_.front();
// TODO: return true if any point of the snake has the same position as the head
// remember to skip the head as the snake is not flexible enough to bite off its own head
return false;
}
bool Snake::hasBitSnack(uint32_t snackY, uint32_t snackX) const {
const Point& head = snake_.front();
// TODO: return true if the heads position is on the same coordinates as snack
// and return false otherwise
}
bool Snake::hasCrashedWall() const {
const Point& head = snake_.front();
return (head.getY() < GAME_TOP_WALL_Y) ||
(head.getY() > GAME_BOTTOM_WALL_Y) ||
(head.getX() < GAME_LEFT_WALL_X) ||
(head.getX() > GAME_RIGHT_WALL_X);
}

4
components/Snake.h

@ -24,4 +24,8 @@ public:
void moveLeft(); void moveLeft();
void moveRight(); void moveRight();
void move(); void move();
bool isBitten() const;
bool hasBitSnack(uint32_t snackY, uint32_t snackX) const;
bool hasCrashedWall() const;
}; };

22
main.cpp

@ -85,7 +85,27 @@ void mainL04() {
std::cout << "Helper QUIT" << std::endl; std::cout << "Helper QUIT" << std::endl;
} }
void mainL06() {
Graphics::get().init("Learners Helper 06");
Snake snake;
// TODO: Now the snake will eventually crash into the lower wall which we detect and then abort
// TODO: Figure out how to test the bit-itself and bit-snack functions yourself
while(true) {
snake.moveDown();
Graphics::get().refreshScreen();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (snake.hasCrashedWall()) break;
}
Graphics::get().finalize();
std::cout << "Helper QUIT" << std::endl;
}
int main() { int main() {
mainL04();
mainL06();
return 0; return 0;
} }
Loading…
Cancel
Save