Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I made a pong game and I need to make an input to get the users up and down input. but Whenever I put ncurses into the program I doesn't print the board. what is going on with ncurses? am I not doing something correct? Please help me fix this problem.

C++
<pre>#include <curses.h>
#include<iostream>
#include <time.h>
using namespace std;
enum eDir { STOP = 0, LEFT = 1, UPLEFT = 2, DOWNLEFT = 3, RIGHT = 4, UPRIGHT = 5, DOWNRIGHT = 6};
class cBall
{
private:
    int x, y;
    int originalX, originalY;
    eDir direction;
public:
    cBall(int posX, int posY)
    {
        originalX = posX;
        originalY = posY;
        x = posX; y = posY;
        direction = STOP;
    }
    void Reset()
    {
        x = originalX; y = originalY;
        direction = STOP;
    }
    void changeDirection(eDir d)
    {
        direction = d;
    }
    void randomDirection()
    {
        direction = (eDir)((rand() % 6) + 1);
    }
    inline int getX() { return x; }
    inline int getY() { return y; }
    inline eDir getDirection() { return direction; }
    void Move()
    {
        switch (direction)
        {
        case STOP:
            break;
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case UPLEFT:
            x--; y--;
            break;
        case DOWNLEFT:
            x--; y++;
            break;
        case UPRIGHT:
            x++; y--;
            break;
        case DOWNRIGHT:
            x++; y++;
            break;
        default:
            break;
        }
    }
    friend ostream & operator<<(ostream & o, cBall c)
    {
        o << "Ball [" << c.x << "," << c.y << "][" << c.direction << "]";
        return o;
    }
};
class cPaddle
{
private:
    int x, y;
    int originalX, originalY;
public:
    cPaddle()
    {
        x = y = 0;
    }
    cPaddle(int posX, int posY) : cPaddle()
    {
        originalX = posX;
        originalY = posY;
        x = posX;
        y = posY;
    }
    inline void Reset() { x = originalX; y = originalY; }
    inline int getX() { return x; }
    inline int getY() { return y; }
    inline void moveUp() { y--; }
    inline void moveDown() { y++; }
    friend ostream & operator<<(ostream & o, cPaddle c)
    {
        o << "Paddle [" << c.x << "," << c.y << "]";
        return o;
    }
};
class cGameManger
{
private:
    int width, height;
    int score1, score2;
    char up1, down1, up2, down2;
    bool quit;
    cBall * ball;
    cPaddle *player1;
    cPaddle *player2;
public:
    cGameManger(int w, int h)
    {
        srand(time(NULL));
        quit = false;
        up1 = 'w'; up2 = 'i';
        down1 = 's'; down2 = 'k';
        score1 = score2 = 0;
        width = w; height = h;
        ball = new cBall(w / 2, h / 2);
        player1 = new cPaddle(1, h / 2 - 3);
        player2 = new cPaddle(w - 2, h / 2 - 3);
    }
    ~cGameManger()
    {
        delete ball, player1, player2;
    }
    void ScoreUp(cPaddle * player)
    {
        if (player == player1)
            score1++;
        else if (player == player2)
            score2++;
 
        ball->Reset();
        player1->Reset();
        player2->Reset();
    }
    void Draw()
    {
        system("clear");
        for (int i = 0; i < width + 2; i++)
            cout << "\u2593";
        cout << endl;
 
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                int ballx = ball->getX();
                int bally = ball->getY();
                int player1x = player1->getX();
                int player2x = player2->getX();
                int player1y = player1->getY();
                int player2y = player2->getY();
 
                if (j == 0)
                    cout << "\u2593";
 
                if (ballx == j && bally == i)
                    cout << "O"; //ball
                else if (player1x == j && player1y == i)
                    cout << "\u2588"; //player1
                else if (player2x == j && player2y == i)
                    cout << "\u2588"; //player2
 
                else if (player1x == j && player1y + 1 == i)
                    cout << "\u2588"; //player1
                else if (player1x == j && player1y + 2 == i)
                    cout << "\u2588"; //player1
                else if (player1x == j && player1y + 3 == i)
                    cout << "\u2588"; //player1
 
                else if (player2x == j && player2y + 1 == i)
                    cout << "\u2588"; //player1
                else if (player2x == j && player2y + 2 == i)
                    cout << "\u2588"; //player1
                else if (player2x == j && player2y + 3 == i)
                    cout << "\u2588"; //player1
                else
                    cout << " ";
 
                if (j == width - 1)
                    cout << "\u2593";
            }
            cout << endl;
        }
 
        for (int i = 0; i < width + 2; i++)
            cout << "\u2593";
        cout << endl;
 
        cout << "Score 1: " << score1 << endl << "Score 2: " << score2 << endl;
    }
    void Input()
    {
        ball->Move();
 
        int ballx = ball->getX();
        int bally = ball->getY();
        int player1x = player1->getX();
        int player2x = player2->getX();
        int player1y = player1->getY();
        int player2y = player2->getY();
 	
	cbreak();
	while(true){
            char current = getch();
            if (current == up1)
                if (player1y > 0)
                    player1->moveUp();
            if (current == up2)
                if (player2y > 0)
                    player2->moveUp();
            if (current == down1)
                if (player1y + 4 < height)
                    player1->moveDown();
            if (current == down2)
                if (player2y + 4 < height)
                    player2->moveDown();
 
            if (ball->getDirection() == STOP)
                ball->randomDirection();
 
            if (current == 'q')
                quit = true;
        }
    }
    void Logic()
    {
        int ballx = ball->getX();
        int bally = ball->getY();
        int player1x = player1->getX();
        int player2x = player2->getX();
        int player1y = player1->getY();
        int player2y = player2->getY();
 
        //left paddle
        for (int i = 0; i < 4; i++)
            if (ballx == player1x + 1)
                if (bally == player1y + i)
                    ball->changeDirection((eDir)((rand() % 3) + 4));
 
        //right paddle
        for (int i = 0; i < 4; i++)
            if (ballx == player2x - 1)
                if (bally == player2y + i)
                    ball->changeDirection((eDir)((rand() % 3) + 1));
 
        //bottom wall
        if (bally == height - 1)
            ball->changeDirection(ball->getDirection() == DOWNRIGHT ? UPRIGHT : UPLEFT);
        //top wall
        if (bally == 0)
            ball->changeDirection(ball->getDirection() == UPRIGHT ? DOWNRIGHT : DOWNLEFT);
        //right wall
        if (ballx == width - 1)
            ScoreUp(player1);
        //left wall
        if (ballx == 0)
            ScoreUp(player2);
    }
    void Run()
    {
        while (!quit)
        {
            Draw();
            Logic();
        }
    }
};
int main()
{
    cGameManger c(40, 20);
    c.Run();
    return 0;
}


