Some fixes & start to make gameplay
- Fix input function, so now it can be used as thread - Edit main loop, to quit press q - Add score incrementing for playerpull/2/head
parent
75fb0965f1
commit
abda36c1c3
|
@ -9,6 +9,6 @@ typedef struct input_args_t
|
||||||
bool *alive;
|
bool *alive;
|
||||||
} InputArgs;
|
} InputArgs;
|
||||||
|
|
||||||
void input(void *vargp);
|
void *input(void *vargp);
|
||||||
|
|
||||||
#endif /* __INPUT_H__ */
|
#endif /* __INPUT_H__ */
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ char getch(void)
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void input(void *vargp)
|
void *input(void *vargp)
|
||||||
{
|
{
|
||||||
char *out = ((InputArgs *)vargp)->out;
|
char *out = ((InputArgs *)vargp)->out;
|
||||||
bool *alive = ((InputArgs *)vargp)->alive;
|
bool *alive = ((InputArgs *)vargp)->alive;
|
||||||
|
@ -30,4 +31,5 @@ void input(void *vargp)
|
||||||
{
|
{
|
||||||
*out = getch();
|
*out = getch();
|
||||||
}
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
30
src/main.c
30
src/main.c
|
@ -1,8 +1,34 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "input.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
|
#include "player.h"
|
||||||
|
|
||||||
|
void drawPlayer(Player *player, Screen *screen)
|
||||||
|
{
|
||||||
|
PlayerNode *node;
|
||||||
|
for (node = player->tail; node != NULL; node = node->next)
|
||||||
|
*screenGetPoint(screen, node->x, node->y) = '#';
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
Screen *screen = screenCreate(5, 5, '#');
|
Player *player = playerCreate(DOWN, 0, 0, 0);
|
||||||
screenShow(screen);
|
Screen *screen = screenCreate(10, 10, ' ');
|
||||||
|
pthread_t input_thread;
|
||||||
|
|
||||||
|
bool *running = malloc(sizeof(bool)); *running = true;
|
||||||
|
char *key = malloc(sizeof(char)); *key = 0;
|
||||||
|
InputArgs input_args = (InputArgs){ key, running };
|
||||||
|
|
||||||
|
pthread_create(&input_thread, NULL, input, &input_args);
|
||||||
|
while (*running)
|
||||||
|
{
|
||||||
|
if (*key == 'q') *running = false;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,5 +54,8 @@ void playerDoTick(Player *player, bool food_collision)
|
||||||
PlayerNode *new_tail = player->tail->next;
|
PlayerNode *new_tail = player->tail->next;
|
||||||
free(player->tail);
|
free(player->tail);
|
||||||
player->tail = new_tail;
|
player->tail = new_tail;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
++player->score;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue