BYTEPATH #4 - Exercises #14

Closed
opened this Issue Apr 25, 2017 · 13 comments

Projects
None yet
7 participants
Owner

＠ＳＳＹＧＥＡ

In the previous three tutorials we went over a lot of code that didn't have anything to do directly with the game. All of that code can be used independently of the game you're making which is why I call it the `engine` code in my head, even though I guess it's not really an engine. As we make more progress in the game I'll constantly be adding more and more code that falls into that category and that can be used across multiple games. If you take anything out of these tutorials that code should definitely be it and it has been extremely useful to me over time.

Before moving on to the next part where we'll start with the game itself you need to be comfortable with some of the concepts taught in the previous tutorials, so here are some more exercises. If you can't do some of them it's fine, but it's important to at least try!

Exercises

1. Create a `getGameObjects` function inside the `Area` class that works as follows:

```-- Get all game objects of the Enemy class
all_enemies = area:getGameObjects(function(e)
if e:is(Enemy) then
return true
end
end)

-- Get all game objects with over 50 HP
healthy_objects = area:getGameObjects(function(e)
if e.hp and e.hp >= 50 then
return true
end
end)```

It receives a function that receives a game object and performs some test on it. If the result of the test is true then the game object will be added to the table that is returned once `getGameObjects` is fully run.

2. What is the value in `a`, `b`, `c`, `d`, `e`, `f` and `g`?

```a = 1 and 2
b = nil and 2
c = 3 or 4
d = 4 or false
e = nil or 4
f = (4 > 3) and 1 or 2
g = (3 > 4) and 1 or 2```

3. Create a function named `printAll` that receives an unknown number of arguments and prints them all to the console. `printAll(1, 2, 3)` will print 1, 2 and 3 to the console and `printAll(1, 2, 3, 4, 5, 6, 7, 8, 9)` will print from 1 to 9 to the console, for instance. The number of arguments passed in is unknown and may vary.

4. Similarly to the previous exercise, create a function named `printText` that receives an unknown number of strings, concatenates them all into a single string and then prints that single string to the console.

5. How can you trigger a garbage collection cycle?

6. How can you show how much memory is currently being used up by your Lua program?

7. How can you trigger an error that halts the execution of the program and prints out a custom error message?

8. Create a class named `Rectangle` that draws a rectangle with some width and height at the position it was created. Create 10 instances of this class at random positions of the screen and with random widths and heights. When the `d` key is pressed a random instance should be deleted from the environment. When the number of instances left reaches 0, another 10 new instances should be created at random positions of the screen and with random widths and heights.

9. Create a class named `Circle` that draws a circle with some radius at the position it was created. Create 10 instances of this class at random positions of the screen with random radius, and also with an interval of 0.25 seconds between the creation of each instance. After all instances are created (so after 2.5 seconds) start deleting once random instance every [0.5, 1] second (a random number between 0.5 and 1). After all instances are deleted, repeat the entire process of recreation of the 10 instances and their eventual deletion. This process should repeat forever.

10. Create a `queryCircleArea` function inside the `Area` class that works as follows:

```-- Get all objects of class 'Enemy' and 'Projectile' in a circle of 50 radius around point 100, 100
objects = area:queryCircleArea(100, 100, 50, {'Enemy', 'Projectile'})```

It receives an `x`, `y` position, a `radius` and a list of strings containing names of target classes. Then it returns all objects belonging to those classes inside the circle of radius `radius` centered in position `x, y`.

11. Create a `getClosestGameObject` function inside the `Area` class that works follows:

```-- Get the closest object of class 'Enemy' in a circle of 50 radius around point 100, 100
closest_object = area:getClosestObject(100, 100, 50, {'Enemy'})```

It receives the same arguments as the `queryCircleArea` function but returns only one object (the closest one) instead.

12. How would you check if a method exists on an object before calling it? And how would you check if an attribute exists before using its value?

13. Using only one `for` loop, how can you write the contents of one table to another?

14. Like I did for Nuclear Throne and The Binding of Isaac, break down the game Recettear in terms of rooms. Watch this gameplay video up to to 2 minutes or so (when the player moves on to the next level of the dungeon) to get an idea of most of the screens involved.

15. Break down the game Shovel Knight in terms of rooms. Watch this video up to the end when the overworld is shown (you can skip most of the gameplay in the middle) to get an idea of most of the screens involved.

frarf commented May 25, 2017

 Man, I hope the next post comes out soon. These are really good, thanks!

synthkaf commented May 25, 2017

 @frarf hahah, same! This series is awesome. It felt great when I did that exercise for the HP bar animation. Fascinating how something so simple can look so damn cool.
Owner Author

 @frarf @snowcrshd I'm writing the rest of it first and then I'm going to release it all at once. Hopefully I'll be done sometime next month.

frarf commented May 25, 2017

 Would personally prefer it to be sooner, but since it's in summer, what the heck? Seriously though, these are awesome. Keep up the great work!

synthkaf commented May 25, 2017

 @SSYGEA thanks for the update 😄 Please take your time, this must be a hell of a lot of work to do.

frarf commented Jul 7, 2017

 Although I'm being an impatient twat, when can we expect new posts? Still, take your time, these are amazing.
Owner Author

 @frarf Sorry, they're coming eventually, but it's taking longer than I expected and I also have been slower than I wanted to be. I'm not gonna make any promises about when anymore because I wasn't able to keep myself to my previous timeline, but the posts will be done and they will be posted all at once like I said previously.

kevinfiol commented Aug 11, 2017

 Lookin forward to this!

4v0v commented Aug 12, 2017

 Same here, can we know how many articles are left ?
Owner Author

 @4-V-0-V Same here, can we know how many articles are left? I finished 10 articles so far and there are 5 more left.

4v0v commented Aug 14, 2017

 Nice ! I can't wait to see them, your tutorial is one of the only to talk about game architecture, it already helped me a lot to understant it, thanks !

nmuth commented Aug 15, 2017

 Might as well turn the posts into an ebook at this point 😛 Should be a good read!