thank reading thread. beginning programmer , trying make simple snake game c++. isn't finished yet, think got nice start it. however, when try run program instantly crashes. (the compiler says there 0 warnings , errors. using code::blocks ide. know why program isn't working? think may have "vector coordhistory", can't tell sure. @ least last thing added program.
this code:
#include <iostream> #include <cstdlib> #include <string> #include <cstdio> #include <windows.h> #include <conio.h> #include <vector> #define maxx 156 //number of columns fits on screen #define maxy 62 //number of rows fits on screen using namespace std; // function clears console window void clearconsole() { system("cls"); //empties console window }; // function returns x position of cursor int getcursorx() { console_screen_buffer_info csbi; if(getconsolescreenbufferinfo(getstdhandle(std_output_handle), &csbi)) { return csbi.dwcursorposition.x; } }; // function returns y position of cursor int getcursory() { console_screen_buffer_info csbi; if(getconsolescreenbufferinfo(getstdhandle(std_output_handle), &csbi)) { return csbi.dwcursorposition.y; } }; // function sets x position of cursor void setcursorx(int x) { coord coord = {x, getcursory()}; setconsolecursorposition(getstdhandle(std_output_handle), coord); } // function sets y position of cursor void setcursory(int y) { coord coord = {getcursorx(), y}; setconsolecursorposition(getstdhandle(std_output_handle), coord); } // snake class contains coordinates of snake , direction in moving class snake { private: bool isalive; int snakexcoord; int snakeycoord; char snakedirection; int snakelength; public: //getters int getsnakexcoord() { return snakexcoord; }; int getsnakeycoord() { return snakeycoord; }; char getsnakedirection() { return snakedirection; }; bool getisalive() { return isalive; }; int getsnakelength() { return snakelength; }; //setters void setsnakexcoord(int newsnakexcoord) { snakexcoord = newsnakexcoord;}; void setsnakeycoord(int newsnakeycoord) { snakeycoord = newsnakeycoord;}; void setsnakedirection(char newsnakedirection) { snakedirection = newsnakedirection;}; void setisalive(bool newisalive) { isalive = newisalive; }; void setsnakelength(int newsnakelength) { snakelength = newsnakelength; }; //constructor snake() { snakexcoord = maxx / 2; snakeycoord = maxy / 2; snakedirection = 'e'; isalive = true; snakelength = 1; }; //destructor ~snake(){}; }; int main() { int i; //iterator system("mode 650"); //makes console window full-screen snake snake; //initializes snake object snake char c; //char stores user input change snake direction vector<int[2]> coordhistory; //vector of arrays stores previous locations of snake while (snake.getisalive()) { //adds snake coordinates coordhistory coordhistory[coordhistory.size()][0] = snake.getsnakexcoord(); coordhistory[coordhistory.size()-1][1] = snake.getsnakeycoord(); //iterates backwards through coordhistory , draws "o" until snake long should for(i = coordhistory.size() - 1; > coordhistory.size() - 1 - snake.getsnakelength(); i--) { setcursorx(coordhistory[i][0]); setcursory(coordhistory[i][1]); cout << "o"; } //allows user change snake direction c = _getch(); switch (c){ case 'w': snake.setsnakedirection('n'); break; case 'd': snake.setsnakedirection('e'); break; case 's': snake.setsnakedirection('s'); break; case 'a': snake.setsnakedirection('w'); break; } //checks in direction snake going , changes coordinates accordingly switch (snake.getsnakedirection()) { case 'n': snake.setsnakeycoord(snake.getsnakeycoord()-1); break; case 'e': snake.setsnakexcoord(snake.getsnakexcoord()+1); break; case 's': snake.setsnakeycoord(snake.getsnakeycoord()+1); break; case 'w': snake.setsnakexcoord(snake.getsnakexcoord()-1); break; } //checks if snake goes out of boundaries if ((snake.getsnakexcoord() > maxx) || (snake.getsnakexcoord() < 0) || (snake.getsnakeycoord() > maxy) || (snake.getsnakeycoord() < 0)) { snake.setisalive(false); } //sleep(200); ignore wip clearconsole(); } return 0; }
your vector usage wrong.
coordhistory[coordhistory.size()][0] = snake.getsnakexcoord(); coordhistory[coordhistory.size()-1][1] = snake.getsnakeycoord(); you appear assume vector[n-1] automatically create many elements needed result in vector of size n. it doesn't.
you twice accessing first element of vector coordhistory, vector empty.
to add elements, in general use member function push_back or emplace_back. you're going struggle vector of arrays, though, since arrays neither assignable nor copyable. that's why can't use push_back here; have explicitly resize vector access newly-created elements doing before:
coordhistory.resize(1); coordhistory[0][0] = snake.getsnakexcoord(); coordhistory[0][1] = snake.getsnakeycoord(); this pretty awkward. why don't instead store nice delicious class type x , y members?
std::vector<deliciousclasstype> coordhistory; coordhistory.emplace_back( snake.getsnakexcoord(), snake.getsnakeycoord() );
Comments
Post a Comment