Browse Source

implement lesson02

lesson02
Constantin Fürst 8 months ago
parent
commit
81d143a593
  1. 23
      LESSONS.md
  2. 8
      components/Snack.cpp
  3. 6
      components/Snack.h

23
LESSONS.md

@ -19,4 +19,25 @@
Once you're ready, you can _git merge lesson02_.
---
---
## Lesson 02
#### Prequisites
- Random Numbers
- Functions
- Pointers/References
#### Files
- _components/Snack.cpp_
- _components/Snack.h_
- _main.cpp_
#### Task
Every little snake needs to eat. And no snake will appear without food, which is why we begin by providing this essential item.
1) We want to implement the _Snack_ as an instance of the Point-Class from the previous Task. Therefore we will write a function that turns an ordinary Point into a delicious Snack. This will be called _generateSnack_ and we will implement it in _components/Snack.*_ where you can see the skeleton for the function.
2) A Snack must be placed at a random point when generating, so set its x and y value to a random value that fits on the playing field (see the comment in the function skeleton for a hint).
3) A Snack must also look enticing, so set its image to the one defined as SNACK_CHAR and then display the delicious snack.
4) Experiment and use __mainL02__ to test if your code works.
5) Notice anything interesting with how we pass the Point to the generateSnack-function? The function requests a pointer which is the address of an object instance. To get the address of an existing object, we use operator&. Read up on pointers and references.

8
components/Snack.cpp

@ -0,0 +1,8 @@
#include "Snack.h"
void generateSnack(Point* snack){
// TODO: implement me
// should set a random position for the point, update its character/image and display the point
// x should be between (0, GAME_RIGHT_WALL_X)
// y should be between (0, GAME_BOTTOM_WALL_Y)
}

6
components/Snack.h

@ -0,0 +1,6 @@
#pragma once
#include "Point.h"
static constexpr int SNACK_CHAR = '*';
void generateSnack(Point* snack);
Loading…
Cancel
Save