Page 3 of 6 [ 51 posts ] Go to page Previous  1, 2, 3, 4, 5, 6  Next

#### Snake Game Tutorial 14a

 Print view Previous topic | Next topic

#### Snake Game Tutorial 14a

Author Message
 Post subject: Re: Snake Game Tutorial 14a  Posted: January 21st, 2017, 6:06 am

Joined: November 30th, 2013, 7:40 pm
Posts: 618
From what i remember of the video, i believe that you need to move from tail to head so that you have the loc, or data positions still available to move to. If you follow someone, you want to move to their last known location before they move ahead. In the case of snek, so long as all tail segments move to the one ahead of it, the head can move to any location, because the one behind it will occupy the heads last known location. And so on and so on, until of course, collision decisions are made. haha, never was good at explaining, but thats how i took it
Peace Out

_________________
Curiosity killed the cat, satisfaction brought him back

 Post subject: Re: Snake Game Tutorial 14a  Posted: January 21st, 2017, 2:28 pm

Joined: January 12th, 2017, 12:28 am
Posts: 18
Thanks for that (:

I'm a little more confused by the MoveBy function, though.

 Post subject: Re: Snake Game Tutorial 14a  Posted: January 23rd, 2017, 2:33 pm

Joined: December 31st, 2011, 4:53 pm
Posts: 3497
Location: Japan
Blood wrote:
Thanks for that (:

I'm a little more confused by the MoveBy function, though.

Well the MoveBy() function basically does exactly what Godin said above. Every segment moves to the position of the segment in front of it, thus advancing the Snek. The head segment has no segment in front of it to move to; it is moved based on the delta_loc parameter. For example, a delta_loc of {1,0} would move the Snek head 1 square to the right, and a delta of {0,-1} would move it one square up.

_________________
Chili

 Post subject: Re: Snake Game Tutorial 14a  Posted: January 23rd, 2017, 7:03 pm

Joined: November 30th, 2013, 7:40 pm
Posts: 618
Haha, do i get a big ole star for getting that right .. haha, just kidding, hope the explanation helps you out there Blood.

_________________
Curiosity killed the cat, satisfaction brought him back

 Post subject: Re: Snake Game Tutorial 14a  Posted: March 13th, 2017, 10:20 am

Joined: March 13th, 2017, 9:45 am
Posts: 2
Hi guys.
I have a problem that has stopped me for a while.
The problem is that I cannot get what Cili is doing with Initializers and Constructions. At the first in tutorial 12 he mentioned that we should use constructors because it is good for encapsulation and setters and getters are not good but, they are possible. Also the constructor that my friend Chili used for class poo in tutorial 12 is different from that one used in tutorial 14 for snake and it seems that we have to use constructor also the way we implement that construction is important either.
Would you please talk a bit more about these stuff like when we have to use construction and how we should use them? Thanks a lot.

 Post subject: Re: Snake Game Tutorial 14a  Posted: March 13th, 2017, 12:53 pm

Joined: February 28th, 2013, 3:23 am
Posts: 2866
Location: Oklahoma, United States
When you create a class, you have a few options on how to initialize your data members like position, velocity or anything else you declare inside your class.

1) For data types you can initialize them in the header
Code:
Vec2 position = { 0.f, 0.f };
float speed = 10.f;

2) You can use the default constructor, which is the name of the class with empty set of parentheses
Code:
class MyClass
{
public:
MyClass();
};

3) You can create an init function that you can call separately after declaration.
Code:
MyClass object;
object.Initialize();

The idea about constructors is you use them to initialize the data members of that class. This way you don't have to declare them on one line and initialize them on another. Sometimes, you may forget to initialize them, but if you initialize everything in the constructor, you can't forget because it's done when you declare the object.

If you have a class that shouldn't be created without specific data, then you should define a constructor that takes parameters and NOT define a default constructor.
Code:
MyClass( const Vec2 &Position, float Speed );

In this case, you are forced to provide data before you can create a MyClass object. There is a small problem with this though, you can't have an array of those objects, because when setting up the array the compiler will need access to a default constructor ( the one without parameters ). In this case, you will have to provide both the default constructor and a constructor that takes initialization data.
Code:
class MyClass
{
public:
MyClass();  // default constructor
MyClass( const Vec2 &Position, float Speed );

private:
Vec2 position;
float speed;
};

