Difference between revisions of "Beginner C++ Game Programming Tutorial 14"

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
m (Text replacement - "http://www.planetchili.net/" to "https://www.planetchili.net/")
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
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.
 
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.
 +
 +
== Super Important NOTE(S) ==
 +
The Snek game is a little more complicated than the Poo game. Don't get butthurt and give up if you can't make it work. Just make sure you understand the Main Concepts Taught listed below. <span style="color:pink">You can always move on to the next tutorials and come back to the Snek game later when you've got a little more experience under your belt.</span> And as usual, if you're lost or just have some lingering questions, the forum is a great place to go for help. Here is a forum discussion about the Snek game code: [https://www.planetchili.net/forum/viewtopic.php?f=3&t=3838 Snek Talk]. Feel free to chime in and add info or ask questions! You can also @mention me on the [https://www.planetchili.net/discord Discord] and maybe we can talk in real time (timezone diffs might make this hard thoough).
 +
 +
If you are going to attempt to finish the game off by yourself at the end of T14a (good on you mate), there is something you should be aware of. The function <code>Snake::Grow()</code> does not set the position of the "new" segment when it grows the snake. So you '''absolutely must''' call <code>Snake::MoveBy(...)</code> '''''before''''' you draw the snake. If you attempt to draw the snake after growing, but without moving first, the last segment will have an invalid <code>loc</code>, and trying to draw it in that state will most likely crash the program.
 +
 +
Also, there is a bug in the <code>Snake::Grow()</code> function at the end of 14a. See if you can figure out what it is, and how to fix it... :)
  
 
== Main Concepts Taught ==
 
== Main Concepts Taught ==
Line 6: Line 13:
 
* Inner (nested) <code>class</code>es
 
* Inner (nested) <code>class</code>es
 
* <code>operator</code> overloading
 
* <code>operator</code> overloading
 +
* Special functions: copy constructor and copy assignment (briefly)
 +
* The <code>%</code> (modulus) operator (in the solution video)
 +
* The <code>std::max()</code> function (in the solution video)
  
 
== Other New(ish) Things ==
 
== Other New(ish) Things ==
Line 13: Line 23:
 
==== Aggregate Initialization <code>{}</code> ====
 
==== Aggregate Initialization <code>{}</code> ====
 
With the <code>struct Location</code> (and later, <code>class</code>), 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 <code>Location</code> at one point in the video, Chili used something like <code>Location loc = {69,69};</code>. This is allowed, but the data type being initialized must satisfy certain conditions. Namely, it must have only <code>public</code> data members and it must have no constructors defined. For the full details on the requirements, see [http://en.cppreference.com/w/cpp/language/aggregate_initialization this page]. The brace <code>{}</code> initialization can be used for <code>class</code>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.
 
With the <code>struct Location</code> (and later, <code>class</code>), 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 <code>Location</code> at one point in the video, Chili used something like <code>Location loc = {69,69};</code>. This is allowed, but the data type being initialized must satisfy certain conditions. Namely, it must have only <code>public</code> data members and it must have no constructors defined. For the full details on the requirements, see [http://en.cppreference.com/w/cpp/language/aggregate_initialization this page]. The brace <code>{}</code> initialization can be used for <code>class</code>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 <code>operator</code> ====
 +
In the tutorial, we assign <code>Location</code> objects to one another, and we construct a <code>Location</code> by copying an existing one. These operations actually call member functions that are defined for the <code>class</code> (or <code>struct</code>), and if you do not supply these member functions, the compiler will generate them by default (as is the case in this tutorial).
 +
 +
* <code>ClassName::ClassName( const ClassName& )</code>
 +
This is the default copy constructor, and its behavior is to, in turn, copy construct each of the members of the object under construction using the corresponding member in the object being passed in.
 +
* <code>ClassName& ClassName::operator=( const ClassName& )</code>
 +
This is the  default copy assignment operator, and its behavior is to, 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 ==
 
== Video Timestamp Index ==
 
[https://youtu.be/KTyemy82Zqk Tutorial 14a]
 
[https://youtu.be/KTyemy82Zqk Tutorial 14a]
  
Tutorial 14b (coming soon!)
+
[https://youtu.be/DoO_NEksf3c Tutorial 14b]
  
 
== Homework ==
 
== Homework ==
To be announced!
+
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 [[Planet_Chili_Forums|forum]] or the [http://www.planetchili.net Discord].
 +
 
 +
The solution video is [https://www.youtube.com/watch?v=aE9fGMRRe6U here].
  
 
== Downloads ==
 
== Downloads ==
  
* [http://www.planetchili.net/downloads/T14-Snek-Start.zip Snek Starting Solution]
+
* [https://www.planetchili.net/downloads/T14-Snek-Start.zip Snek Starting Solution]
 +
* [https://github.com/planetchili/snek GitHub Repo Containing Finished Snek Game]
  
 
== See also ==
 
== See also ==

Latest revision as of 13:23, 5 May 2022

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.

Super Important NOTE(S)

The Snek game is a little more complicated than the Poo game. Don't get butthurt and give up if you can't make it work. Just make sure you understand the Main Concepts Taught listed below. You can always move on to the next tutorials and come back to the Snek game later when you've got a little more experience under your belt. And as usual, if you're lost or just have some lingering questions, the forum is a great place to go for help. Here is a forum discussion about the Snek game code: Snek Talk. Feel free to chime in and add info or ask questions! You can also @mention me on the Discord and maybe we can talk in real time (timezone diffs might make this hard thoough).

If you are going to attempt to finish the game off by yourself at the end of T14a (good on you mate), there is something you should be aware of. The function Snake::Grow() does not set the position of the "new" segment when it grows the snake. So you absolutely must call Snake::MoveBy(...) before you draw the snake. If you attempt to draw the snake after growing, but without moving first, the last segment will have an invalid loc, and trying to draw it in that state will most likely crash the program.

Also, there is a bug in the Snake::Grow() function at the end of 14a. See if you can figure out what it is, and how to fix it... :)

Main Concepts Taught

  • Managing used/unused sections of an array
  • struct vs class
  • Inner (nested) classes
  • operator overloading
  • Special functions: copy constructor and copy assignment (briefly)
  • The % (modulus) operator (in the solution video)
  • The std::max() function (in the solution video)

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 classes 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& )

This is the default copy constructor, and its behavior is to, 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& )

This is the default copy assignment operator, and its behavior is to, 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 14a

Tutorial 14b

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.

The solution video is here.

Downloads

See also