您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關VC++ 6.0 C語言如何實現俄羅斯方塊的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
今天把我之前寫的大作業分享一下吧,并教你們如何實現,希望你們看了前面的教程也能自己寫一個。
1.要先下載一個 graphics.h 的頭文件來繪圖。
2.初始化窗口:initgraph(x, y);這是先創建一個窗口的函數,以左上角為(0,0),向右為x軸,向下為y軸,其中x表示長x個單位,y表示寬y個單位。
3.關閉圖像窗口:closegraph();結束時用來關閉用的。
4.按任意鍵繼續:getch();這個就和getchar();差不多,為了防止以運行完就關了,這樣能停頓一下,他的頭文件是:conio.h 。
5.畫線:line(x1, y1, x2, y2);在你創建的那么窗口里以(x1,y1)和(x2,y2)為兩個端點畫線。
6.畫矩形:rectangle(x1,y1,x2,y2);以(x1,y1)和(x2,y2)為對角畫一個矩形。
7.畫圓:circle(x,y,r);以(x,y)為圓點,r為半徑畫圓。
8.顏色:setcolor(x);用來設置顏色的,其中x是你要設置的顏色,可以填這16種:黑 BLACK、藍 BLUE、綠 GREEN、青 CYAN、紅 RED、紫 MAGENTA、棕 BROWN、淺灰 LIGHTGRAY、深灰 DARKGRAY、亮藍 LIGHTBLUE、亮綠 LIGHTGREEN、亮青 LIGHTCYAN、亮紅 LIGHTRED、亮紫 LIGHTMAGENTA、黃 YELLOW、白 WHITE;當然,你也可以根據光的三原色來調自己喜歡的顏色,方法是:setcolor(RGB(x,y,z));其中RGB分別代表紅綠藍,對應的x,y,z是你選的該顏色的多少,范圍是[0,255]。
根據上面的這些就能畫出很多圖形了.比如:
# include <graphics.h> # include <conio.h> void main() { initgraph(640,480); setcolor(BROWN); line(200,200,300,130); line(400,200,300,130); rectangle(200,200,400,380); setcolor(RGB(100,150,200)); circle(255,260,20); circle(345,260,20); rectangle(280,300,310,360); getch(); closegraph(); }
還有這個,畫一個圓,圓心是(320,240),半徑r=200,根據角度的公式畫一個圓:
# include <graphics.h> # include <conio.h> # include <math.h> # define PI 3.1415926 void main() { initgraph(640, 480); int x,y,r=200,c; double a; for(a=0; a<PI*2; a+=0.0001) { x=(int)(r*cos(a)+320+0.5); y=(int)(r*sin(a)+240+0.5); c=(int)(a*255/(2*PI)); setcolor(RGB(c,200-c/2,100+c/2)); line(320,240,x,y); } getch(); closegraph(); }
9.停頓:Sleep(x);停頓x/1000秒。
有了Sleep();函數,就能讓線動起來了,原理就是:先畫一條線,然后再畫一條黑色的線覆蓋原來的那條,然后再畫一條線,這樣不斷的畫線能行了。
# include <graphics.h> # include <conio.h> void main() { int i,y; initgraph(640,480); for(y=0;y<480-2;y++) { setcolor(RGB(y,125,y/2%256)); line(0,y,639,y); line(0,y+2,639,y+2); Sleep(10); setcolor(BLACK); line(0,y,639,y); } getch(); closegraph(); }
10.隨機數發生器:srand();使用方法:srand(time(NULL));使用時要有頭文件time.h。
11.隨機函數:rand();隨機生成一個數,頭文件是:stdlib.h,比如:
# include <stdio.h> # include <time.h> # include <graphics.h> void main() { int t=10; while(t--) { srand(time(NULL)); printf("%d\n",rand()); Sleep(1000); } }
12.判斷鍵盤是否有輸入:kbhit();如果有的話返回1,否則返回0.
13.方向鍵:方健健的ASCII值我們不知道,普通的getchar();也不能輸入,但是getch();通過運行程序可以發現方向比較特殊,分別是:上 224+72、下 224+80、左 224+75、右 224+77,就是說他們是由兩個字符組成的,所以判斷上下左右時就先判斷if(kbhit()),然后判斷if(getch()==224),如果是的話在判斷if(getch()==72),是的話就是上,下左右同理。
# include <stdio.h> # include <graphics.h> # include <conio.h> void main() { int key; while(1) { if(kbhit()) { key=getch(); if(key==224) key=getch(); printf("%d",key); } else printf("*"); Sleep(500); } }
14.輸出:outtextxy(x,y,s);在坐標為(x,y)的地方輸出字符串s。
15.數字轉字符串:sprintf(s,"%d",a);將數字a,轉化為字符串s,頭文件是stdio.h。
16.設置字體:setfont(x,y,"字體");其中x表示你你要輸出的字的大小,y會改變形狀一般都是設為0,"字體"為你想要輸出的字體,比如:setfont(40,0,"楷體");
# include <graphics.h> # include <conio.h> # include <stdio.h> void main() { char s[10]; initgraph(640,480); setfont(42,0,"隸體"); outtextxy(200,200,"俄羅斯方塊"); sprintf(s,"%d",2015); outtextxy(250,250,s); getch(); closegraph(); }
上面這些都學會了的話,差不多就能夠自己完成編寫一個俄羅斯方塊了,下面分享一下我寫的代碼。
# include <stdio.h> # include <time.h> # include <conio.h> # include <graphics.h> int a,visit[30][15],Color[30][15],minX=30,minY=30,maxX=310,maxY=610,speed=500,score=0,rank=0; int color[]={GREEN,CYAN,MAGENTA,BROWN,LIGHTGRAY,LIGHTBLUE,LIGHTGREEN,LIGHTRED,YELLOW}; int block[36][5][5]= { {0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,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,0,0}, {0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,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0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0} }; void Interface();//界面的設計 void nextblock();//生成下一個方塊 void newblock();//將下一個放到主方框中 void move(int x,int y,int A);//移動方塊 int couldmove(int x0,int y0,int A,char dir,int next);//判斷能否移動 int couldrotate(int x0,int y0,int A,int next);//判斷能否旋轉 void mark(int x0,int y0,int A,int next);//標記方塊固定后的位置 void full();//判斷有沒有能夠消行的 void down(int x);//有消行的將上面的方塊下移一行 int gameover();//判斷游戲是否結束 int main() { int i,j; Interface(); nextblock(); Sleep(500); memset(visit,0,sizeof(visit)); while(1) { newblock(); full(); if(gameover()) break; } getch(); closegraph(); return 0; } void Interface() { int i,j; char str[10]; initgraph(550, 660); HWND hwnd = GetHWnd(); SetWindowText(hwnd, "NEW俄羅斯方塊 制作:丶阿明"); Sleep(2000); setfont(40,0,"隸體"); setcolor(RED); outtextxy(145,200,"NEW"); setfont(40,0,"楷體"); for(i=0;i<=255;i++) for(j=0;j<=255;j++) { setcolor(RGB((i+j+128)%255,i,j)); outtextxy(205,200,"俄羅斯方塊!"); } Sleep(2000); setcolor(YELLOW); setfont(22,0,"隸體"); outtextxy(310,300,"←_←"); setfont(22,0,"楷體"); outtextxy(180,300,"這逼裝的可以"); setcolor(LIGHTMAGENTA); Sleep(2500); outtextxy(175,300,"請按任意鍵開始游戲!"); getch(); cleardevice(); setcolor(WHITE); rectangle(29,29,334,633); rectangle(28,28,335,634); rectangle(370,50,515,195); setfont(24,0, "楷體"); setcolor(LIGHTGRAY); outtextxy(405,215,"下一個:"); setcolor(RED); outtextxy(405,280,"分數:"); sprintf(str,"%d",score); outtextxy(415,310,str); outtextxy(405,375,"等級:"); sprintf(str,"%d",rank); outtextxy(425,405,str); setfont(22,0, "楷體"); setcolor(LIGHTBLUE); outtextxy(390,475,"操作說明:"); outtextxy(390,500,"↑: 旋轉"); outtextxy(390,525,"↓: 下降"); outtextxy(390,550,"←: 左移"); outtextxy(390,575,"→: 右移"); outtextxy(390,600,"空格: 暫停"); } void nextblock() { int i,j,x=391,y=71; setfont(23,0,"楷體"); setcolor(BLACK); for(i=0;i<5;i++) for(j=0;j<5;j++) outtextxy(x+20*j,y+20*i,"■"); srand(time(NULL)); a=rand()%9; setcolor(color[a]); for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*a][i][j]==1) outtextxy(x+20*j,y+20*i,"■"); } void newblock() { int i,j,x=130,y=30,A=a; if(A==0||A==1||A==6) y-=60; else if(A==2||A==3||A==5||A==7||A==8) y-=40; else y-=80; setfont(23,0,"楷體"); setcolor(color[A]); for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A][i][j]==1&&y+20*i>=30) outtextxy(x+20*j,y+20*i,"■"); Sleep(100); nextblock(); move(x,y,A); } void move(int x,int y,int A) { int i,j,f,k=0,next=0,Speed=speed,key,nex; while(1) { f=couldmove(x,y+k,A,'x',next); if(f==0) { mark(x,y+k,A,next); break; } if(kbhit()) { key=getch(); if(key==32) getch(); } setcolor(BLACK); for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A+next][i][j]==1&&y+20*i+k>=30) outtextxy(x+20*j,y+20*i+k,"■"); if(kbhit()) { key=getch(); if(key==72) { nex=(next+1)%4; if(couldrotate(x,y+k,A,nex)) next=nex; } else if(key==80) Speed=50; else if(key==75&&couldmove(x,y+k+20,A,'z',next)) x-=20; else if(key==77&&couldmove(x,y+k+20,A,'y',next)) x+=20; } setcolor(color[A]); for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A+next][i][j]==1&&y+20*i+k+20>=30) outtextxy(x+20*j,y+20*i+k+20,"■"); Sleep(Speed); k+=20; } } int couldmove(int x0,int y0,int A,char dir,int next) { int i,j,x,y,f=1; x=(y0-minY)/20; y=(x0-minX)/20; if(dir=='x') { for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A+next][i][j]==1&&(x+i+1==30||(x+i+1>=0&&x+i+1<30&&y+j>=0&&y+j<15&&visit[x+i+1][y+j]==1))) f=0; } else if(dir=='z') { for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A+next][i][j]==1&&(y+j==0||(x+i>=0&&x+i<30&&y+j-1>=0&&y+j-1<15&&visit[x+i][y+j-1]==1))) f=0; } else if(dir=='y') { for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A+next][i][j]==1&&(y+j+1==15||(x+i>=0&&x+i<30&&y+j+1>=0&&y+j+1<15&&visit[x+i][y+j+1]==1))) f=0; } return f; } int couldrotate(int x0,int y0,int A,int next) { int i,j,x,y,f=1; x=(y0-minY)/20; y=(x0-minX)/20; if(!couldmove(x0,y0,A,'x',next)) f=0; for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A+next][i][j]==1&&(y+j==-1||y+j==15||(x+i>=0&&x+i<30&&y+j>=0&&y+j<15&&visit[x+i][y+j]==1))) f=0; return f; } void mark(int x0,int y0,int A,int next) { int i,j,x,y; x=(y0-minY)/20; y=(x0-minX)/20; for(i=0;i<5;i++) for(j=0;j<5;j++) if(block[4*A+next][i][j]==1) { visit[x+i][y+j]=1; Color[x+i][y+j]=color[A]; } } void full() { int i,j,k=0; char str[10]; for(i=29;i>=0;i--) { for(j=0;j<15;j++) if(visit[i][j]==0) break; if(j==15) { k++; down(i); i++; } } setcolor(RED); score+=10*k; sprintf(str,"%d",score); outtextxy(415,310,str); rank=(score/50); sprintf(str,"%d",rank); outtextxy(425,405,str); speed=500-100*rank; if(speed==0) speed=100; if(score>=300) speed=75; } void down(int x) { int i,j; for(i=x;i>0;i--) for(j=0;j<15;j++) if(visit[i-1][j]==1) { visit[i][j]=visit[i-1][j]; Color[i][j]=Color[i-1][j]; setcolor(Color[i-1][j]); outtextxy(minX+20*j,minY+20*i,"■"); } else { visit[i][j]=visit[i-1][j]; setcolor(BLACK); outtextxy(minX+20*j,minY+20*i,"■"); } setcolor(BLACK); for(j=0;j<15;j++) { visit[0][j]=0; outtextxy(minX+20*j,minY,"■"); } } int gameover() { int i,j; for(i=0;i<15;i++) if(visit[0][i]==1) { for(i=0;i<=7;i++) { setcolor(RGB(35*i,255-35*i,255)); for(j=i;j<30-i;j++,Sleep(20)) outtextxy(30+20*i,30+20*j,"■"); for(j=i;j<15-i;j++,Sleep(20)) outtextxy(30+20*j,30+20*(29-i),"■"); for(j=30-1-i;j>=i;j--,Sleep(20)) outtextxy(30+20*(14-i),30+20*j,"■"); for(j=15-1-i;j>=i;j--,Sleep(20)) outtextxy(30+20*j,30+20*i,"■"); } setcolor(WHITE); setfont(45,0,"隸體"); outtextxy(75,300,"GAME OVER!"); Sleep(5000); return 1; } return 0; }
我設置的一行能放15個小格子,一共能放30行,二維數組visit[30][15],來判斷某個位置是否有方塊,比如visit[29][5]=1表示第30行第6個方塊的位置有方塊了,0表示沒方塊,Color[30][15]用來記錄某個位置的方塊的顏色。minX=30,minY=30表示左上角的方塊也就是第1行第1列的方塊對應在界面的坐標,maxX,maxY同理。speed表示方塊的下落速度,一開始初始化為500,沒0.5s往下落一行,score記錄分數,每消一行加10分,rank表示等級,每50分等級+1,且速度減100,當速度減到100時,就不變了,只有當分數超過300速度才會減為75,且以后都不會再減少了。
我一個設計了9種方塊,每種方塊都有4種形態,每順時針旋轉90°就有一種形態,所以一共是36個方塊。每個方塊都用5*5的二維數組來表示,36種就是36個二維數組,所以用block[36][5][5]來表示,block[k][5][5],(k<36&&k%4==0)表示每種方塊的初始形態,k+1,+2,+3,分別表示第k種方塊選擇90°、180°、270°之后的形態。比如block[1][5][5]保存的是第1種方塊旋轉90°的方塊,其中block[1][i][j]=1表示這個方塊第i+1行,j+1列有一個小方塊,=0代表沒有,所以我的block[36][5][5],有36行,一行25個數列,表示一種方塊。一維數組color[]依次表示9種方塊的顏色。
附上效果圖:
感謝各位的閱讀!關于“VC++ 6.0 C語言如何實現俄羅斯方塊”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。