Snake game c++ tail class how to add new object





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















i'm a beginner programmer of c++ and i would like some help to continue my snake project.
For making a bit of practice with classes i decided to this program using them to not take the bad habit of use things like hundreads of functions that can be sintethyzed.



My problem comes when i have to make the tail for my snake because for making it with classes i would need a sort of command that "summons" a new tail object in the program.



for example: my head just collided with food and the tail has got to grow, but
how can i summon a new tail object??



here is the code (i will work soon on removing system("cls") later to make the program run smoother):



::::::::::::::::SNAKE.cpp:::::::::::::::::



#include <iostream>
#include <conio.h>
#include <Windows.h>

#include "Head.h"


int score = 0;


int main()
{
HEAD head;


while (1)
{
head.key();
head.movement();
head.show();

if (GetKeyState(VK_ESCAPE) & 0x8000)
{
return 0;
}

Sleep(100);
system("cls");
}

}


:::::::::::::HEAD.h:::::::::::::



#include <iostream>

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

class HEAD
{

private:
int x = 20;
int y = 20;
int direction, lastdirection;


public:

void show()
{
gotoxy(x, y); std::cout << "@";
}

void movement()
{
lastdirection = direction;

switch (direction)
{
case 1:
y--;
break;
case 2:
x--;
break;
case 3:
y++;
break;
case 4:
x++;
break;
}

}
void key()
{
//------------directions-----------

if (GetKeyState('W') & 0x8000)
{
direction = 1;
}

if (GetKeyState('A') & 0x8000)
{
direction = 2;
}

if (GetKeyState('S') & 0x8000)
{
direction = 3;
}

if (GetKeyState('D') & 0x8000)
{
direction = 4;
}

//-------specific direction changes--------

if (lastdirection == 1 && direction == 3 || lastdirection == 3 && direction == 1)
{
direction = lastdirection;
}

if (lastdirection == 2 && direction == 4 || lastdirection == 4 && direction == 2)
{
direction = lastdirection;
}


}

};


i hope that you can help me because this is a project that need to finish because for me it is the hardest thing i've ever programmed in c++ (after pong)










share|improve this question


















  • 1





    Try creating a class Tail or TailSegment or ehatever. Then you can add an std::vector<TailSegment> to the head class which is a dynamically allocated array so you can insert TailSegments as you want. Then add a AddTailSegment Method to your head and call it when you pick up a piece of food. In your Head::show you then simply iterate over the tail segments and print them to their position.

    – Yastanub
    Nov 22 '18 at 12:57











  • Also mind that this question might rather go to Software engeneering

    – Yastanub
    Nov 22 '18 at 13:00













  • or here gamedev.stackexchange.com

    – RyanN1220
    Nov 22 '18 at 13:02











  • @RyanN1220 possibly but this question has more to do with code design and coding solutions than with game design if you ask me.

    – Yastanub
    Nov 22 '18 at 13:14











  • @Yastanub ok i see, but i still do not fully comprehend how i should with that vector. can you make me an example please? or can you give me something like a youtube video where someone explains it clearly?

    – Sir.Rosticciano
    Nov 22 '18 at 13:58


















1















i'm a beginner programmer of c++ and i would like some help to continue my snake project.
For making a bit of practice with classes i decided to this program using them to not take the bad habit of use things like hundreads of functions that can be sintethyzed.



My problem comes when i have to make the tail for my snake because for making it with classes i would need a sort of command that "summons" a new tail object in the program.



for example: my head just collided with food and the tail has got to grow, but
how can i summon a new tail object??



here is the code (i will work soon on removing system("cls") later to make the program run smoother):



::::::::::::::::SNAKE.cpp:::::::::::::::::



#include <iostream>
#include <conio.h>
#include <Windows.h>

#include "Head.h"


int score = 0;


int main()
{
HEAD head;


while (1)
{
head.key();
head.movement();
head.show();

if (GetKeyState(VK_ESCAPE) & 0x8000)
{
return 0;
}

Sleep(100);
system("cls");
}

}


:::::::::::::HEAD.h:::::::::::::



#include <iostream>

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

class HEAD
{

private:
int x = 20;
int y = 20;
int direction, lastdirection;


public:

void show()
{
gotoxy(x, y); std::cout << "@";
}

void movement()
{
lastdirection = direction;

switch (direction)
{
case 1:
y--;
break;
case 2:
x--;
break;
case 3:
y++;
break;
case 4:
x++;
break;
}

}
void key()
{
//------------directions-----------

if (GetKeyState('W') & 0x8000)
{
direction = 1;
}

if (GetKeyState('A') & 0x8000)
{
direction = 2;
}

if (GetKeyState('S') & 0x8000)
{
direction = 3;
}

if (GetKeyState('D') & 0x8000)
{
direction = 4;
}

//-------specific direction changes--------

if (lastdirection == 1 && direction == 3 || lastdirection == 3 && direction == 1)
{
direction = lastdirection;
}

if (lastdirection == 2 && direction == 4 || lastdirection == 4 && direction == 2)
{
direction = lastdirection;
}


}

};


