當前位置:首頁 » 編程語言 » c語言寫的俄羅斯方塊

c語言寫的俄羅斯方塊

發布時間: 2025-08-14 15:13:01

c語言寫俄羅斯方塊代碼

一、我們可以用編號,不同的編號代表不同的俄羅斯方塊,根據編號把不同方塊的畫法寫在代碼中,這樣19種。方塊就得有19種相應的代碼來描繪。而且這樣擴展性不好,若以後設計了新的方塊,則需要更改大量源代碼。

二、我們很自然的想到可用字模點陣的形式來表示,即設置一個4行4列的數組,元素置1即代表這個位置有小方塊,元素置0即代表這個位置無小方塊,這個整個的4*4的數組組成俄羅斯方塊的形狀。

我們把俄羅斯方塊點陣的數位存在rockArray中,我們可以事先把這19種方塊的字模點陣自己轉化成十六進制,然後在rockArray數組的初始化時賦值進去。但這樣做未免有點太費力,且擴展性也不太好,若以後設計的新方塊種類加入,要改變數組rockArray中的值。

我們可以考慮把所有俄羅斯方塊的點陣存儲在配置文件中,在程序初始化時讀取文件,把這些點陣轉換成unsigned int的變數存儲在rockArray中。

⑵ 一個C語言寫的俄羅斯方塊的不懂之處請指教

我仔細看了一下。

1.首先b的取值已經給出,請看數組g_Blocks的定義部份(前四個元素,後面一個是顏色)。

2.和0x8000做與運算是為了測試最高位是否為1.

3.b左移一位的意義在與,b這個值以二進制來看,是否還有某位為1.

為什麼要以二進制來看呢,是否還是不太明白為什麼要這要做?簡單說一下,你把每個俄羅斯圖形都看成是一個4x4的矩陣,你可以在紙上畫個4x4的方塊圖,從左邊第一列,從上往下開始,依次開始編碼,如果某個塊對應俄羅斯圖為實心塊,我們編碼時為1,否則為0.

見下圖:

g_Blocks裡面定義的四個值,即為該圖旋轉成四個方向後的編碼值。

⑶ C語言中的俄羅斯方塊

