Problem with Snake Game Display

Hi all,

I am a beginning programmer working on a simple snake game. The program was constructed by my professor, and we have to debug it, however, he left out the main.cpp file on accident (he actually did not mean to leave it out). But I went ahead and constructed my main.cpp from an older file.

I included the ncurses library for all the curses functions. In class, we use DEV C++, but I am using XCODE. I just copy pasted the files.

Strange thing is, in class, using the same exact code except for the main.cpp file, the snake would display after each keyboard arrow input. But now, I am only able to display the snake and its position on the very first display call, and none after. Below is the code in main():


#include <cstdlib>
#include <iostream>
#include "keyboard.h"
#include "initialize.h"
#include "snake.h"
#include "mice.h"
#include <ctime>


using namespace std;

int main(int argc, char *argv[])
{
unsigned int input;
int length = 5;
int numLives = 3;
initialize();
Snake python = Snake(numLives, length);
python.display();
while((input = kbinput()) != 27)
{
python.move(input);
}
system("PAUSE");
return EXIT_SUCCESS;
}

And here is the code for the snakes constructor/destructor, move, and display functions:

Snake::Snake(int numLives, int howLong)
{
lives = numLives;
length = howLong;
start.x = 40;
start.y = 12;
body.push_back(start);
}

Snake::~Snake()
{}

void Snake::move(int dir)
{ int xPos = body[body.size()-1].x , yPos = body[body.size()-1].y;
static LOCATION next;
switch(dir)
{
case KEY_UP: yPos = yPos == 0 ? 24 : yPos-1;
break;

case KEY_DOWN: yPos = yPos == 24 ? 0 : yPos+1;
break;

case KEY_LEFT: xPos = xPos == 0 ? 79 : xPos-1;
break;

case KEY_RIGHT: xPos = xPos == 79 ? 0 : xPos+1;
}
next.x = xPos;
next.y = yPos;
body.push_back(next);
display();
}

void Snake::display()
{ mvprintw(0,50,"Snake Size %d length %d",body.size(), length);
clrtoeol();

if(body.size() > length)
{ body.erase(body.begin());
mvprintw(1,50,"Snake Snipped Size %d",body.size());
clrtoeol();
}
attron(COLOR_PAIR(BLACKONYELLOW));
for(int i = 0; i<body.size(); i++)
{
mvprintw(i,1,"The %d position of the snake is at %d %d",i,body[i].y, body[i].x);
mvprintw(body[i].y,body[i].x,"%c",'s');
}
attron(COLOR_PAIR(WHITEONBLACK));
refresh();
}

Everything works on the first python.display() call, but then after entering the while loop, nothing is displayed except a gray block which moves along with my keyboard arrow inputs. I inserted cout statements into the display and move functions, and they are being called at the right times. But nothing is showing up on the screen, neither the snake itself or the lines that should be displaying the snake's position, only the cout statements. Maybe it has to do with "location"'s constructors/destructors? Please see below:

#include "location.h"


LOCATION::LOCATION(int firstX, int firstY)
{
x = firstX;
y = firstY;
me = number++;
}

LOCATION::~LOCATION()
{
attron(WHITEONBLACK);
mvprintw(10,1,"Erasing location number %d at %d %d",me, y,x);
mvprintw(y, x,"%c", '*');
refresh();
}

int LOCATION::number = 0;

But even still, the statement that should be fired by the destructor is not appearing on the screen.

Quite confused here, not sure why nothing is showing up when the first display() call works fine, and I know the following ones are being called as well, but nothing shows up on screen. Is it something with that happened when I converted the project to XCODE from DEV C++, or the ncurses lib used by XCode? Just not sure why it is not behaving as it did in class with the same code except for the main.cpp file. Any advice would be appreciated!

Thank you!
what are these attron/mvprintw functions? What lib are you using?

Also... obligatory link: http://cplusplus.com/forum/articles/28558/
Topic archived. No new replies allowed.