i hope that you can help me because this is a project that need to finish because for me it is the hardest thing i've ever programmed in c++ (after pong)










share|improve this question


















  • 1





    Try creating a class Tail or TailSegment or ehatever. Then you can add an std::vector<TailSegment> to the head class which is a dynamically allocated array so you can insert TailSegments as you want. Then add a AddTailSegment Method to your head and call it when you pick up a piece of food. In your Head::show you then simply iterate over the tail segments and print them to their position.

    – Yastanub
    Nov 22 '18 at 12:57











  • Also mind that this question might rather go to Software engeneering

    – Yastanub
    Nov 22 '18 at 13:00













  • or here gamedev.stackexchange.com

    – RyanN1220
    Nov 22 '18 at 13:02











  • @RyanN1220 possibly but this question has more to do with code design and coding solutions than with game design if you ask me.

    – Yastanub
    Nov 22 '18 at 13:14











  • @Yastanub ok i see, but i still do not fully comprehend how i should with that vector. can you make me an example please? or can you give me something like a youtube video where someone explains it clearly?

    – Sir.Rosticciano
    Nov 22 '18 at 13:58














1












1








1








i'm a beginner programmer of c++ and i would like some help to continue my snake project.
For making a bit of practice with classes i decided to this program using them to not take the bad habit of use things like hundreads of functions that can be sintethyzed.



My problem comes when i have to make the tail for my snake because for making it with classes i would need a sort of command that "summons" a new tail object in the program.



for example: my head just collided with food and the tail has got to grow, but
how can i summon a new tail object??



here is the code (i will work soon on removing system("cls") later to make the program run smoother):



::::::::::::::::SNAKE.cpp:::::::::::::::::



#include <iostream>
#include <conio.h>
#include <Windows.h>

#include "Head.h"


int score = 0;


int main()
{
HEAD head;


while (1)
{
head.key();
head.movement();
head.show();

if (GetKeyState(VK_ESCAPE) & 0x8000)
{
return 0;
}

Sleep(100);
system("cls");
}

}


:::::::::::::HEAD.h:::::::::::::



#include <iostream>

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

class HEAD
{

private:
int x = 20;
int y = 20;
int direction, lastdirection;


public:

void show()
{
gotoxy(x, y); std::cout << "@";
}

void movement()
{
lastdirection = direction;

switch (direction)
{
case 1:
y--;
break;
case 2:
x--;
break;
case 3:
y++;
break;
case 4:
x++;
break;
}

}
void key()
{
//------------directions-----------

if (GetKeyState('W') & 0x8000)
{
direction = 1;
}

if (GetKeyState('A') & 0x8000)
{
direction = 2;
}

if (GetKeyState('S') & 0x8000)
{
direction = 3;
}

if (GetKeyState('D') & 0x8000)
{
direction = 4;
}

//-------specific direction changes--------

if (lastdirection == 1 && direction == 3 || lastdirection == 3 && direction == 1)
{
direction = lastdirection;
}

if (lastdirection == 2 && direction == 4 || lastdirection == 4 && direction == 2)
{
direction = lastdirection;
}


}

};


i hope that you can help me because this is a project that need to finish because for me it is the hardest thing i've ever programmed in c++ (after pong)










share|improve this question














i'm a beginner programmer of c++ and i would like some help to continue my snake project.
For making a bit of practice with classes i decided to this program using them to not take the bad habit of use things like hundreads of functions that can be sintethyzed.



My problem comes when i have to make the tail for my snake because for making it with classes i would need a sort of command that "summons" a new tail object in the program.



for example: my head just collided with food and the tail has got to grow, but
how can i summon a new tail object??



here is the code (i will work soon on removing system("cls") later to make the program run smoother):



::::::::::::::::SNAKE.cpp:::::::::::::::::



#include <iostream>
#include <conio.h>
#include <Windows.h>

#include "Head.h"


int score = 0;


int main()
{
HEAD head;


while (1)
{
head.key();
head.movement();
head.show();

if (GetKeyState(VK_ESCAPE) & 0x8000)
{
return 0;
}

Sleep(100);
system("cls");
}

}


:::::::::::::HEAD.h:::::::::::::



#include <iostream>

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

