Snake game in vector view c++

Multi tool use
up vote
-2
down vote
favorite
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
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.
add a comment |
up vote
-2
down vote
favorite
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
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.
1
This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
1 hour ago
it works, but too much code
– Markontr
1 hour ago
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
36 secs ago
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
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
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
c opengl
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.
edited 1 hour ago
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 1 hour ago
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.
1
This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
1 hour ago
it works, but too much code
– Markontr
1 hour ago
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
36 secs ago
add a comment |
1
This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
1 hour ago
it works, but too much code
– Markontr
1 hour ago
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
36 secs ago
1
1
This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
1 hour ago
This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
1 hour ago
it works, but too much code
– Markontr
1 hour ago
it works, but too much code
– Markontr
1 hour ago
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
36 secs ago
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
36 secs ago
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Markontr is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209773%2fsnake-game-in-vector-view-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Markontr is a new contributor. Be nice, and check out our Code of Conduct.
Markontr is a new contributor. Be nice, and check out our Code of Conduct.
Markontr is a new contributor. Be nice, and check out our Code of Conduct.
Markontr is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209773%2fsnake-game-in-vector-view-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
TJwYL0ZQbvtIHRlF62zOu,u2Ybxi8e7NHBP tXq5cVEQ4
1
This site is for code that’s working. Stack Overflow is for code that isn’t working.
– Tom Zych
1 hour ago
it works, but too much code
– Markontr
1 hour ago
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
36 secs ago