Snake game in vector view c++ [closed]












-4














I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do.
I used the class to create the fruit and the structure for the snake. Also please tell me the esc key which number to call the key case (27 didn’t fit)



#include "header.hpp"
#include <iostream>

void drawField() //рисует клеточки на экране
{
glColor3f(0.2, 0.5, 0.4);
glBegin(GL_LINES);
for (int i = 0; i < width; i += scale)
{
glVertex2f(i, 0);
glVertex2f(i, height);
}
for (int j = 0; j < height; j += scale)
{
glVertex2f(0, j);
glVertex2f(width, j);
}
glEnd();
}

void drawSnake()
{
glColor3f(1.0, 0.8, 0.9);
for (int i = 0; i < num; i++)
{
glRectf(s[i].x*scale, s[i].y*scale, (s[i].x + 1)*scale, (s[i].y +
1)*scale);
}
}

void keyBoard(int key, int a, int b)
{
switch (key)
{
case 101:
direction = 0;
break;
case 102:
direction = 2;
break;
case 100:
direction = 1;
break;
case 103:
direction = 3;
break;
}
}

void step()
{
for (int i = num; i > 0; --i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}

if (direction == 0) s[0].y += 1;
if (direction == 1) s[0].x -= 1;
if (direction == 2) s[0].x += 1;
if (direction == 3) s[0].y -= 1;

for (int i = 0; i < 10; i++) //
{
if ((s[0].x == m[i].x) && (s[0].y == m[i].y))
{
num++;
m[i].New();
}
}

if (s[0].x > coundSquarew)
{
direction = 1;
};

if (s[0].x < 0)
{
direction = 2;
};

if (s[0].y > coundSquareh)
{
direction = 3;
};

if (s[0].y < 0)
{
direction = 0;
};

for (int i = 1; i < num; i++)
{
if ((s[0].x == s[i].x) && (s[0].y == s[i].y))
{
num = i;
}
};
};

void display()
{

glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < 10; i++)
{
m[i].drawApple();
}
drawField();
drawSnake();
glFlush();
}

void timer(int = 0)
{
display();

step();

glutTimerFunc(70, timer, 0);
}
---main.cpp----

int main(int argc, char **argv)
{
for (int i = 0; i < 10; i++)
{
m[i].New();
};

s[0].x = 0;
s[0].y = 0;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Snake");
glClearColor(0.0, 0.3, 0.2, 0.5);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);

glutDisplayFunc(display);
glutTimerFunc(50, timer, 0);
glutSpecialFunc(keyBoard);

glutMainLoop();
}









share|improve this question









New contributor




Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











closed as off-topic by πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0 Dec 17 at 2:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    This site is for code that’s working. Stack Overflow is for code that isn’t working.
    – Tom Zych
    Dec 16 at 17:17










  • it works, but too much code
    – Markontr
    Dec 16 at 17:33






  • 3




    I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do. Those two sentences indicate that its broken, otherwise what did you mean?
    – bruglesco
    Dec 16 at 18:38










  • I tried to do it, but my friend said that what I was doing was wrong, but he himself could not understand and remake. I'm just learning and asking for help.
    – Markontr
    Dec 16 at 19:13






  • 1




    OT: regarding: m[i].New(); new is a keyword in C++. Suggest not using the same spelling (regardless of capitalization) for field names as it is confusing to the reader of the code
    – user3629249
    Dec 17 at 1:49


















-4














I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do.
I used the class to create the fruit and the structure for the snake. Also please tell me the esc key which number to call the key case (27 didn’t fit)



#include "header.hpp"
#include <iostream>

void drawField() //рисует клеточки на экране
{
glColor3f(0.2, 0.5, 0.4);
glBegin(GL_LINES);
for (int i = 0; i < width; i += scale)
{
glVertex2f(i, 0);
glVertex2f(i, height);
}
for (int j = 0; j < height; j += scale)
{
glVertex2f(0, j);
glVertex2f(width, j);
}
glEnd();
}

void drawSnake()
{
glColor3f(1.0, 0.8, 0.9);
for (int i = 0; i < num; i++)
{
glRectf(s[i].x*scale, s[i].y*scale, (s[i].x + 1)*scale, (s[i].y +
1)*scale);
}
}

void keyBoard(int key, int a, int b)
{
switch (key)
{
case 101:
direction = 0;
break;
case 102:
direction = 2;
break;
case 100:
direction = 1;
break;
case 103:
direction = 3;
break;
}
}

void step()
{
for (int i = num; i > 0; --i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}

if (direction == 0) s[0].y += 1;
if (direction == 1) s[0].x -= 1;
if (direction == 2) s[0].x += 1;
if (direction == 3) s[0].y -= 1;

for (int i = 0; i < 10; i++) //
{
if ((s[0].x == m[i].x) && (s[0].y == m[i].y))
{
num++;
m[i].New();
}
}

if (s[0].x > coundSquarew)
{
direction = 1;
};

if (s[0].x < 0)
{
direction = 2;
};

if (s[0].y > coundSquareh)
{
direction = 3;
};

if (s[0].y < 0)
{
direction = 0;
};

for (int i = 1; i < num; i++)
{
if ((s[0].x == s[i].x) && (s[0].y == s[i].y))
{
num = i;
}
};
};

