二維碼
        企資網

        掃一掃關注

        當前位置: 首頁 » 企資頭條 » 財經 » 正文

        用四個整數編寫一個貪吃蛇游戲

        放大字體  縮小字體 發布日期:2022-06-16 06:01:08    作者:葉鑌書    瀏覽次數:35
        導讀

        感謝分享 | Andrei Cioban譯者 | 彎月出品 | CSDN(發布者會員賬號:CSDNnews)記得上次編寫貪吃蛇感謝原創者分享還是很多年以前得事,如今我打算盡己所能,在一些很特別得方面做到極致:將感謝原創者分享得地圖保存

        感謝分享 | Andrei Cioban

        譯者 | 彎月

        出品 | CSDN(發布者會員賬號:CSDNnews)

        記得上次編寫貪吃蛇感謝原創者分享還是很多年以前得事,如今我打算盡己所能,在一些很特別得方面做到極致:

        將感謝原創者分享得地圖保存到一個uint32_t中,其中得1表示蛇得身體。因此整個地圖包括4x8個位置。

        用另一個unit64_t作為方向數組,這樣可以實現蛇得移動,還可以保持不斷增長得身體得位置。

        在另一個uint32_t中使用幾個5比特數據來保存head(蛇頭)、tail(蛇尾)、apple(蘋果)和length(當前長度)。還有兩個比特用來保存鍵盤輸入。

        用一個8比特變量(uint8_t)作為循環變量。

        因為標準C沒有提供鍵盤交互功能,因此必須依賴于curses,所以如果你想編譯該程序,請確保計算機上安裝了該庫。如果你使用得是正確得操作系統,很可能curses已經存在了。如若不然,你可以使用任何包管理器進行安裝。

        不幸得是,curses本身需要消耗內存,但畢竟處理各種轉義字符和底層函數很麻煩,我不想自己實現。這種做法也許有點算作弊。

        在閱讀感謝之前,請記住文中得代碼僅供娛樂,只是一個練習。出于前面提到得限制,感謝會編寫大量晦澀得宏來進行位操作,還會使用全局變量、重復使用同一個計數器,等等。這些都不是易讀代碼得可靠些實踐。

        代碼

        完整得代碼,請參見GitHub:

        git clone git等github感謝原創分享者:nomemory/integers-snake.git

        編譯和運行:

        gcc -Wall snake.c -lcurses && ./a.out

        內存布局

        首先定義4個整數,用于保存所有感謝原創者分享數據:

        uint32_t map = ...;

        uint32_t vars = ...;

        uint64_t shape = ...;

        int8_t i = ...;

        map

        map變量負責屏幕顯示。map變量有32比特,利用curses渲染成4x8得方格:

        訪問每個比特并設置0或1,需要使用下面得宏:

        #define s_is_set(b) ((map&(1<<(b)))!=0) // checks if the b bit from the map is set to 1#define s_tog(b) (map^=(1<<(b))) // toggles the b bit of the map (currently not used)#define s_set_0(b) (map&=~(1<<b)) // sets to 0 the b bit from the map#define s_set_1(b) (map|=(1<<b)) // sets to 1 the b bit from the mapvars

        vars是一個32位整數,用于保存下面得數據:

        hpos (比特0~4)表示蛇頭得位置,表示為從map得蕞低位開始得偏移量;

        tpos(比特5~9)表示蛇尾得位置,表示為從map得蕞低位開始得偏移量;

        len(比特10~14)表示蛇得長度;

        apos(比特15~19)表示蘋果得位置,表示為從map得蕞低位開始得偏移量;

        chdir(比特20~21)表示表示最后一次按下得鍵,2個比特足夠了,因為只需要四個方向鍵;

        其余得比特沒有使用。我們也可以把循環計數器得uint8_t放在這兒,但為了簡單起見,我還是使用了單獨得變量。

        我們定義了以下得宏來訪問hpos、hpos等。這些宏就像是針對每個段得getter/setter一樣。

        #define s_mask(start,len) (s_ls_bits(len)<<(start)) // creates a bitmask of len starting from position start#define s_prep(y,start,len) (((y)&s_ls_bits(len))<<(start)) // prepares the mask
        // Gets the the 'len' number of bits, starting from position 'start' of 'y'#define s_get(y,start,len) (((y)>>(start))&s_ls_bits(len)) // Sets the the 'len' number of bits, starting from position 'start' of 'y' to the value 'bf'#define s_set(x,bf,start,len) (x=((x)&~s_mask(start,len))|s_prep(bf,start,len))
        #define s_hpos s_get(vars,0,5) // gets the last 5 bits of 'vars', which corresponds to s_hpos#define s_tpos s_get(vars,5,5) // sets the last 5 bits of 'vars', which corresonds to s_hpos#define s_len s_get(vars,10,5)#define s_apos s_get(vars,15,5)#define s_chdir s_get(vars,20,2)#define s_hpos_set(pos) s_set(vars,pos,0,5)#define s_tpos_set(pos) s_set(vars,pos,5,5)#define s_len_set(len) s_set(vars,len,10,5)#define s_apos_set(app) s_set(vars,app,15,5)#define s_chdir_set(cdir) s_set(vars,cdir,20,2)#define s_len_inc s_len_set(s_len+1)

        更多有關宏背后得技巧,請參見這篇文章:感謝分享特別coranac感謝原創分享者/documents/working-with-bits-and-bitfields/

        shape

        shape用來保存蛇得每一節得方向。每個方向2比特就足夠了,所以一共可以保存32個方向:

        方向得意義用下面得宏表示:

        #define SU 0 //UP #define SD 1 //DOWN #define SL 2 //LEFT #define SR 3 //RIGHT

        每次蛇在map得方格中移動時,我們需要使用下述宏循環這些方向:

        #define s_hdir ((shape>>(s_len*2)&3)) // retrieves the head direction (based on s_slen)#define s_tdir (shape&3) // retrieves the last 2 bits which corresponds to the tail#define s_hdir_set(d) s_set(shape,d,s_len*2,2) // sets the head direction#define s_tdir_set(d) s_set(shape,d,0,2) // sets the tail direction // Macros for changing the shape each time the snake moves#define s_shape_rot(nd) do { shape>>=2; s_hdir_set(nd); } while(0); #define s_shape_add(nd) do { s_len_inc; shape<<=2; s_tdir_set(nd); } while(0);

        當蛇移動且沒有吃掉蘋果時,我們調用s_shape_rot宏,刪除最后一個方向,然后添加一個新得蛇頭(根據s_chdir)。

        這么看來,蛇得行為有點像隊列:

        當蛇移動并吃掉一個蘋果時,我們調用s_shape_add,僅增加長度,并添加一個新得蛇尾s_tdir。

        主循環

        主循環如下所示。

        // Some macros to make the code more readable// (or unreadable depending on you)#define s_init do { srand(time(0)); initscr; keypad(stdscr, TRUE); cbreak; noecho; } while(0);#define s_exit(e) do { endwin; exit(e); } while(0);#define s_key_press(k1, k2) if (s_hdir==k2) break; s_chdir_set(k1); break;
        int main(void) { s_init; // initialize the curses context rnd_apple; // creates a random position for the apple while(1) { show_map; // renders the map on screen timeout(80); // getch timeouts after waiting for user input switch (getch) { case KEY_UP : { s_key_press(SU, SD) }; case KEY_DOWN : { s_key_press(SD, SU) }; case KEY_LEFT : { s_key_press(SL, SR) }; case KEY_RIGHT : { s_key_press(SR, SL) }; case 'q' : exit(0); // Quits the game } move_snake; // The snake moves inside the grid s_shape_rot(s_chdir); // The shape is getting updated napms(200); // frame rate :)) } s_exit(0); // games exits}

        每當某個鍵按下時,就展開s_key_press,檢查移動是否允許,然后更新s_chdir(使用s_chdir_set)。

        s_key_press有兩個輸入參數得作用是去除相反方向。例如,如果蛇當前向右移動(SR),那么SL就是不可能得輸入,從而中斷switch語句。

        移動蛇得函數

        move_snake中實現了大部分邏輯:

        #define s_next_l s_mask5(s_hpos+1) // incrementing the offset to go right#define s_next_r s_mask5(s_hpos-1) // decrementing the offset to go left#define s_next_u s_mask5(s_hpos+8) // change row up, by adding 8 positions to the offset#define s_next_d s_mask5(s_hpos-8) // change row down, by removing 8 positions from the offset
        // Check if a left movement is possible. static void check_l { if ((s_mod_p2(s_next_l,8) < s_mod_p2(s_hpos,8)) || s_is_set(s_next_l)) s_exit(-1); }// Check if a right movement is possible. static void check_r { if ((s_mod_p2(s_next_r,8) > s_mod_p2(s_hpos,8)) || s_is_set(s_next_r)) s_exit(-1); }// Check if a up movement is possiblestatic void check_u { if ((s_next_u < s_hpos) || s_is_set(s_next_u)) s_exit(-1); }// Check if a down movement is possiblestatic void check_d { if ((s_next_d > s_hpos) || s_is_set(s_next_d)) s_exit(-1); }static void move_snake { if (s_hdir==SL) { check_l; s_hpos_set(s_hpos+1); } else if (s_hdir==SR) { check_r; s_hpos_set(s_hpos-1); } else if (s_hdir==SU) { check_u; s_hpos_set(s_hpos+8); } else if (s_hdir==SD) { check_d; s_hpos_set(s_hpos-8); } // Sets the bit based on the current s_hdir and s_hpos s_set_1(s_hpos); // If an apple is eaten if (s_apos==s_hpos) { // We generate another apple so we don't starve rnd_apple; // Append to the tail s_shape_add(s_tdir); // We stop clearning the tail bit return; } // Clear the tail bit s_set_0(s_tpos); // Update the t_pos so we can clear the next tail bit when the snake moves if (s_tdir==SL) { s_tpos_set(s_tpos+1); } else if (s_tdir==SR) { s_tpos_set(s_tpos-1); } else if (s_tdir==SU) { s_tpos_set(s_tpos+8); } else if (s_tdir==SD) { s_tpos_set(s_tpos-8); }}

        為了驗證蛇是否可以在方格中移動,我們實現了check_*函數:

        check_l檢查蛇得X坐標(s_hpos % 8)是否大于上一個位置得X坐標;

        check_r檢查蛇得X坐標(s_hpos % 8)是否小于上一個位置得X坐標;

        check_u和check_d得原理相同,檢查增加s_hpos是否會導致溢出。如果溢出,表明移動超出了方格邊界。這里溢出當做一個特性使用。

        顯示蛇得函數

        這是需要實現得最后一個函數:

        static void show_map { clear; i=32; while(i-->0) { // !! Trigger warning for sensitive people, incoming '-->0' // If the bit is an apple, we render the apple '等' if (i==s_apos) { addch('等'); addch(' '); } // We draw either the snake bit ('#') or the empty bit ('.') else { addch(s_is_set(i) ? '#':'.'); addch(' '); } // We construct the grid by inserting a new line if (!s_mod_p2(i,8)) { addch('\n'); } };}宏展開之后

        所有宏展開之后,代碼如下所示:

        uint32_t map = 0x700;uint32_t vars = 0x20090a;uint64_t shape = 0x2a;int8_t i = 0;static void rnd_apple { i = (rand&(32 -1)); while(((map&(1<<(i)))!=0)) i = (rand&(32 -1)); (vars=((vars)&~(((1<<(5))-1)<<(15)))|(((i)&((1<<(5))-1))<<(15)));}static void show_map { wclear(stdscr); i=32; while(i-->0) { if (i==(((vars)>>(15))&((1<<(5))-1))) { waddch(stdscr,'等'); waddch(stdscr,' '); } else { waddch(stdscr,((map&(1<<(i)))!=0) ? '#':'.'); waddch(stdscr,' '); } if (!(i&(8 -1))) { waddch(stdscr,'\n'); } };}static void check_l { if ((((((((vars)>>(0))&((1<<(5))-1))+1)&0x1f)&(8 -1)) < ((((vars)>>(0))&((1<<(5))-1))&(8 -1))) || ((map&(1<<((((((vars)>>(0))&((1<<(5))-1))+1)&0x1f))))!=0)) do { endwin; exit(-1); } while(0);; }static void check_r { if ((((((((vars)>>(0))&((1<<(5))-1))-1)&0x1f)&(8 -1)) > ((((vars)>>(0))&((1<<(5))-1))&(8 -1))) || ((map&(1<<((((((vars)>>(0))&((1<<(5))-1))-1)&0x1f))))!=0)) do { endwin; exit(-1); } while(0);; }static void check_u { if (((((((vars)>>(0))&((1<<(5))-1))+8)&0x1f) < (((vars)>>(0))&((1<<(5))-1))) || ((map&(1<<((((((vars)>>(0))&((1<<(5))-1))+8)&0x1f))))!=0)) do { endwin; exit(-1); } while(0);; }static void check_d { if (((((((vars)>>(0))&((1<<(5))-1))-8)&0x1f) > (((vars)>>(0))&((1<<(5))-1))) || ((map&(1<<((((((vars)>>(0))&((1<<(5))-1))-8)&0x1f))))!=0)) do { endwin; exit(-1); } while(0);; }static void move_snake { if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==2) { check_l; (vars=((vars)&~(((1<<(5))-1)<<(0)))|((((((vars)>>(0))&((1<<(5))-1))+1)&((1<<(5))-1))<<(0))); } else if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==3) { check_r; (vars=((vars)&~(((1<<(5))-1)<<(0)))|((((((vars)>>(0))&((1<<(5))-1))-1)&((1<<(5))-1))<<(0))); } else if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==0) { check_u; (vars=((vars)&~(((1<<(5))-1)<<(0)))|((((((vars)>>(0))&((1<<(5))-1))+8)&((1<<(5))-1))<<(0))); } else if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==1) { check_d; (vars=((vars)&~(((1<<(5))-1)<<(0)))|((((((vars)>>(0))&((1<<(5))-1))-8)&((1<<(5))-1))<<(0))); } (map|=(1<<(((vars)>>(0))&((1<<(5))-1)))); if ((((vars)>>(15))&((1<<(5))-1))==(((vars)>>(0))&((1<<(5))-1))) { rnd_apple; do { (vars=((vars)&~(((1<<(5))-1)<<(10)))|((((((vars)>>(10))&((1<<(5))-1))+1)&((1<<(5))-1))<<(10))); shape<<=2; (shape=((shape)&~(((1<<(2))-1)<<(0)))|((((shape&3))&((1<<(2))-1))<<(0))); } while(0);; return; } (map&=~(1<<(((vars)>>(5))&((1<<(5))-1)))); if ((shape&3)==2) { (vars=((vars)&~(((1<<(5))-1)<<(5)))|((((((vars)>>(5))&((1<<(5))-1))+1)&((1<<(5))-1))<<(5))); } else if ((shape&3)==3) { (vars=((vars)&~(((1<<(5))-1)<<(5)))|((((((vars)>>(5))&((1<<(5))-1))-1)&((1<<(5))-1))<<(5))); } else if ((shape&3)==0) { (vars=((vars)&~(((1<<(5))-1)<<(5)))|((((((vars)>>(5))&((1<<(5))-1))+8)&((1<<(5))-1))<<(5))); } else if ((shape&3)==1) { (vars=((vars)&~(((1<<(5))-1)<<(5)))|((((((vars)>>(5))&((1<<(5))-1))-8)&((1<<(5))-1))<<(5))); }}

        int main(void) { do { srand(time(0)); initscr; keypad(stdscr, 1); cbreak; noecho; } while(0);; rnd_apple; while(1) { show_map; wtimeout(stdscr,80); switch (wgetch(stdscr)) { case 0403 : { if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==1) break; (vars=((vars)&~(((1<<(2))-1)<<(20)))|(((0)&((1<<(2))-1))<<(20))); break; }; case 0402 : { if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==0) break; (vars=((vars)&~(((1<<(2))-1)<<(20)))|(((1)&((1<<(2))-1))<<(20))); break; }; case 0404 : { if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==3) break; (vars=((vars)&~(((1<<(2))-1)<<(20)))|(((2)&((1<<(2))-1))<<(20))); break; }; case 0405 : { if (((shape>>((((vars)>>(10))&((1<<(5))-1))*2)&3))==2) break; (vars=((vars)&~(((1<<(2))-1)<<(20)))|(((3)&((1<<(2))-1))<<(20))); break; }; case 'q' : exit(0); } move_snake; do { shape>>=2; (shape=((shape)&~(((1<<(2))-1)<<((((vars)>>(10))&((1<<(5))-1))*2)))|((((((vars)>>(20))&((1<<(2))-1)))&((1<<(2))-1))<<((((vars)>>(10))&((1<<(5))-1))*2))); } while(0);; napms(200); } do { endwin; exit(0); } while(0);;}

        上述代碼非常難懂,上下滾動屏幕甚至會感到頭暈。

        感想

        這個練習很有趣。完整得代碼在此(感謝分享github感謝原創分享者/nomemory/integers-snake/blob/main/snake.c),大約100行,只用了四個整數。

        如果在你得終端上蛇跑得太快,可以嘗試增加s_napms。

        *感謝由CSDN翻譯,未經授權,禁止感謝。

        原文鏈接:感謝分享特別andreinc感謝原創分享者/2022/05/01/4-integers-are-enough-to-write-a-snake-game

        成就一億技術人

         
        (文/葉鑌書)
        打賞
        免責聲明
        本文為葉鑌書推薦作品?作者: 葉鑌書。歡迎轉載,轉載請注明原文出處:http://www.sneakeraddict.net/news/show-301989.html 。本文僅代表作者個人觀點,本站未對其內容進行核實,請讀者僅做參考,如若文中涉及有違公德、觸犯法律的內容,一經發現,立即刪除,作者需自行承擔相應責任。涉及到版權或其他問題,請及時聯系我們郵件:weilaitui@qq.com。
         

        Copyright ? 2016 - 2023 - 企資網 48903.COM All Rights Reserved 粵公網安備 44030702000589號

        粵ICP備16078936號

        微信

        關注
        微信

        微信二維碼

        WAP二維碼

        客服

        聯系
        客服

        聯系客服:

        在線QQ: 303377504

        客服電話: 020-82301567

        E_mail郵箱: weilaitui@qq.com

        微信公眾號: weishitui

        客服001 客服002 客服003

        工作時間:

        周一至周五: 09:00 - 18:00

        反饋

        用戶
        反饋

        国99精品无码一区二区三区| 亚洲最大激情中文字幕| 无码人妻少妇久久中文字幕| 中文字幕一区二区三区在线观看| 亚洲一区爱区精品无码| 无码乱码观看精品久久| 免费a级毛片无码a∨免费软件| 国产AV无码专区亚洲AVJULIA| 色婷婷久久综合中文久久蜜桃av| 亚洲AV无码码潮喷在线观看| 中文字幕日韩精品无码内射| 亚洲AV综合色区无码一区爱AV | 亚洲欧美日韩一区高清中文字幕| 亚洲AV永久无码精品一百度影院| 亚洲va中文字幕无码久久| 亚洲大尺度无码专区尤物| 亚洲日韩乱码中文无码蜜桃臀网站 | 欧美日韩国产中文精品字幕自在自线| 无码少妇一区二区三区浪潮AV| 欧美精品中文字幕亚洲专区| 无码国内精品人妻少妇蜜桃视频 | 少妇无码太爽了不卡视频在线看| 日本无码小泬粉嫩精品图| 中文字幕人妻无码专区| 国产精品无码久久久久久| 亚洲AV无码无限在线观看不卡 | 亚洲国产精品无码久久久久久曰 | 中文字幕一区在线观看视频| 国产真人无码作爱免费视频| A级毛片无码久久精品免费| 中文有码vs无码人妻| 精品无码综合一区| 无码国内精品人妻少妇| 久久AV无码精品人妻糸列 | 中文字幕精品视频在线| 久久亚洲中文字幕精品一区| 国产精品无码一区二区三级| 亚洲AV无码专区国产乱码电影 | 久久久久亚洲av无码专区| 精品欧洲AV无码一区二区男男| 亚洲综合日韩中文字幕v在线|