⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake.c

📁 linux curses 图形编程的例子
💻 C
📖 第 1 页 / 共 2 页
字号:
 
/*  
	for linux
	编译: g++ snake.c -lcurses -o snake

*/


#include <sys/types.h>
#include <unistd.h>
#include <curses.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
 
 
#define L 20
#define N 12
#define LEVEL 1
#define SNAKE_LONG 3
#define SNAKE_HEAD_N 3
#define SNAKE_HEAD_L 5
 
int head_n, head_l, rear_n, rear_l;
int food_n, food_l;
int snake_speed, per_level_time;
int level, score;
int is_game_over;
char snake_box[N+2][L+2];
char move_box[N+2][L+2];
char current_way;
char ch_score[6];
char one[6][6] =  {"  0","  0","  0","  0","  0"};
char two[6][6] =  {"00000","    0", "00000", "0", "00000"};
char three[6][6] = {"00000","    0","00000","    0","00000"};
char four[6][6] = {"0 0","0 0","00000","  0","  0"};
char five[6][6] = {"00000","0","00000","    0","00000"};
char six[6][6] = {"00000","0","00000","0   0","00000"};
char seven[6][6] = {"00000","    0","    0","    0","    0"};
char eight[6][6] = {"00000","0   0","00000","0   0","00000"};
char nine[6][6] = {"00000","0   0","00000","    0","00000"};
char zero[6][6] = {"00000","0   0","0   0","0   0","00000"};
 
void handler(int signo);
int snake_refresh(void);
int create_snake(int i, int j);
int create_food();
void initial();
unsigned char my_random(void);
void init_sigaction(void);
void init_time();
void snake_log();
 
 
 
 
int main(void)
{
        int ch, last_ch;        // in case snake run too quick
        int i, j, l, n;
        int snake_head, snake_rear;
 
        is_game_over = 1;
        level = LEVEL;
        score = 0;
        l = L + 2;
        n = N + 2;
 
        /* init move_box */
        for (i=0; i<n; i++)
                for (j=0; j<l; j++) {
                        move_box[i][j] = 'l';
                }
 
        /* init snake_box */
        for (i=0; i<n; i++)
                for (j=0; j<l; j++) {
                        if (i == 0 || j == 0) {
                                snake_box[i][j] = 1;
                        } else if (i == n-1 || j == l-1) {
                                snake_box[i][j] = 1;
                        } else {
                                snake_box[i][j] = 0;
                        }
                }
 
        head_n = SNAKE_HEAD_N;
        head_l = SNAKE_HEAD_L;
        rear_n = SNAKE_HEAD_N;
        rear_l = SNAKE_HEAD_L - SNAKE_LONG + 1;
        current_way = 'r';
 
        /* create a snke */
        create_snake(head_n, head_l);
 
        /* create a food ($ ^_^) */
        create_food();
 
        /* set time */
        snake_speed = 500000 - level*50000;
        per_level_time = 0;
        init_sigaction();       /* SIGNAL */
        init_time();            /* SIGNAL */
 
        initial();
        snake_refresh();
        signal(SIGALRM, handler);
        last_ch = 'a';
        while (is_game_over) {
                ch = getch();
                if (last_ch == ch) {    /* in case input lot of the same key */
                        continue;
                }
                switch (ch) {
                                  case KEY_RIGHT:
                                                         if (current_way == 'l') { break;}
                                                         current_way = 'r';
                                                         snake_refresh();
                                                         break;
                                  case KEY_DOWN:
                                                         if (current_way == 'u') { break;}
                                                         current_way = 'd';
                                                         snake_refresh();
                                                         break;
                                  case KEY_LEFT:
                                                         if (current_way == 'r') { break;}
                                                         current_way = 'l';
                                                         snake_refresh();
                                                         break;
                                  case KEY_UP:
                                                         if (current_way == 'd') { break;}
                                                         current_way = 'u';
                                                         snake_refresh();
                                                         break;
                                  default:
                                                         break;
                }
                last_ch = ch;
                sleep(1);
        }
        endwin();
        snake_log();            /* log to score.txt */
        exit(1);
}
 
void handler(int signo)
{
        per_level_time += snake_speed/100000;
        if (per_level_time >= 300) {            /* 10 is 1 sec */
                if (level <= 9) {
                        level += 1;
                        snake_speed = 500000 - level*50000;
                        init_sigaction();       /* SIGNAL */
                        init_time();            /* SIGNAL */
                        per_level_time = 0;
                }
        }
 
        if (current_way == 'r') {
                snake_right();
        } else if (current_way == 'd') {
                snake_down();
        } else if (current_way == 'l') {
                snake_left();
        } else if (current_way == 'u') {
                snake_up();
        }
        snake_refresh();
        mvprintw(20, 20, "Level: %d\n", level);
}
 
void init_sigaction(void)
{
        struct sigaction act;
        act.sa_handler=handler;
        act.sa_flags=0;
        sigemptyset(&act.sa_mask);
        sigaction(SIGPROF,&act,NULL);
}
 
void init_time()
{
        struct itimerval value;
        value.it_value.tv_sec=0;
        value.it_value.tv_usec=snake_speed;
        value.it_interval=value.it_value;
        setitimer(ITIMER_REAL,&value,NULL);
}
 
int snake_refresh(void)
{
        int x, y;
        int  i, j, l, n;
        int score_len, length;
        l = L + 2;
        n = N + 2;
 
        x = 5, y = 5;
        for (i=0; i<n; i++)
                for (j=0; j<l; j++) {
                        if (snake_box[i][j] == 0) {
                                mvprintw(x, y, " ");
                        } else if (snake_box[i][j] == 1) {
                                mvprintw(x, y, "@ ");
                        } else if (snake_box[i][j] == 2) {      /* food */
                                mvprintw(x, y, "$");
                        }
                        y+=2;
                        if (j == l-1) {
                                mvprintw(x, y, "\n");
                                x++;
                                y = 5;
                        }
                }
        x = 6; y = 50;
        mvprintw(x++, y, "00000 00000 00000 00000 00000\n");
        mvprintw(x++, y, "0     0     0   0 0   0 0   \n");
        mvprintw(x++, y, "00000 0     0   0 00000 00000\n");
        mvprintw(x++, y, "    0 0     0   0 0  0  0   \n");
        mvprintw(x, y  , "00000 00000 00000 0   0 00000\n");
 
        attron(A_BOLD);
        sprintf(ch_score, "%d", score);
        score_len = strlen(ch_score);
        i = 0;
        n = 0;
        length = 0;
        x = 60;
        y = 13;
        while (i < score_len) {
                if (ch_score[i] == '0')
                        while (n<=4) {
                                mvprintw(y, x+length, "%s", *(zero+n));
                                n++;
                                y++;
                        }
                if (ch_score[i] == '1')
                        while (n<=4) {
                                mvprintw(y, x+length, "%s", *(one+n));
                                n++;
                                y++;
                        }
                if (ch_score[i] == '2')
                        while (n<=4) {
                                mvprintw(y, x+length, "%s", *(two+n));
                                n++;
                                y++;
                        }
                if (ch_score[i] == '3')
                        while (n<=4) {
                                mvprintw(y, x+length, "%s", *(three+n));
                                n++;
                                y++;
                        }
                if (ch_score[i] == '4')
                        while (n<=4) {
                                mvprintw(y, x+length, "%s", *(four+n));
                                n++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -