Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include "lib.h"
- #include <sys/poll.h>
- #include <signal.h>
- #define TIMEOUT 5
- #define ROWS 50
- #define COLS 50
- #define HERO_SYMBOL '@'
- #define FOOD_SYMBOL '$'
- void clrScr(void){
- printf("\033[0;0H\033[2J");
- fflush(stdout);
- }
- static int scores;
- struct Food{
- size_t x;
- size_t y;
- char symbol;
- };
- struct Game{
- char hero;
- size_t x;
- size_t y;
- char board[COLS][ROWS];
- struct Food food;
- };
- void generateFood(struct Game *game) {
- game->food.x = generate_random_in_range(0, COLS);
- game->food.y = generate_random_in_range(0, ROWS);
- struct Food food = game->food;
- game->board[food.x][food.y] = food.symbol;
- }
- struct Game init_board(size_t x, size_t y){
- struct Game game;
- for (int i = 0; i < ROWS; i++) {
- for (int j = 0; j < COLS; j++) {
- game.board[i][j] = ' ';
- }
- }
- game.x = x;
- game.y = y;
- game.board[x][y] = HERO_SYMBOL;
- game.hero = HERO_SYMBOL;
- game.food.symbol = FOOD_SYMBOL;
- generateFood(&game);
- return game;
- }
- void drawCurrentState(struct Game game) {
- int cols;
- int rows;
- tc_get_cols_rows(&cols, &rows);
- tc_move_cursor((cols - 12)/2, rows/2);
- printf("%s%s Scores %d %s", TC_YEL, TC_BG_GRN, scores, TC_NRM);
- tc_move_cursor(0,0);
- for (size_t i = 0; i <= ROWS; ++i) {
- printf("--");
- }
- printf("\n");
- // Print the current state of the board
- for (int i = 0; i < COLS; i++) {
- for (int j = 0; j < ROWS; j++) {
- if(j == 0){
- printf("| ");
- }
- printf("%c ", game.board[i][j]);
- if(j == ROWS - 1){
- printf("|");
- }
- }
- printf("\n");
- }
- for (size_t i = 0; i <= ROWS; ++i) {
- printf("--");
- }
- printf("\n");
- }
- void exit_game() {
- tc_exit_alt_screen();
- reset_keypress();
- exit(0);
- }
- void move(struct Game *game, size_t x, size_t y){
- if(x >= COLS || y >= ROWS){
- printf("Exit");
- exit_game();
- }
- if(game->food.x == x && game->food.y == y){
- scores++;
- generateFood(game);
- }
- game->board[game->x][game->y] = ' ';
- game->x = x;
- game->y = y;
- game->board[x][y] = game->hero;
- }
- void handleButton(struct Game *game, char c) {
- if(c == 'w'){
- move(game, game->x - 1, game->y);
- }
- if(c == 'a'){
- move(game, game->x, game->y - 1);
- }
- if(c == 's'){
- move(game, game->x + 1, game->y);
- }
- if(c == 'd'){
- move(game, game->x, game->y + 1);
- }
- }
- int main(int argc, char **argv){
- srand(time(NULL));
- struct Game game = init_board(1, 1);
- set_keypress();
- tc_enter_alt_screen();
- signal(SIGINT, SIG_IGN); // signal interrupt
- signal(SIGTSTP, SIG_IGN); // terminal stop SIGSTOP cannot be ignored. SIGTSTP might be.
- clrScr();
- struct pollfd fds[1];
- fds[0].fd = 0;
- fds[0].events = POLLIN; // without blocking
- char lastInput = 'x';
- while(1) {
- clrScr();
- drawCurrentState(game);
- int ret = poll(fds, 1, 1000);
- // printf("ASD %d\n", ret);
- if (ret == -1){
- perror("poll()");
- exit_game();
- }
- if(ret > 0){
- char c = (char)getc(stdin);
- if(c == 'q'){
- exit_game();
- break;
- }
- handleButton(&game, c);
- lastInput = c;
- }else{
- handleButton(&game, lastInput);
- }
- }
- exit_game();
- }
Advertisement
Add Comment
Please, Sign In to add comment