我給你,我有源代碼,可以運行
/*************************
<Russia Diamonds Ver 1.0>
Copyright by forever_chang
[email protected]
2001.11.1
*************************/
/*****************************************************************************************/
#include "graphics.h" /*頭文件*/
#include "time.h"
#include "stdlib.h"
#include "bios.h"
#include "dos.h"
#include "stdio.h"
#define ESC 0x11b /*鍵盤掃描碼*/
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define F1 0x3b00
#define RIGHT 0x4d00
#define YES 0x1579
#define NO 0x316e
#define RESTART 0x1372
/*****************************************************************************************/
struct diamond /*記錄每種方塊每種狀態的信息*/
{
int x[4];
int y[4];
int start_x;
int start_y;
int color;
struct diamond *next;
};
int grid[12][23]; /*記錄所有格子的狀態 (0)沒有方塊 (1)有固定方塊 (2)有活動方塊*/
int x; /*活動方塊所在位置*/
int y;
int level; /*游戲難度*/
int count; /*計數器*/
int score;/*得分*/
struct diamond *nowinfo; /*當前活動方塊*/
struct diamond *nextinfo; /*下一個方塊*/
int color;/*畫格子的顏色*/
int backcolor;/*游戲區域背景色*/
/*****************************************************************************************/
void show_interface (int mode);/*顯示游戲界面*/
void show_copsign (int topx,int topy,int size,int color);/*顯示公司標志--恆基偉業*/
void show_intro (int xs,int ys);/*顯示軟體介紹區*/
/*void print(); 測試用函數*/
void scandel();/*掃描所有格子看是否有可消除行*/
void show_down ();/*方塊下落後的下一個狀態*/
void show_next ();/*方塊翻轉後的下一個狀態*/
void show_left ();/*方塊向左移動後的下一個狀態*/
void show_right ();/*方塊向右移動後的下一個狀態*/
void interrupt (*oldtimer)();/*指向未安裝前的中斷向量,即函數指針,指向一段可執行的代碼*/
void install();/*安裝新的中斷向量,即將中斷服務程序安裝到中斷向量表中*/
void interrupt newtimer();/*中斷服務程序*/
struct diamond *get_diamond();/*隨機得到一個方塊*/
struct diamond *create_I();/*函數用來構造各種類形方塊的環形鏈表*/
struct diamond *create_T();/*返回鏈表中隨機一個狀態的指針*/
struct diamond *create_L();
struct diamond *create_J();
struct diamond *create_Z();
struct diamond *create_N();
struct diamond *create_H();
void delinfo (struct diamond *info);/*釋放當前方塊所佔用的空間*/
void addtobuffer(int c);/*向鍵盤緩沖區中增加一個DOWN*/
/*void clrkey();調用dos中斷清空鍵盤緩沖區,未使用此方法.*/
void showsubwin(struct diamond *next);/*在小窗口顯示下一個方塊*/
void showscore(int scoreget);/*顯示分數*/
void startset();/*初始化游戲*/
/*****************************************************************************************/
main ()
{
int driver=VGA;
int mode=VGAHI;
int i;/*計數器*/
int j;
int key;/*記錄鍵盤掃描碼*/
initgraph (&driver,&mode,"");/*初始化*/
color=8;
backcolor=7;
level=1;
score=298;
count=0;
show_interface (9);/*顯示界面*/
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Press any key to start...");
getch();
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
showscore(0);/*顯示分數*/
randomize();
nowinfo=get_diamond ();/*得到一個當前方塊*/
oldtimer=getvect(0x1c);
install(newtimer);
for(i=0;i<=21;i++)/*初始化grid[12][23]*/
{
for(j=1;j<=10;j++)
grid[j][i]=0;
}
for(i=0;i<=22;i++)
{
grid[0][i]=1;
grid[11][i]=1;
}
for(i=0;i<=11;i++)
{
grid[i][22]=1;
}

x=nowinfo->start_x;/*初始化方塊位置*/
y=nowinfo->start_y;
nextinfo=get_diamond ();/*得到下一個方塊*/
showsubwin(nextinfo);

for (;
{
key=bioskey(0);/*得到鍵盤掃描碼*/
if (key)
{
switch(key)
{

case DOWN:{
show_down ();
break;
}
case UP:{
show_next ();
break;
}
case LEFT:{
show_left();
break;
}
case RIGHT:{
show_right();
break;
}
case RESTART:{
install(oldtimer);
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Are you sure to restart (Y/N)...");
for (;
{
key=bioskey(0);/*得到鍵盤掃描碼*/
if (key==YES)
{
startset();
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
break;
}
if (key==NO)
{
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
install(newtimer);
break;
}
}
break;
}
/* case F1:{
print();
break;
}
*/
case ESC:{
install(oldtimer);
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Are you sure to exit (Y/N)...");
for (;
{
key=bioskey(0);/*得到鍵盤掃描碼*/
if (key==YES)
{
closegraph();
exit(0);
}
if (key==NO)
{
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
install(newtimer);
break;
}
}

}
}
}
}
}
/*****************************************************************************************/
struct diamond *get_diamond()
{
struct diamond *now;
switch (random(7)+1)
{
case 1:{
now=create_I();
return now;
}
case 2:{
now=create_T();
return now;
}
case 3:{
now=create_L();
return now;
}
case 4:{
now=create_J();
return now;
}
case 5:{
now=create_Z();
return now;
}
case 6:{
now=create_N();
return now;
}
case 7:{
now=create_H();
return now;
}
}
}
/*****************************************************************************************/
void show_interface (int fill_mode)
{
int i;
setbkcolor (9);
setcolor (color);
setfillstyle (SOLID_FILL,backcolor);
bar (100,60,300,420);
bar (360,60,440,140);
rectangle (100,60,300,420);
rectangle (96,56,304,424);

rectangle (360,60,440,140);
rectangle (356,56,444,144);
setfillstyle (fill_mode,14);
floodfill (97,57,color);
floodfill (397,57,color);
setcolor(1);
rectangle(96,56,304,424);
setcolor (color);
for (i=80;i<=400;i+=20)
{
line (100,i,300,i);
}
for (i=120;i<=280;i+=20)
{
line (i,60,i,420);
}
for (i=80;i<=120;i+=20)
{
line (360,i,440,i);
}
for (i=380;i<=420;i+=20)
{
line (i,60,i,140);
}
show_intro (360,180);
show_copsign (475,320,40,1);
setfillstyle(SOLID_FILL,1);
setcolor(14);
rectangle(420,405,534,417);
floodfill (421,406,14);
outtextxy(422,408,"HI-TECH WEALTH");
}
/*****************************************************************************************/
void show_copsign (int topx,int topy,int size,int color)
{
int halfsize,qtrsize;
int xadd,xdel,yadd1,yadd2;
halfsize=0.5*size;
qtrsize=0.25*size;

xadd=topx+size;
xdel=topx-size;
yadd1=topy+size;
yadd2=topy+2*size;

setcolor(color);
line (topx,topy,xdel,yadd1);
line (xdel,yadd1,topx,yadd2);
line (topx,yadd2,xadd,yadd1);
line (xadd,yadd1,topx,topy);
rectangle (topx-halfsize,topy+halfsize,topx+halfsize,yadd1+halfsize);
setfillstyle (SOLID_FILL,color);
floodfill (topx,topy+1,color);
floodfill (xdel+1,yadd1,color);
floodfill (topx,yadd2-1,color);
floodfill (xadd-1,yadd1,color);
rectangle (topx-halfsize,yadd1-qtrsize,topx-0.75*halfsize,yadd1+qtrsize);
floodfill (topx-halfsize+1,yadd1-qtrsize+1,color);
rectangle (topx-qtrsize,yadd1-halfsize,topx+qtrsize,yadd1-0.25*halfsize);
floodfill (topx-qtrsize+1,yadd1-halfsize+1,color);
rectangle (topx+0.75*halfsize,yadd1-qtrsize,topx+halfsize,yadd1+qtrsize);
floodfill (topx+0.75*halfsize+1,yadd1-qtrsize+1,color);
rectangle (topx-qtrsize,yadd1+0.25*halfsize,topx+qtrsize,yadd2-halfsize);
floodfill (topx-qtrsize+1,yadd1+0.25*halfsize+1,color);
setcolor(14);
line (topx,topy-1,xdel-1,yadd1);
line (xdel-1,yadd1,topx,yadd2+1);
line (topx,yadd2+1,xadd+1,yadd1);
line (xadd+1,yadd1,topx,topy-1);
setfillstyle (SOLID_FILL,14);
floodfill (topx,yadd1,color);
}
/*****************************************************************************************/
void show_intro (int xs,int ys)
{
char stemp[20];
setcolor (15);
rectangle(xs-3,ys-3,xs+239,ys+115);
line (xs-3,ys+26,xs+239,ys+26);
line (xs-3,ys+72,xs+239,ys+72);
outtextxy(xs,ys,"Level:");
outtextxy(xs,ys+15,"Score:");
sprintf(stemp," -Roll -Downwards");
stemp[0]=24;
stemp[7]=25;
outtextxy(xs,ys+30,"Help:");
setcolor(14);
outtextxy(xs+40,ys+30,stemp);
outtextxy(xs+40,ys+45,"<-Turn Left >-Turn Right");
outtextxy(xs+40,ys+60,"Esc-Exit R-Restart");
outtextxy(xs,ys+75,"Russia Diamonds Ver 1.0");
outtextxy(xs,ys+90,"CopyRight By ChangYong.01-11-1");
outtextxy(xs,ys+105,"Email:[email protected]");
}
/*****************************************************************************************/
struct diamond *create_I()
{
struct diamond *info;
struct diamond *first;
first=(struct diamond *)malloc(sizeof(struct diamond));
info=(struct diamond *)malloc(sizeof(struct diamond));
first->next=info;
info->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=-1;
first->y[1]=0;
first->x[2]=1;
first->y[2]=0;
first->x[3]=2;
first->y[3]=0;
first->start_x=5;
first->start_y=3;
first->color=2;

info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=-1;
info->x[2]=0;
info->y[2]=1;
info->x[3]=0;
info->y[3]=2;
info->start_x=5;
info->start_y=1;
info->color=2;

if(random(2)==0) {return first;}
else {return first->next;}
}
/*****************************************************************************************/
struct diamond *create_T()
{
struct diamond *info;
struct diamond *first;
struct diamond *last;
int i;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=0;
info->start_x=5;
info->start_y=3;
info->color=4;
first=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=0;
info->start_x=5;
info->start_y=2;
info->color=4;
last->next=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=0;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=4;
last->next=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=-1;
info->y[3]=0;
info->start_x=5;
info->start_y=2;
info->color=4;
last->next=info;
last=info;
last->next=first;

for (i=0;i<=random(4);i++)
{
first=first->next;
}
return first;
}
/*****************************************************************************************/
struct diamond *create_L()
{
struct diamond *info;
struct diamond *first;
struct diamond *last;
int i;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=5;
first=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=-1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=5;
last->next=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=-1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=5;
last->next=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=5;
last->next=info;
last=info;
last->next=first;

for (i=0;i<=random(4);i++)
{
first=first->next;
}
return first;
}
/*****************************************************************************************/
struct diamond *create_J()
{
struct diamond *info;
struct diamond *first;
struct diamond *last;
int i;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=-1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=6;
first=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=-1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=6;
last->next=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=6;
last->next=info;
last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=6;
last->next=info;
last=info;
last->next=first;

for (i=0;i<=random(4);i++)
{
first=first->next;
}
return first;
}
/*****************************************************************************************/
struct diamond *create_Z()
{
struct diamond *info;
struct diamond *first;
first=(struct diamond *)malloc(sizeof(struct diamond));
info=(struct diamond *)malloc(sizeof(struct diamond));
first->next=info;
info->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=-1;
first->y[1]=0;
first->x[2]=0;
first->y[2]=1;
first->x[3]=1;
first->y[3]=1;
first->start_x=5;
first->start_y=2;
first->color=9;

info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=1;
info->y[2]=0;
info->x[3]=1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=9;

if(random(2)==0) {return first;}
else {return first->next;}
}
/*****************************************************************************************/
struct diamond *create_N()
{
struct diamond *info;
struct diamond *first;
first=(struct diamond *)malloc(sizeof(struct diamond));
info=(struct diamond *)malloc(sizeof(struct diamond));
first->next=info;
info->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=0;
first->y[1]=1;
first->x[2]=-1;
first->y[2]=1;
first->x[3]=1;
first->y[3]=0;
first->start_x=5;
first->start_y=2;
first->color=14;

info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=-1;
info->x[2]=1;
info->y[2]=0;
info->x[3]=1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=14;

if(random(2)==0) {return first;}
else {return first->next;}
}
/*****************************************************************************************/
struct diamond *create_H()
{
struct diamond *first;
first=(struct diamond *)malloc(sizeof(struct diamond));
first->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=0;
first->y[1]=1;
first->x[2]=1;
first->y[2]=0;
first->x[3]=1;
first->y[3]=1;
first->start_x=5;
first->start_y=2;
first->color=1;
return first;
}
/*****************************************************************************************/
void show_next ()
{
int nowx;/*記錄當前每個格子的位置*/
int nowy;
int i;/*計數器*/
int j;
int haveit;/*當前格子是否已經顯示*/
struct diamond *next;/*當前方塊的翻轉後的下一個狀態*/
next=nowinfo->next;
if (next==NULL) {gotoxy(1,1);printf("null");}
for (i=0;i<=3;i++)/*判斷是否能夠翻轉,若不能,就直接退出該函數*/
{
if (grid[x+next->x[i]][y+next->y[i]]==1)
{
return;
}
}

setfillstyle(SOLID_FILL,backcolor);/*設置背景色以消除不需要的格子*/
for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (next->x[j]==nowinfo->x[i]&&next->y[j]==nowinfo->y[i]) {haveit=1;break;}
}
if (haveit==0) /*判斷翻轉後該格子是否顯示,若不顯示,將該格子設為背景色*/
{
grid[x+nowinfo->x[i]][y+nowinfo->y[i]]=0;
if (y+nowinfo->y[i]>=4)/*判斷該格子是否到了可以顯示的區域*/
floodfill(80+(nowinfo->x[i]+x)*20+1,-20+(nowinfo->y[i]+y)*20+1,color);
}
}

nowinfo=next;
nowx=x;
nowy=y;
setfillstyle(SOLID_FILL,nowinfo->color);/*設置填沖色為方塊的顏色*/
for (i=0;i<=3;i++)
{
if (grid[x+nowinfo->x[i]][y+nowinfo->y[i]]!=2)/*如果該格子還沒有被顯示*/
{
nowx=x+nowinfo->x[i];
nowy=y+nowinfo->y[i];
if (y+nowinfo->y[i]>=4)
floodfill(80+nowx*20+1,-20+nowy*20+1,color);
grid[nowx][nowy]=2; /*設置該格子當前有活動方塊*/
}
}
return;
}
/*****************************************************************************************/
void show_left ()
{
int i;/*計數器*/
int j;
int haveit;/*當前格子是否已經顯示*/
int nowx;/*記錄當前每個格子的位置*/
int nowy;
for (i=0;i<=3;i++)/*判斷是否可以向左移動*/
{
if (grid[x-1+nowinfo->x[i]][y+nowinfo->y[i]]==1)
{
return;
}
}

setfillstyle(SOLID_FILL,backcolor);/*設置背景色以消除不需要的格子*/
for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (nowinfo->x[i]==nowinfo->x[j]-1&&nowinfo->y[i]==nowinfo->y[j]) {haveit=1;break;}
}
if (haveit==0) /*判斷翻轉後該格子是否顯示,若不顯示,將該格子設為背景色*/
{
grid[x+nowinfo->x[i]][y+nowinfo->y[i]]=0;
if (y+nowinfo->y[i]>=4)/*判斷該格子是否到了可以顯示的區域*/
floodfill(80+(nowinfo->x[i]+x)*20+1,-20+(nowinfo->y[i]+y)*20+1,color);
}
}