void display()
{

glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < 10; i++)
{
m[i].drawApple();
}
drawField();
drawSnake();
glFlush();
}

void timer(int = 0)
{
display();

step();

glutTimerFunc(70, timer, 0);
}
---main.cpp----

int main(int argc, char **argv)
{
for (int i = 0; i < 10; i++)
{
m[i].New();
};

s[0].x = 0;
s[0].y = 0;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Snake");
glClearColor(0.0, 0.3, 0.2, 0.5);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);

glutDisplayFunc(display);
glutTimerFunc(50, timer, 0);
glutSpecialFunc(keyBoard);

glutMainLoop();
}









share|improve this question









New contributor




Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











closed as off-topic by πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0 Dec 17 at 2:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    This site is for code that’s working. Stack Overflow is for code that isn’t working.
    – Tom Zych
    Dec 16 at 17:17










  • it works, but too much code
    – Markontr
    Dec 16 at 17:33






  • 3




    I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do. Those two sentences indicate that its broken, otherwise what did you mean?
    – bruglesco
    Dec 16 at 18:38










  • I tried to do it, but my friend said that what I was doing was wrong, but he himself could not understand and remake. I'm just learning and asking for help.
    – Markontr
    Dec 16 at 19:13






  • 1




    OT: regarding: m[i].New(); new is a keyword in C++. Suggest not using the same spelling (regardless of capitalization) for field names as it is confusing to the reader of the code
    – user3629249
    Dec 17 at 1:49
















-4












-4








-4







I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do.
I used the class to create the fruit and the structure for the snake. Also please tell me the esc key which number to call the key case (27 didn’t fit)



#include "header.hpp"
#include <iostream>

void drawField() //рисует клеточки на экране
{
glColor3f(0.2, 0.5, 0.4);
glBegin(GL_LINES);
for (int i = 0; i < width; i += scale)
{
glVertex2f(i, 0);
glVertex2f(i, height);
}
for (int j = 0; j < height; j += scale)
{
glVertex2f(0, j);
glVertex2f(width, j);
}
glEnd();
}

void drawSnake()
{
glColor3f(1.0, 0.8, 0.9);
for (int i = 0; i < num; i++)
{
glRectf(s[i].x*scale, s[i].y*scale, (s[i].x + 1)*scale, (s[i].y +
1)*scale);
}
}

void keyBoard(int key, int a, int b)
{
switch (key)
{
case 101:
direction = 0;
break;
case 102:
direction = 2;
break;
case 100:
direction = 1;
break;
case 103:
direction = 3;
break;
}
}

void step()
{
for (int i = num; i > 0; --i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}

if (direction == 0) s[0].y += 1;
if (direction == 1) s[0].x -= 1;
if (direction == 2) s[0].x += 1;
if (direction == 3) s[0].y -= 1;

for (int i = 0; i < 10; i++) //
{
if ((s[0].x == m[i].x) && (s[0].y == m[i].y))
{
num++;
m[i].New();
}
}

if (s[0].x > coundSquarew)
{
direction = 1;
};

if (s[0].x < 0)
{
direction = 2;
};

if (s[0].y > coundSquareh)
{
direction = 3;
};

if (s[0].y < 0)
{
direction = 0;
};

for (int i = 1; i < num; i++)
{
if ((s[0].x == s[i].x) && (s[0].y == s[i].y))
{
num = i;
}
};
};

void display()
{

glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < 10; i++)
{
m[i].drawApple();
}
drawField();
drawSnake();
glFlush();
}

void timer(int = 0)
{
display();

step();

glutTimerFunc(70, timer, 0);
}
---main.cpp----

int main(int argc, char **argv)
{
for (int i = 0; i < 10; i++)
{
m[i].New();
};

s[0].x = 0;
s[0].y = 0;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Snake");
glClearColor(0.0, 0.3, 0.2, 0.5);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);

glutDisplayFunc(display);
glutTimerFunc(50, timer, 0);
glutSpecialFunc(keyBoard);

glutMainLoop();
}









share|improve this question









New contributor




Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do.
I used the class to create the fruit and the structure for the snake. Also please tell me the esc key which number to call the key case (27 didn’t fit)



#include "header.hpp"
#include <iostream>

void drawField() //рисует клеточки на экране
{
glColor3f(0.2, 0.5, 0.4);
glBegin(GL_LINES);
for (int i = 0; i < width; i += scale)
{
glVertex2f(i, 0);
glVertex2f(i, height);
}
for (int j = 0; j < height; j += scale)
{
glVertex2f(0, j);
glVertex2f(width, j);
}
glEnd();
}

