0

I have a test programm from my programming course, it must to be a console game like a across the street. It's look like

┌###################################################################┐

X        X                    X  X
                                 X          X
                 X            X
                              X        XX
       X                             X     X  X
                        X  X           X    X
 X   X                   X  X            X
           X         X
               X                            X
     X  X

                      @

└####################################################################┘

@ - player (Froggy)

X - cars that are moving

I have never worked with ncurses and threads, and my task is to implement the parallel working of gechar() function, that take the input of moving direction from the user and move the player and ncruses functions ( they are already implement and working, i don't need to change something ). I also have declared global variables and structures. As i understood, i need also to use mutex. I did it like this:

// Represents a specific screen positions by Cartesian coordinates
typedef struct{
int x;
int y;
} Point;

// Cars on the motorway are organized using a linked list
// Every list element stores the car's coordinates and a pointer to the next car
typedef struct Car {
Point position;
struct Car* next;
} Car;

// List of cars on motorway (pointer to first list element)
Car* motorway;

// Position of froggy on screen
Point froggy;

// Variables to represent current game state
unsigned int error;
unsigned int quit;
unsigned int player_lost;
unsigned int player_won;
bool checker;
bool first_frogy;

// Other global variables
WINDOW* screen;
int old_cursor = 0;
char new_input;
pthread_mutex_t my_mutex;

Is it enough one mutex for all global variables and structures? Did i do it correctly?

In main function i have while(), that call all functions in circle and i declared hier the new thread, that call my new catch_input() function.

status = pthread_create(&thread, NULL, catch_input(), NULL);
if (status != 0) {
   printf("main error: can't create thread, status = %d\n", status);
   exit(10);
}

main(){
...
while(!quit && !error && !player_lost && !player_won)
{
    moveFroggy(new_input);
    moveCarsOnMotorway();
    startCar((SCREEN_WIDTH - OUTER_BORDER));
    drawScreen();
    usleep(GAME_SPEED);
}
...
}

void* catch_input(){
char buf;
while((buf = getchar())){
    pthread_mutex_lock(&my_mutex);
    new_input = buf;
    checker = true;
    pthread_mutex_unlock(&my_mutex);
}
pthread_exit(NULL);
}

The thread working correct and take all inputs ( i checked it with printw() ), but the graphical functions are not working. If i delete thread, the graphical functions working correctly and i can see the map with moving cars and the player on my screen. What can be a problem? Thank you for the answers

Anton Barinov
  • 23
  • 1
  • 5
  • 1
    For such games, ncurses is love, life and everything, but the only thing you can really do to learn ncurses is to read the docs. And ncurses is also oen of the simplest libs in c, so take a look at the docs, that will solve some of your problems :) – N.K Oct 19 '16 at 10:10
  • thank you for the answer :) The thing that i really dont need it, it's just one assignment, and the main thing here is threads. So, i need to understand the working process of threads , not the ncurses )) – Anton Barinov Oct 19 '16 at 10:13
  • The only place `ncurses` appears in the incomplete example is in the declaration of `screen` (and making it work with mutexes is complicated). – Thomas Dickey Oct 19 '16 at 23:11

0 Answers0