Browse Source

implement lesson03

lesson03
Constantin Fürst 6 months ago
parent
commit
57b7127b24
  1. 22
      LESSONS.md
  2. 16
      components/Point.cpp
  3. 5
      components/Point.h
  4. 31
      main.cpp

22
LESSONS.md

@ -45,3 +45,25 @@ Every little snake needs to eat. And no snake will appear without food, which is
Once you're ready, you can _git merge lesson03_.
---
## Lesson 03
#### Prequisites
- Loops
#### Files
- _components/Point.cpp_
- _components/Point.h_
- _main.cpp_
#### Task
This Task will be a little on the short side. We plan to implement our Snake as a collection of points on the screen and for that, each point should be able to move by itself. This is the objective of this lesson.
1) Some new functions have appeared in _components/Point.*_ which you should inspect and try to implement.
2) Go to _main.cpp_ and see the new __mainL03__ which will use the movement functions now implemented to make one point move across the screen diagonally. Try it out!
3) As you can see, we use a for-loop for this but C++ has some other types of loop which you should familiarize yourself with. Try and play around with the commented-out loops and get the same result by using while and do-while loops.
Once you're ready, you can _git merge lesson04_.
---

16
components/Point.cpp

@ -22,6 +22,22 @@ uint32_t Point::getY() const {
// TODO: implement this getter
}
void Point::moveUp() {
// TODO: implement me
}
void Point::moveDown() {
// TODO: implement me
}
void Point::moveLeft() {
// TODO: implement me
}
void Point::moveRight() {
// TODO: implement me
}
int Point::getImg() const {
return img_;
}

5
components/Point.h

@ -16,6 +16,11 @@ public:
uint32_t getX() const;
uint32_t getY() const;
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
int getImg() const;
void setImg(int image);

31
main.cpp

@ -32,7 +32,36 @@ void mainL02() {
std::cout << "Helper QUIT" << std::endl;
}
void mainL03() {
Graphics::get().init("Learners Helper 02");
Point p(10,10);
for (uint32_t i = 0; i < 100; i++) {
p.moveDown();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
p.moveRight();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
/*
while(...) {
// TODO: play with this
}
*/
/*
do {
// TODO: play with this
} while (...)
*/
Graphics::get().finalize();
std::cout << "Helper QUIT" << std::endl;
}
int main() {
mainL02();
mainL03();
return 0;
}
Loading…
Cancel
Save