class HEAD
{

private:
int x = 20;
int y = 20;
int direction, lastdirection;


public:

void show()
{
gotoxy(x, y); std::cout << "@";
}

void movement()
{
lastdirection = direction;

switch (direction)
{
case 1:
y--;
break;
case 2:
x--;
break;
case 3:
y++;
break;
case 4:
x++;
break;
}

}
void key()
{
//------------directions-----------

if (GetKeyState('W') & 0x8000)
{
direction = 1;
}

if (GetKeyState('A') & 0x8000)
{
direction = 2;
}

if (GetKeyState('S') & 0x8000)
{
direction = 3;
}

if (GetKeyState('D') & 0x8000)
{
direction = 4;
}

//-------specific direction changes--------

if (lastdirection == 1 && direction == 3 || lastdirection == 3 && direction == 1)
{
direction = lastdirection;
}

if (lastdirection == 2 && direction == 4 || lastdirection == 4 && direction == 2)
{
direction = lastdirection;
}


}

};


i hope that you can help me because this is a project that need to finish because for me it is the hardest thing i've ever programmed in c++ (after pong)







c++ class console






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 12:40









Sir.RosticcianoSir.Rosticciano

106




106








  • 1





    Try creating a class Tail or TailSegment or ehatever. Then you can add an std::vector<TailSegment> to the head class which is a dynamically allocated array so you can insert TailSegments as you want. Then add a AddTailSegment Method to your head and call it when you pick up a piece of food. In your Head::show you then simply iterate over the tail segments and print them to their position.

    – Yastanub
    Nov 22 '18 at 12:57











  • Also mind that this question might rather go to Software engeneering

    – Yastanub
    Nov 22 '18 at 13:00













  • or here gamedev.stackexchange.com

    – RyanN1220
    Nov 22 '18 at 13:02











  • @RyanN1220 possibly but this question has more to do with code design and coding solutions than with game design if you ask me.

    – Yastanub
    Nov 22 '18 at 13:14











  • @Yastanub ok i see, but i still do not fully comprehend how i should with that vector. can you make me an example please? or can you give me something like a youtube video where someone explains it clearly?

    – Sir.Rosticciano
    Nov 22 '18 at 13:58














  • 1





    Try creating a class Tail or TailSegment or ehatever. Then you can add an std::vector<TailSegment> to the head class which is a dynamically allocated array so you can insert TailSegments as you want. Then add a AddTailSegment Method to your head and call it when you pick up a piece of food. In your Head::show you then simply iterate over the tail segments and print them to their position.

    – Yastanub
    Nov 22 '18 at 12:57











  • Also mind that this question might rather go to Software engeneering

    – Yastanub
    Nov 22 '18 at 13:00













  • or here gamedev.stackexchange.com

    – RyanN1220
    Nov 22 '18 at 13:02











  • @RyanN1220 possibly but this question has more to do with code design and coding solutions than with game design if you ask me.

    – Yastanub
    Nov 22 '18 at 13:14











  • @Yastanub ok i see, but i still do not fully comprehend how i should with that vector. can you make me an example please? or can you give me something like a youtube video where someone explains it clearly?

    – Sir.Rosticciano
    Nov 22 '18 at 13:58








1




1





Try creating a class Tail or TailSegment or ehatever. Then you can add an std::vector<TailSegment> to the head class which is a dynamically allocated array so you can insert TailSegments as you want. Then add a AddTailSegment Method to your head and call it when you pick up a piece of food. In your Head::show you then simply iterate over the tail segments and print them to their position.

– Yastanub
Nov 22 '18 at 12:57





Try creating a class Tail or TailSegment or ehatever. Then you can add an std::vector<TailSegment> to the head class which is a dynamically allocated array so you can insert TailSegments as you want. Then add a AddTailSegment Method to your head and call it when you pick up a piece of food. In your Head::show you then simply iterate over the tail segments and print them to their position.

– Yastanub
Nov 22 '18 at 12:57













Also mind that this question might rather go to Software engeneering

– Yastanub
Nov 22 '18 at 13:00







Also mind that this question might rather go to Software engeneering

– Yastanub
Nov 22 '18 at 13:00















or here gamedev.stackexchange.com

– RyanN1220
Nov 22 '18 at 13:02





or here gamedev.stackexchange.com

– RyanN1220
Nov 22 '18 at 13:02













@RyanN1220 possibly but this question has more to do with code design and coding solutions than with game design if you ask me.

– Yastanub
Nov 22 '18 at 13:14





@RyanN1220 possibly but this question has more to do with code design and coding solutions than with game design if you ask me.

– Yastanub
Nov 22 '18 at 13:14













@Yastanub ok i see, but i still do not fully comprehend how i should with that vector. can you make me an example please? or can you give me something like a youtube video where someone explains it clearly?

– Sir.Rosticciano
Nov 22 '18 at 13:58





@Yastanub ok i see, but i still do not fully comprehend how i should with that vector. can you make me an example please? or can you give me something like a youtube video where someone explains it clearly?

– Sir.Rosticciano
Nov 22 '18 at 13:58












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431257%2fsnake-game-c-tail-class-how-to-add-new-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431257%2fsnake-game-c-tail-class-how-to-add-new-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)