Beginner C++ Game Programming Tutorial 14
This is a two-part tutorial in which we tackle a game with a little more substance--the Snake Game. Grids and arrays galore. Majestic.
Contents
Main Concepts Taught
- Managing used/unused sections of an array
-
struct
vsclass
- Inner (nested)
class
es -
operator
overloading
Other New(ish) Things
There was a bunch of other newish bullshit that Chili used, some of which he didn't go into much depth on. For those of you who give a shit, here is some extra info.
Aggregate Initialization {}
With the struct Location
(and later, class
), we didn't define any constructors, so all we had was the default do-nothing constructor that is generated by the compiler. In order to initialize a Location
at one point in the video, Chili used something like Location loc = {69,69};
. This is allowed, but the data type being initialized must satisfy certain conditions. Namely, it must have only public
data members and it must have no constructors defined. For the full details on the requirements, see this page. The brace {}
initialization can be used for class
es etc. that have constructors as well, but then the parameters have to conform to the constructor(s) and it will just call the applicable constructor.
Compiler-Generated Copy Constructor and Copy Assignment operator
In the tutorial, we assign Location
objects to one another, and we construct a Location
by copying an existing one. These operations actually call member functions that are defined for the class
(or struct
), and if you do not supply these member functions, the compiler will generate them by default (as is the case in this tutorial).
-
ClassName::ClassName( const ClassName& )
The default copy constructor will in turn copy construct each of the members of the object under construction using the corresponding member in the object being passed in.
-
ClassName& ClassName::operator=( const ClassName& )
The default copy assignment operator will in turn copy assign each of the members of the object under assignment using the corresponding member in the object being passed in. It also returns a reference to the object under assignment.
Video Timestamp Index
Tutorial 14b (coming soon!)
Homework
The homework is to add some polish to the Snek Game. The following features are suggested and will be covered in the solution video.
- Adjustable board position
- Border drawn around board
- Padding between board tiles
- Display title screen
- Give the Snek a pattern of colors for its segments
- Make it so the Snek speeds up over time
In addition, it is suggested that you extend the Snek game by adding some new mechanics to spice things up. One suggestion is making randomly spawning obstacles that accrue on the game board. Note that this extra challenge will not be covered in the solution video. If you are attempting it (or some other feature) and need some help, you can seek help on the forum or the Discord.