From 81d143a593219a07559f26d44f2b21d0d0f96698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Constantin=20F=C3=BCrst?= Date: Wed, 1 May 2024 15:24:53 +0200 Subject: [PATCH] implement lesson02 --- LESSONS.md | 23 ++++++++++++++++++++++- components/Snack.cpp | 8 ++++++++ components/Snack.h | 6 ++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 components/Snack.cpp create mode 100644 components/Snack.h diff --git a/LESSONS.md b/LESSONS.md index c62f358..f432ee4 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -19,4 +19,25 @@ Once you're ready, you can _git merge lesson02_. ---- \ No newline at end of file +--- + +## 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. \ No newline at end of file diff --git a/components/Snack.cpp b/components/Snack.cpp new file mode 100644 index 0000000..d6b7ccf --- /dev/null +++ b/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) +} diff --git a/components/Snack.h b/components/Snack.h new file mode 100644 index 0000000..c249720 --- /dev/null +++ b/components/Snack.h @@ -0,0 +1,6 @@ +#pragma once +#include "Point.h" + +static constexpr int SNACK_CHAR = '*'; + +void generateSnack(Point* snack);