setfillstyle(SOLID_FILL,nowinfo->color);/*設置填沖色為方塊的顏色*/
for (i=0;i<=3;i++)
{
nowx=x+nowinfo->x[i]-1;
nowy=y+nowinfo->y[i];
if (grid[nowx][nowy]!=2)/*如果該格子還沒有被顯示*/
{
if (nowy>=4)
floodfill(80+nowx*20+1,-20+nowy*20+1,color);
grid[nowx][nowy]=2;
}
}
x=x-1;
return;
}
/*****************************************************************************************/
void show_right ()
{
int i;/*計數器*/
int j;
int haveit;/*當前格子是否已經顯示*/
int nowx;/*記錄當前每個格子的位置*/
int nowy;
for (i=0;i<=3;i++)
{
if (grid[x+1+nowinfo->x[i]][y+nowinfo->y[i]]==1)
{
return;/*判斷是否可以向右移動*/
}
}

setfillstyle(SOLID_FILL,backcolor);/*設置背景色以消除不需要的格子*/
for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (nowinfo->x[i]==nowinfo->x[j]+1&&nowinfo->y[i]==nowinfo->y[j]) {haveit=1;break;}
}
if (haveit==0)/*判斷翻轉後該格子是否顯示,若不顯示,將該格子設為背景色*/
{
grid[x+nowinfo->x[i]][y+nowinfo->y[i]]=0;
if (y+nowinfo->y[i]>=4)/*判斷該格子是否到了可以顯示的區域*/
floodfill(80+(nowinfo->x[i]+x)*20+1,-20+(nowinfo->y[i]+y)*20+1,color);
}
}

