首页 > 代码库 > 架构练习:c语言实现贪吃蛇(二):使用libncurses.a库实现地图和蘑菇不换页刷新

架构练习:c语言实现贪吃蛇(二):使用libncurses.a库实现地图和蘑菇不换页刷新

现在已经实现地图和蘑菇的,或者说游戏界面的不换页刷新,

这里需要使用libncurses.a库来实现,代码如下。

下一步:实现蛇的移动。


源代码:

root@ubuntu:/mnt/shared/appbox/snake# cat snake.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <curses.h>
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>


#define GAME_WIDTH 50
#define GAME_HIGTH 25
int middle_line_create(WINDOW *win,int x_site, int y_site)
{
        int i, j;

        for(j=0;j<GAME_HIGTH;j++)
        {
                for(i=0;i<GAME_WIDTH;i++)
                {

                        if(i == x_site && j == y_site)
                        {
                                //printf("*");
                                mvwprintw(win,j,i,"*");
                        }
                        else if(j == 24 || j ==0)
                        {
                                mvwprintw(win,j,i,"-");
                        }
                        else if(1 == i)
                        {
                                //printf("|");
                                mvwprintw(win,j,i,"|");
                        }
                        else if(i== (GAME_WIDTH-1))
                        {
                                //printf("|\n");
                                mvwprintw(win,j,i,"|");
                        }
                        else 
                        {
                                //printf(" ");
                                mvwprintw(win,j,i," ");
                        }
                }
        }

        return 0;
}


int target_create(int *x_site, int *y_site)
{
        int seconds= time((time_t*)NULL); 

        *y_site = abs(seconds*random())%GAME_HIGTH;
        *x_site = abs(seconds*random())%GAME_WIDTH;

        return 0;
}


static int need_new_target = 1;
static int x_site, y_site;
int frame_create(WINDOW *win)
{
        int i, j;

        if(need_new_target == 1)
        {
                target_create(&x_site, &y_site);
                need_new_target = 0;
        }
//      printf("x:%d, y:%d\n", x_site, y_site);
        middle_line_create(win, x_site, y_site);

        return 0;
}

/*
resource ncurses_newwin ( int rows, int cols, int y, int x);

ncurses_newwin() creates a new window to draw elements in. Windows can be positioned using x, y, rows and cols. When creating additional windows, remember to use ncurses_getmaxyx() to check for available space, as terminal size is individual and may vary. The return value is a resource ID used to differ between multiple windows.
*/

int main(int argc, char *argv[])
{
        int x,y;
        time_t t;
        pthread_t thread;
        WINDOW *win;

        initscr();          //初始化标准窗口,必需滴
        curs_set(0);        //隐藏光标动态
        noecho();           //输入无回显
        win=newwin(25,50,0,0);
        getmaxyx(win,y,x);   
        printf("a:%d, b:%d\n",x,y);
        refresh();          //刷新一下标准窗口,不然无法显示新建的窗口
        wrefresh(win);      //刷新一下新建的窗口
        while(1)
        {
                frame_create(win);
                wrefresh(win);  
                sleep(1);
        }

        endwin();

        return 0;
}
root@ubuntu:/mnt/shared/appbox/snake# 


架构练习:c语言实现贪吃蛇(二):使用libncurses.a库实现地图和蘑菇不换页刷新