Because of this limitation and the fact chili hasn't covered pointers in the tuts yet nor std::vector is probably why he chose to use the Initialize() function route, though he'd have to answer that for himself.

So, when do you need to use a constructor? Every class needs a constructor, but if you don't create one, the compiler will create a hidden default constructor for you. If you create a constructor other than the default, you will have to use that constructor each time to create an object of that type. You can force the compiler to create a default constructor by ending the line with the word default.
Code:
MyClass() = default;

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

 Post subject: Re: Snake Game Tutorial 14a  Posted: March 14th, 2017, 9:59 am

Joined: March 13th, 2017, 9:45 am
Posts: 2
Dear Albinopapa, thanks for comprehensive response.
Code:
Board::Board( Graphics& gfx )
:
gfx( gfx )
{}

and an other question is that in the Board class, Chili has interred random number generator in the initializer. why didn'd he just generate one inside the class?

 Post subject: Re: Snake Game Tutorial 14a  Posted: March 14th, 2017, 1:49 pm

Joined: February 28th, 2013, 3:23 am
Posts: 2866
Location: Oklahoma, United States
The Board constructor you list, what is it you want to know? Are you referring to the initializer list? The part after the colon?

If so, this is where you initialize your class members. Since Board holds a reference to Graphics, you have to initialize it with something since you can't reassign a reference. Values in the initializer list are assigned to class members before the function body ( the code between the curly braces {} ).

Code:
// It's something similar to
int a = 7;
// as opposed to
int a;
a = 7;

When you use the initializer list, class members are initialized with values at the same time memory is allocated as opposed to allocating memory, default initializing the variable then assigning it a new value.

In some cases as is the case with storing a reference as a member of a class, you are required to use the initializer list.
In most cases you should use it or initialize your data in the class declaration ( ie in the header file ).
In rare cases, you will not be able to use the initializer list, like when you store an array, because each object in the array is first default initialized and you must assign them new values using the function body of the constructor.

Constructors aren't really all the difficult to understand, they are just used to initialize your class members. They have two sections; the initializer list and the function body. The initializer list is used to initialize class members when the class object is instantiated. The function body is used to initialize arrays or more complex data members, like structs that don't have constructors for instance.

Another use for the constructor body would be to call a function to initialize members that might be reset, like when a player dies, you might want to reset their position to some point as well as all the rest of the entities in that level. You would probably want to call that reset function at will, so instead of retyping all your initialization code in the constructor, you would just call the reset function in the constructor body and some where else where the condition is met to do so.

Look up RAII ( Resource Acquisition Is Initialization ) to get a better idea of why constructors and their counter-parts the destructors are useful.

That's about all I can really say about constructors, hopefully between this and information you find on RAII, you'll have a better (solid) understanding of constructors.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

 Post subject: Re: Snake Game Tutorial 14a  Posted: March 18th, 2017, 1:52 pm

Joined: February 14th, 2017, 7:32 pm
Posts: 170
I've got a few questions too.
When he initializes
Code:
Location loc;

for what is he initializing the location?
Because it isn't an array, it isn't for the snake segments.
If I recall correctly he used it in a for statement and initialized them on the go (although I also recall him using loc in other places, probably as a local variable), but for what did he do so?
Also, the x and y coordinates, are those the spaces in the grid, (for example 0, 1 is the square to the right of the one in the top left) or are they the coordinates on the screen. (where 0, 1 would be one pixel to the right)

 Post subject: Re: Snake Game Tutorial 14a  Posted: March 19th, 2017, 7:49 am

Joined: February 28th, 2013, 3:23 am
Posts: 2866
Location: Oklahoma, United States
You should go back through the video. Yeah, location is the grid position, not screen position. Escpecially if it is inside the Board class. Locations inside the board class deal with grid locations, while draw functions deal in pixels.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Display posts from previous:  Sort by

 Page 3 of 6 [ 51 posts ] Go to page Previous  1, 2, 3, 4, 5, 6  Next

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: