使用C語言如何實(shí)現(xiàn)一個(gè)俄羅斯方塊小游戲?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
為鹽城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及鹽城網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、網(wǎng)站制作、鹽城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!具體內(nèi)容如下
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define TTY_PATH "/dev/tty" #define STTY_ON "stty raw -echo -F" #define STTY_OFF "stty -raw echo -F" int map[21][14]; char direct; int node[7][4][16]={ {{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},//長(zhǎng)方形 {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}, {0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}}, {{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},//正方形 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}}, {{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},//3邊加一中點(diǎn) {0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0}, {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}}, {{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},//右鋤頭型 {0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}, {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}, {1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0}}, {{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},//左鋤頭型 {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0}, {0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0}}, {{0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},//右曲折型 {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0}, {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}}, {{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},//左曲折型 {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}, {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}} }; typedef struct block { int x; int y; int blockType; int blockDirect; }Block; Block bl; void init_map()//初始化邊框 { int i,j; for(i=0; i<21; i++) for(j=0; j<14; j++) { if(j==0 || j==13) map[i][j] = 200; else if(i==20) map[i][j] = 201; else map[i][j] = 0; } } void new_block()//生成隨機(jī)的俄羅斯方塊 { int blockType = rand()%7; int blockDirect = rand()%4; int x = 1; int y = 5; bl.x = x; bl.y = y; bl.blockType = blockType; bl.blockDirect = blockDirect; } void input()//將移動(dòng)后的俄羅斯方塊,導(dǎo)入地圖中作標(biāo)記 { int i, j; for(i=0; i<4; i++) for(j=0; j<4; j++) if(node[bl.blockType][bl.blockDirect][i*4+j]==1) { map[bl.x+i][bl.y+j] = 1; } } void output()//移動(dòng)時(shí),將之前俄羅斯方塊在地圖信息清空。 { int i, j; for(i=0; i<4; i++) for(j=0; j<4; j++) if(node[bl.blockType][bl.blockDirect][i*4+j]==1) { map[bl.x+i][bl.y+j] = 0; } } void change()//俄羅斯方格在碰撞后融入,固定 { int i, j; for(i=0; i<4; i++) for(j=0; j<4; j++) if(node[bl.blockType][bl.blockDirect][i*4+j]==1) { map[bl.x+i][bl.y+j] = 10; } for(j=1; j<13; j++) if(map[5][j] == 10) { system("clear"); printf("game over !!!!!!!!!\n"); exit(1); } } void print_map()//打印地圖,顯示信息 { int i,j; for(i=5; i<21; i++) { for(j=0; j<14; j++) { if(map[i][j]==200)//左右邊界 printf("#"); else if(map[i][j]==201)//下邊界 printf(" # "); else if(map[i][j]==0)//空白地 printf(" "); else if(map[i][j]==1)//移動(dòng)的俄羅斯方塊 printf(" * "); else if(map[i][j]==10)//固定的俄羅斯方塊 printf(" @ "); } printf("\n"); } } void delLine(int n)//消行 { int i,j; for(j = 1; j<13; j++) map[n][j] = 0; for(i = n; i>5 ; i--) for(j = 1; j<13; j++) if(map[i-1][j] != 1) map[i][j] = map[i-1][j]; } void isFillLine()//是否滿足消行條件 { int i,j; int fals; for(i=19; i>5; i--) { fals = 1; for(j=1; j<13; j++) { if(map[i][j] != 10) { fals = 0; continue; } } if(fals) { delLine(i); } } } void down()//下移 { int i, j; int fale = 1; for(i=3; i>=0; i--) for(j=0; j<4; j++) if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) if(map[bl.x+i+1][bl.y+j] == 10 || map[bl.x+i+1][bl.y+j] == 201) { change(); fale = 0; new_block(); isFillLine(); return; } if(fale) { output(); bl.x += 1; input(); } } void right()//右移 { int i, j; int fale = 1; for(i=3; i>=0; i--) for(j=0; j<4; j++) if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) if(map[bl.x+i][bl.y+j+1] == 10 || map[bl.x+i][bl.y+j+1] == 200) { fale = 0; return; } if(fale) { output(); bl.y += 1; input(); } } void left()//左移 { int i, j; int fale = 1; for(i=3; i>=0; i--) for(j=0; j<4; j++) if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) if(map[bl.x+i][bl.y+j-1] == 10 || map[bl.x+i][bl.y+j-1] == 200) { fale = 0; return; } if(fale) { output(); bl.y -= 1; input(); } } void change_block()//俄羅斯方塊變形 { int i,j; output(); int fals = 1; bl.blockDirect += 1; bl.blockDirect %= 4; for(i=0; i<4; i++) for(j=0; j<4; j++) if(node[bl.blockType][bl.blockDirect][i*4+j]==1) if(map[bl.x+i][bl.y+j] != 0 ) { fals = 0; break; } if(fals) { input(); }else { bl.blockDirect -= 1; input(); } } char in_direct()//非堵塞輸入 { fd_set fd; struct timeval tv; char ch; FD_ZERO(&fd); FD_SET(0, &fd); tv.tv_sec = 0; tv.tv_usec = 10; if(select(1, &fd ,NULL, NULL, &tv) > 0) { ch = getchar(); } return ch; } int main()//q 退出游戲,a,d 左右移動(dòng),空格變形 { srand(time(NULL)); init_map(); new_block(); input(); char ch; int num = 0; while(1) { usleep(500000); system(STTY_ON TTY_PATH); ch = in_direct(); system(STTY_OFF TTY_PATH); system("clear"); if(ch == 'a' && num <= 1) { left(); print_map(); num++; continue; }else if(ch == 'd' && num <= 1) { right(); print_map(); num++; continue; }else if(ch == ' ' && num <= 1 ) { change_block(); print_map(); num++; continue; }else if(ch == 'q') { system("clear"); printf("gave over!!!!!\n"); exit(0); } down(); print_map(); num = 0; } return 0; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)建站的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.rwnh.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、建站服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁名稱:使用C語言如何實(shí)現(xiàn)一個(gè)俄羅斯方塊小游戲-創(chuàng)新互聯(lián)
文章源于:http://www.rwnh.cn/article0/copeoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站設(shè)計(jì)、做網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容