1. 程式人生 > >初試linux編譯(ubuntu+vim)+玩轉智慧蛇

初試linux編譯(ubuntu+vim)+玩轉智慧蛇

一.初試linux編譯(ubuntu+vim)

步驟:
①下載vmware15+ubuntu桌面版映像
②安裝ubuntu
③下載vim+gcc
在ubuntu終端輸入:

sudo apt-get install vim-gtk
sudo apt-get install gcc

④安裝完畢後進行編譯測試
1)新建helloworld.c

vim helloworld.c
在這裡插入圖片描述

2)進入介面按 i 即可輸入程式碼
在這裡插入圖片描述

3)按ESC再按ZZ(大寫)即可儲存並退出

4)再在終端輸入以下程式碼進行編譯

gcc helloworld.c

5)輸入以下程式碼執行

./a.out

在這裡插入圖片描述
在這裡插入圖片描述

二.執行sin-demo(一個好玩的正弦函式動態展示)

在這裡插入圖片描述

三.簡單貪吃蛇(字元版)——不可使用getch()和清屏等操作

①程式頭部(生成初始地圖 禮物 蛇)
程式實現思想已經在我的註釋裡面啦~~
我想大家應該能很快理解的

/* 
	snake_move.c
	
	2018.11.30
	Created by sysu-18342026
	
	Use 'w'(up),'s'(down),'a'(left),'d'(right) to move and eat the gold.
	Be careful not to hit yourseld as well as the wall(*)!
	
	Happy playing!
*/
#include <stdio.h> #include <stdlib.h> #include <time.h> #define SNAKE_MAX_LENGTH 10 #define SNAKE_HEAD 'H' #define SNAKE_BODY 'X' #define BLANK_CELL ' ' #define SNAKE_FOOD '$' #define WALL_CELL '*' //output the initial map,food and snake. void output(); void creatrandomfood(); //use 'w'(up),'s'(down),'a'(left),'d'(right) and 'Enter' to move.
void snakemove(); //When the snake hits itself or hits the wall,the game will be over. int gameover(); //the begining char map[12][12] = { "***********", "*XXXXH *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "***********", }; //the initial position and length int snake_length = 5; int snake_x[SNAKE_MAX_LENGTH] = {5, 4, 3, 2, 1}; int snake_y[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1}; int food_x; int food_y;

②讓蛇動起來
先參考TA的虛擬碼如下
在這裡插入圖片描述

再簡單地轉成c語言(實際上磕磕絆絆弄了好久,哭了~~)

/* 
	snake_eat.c
	
	2018.11.30
	Created by sysu-18342026
	
	Use 'w'(up),'s'(down),'a'(left),'d'(right) to move and eat the gold.
	Be careful not to hit yourseld as well as the wall(*)!
	
	Happy playing!
*/