setfillstyle(SOLID_FILL,nowinfo->color);/*設置填沖色為方塊的顏色*/
for (i=0;i<=3;i++)
{
nowx=x+nowinfo->x[i]+1;
nowy=y+nowinfo->y[i];
if (grid[nowx][nowy]!=2)
{
if (nowy>=4)/*判斷該格子是否到了可以顯示的區域*/
floodfill(80+nowx*20+1,-20+nowy*20+1,color);
grid[nowx][nowy]=2;
}
}
x=x+1;
return;
}
/*****************************************************************************************/
void show_down ()
{

int i;/*計數器*/
int j;
int haveit;/*當前格子是否已經顯示*/
int nowx;/*記錄當前每個格子的位置*/
int nowy;
int key;
for (i=0;i<=3;i++)
{
if (grid[x+nowinfo->x[i]][y+nowinfo->y[i]+1]==1)/*判斷方塊是否能夠下落*/
{
for (j=0;j<=3;j++)
{
grid[x+nowinfo->x[j]][y+nowinfo->y[j]]=1;
if (y+nowinfo->y[j]<=3)
{/*判斷游戲是否已經玩完*/
install(oldtimer);
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Do you want to restart (Y/N)...");
for (;
{
key=bioskey(0);
if (key==YES)
{
startset();
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
return;
}
if (key==NO)
{
closegraph();
exit (0);}
}
}
}

delinfo(nowinfo);
scandel();/*掃描,刪除*/
delay(2500);
while(bioskey(1)) bioskey(0);/*清除鍵盤緩沖區*/
/* clrkey();*/
nowinfo=nextinfo;/*得到新的方塊*/
nextinfo=get_diamond();/*得到下一個方塊*/
showsubwin(nextinfo);
x=nowinfo->start_x;/*重新設置方塊位置*/
y=nowinfo->start_y;
return;
}
}

setfillstyle(SOLID_FILL,backcolor);/*設置背景色以消除不需要的格子*/

for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (nowinfo->x[i]==nowinfo->x[j]&&nowinfo->y[i]==nowinfo->y[j]+1) {haveit=1;break;}
}
......
詳見:
http://topic.csdn.net/t/20030409/15/1638691.html
http://www.anyter.com/doc/doc.asp?doc_id=87

⑷ C語言代碼俄羅斯方塊(yCodeBlocks)

#include "mywindows.h"

HANDLE handle;

// 初始化句柄
void initHandle()
{
handle = GetStdHandle(STD_OUTPUT_HANDLE);
}

// 設置顏色
void setColor(int color)
{
SetConsoleTextAttribute(handle, color);
}

void setPos(int x, int y)
{
//, ,
COORD coord = {x*2, y};
SetConsoleCursorPosition(handle, coord);
}

// 設置游標是否可見
void setCursorVisible(int flag)
{
CONSOLE_CURSOR_INFO info;
info.bVisible = flag; //游標是否可見
info.dwSize = 100; //游標寬度1-100
SetConsoleCursorInfo(handle, &info);
}

// 關閉句柄
void closeHandle()
{
CloseHandle(handle);
}

⑸ 用C語言寫一個小游戲像貪吃蛇,俄羅斯方塊,除了C語言,還需要那些什麼知識

除了C語言,還要學習WINDOWS,也就是WIN32,WINDOWS系統有很多函數,其中包括畫圖等等,所以要想寫小游戲還得學WINDOWS的API。介紹本書給你,《WINDOWS程序設計》國外版。當然,如果你學VB也可以做,或者學別的也可以做,但既然你學了C,也還想做小游戲,那學WINDOWS就行了。相信樓主不太理解,平時我們下載東西的時候,有一些框框讓你選擇,你想知道這是什麼做的嗎?其實這不是學習別的語言,而是學習了WINDOWS,然後讓語言來調用WINDOWS系統的函數幫你做,記住,任何一種語言能都能調用WINDOWS系統的函數,C也可以。還有可以增加菜單,就是那文件,編輯。這些學習WINDOWS都能做,再學VB一些語言,說來說去,不還是WINDOWS的函數。只是它讓用戶方便而己。

熱點內容
我的世界網易伺服器老是崩潰怎麼辦 發布:2025-08-14 17:32:50 瀏覽:148
小米路由存儲分享 發布:2025-08-14 17:32:48 瀏覽:244
a3伺服器ip地址連接不了 發布:2025-08-14 17:19:58 瀏覽:808
mac查看系統文件夾 發布:2025-08-14 17:17:56 瀏覽:341
代碼段加密 發布:2025-08-14 17:06:30 瀏覽:959
我的世界嗨皮伺服器怎麼關服了 發布:2025-08-14 16:48:55 瀏覽:420
怎麼可以修改手機配置 發布:2025-08-14 16:44:09 瀏覽:311
php網頁地址 發布:2025-08-14 16:37:57 瀏覽:559
安卓手機有什麼資源 發布:2025-08-14 16:29:19 瀏覽:408
數列極限的四則運演算法則 發布:2025-08-14 16:28:23 瀏覽:966