What I have tried:

I have tried looking at documents on ncurses but nothing explains how to fix it. Is this a linux problem? Please help! Thanks in advance.
Posted
Updated 29-Apr-21 4:21am
Comments
Richard MacCutchan 29-Apr-21 8:26am    
You need to provide more details, as it is not clear exactly what your problem is.
MARLON LESUEUR 29-Apr-21 8:37am    
Ok, well whenever I put ncurses in my program it does not print my gameboard but when I do not have it it prints the gameboard. I need ncurses in my program so what is a way to print it while still using ncurses
Richard MacCutchan 29-Apr-21 9:12am    
You already said that in your original post, and it tells us nothing useful.
MARLON LESUEUR 29-Apr-21 9:17am    
well then I don't know how to explain it
Richard MacCutchan 29-Apr-21 10:58am    
I just ran a simple ncurses test and it works fine. So whatever it is that you are doing there must be something wrong. But since we cannot see the code, there is not a lot more to say.

1 solution

Apart from including curses.h, you don't seem to be doing any actual curses calls. A curses program would use the curses I/O routines like mvprintw, wattron, etc.
Here's a short C program that shows some of the things you can do with curses
C++
#include <curses.h>

int main()
{
    initscr();                            /* initialize curses() */
    box(stdscr, 0, 0);                    /* draw a box around the whole screen */
    WINDOW *w1 = newwin(10, 60, 4, 10);   /* create a new window, 10 rowss, 60 cols */
                                          /* positioned at row 4, col 10 */
    box(w1, 0, 0);                        /* draw a box around win1 */
    mvwprintw(w1, 1, 5, "Hello World");   /* print "Hello World inside windos */
    wnoutrefresh(stdscr);                 /* tell curses to update its internal ... */
    wnoutrefresh(w1);                     /*    ... data fro w1 and stdscr */
    doupdate();                           /* send output to the screen */
    getch();                              /* wait for user to hit key */
    mvwin(w1, 8, 20);                     /* move the window relative to stdscr */
    touchline(stdscr, 4, 10);             /* tell curses to upddate stdscr after mvwin */             
    wnoutrefresh(stdscr);                 /* update internal data for stdscr */
    wnoutrefresh(w1);                     /*   .. and swindow */
    doupdate();                           /* sned updates to screen */
    getch();                              /* wait for user to hit key */
    endwin();                             /* shutdown curses */

    return 0;
}
compile with gcc example.c -lncurses -o example

There are C++ bindings for curses, but I'm not sure how complete or how usable they are. You should be able to google around for documents and examples of the C++ bindings, and decide if it meets your needs or if you should stick with the C interface.
 
Share this answer
 
Comments
CPallini 29-Apr-21 10:32am    
5.
MARLON LESUEUR 29-Apr-21 12:41pm    
All of my ncurses code is in the input function. Is something wrong with the input function
k5054 29-Apr-21 13:01pm    
You need to call initscr() to initialize ncurses. Once you've done that getch() should work. But then you really should use the curses output routines to draw your playing surface, move the ball and move the paddles. Don't forget that cout is buffered, so it doesn't send output to the screen until either you write a newline ('\n') or use std::endl. In either case, a newline is sent to the screen, which could result in the screen rolling up. If you insist on using stdout, then you should probably use std::flush, instead, which sends output to the screen without adding a newline.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900