void drawSnake()
{
glColor3f(1.0, 0.8, 0.9);
for (int i = 0; i < num; i++)
{
glRectf(s[i].x*scale, s[i].y*scale, (s[i].x + 1)*scale, (s[i].y +
1)*scale);
}
}

void keyBoard(int key, int a, int b)
{
switch (key)
{
case 101:
direction = 0;
break;
case 102:
direction = 2;
break;
case 100:
direction = 1;
break;
case 103:
direction = 3;
break;
}
}

void step()
{
for (int i = num; i > 0; --i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}

if (direction == 0) s[0].y += 1;
if (direction == 1) s[0].x -= 1;
if (direction == 2) s[0].x += 1;
if (direction == 3) s[0].y -= 1;

for (int i = 0; i < 10; i++) //
{
if ((s[0].x == m[i].x) && (s[0].y == m[i].y))
{
num++;
m[i].New();
}
}

if (s[0].x > coundSquarew)
{
direction = 1;
};

if (s[0].x < 0)
{
direction = 2;
};

if (s[0].y > coundSquareh)
{
direction = 3;
};

if (s[0].y < 0)
{
direction = 0;
};

for (int i = 1; i < num; i++)
{
if ((s[0].x == s[i].x) && (s[0].y == s[i].y))
{
num = i;
}
};
};

void display()
{

glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < 10; i++)
{
m[i].drawApple();
}
drawField();
drawSnake();
glFlush();
}

void timer(int = 0)
{
display();

step();

glutTimerFunc(70, timer, 0);
}
---main.cpp----

int main(int argc, char **argv)
{
for (int i = 0; i < 10; i++)
{
m[i].New();
};

s[0].x = 0;
s[0].y = 0;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Snake");
glClearColor(0.0, 0.3, 0.2, 0.5);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);

glutDisplayFunc(display);
glutTimerFunc(50, timer, 0);
glutSpecialFunc(keyBoard);

glutMainLoop();
}






c opengl






share|improve this question









New contributor




Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Dec 16 at 17:33





















New contributor




Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 16 at 17:06









Markontr

11




11




New contributor




Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Markontr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




closed as off-topic by πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0 Dec 17 at 2:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0 Dec 17 at 2:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Martin R, t3chb0t, 200_success, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    This site is for code that’s working. Stack Overflow is for code that isn’t working.
    – Tom Zych
    Dec 16 at 17:17










  • it works, but too much code
    – Markontr
    Dec 16 at 17:33






  • 3




    I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do. Those two sentences indicate that its broken, otherwise what did you mean?
    – bruglesco
    Dec 16 at 18:38










  • I tried to do it, but my friend said that what I was doing was wrong, but he himself could not understand and remake. I'm just learning and asking for help.
    – Markontr
    Dec 16 at 19:13






  • 1




    OT: regarding: m[i].New(); new is a keyword in C++. Suggest not using the same spelling (regardless of capitalization) for field names as it is confusing to the reader of the code
    – user3629249
    Dec 17 at 1:49
















  • 3




    This site is for code that’s working. Stack Overflow is for code that isn’t working.
    – Tom Zych
    Dec 16 at 17:17










  • it works, but too much code
    – Markontr
    Dec 16 at 17:33






  • 3




    I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do. Those two sentences indicate that its broken, otherwise what did you mean?
    – bruglesco
    Dec 16 at 18:38










  • I tried to do it, but my friend said that what I was doing was wrong, but he himself could not understand and remake. I'm just learning and asking for help.
    – Markontr
    Dec 16 at 19:13






  • 1




    OT: regarding: m[i].New(); new is a keyword in C++. Suggest not using the same spelling (regardless of capitalization) for field names as it is confusing to the reader of the code
    – user3629249
    Dec 17 at 1:49










3




3




This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
Dec 16 at 17:17




This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
Dec 16 at 17:17












it works, but too much code
– Markontr
Dec 16 at 17:33




it works, but too much code
– Markontr
Dec 16 at 17:33




3




3




I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do. Those two sentences indicate that its broken, otherwise what did you mean?
– bruglesco
Dec 16 at 18:38




I tried to make a vector out of class and structure, but it didn't work out for me. Please tell me what and how to do. Those two sentences indicate that its broken, otherwise what did you mean?
– bruglesco
Dec 16 at 18:38












I tried to do it, but my friend said that what I was doing was wrong, but he himself could not understand and remake. I'm just learning and asking for help.
– Markontr
Dec 16 at 19:13




I tried to do it, but my friend said that what I was doing was wrong, but he himself could not understand and remake. I'm just learning and asking for help.
– Markontr
Dec 16 at 19:13




1




1




OT: regarding: m[i].New(); new is a keyword in C++. Suggest not using the same spelling (regardless of capitalization) for field names as it is confusing to the reader of the code
– user3629249
Dec 17 at 1:49






OT: regarding: m[i].New(); new is a keyword in C++. Suggest not using the same spelling (regardless of capitalization) for field names as it is confusing to the reader of the code
– user3629249
Dec 17 at 1:49

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Сан-Квентин

Алькесар

Josef Freinademetz