int main() {
	
	output();				//output the initial map and snake.
    creatrandomfood();		//output the random food.
    
    char direction;
    int flag=1;
    
    while (flag==1) {
    	
        scanf(" %c", &direction);		//input the moving direction
        
        snakemove();					//use 'w'(up),'s'(down),'a'(left),'d'(right) and 'Enter' to move.
        
        if (direction == 'w') {
            snake_y[0] -= 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (direction == 's') {
            snake_y[0] += 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (direction == 'a') {
            snake_x[0] -= 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (direction == 'd') {
            snake_x[0] += 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (snake_x[0] == food_x && snake_y[0] == food_y) {
            creatrandomfood();
            
            snake_length++;
            
            snake_x[snake_length - 1] = snake_x[snake_length - 2];
            snake_y[snake_length - 1] = snake_y[snake_length - 2];
            
            map[snake_y[snake_length - 1]][snake_x[snake_length - 1]] = SNAKE_BODY;
        }
        
        if (gameover()!=1) {			//When the snake hits itself or hits the wall,
                                        //the game will be over.

            printf("GAMEOVER!\n\nTHANKS FOR PLAYING!\n");
            flag=0;
        } 
		
		else {
            output();
        }
    }
    
    return 0;
}



//output the initial map and snake.
void output() {
	int i,j;
	
    for (i = 0; i <= 11; i++) {
    	
        for (j = 0; j <= 11; j++) {
            printf("%c", map[i][j]);
        }
        	printf("\n");
    }
}

//output the random food.
void creatrandomfood() {
    srand((unsigned)(time(NULL)));
    
    food_x = rand() % 7;
    food_y = rand() % 7;
    
    while (map[food_y][food_x] != BLANK_CELL) {
        food_x = rand() % 7;
        food_y = rand() % 7;
        
    }
    map[food_y][food_x] = SNAKE_FOOD ;
}

//use 'w'(up),'s'(down),'a'(left),'d'(right) and 'Enter' to move.
void snakemove() {
    int i;
    
    map[snake_y[snake_length - 1]][snake_x[snake_length - 1]] = BLANK_CELL;
    
    for (i = snake_length - 1; i > 0; i--) {
        snake_x[i] = snake_x[i - 1];
        snake_y[i] = snake_y[i - 1];
        map[snake_y[i]][snake_x[i]] = SNAKE_BODY;
    }
}


//When the snake hits itself or hits the wall,the game will be over.
int gameover() {
	int i; 
	
    if (snake_x[0] == SNAKE_MAX_LENGTH || snake_x[0] == 0) {
        return 0;
    }
    
    if (snake_y[0] == SNAKE_MAX_LENGTH || snake_y[0] == 0) {
        return 0;
    }
    
    for (i = 1; i < snake_length; i++) {
        if (snake_x[0] == snake_x[i] && snake_y[0] == snake_y[i]) {
            return 0;
        }
    }
    
    return 1;
}

③簡單的貪吃蛇的效果圖(由於不能用getch()函式,所以比較簡陋= =)
在這裡插入圖片描述

④在ubuntu內執行效果圖(感覺linux中執行得流暢很多啊!!!)
在這裡插入圖片描述

⑤用kbhit()函式進一步完善(醜陋的)貪吃蛇——不需要自己按回車了!!

注:需要在linux執行才行
在這裡插入圖片描述

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SNAKE_MAX_LENGTH 10
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define SNAKE_FOOD '$'
#define WALL_CELL '*'

#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>
#include <unistd.h>

//output the initial map,food and snake.
void output();
void creatrandomfood();

//use 'w'(up),'s'(down),'a'(left),'d'(right) and 'Enter' to move.
void snakemove();

//When the snake hits itself or hits the wall,the game will be over.
int gameover();

static struct termios ori_attr, cur_attr;

static __inline 
int tty_reset(void)
{
        if (tcsetattr(STDIN_FILENO, TCSANOW, &ori_attr) != 0)
                return -1;

        return 0;
}


static __inline
int tty_set(void)
{
        
        if ( tcgetattr(STDIN_FILENO, &ori_attr) )
                return -1;
        
        memcpy(&cur_attr, &ori_attr, sizeof(cur_attr) );
        cur_attr.c_lflag &= ~ICANON;
//        cur_attr.c_lflag |= ECHO;
        cur_attr.c_lflag &= ~ECHO;
        cur_attr.c_cc[VMIN] = 1;
        cur_attr.c_cc[VTIME] = 0;

        if (tcsetattr(STDIN_FILENO, TCSANOW, &cur_attr) != 0)
                return -1;

        return 0;
}

static __inline
int kbhit(void) 
{
                   
        fd_set rfds;
        struct timeval tv;
        int retval;

        /* Watch stdin (fd 0) to see when it has input. */
        FD_ZERO(&rfds);
        FD_SET(0, &rfds);
        /* Wait up to five seconds. */
        tv.tv_sec  = 0;
        tv.tv_usec = 0;

        retval = select(1, &rfds, NULL, NULL, &tv);
        /* Don't rely on the value of tv now! */

        if (retval == -1) {
                perror("select()");
                return 0;
        } else if (retval)
                return 1;
        /* FD_ISSET(0, &rfds) will be true. */
        else
                return 0;
        return 0;
}

//將你的 snake 程式碼放在這裡
char map[12][12] = {
					"***********",
    				"*XXXXH    *",
    				"*         *",
    				"*         *",
    				"*         *",
    				"*         *",
    				"*         *",
    				"*         *",
    				"*         *",
    				"*         *",
    				"***********",
					};

//the initial position and length	        
int snake_length = 5;
int snake_x[SNAKE_MAX_LENGTH] = {5, 4, 3, 2, 1};
int snake_y[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int food_x;
int food_y;


int main()
{
        //設定終端進入非緩衝狀態
        int tty_set_flag;
        tty_set_flag = tty_set();

        //將你的 snake 程式碼放在這裡
        output();				//output the initial map and snake.
    creatrandomfood();		//output the random food.
    
    char direction;
    int flag=1;
    
    while (flag==1) {
    	
        scanf(" %c", &direction);		//input the moving direction
        
        snakemove();					//use 'w'(up),'s'(down),'a'(left),'d'(right) and 'Enter' to move.
        
        if (direction == 'w') {
            snake_y[0] -= 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (direction == 's') {
            snake_y[0] += 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (direction == 'a') {
            snake_x[0] -= 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (direction == 'd') {
            snake_x[0] += 1;
            map[snake_y[0]][snake_x[0]] = SNAKE_HEAD;
        }
        
        if (snake_x[0] == food_x && snake_y[0] == food_y) {
            creatrandomfood();
            
            snake_length++;
            
            snake_x[snake_length - 1] = snake_x[snake_length - 2];
            snake_y[snake_length - 1] = snake_y[snake_length - 2];
            
            map[snake_y[snake_length - 1]][snake_x[snake_length - 1]] = SNAKE_BODY;
        }
        
        if (gameover()!=1) {								//When the snake hits itself or hits the wall,the game will be over.

            printf("GAMEOVER!\n\nTHANKS FOR PLAYING!\n");
            flag=0;
        } 
		
		else {
            output();
        }
    }
        
        printf("pressed `q` to quit!\n");
        while(1) {

                if( kbhit() ) {
                        const int key = getchar();
                        printf("%c pressed\n", key);
                        if(key == 'q')
                                break;
                } else {
                       ;// fprintf(stderr, "<no key detected>\n");
                }
        }

        //恢復終端設定
        if(tty_set_flag == 0) 
                tty_reset();
        return 0;
}

//output the initial map and snake.
void output() {
	int i,j;
	
    for (i = 0; i <= 11; i++) {
    	
        for (j = 0; j <= 11; j++) {
            printf("%c", map[i][j]);
        }
        	printf("\n");
    }
}

//output the random food.
void creatrandomfood() {
    srand((unsigned)(time(NULL)));
    
    food_x = rand() % 7;
    food_y = rand() % 7;
    
    while (map[food_y][food_x] != BLANK_CELL) {
        food_x = rand() % 7;
        food_y = rand() % 7;
        
    }
    map[food_y][food_x] = SNAKE_FOOD ;
}

//use 'w'(up),'s'(down),'a'(left),'d'(right) and 'Enter' to move.
void snakemove() {
    int i;
    
    map[snake_y[snake_length - 1]][snake_x[snake_length - 1]] = BLANK_CELL;
    
    for (i = snake_length - 1; i > 0; i--) {
        snake_x[i] = snake_x[i - 1];
        snake_y[i] = snake_y[i - 1];
        map[snake_y[i]][snake_x[i]]