初试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.c2018.11.30Created by sysu-18342026Use '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.c2018.11.30Created by sysu-18342026Use '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 directionsnakemove(); //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. */elsereturn 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 directionsnakemove(); //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]] = 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;
}
四.玩轉智能蛇
①偽代碼
②實現程序(這一步坑略多,以為可以直接在之前的貪吃蛇加一個函數,不過把自己搞崩了= =)
1)障礙效果
2)自動吃
3)思考:一個長度為5的障礙物能困死該自動跑的蛇嗎?
答:我認為不會困死,設置了最大長度以及最優路線,應該不會困死的。
終于做完了貪吃蛇的學習總結!!!
前前后后踩了很多很多坑,但也學習到了很多東西,這種感覺很充實~~
雖然我的智能蛇還是很(zhizhang)= =
但是它還丑啊(逃)
本次博客到此結束,希望大家也能收獲這份樂趣♂
總結
以上是生活随笔為你收集整理的初试linux编译(ubuntu+vim)+玩转智能蛇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从风投看中国IT行业的发展
- 下一篇: vm无网络解决方法