Page 1 of 7

Snake Game Tutorial 14a

Posted: January 12th, 2017, 1:09 am
by Blood
*New to this forum*

I'm having trouble understanding Snake Game Tutorial 14a. Are there any more in-depth explanations as to what is happening or anywhere I can look to help me understand what is being taught? I don't enjoy the thought of moving on to the other lessons before getting one done. Help would be appreciated, thanks!

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 1:30 am
by chili
Hey bro. There are no other guides besides the videos. What you do have is the man who made the damn videos and code himself, right here and now baby. If you can explain what parts you're having difficulty with (be as specific and detailed as possible/reasonable), we can get to the bottom of this shit.

No need to type out a huge manifesto of everything you're having troubles with all at once. Just start with one or two specific things that you're not getting, and we'll go from there.

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 2:06 am
by Blood
Thanks! I appreciate your willingness to help.

The first thing I'm having a bit of a difficulty understanding (from the beginning of the tutorial) is when you create a reference to the gfx object in the Board class ( 9:36 in the tutorial ), and how you need to create a constructor to initialize the graphics object. Why would you need to initialize it ? Isn't the graphics object already initialized, allowing you to create a reference to it? I don't understand that process. The second thing I didn't quite understand is how you would need to include two parenthesis when you used std::random_device ( 13:31 in the tutorial ). I'm used to using std::random_device to create a seed that can be passed to the random_engine like in ( std::random_device rd; ) or however you say it, so I don't really understand what is going on in "std::random_device () () ". Again, thank you for your help! (:

Another thing, the Snake Game starting solution (downloaded through the wiki page) doesn't seem to have the DrawRectDim and the other function in the Graphics files, not sure if that was intentional or not. o;

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 2:24 am
by albinopapa
1) Board constructor
You are not recreating the graphics object, you are merely initializing the Graphics & that is store in Board.

Code: Select all

class Board
{
public:
    // Pass the graphics object as a reference to Board constructor
    Board(Graphics &gfx)
        :
    gfx(gfx)                 // <- here, the left gfx is the Graphics reference stored in Board
    {}                        // and the right gfx is the constructor parameter used to initialize the left gfx

private:
    // This reference has to be initialized with the Graphics object that is created in Game so it knows
    // where to get it's data.
    Graphics &gfx
};
2) The second set of parans in the std::random_device

In C++ you can overload many operators and one such operator is the function operator. What this means is you can treat a class like a function and you call the function using the name of the object you create...but in this case, you are calling the function operator with the temporary that is created

Code: Select all

std::random_device()()   // This creates a temporary random_device object, then calls the operator() 
                                    // (function operator) on that temporary object.

// It's the same as, just without naming the object.
std::random_device rd;
rd();

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 2:46 am
by chili
Blood wrote:Thanks! I appreciate your willingness to help.

The first thing I'm having a bit of a difficulty understanding (from the beginning of the tutorial) is when you create a reference to the gfx object in the Board class ( 9:36 in the tutorial ), and how you need to create a constructor to initialize the graphics object. Why would you need to initialize it ? Isn't the graphics object already initialized, allowing you to create a reference to it? I don't understand that process.
'Isn't the graphics object already initialized, allowing you to create a reference to it?'

Well, the reference in the Board class is different than the graphics object. When we have a reference to an object of class Graphics, that reference can point to any Graphics object. It needs to be set to point to OUR graphics object (the one that gets created at the beginning of the program). The constructor is how we do that. We pass in a reference to OUR Graphics object, and then the constructor initializes the reference in Board to point to that same object. So the constructor is not initializing a Graphics OBJECT, it is initializing a Graphics REFERENCE to point to a particular Graphics object.
Blood wrote:The second thing I didn't quite understand is how you would need to include two parenthesis when you used std::random_device ( 13:31 in the tutorial ). I'm used to using std::random_device to create a seed that can be passed to the random_engine like in ( std::random_device rd; ) or however you say it, so I don't really understand what is going on in "std::random_device () () ".
The first () is for constructing a random_device. The second () calls the function of the random_device to generate a random number. Like papa said, you could just as easily construct a random device on one line, and then call it's generating function on another line.
Blood wrote:Another thing, the Snake Game starting solution (downloaded through the wiki page) doesn't seem to have the DrawRectDim and the other function in the Graphics files, not sure if that was intentional or not. o;
Thanks for the heads up. I updated all the source code zips, and I might have missed adding that stuff. I'll have to take a look and make some changes if necessary.

What else you got? :D

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 3:18 am
by Blood
Thanks for your awesome replies! That really cleared things up.

Also, thanks Chili for updating the zip files. I'll let you know if I come across anything else that's difficult for me to get a grasp on. I appreciate both your time. It's nice to know that I can rely on Planet Chili to clear things up! lol (:

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 3:52 am
by Blood
Do you think you can further elucidate on your explanation of the DrawRectDim function and how it's working? You said something about multiplying the coordinates by the dimension to create the rectangle. ( 12:02 on the tutorial )

It's still a bit foggy for me on that one.

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 8:29 am
by chili
Well the DrawRectDim() doesn't have anything to do with multiplication... it just draws a rectangle with x,y,width,height instead of with left,right,top,bottom. I suppose what you mean is the cell drawing routine of class Board.

In that one we're not really multiplying the coordinates in cell space by the width/height of the cells to get the coordinates in screen space (if you have cell (2,0) in cell space, and cells are 50x50, then that cell would be (100,0) in screen space, assuming the top left corner of the grid is at (0,0) on the screen). Kinda hard to explain without a pic tho.

What specifically is confusing you. Maybe if you talk it out a little more in text, what parts you understand, what parts you think you understand or guess, and what parts baffle you. Maybe then I can help you better.

Re: Snake Game Tutorial 14a

Posted: January 12th, 2017, 7:58 pm
by Blood
Thanks for that explanation @Chilli
I understand it better now. Practice always helped me to understand everything better as well.
Have you been able to update the starting code for the Snake Game?
I'll definitely keep in touch if I come across something befuddling. (:

Re: Snake Game Tutorial 14a

Posted: January 13th, 2017, 3:36 am
by chili
Yup, just updated the zip right now. All is well, thanks for the heads up bro.