呵呵(今天翻别人电脑翻出来我以前开始学c写的俄罗斯方块)
呵呵,自己玩了一下。<BR>还可以玩。<BR>由于我的朋友的是上网本,处理速度有点慢,我用sleep() 可能在不同的机子上速度不一样。。<BR>呵呵。。。<BR><BR>看这自己以前的代码,蛮好玩的啊。。。。。<BR><BR>这个貌似是学习2个月c写的。。。。<BR>记得不太清楚了。。<BR>TC 环境 还要drv 要用到DRV 然后改我的inigraph 路径。。。<BR><BR>怀旧一下,一年多就过去了啊。。。<BR>程序代码:
/* Note:Your choice is C IDE */ #include <stdio.h> #include <stdlib.h> #include <bios.h> #include <graphics.h> #include <conio.h> #define right 19712 #define leaf 19200 #define esc 283 #define up 18432 #define down 20480 int curbox[5][5]; int newx,newy; int key; int score=0; int str[10]; int n=0; int next2; int gamespeed=50000; int box[7][5][5] = { { {0,0,0,0,0}, {0,0,0,0,0}, {1,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,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,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,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,0,1,0,0}, {0,0,1,0,0}, {0,0,0,0,0} }, { {0,0,0,0,0}, {0,0,1,1,0}, {0,0,1,0,0}, {0,0,1,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} } }; int curx=6,cury=0; int map[25][18]; int rotatebox() {int x,y; int box1[5][5]; for(x = 0; x < 5; x++) /*这个函数可以须要死记*/ for(y = 4; y >= 0; y--) /*编写一下才能印像深刻*/ box1[y][x]=curbox[x][4-y]; for(y=0;y<5;y++) for(x=0;x<5;x++) if(box1[y][x]==1&&map[y+cury][x+curx]==1) return 0; for(y=0;y<5;y++) for(x=0;x<5;x++) curbox[y][x]=box1[y][x];} int drop() {newy=cury+1; if(test(curx,newy)==1) {cury=newy; return 1;} else return 0;} int initmap() {int x,y; for(y=0;y<25;y++){ for(x=0;x<18;x++){ if((x<2&&x>=0)||(y>22&&y<25)||(x>15&&x<18)) map[y][x]=1; else map[y][x]=0; if(map[y][x]==1&&x>1&&x<16&&y<23) {setcolor(RED); setlinestyle(SOLID_LINE,0,THICK_WIDTH); rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100);} } } for(y=0;y<25;y++) for(x=0;x<18;x++) if(map[y][x]==1&&((x>=0&&x<2)||(y>22&&y<25)||(x>15&&x<18))) {setcolor(GREEN); setlinestyle(SOLID_LINE,0,THICK_WIDTH); rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100); } } int test(int mx,int my) {int x,y; for(y=0;y<5;y++) for(x=0;x<5;x++) if(map[y+my][x+mx]==1&&curbox[y][x]==1) return 0; return 1; } int newfall() { int next; int x; int y; curx=6; cury=0; setfillstyle(SOLID_FILL,BLUE); bar(55,20,200,50); sprintf(str,"score:%d",score); settextstyle(0,0,2); outtextxy(60,25,str); nextbox(); while(drop()) buidbox(); return test(curx,cury); } int buidbox() {int x,y; if(!kbhit()) gamespeed=50000; while(kbhit()) {key=bioskey(0); switch(key){ case right:move(1);break; case leaf:move(0);break; case esc:closegraph();exit(0); case up:rotatebox();break; case down:gamespeed=8000;break; }} for(y=0;y<5;y++){ for(x=0;x<5;x++) {if(curbox[y][x]==1) {setcolor(GREEN); setlinestyle(SOLID_LINE,0,THICK_WIDTH); rectangle((x+curx)*12.5+190,(cury+y)*12.5+90,(x+curx)*12.5+200,(cury+y)*12.5+100);}}} delay(gamespeed); setcolor(BLACK); for(y=0;y<5;y++){ for(x=0;x<5;x++){ if(curbox[y][x]==1){ rectangle((x+curx)*12.5+190,(cury+y)*12.5+90,(x+curx)*12.5+200,(cury+y)*12.5+100); } } } } int rebuidmap() {int x,y; cleardevice(); putbox(); clear(); setfillstyle(SOLID_FILL,BLUE); bar(55,20,200,30); sprintf(str,"score:%d",score); settextstyle(0,0,2); outtextxy(55,20,str); for(y=0;y<25;y++) for(x=0;x<18;x++) if(map[y][x]==1&&x>1&&x<16&&y<23) {setcolor(RED); setlinestyle(SOLID_LINE,0,THICK_WIDTH); rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100); } for(y=0;y<25;y++) for(x=0;x<18;x++) if(map[y][x]==1&&((x>=0&&x<2)||(y>22&&y<25)||(x>15&&x<18))) {setcolor(GREEN); setlinestyle(SOLID_LINE,0,THICK_WIDTH); rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100); } } int putbox() { /*将curbox填充到地图上*/ int x, y; for(y = 0; y < 5; y++) /*这个也简单,主要是要根*/ for(x = 0; x < 5; x++) /*据curx,cury指出位置 */ if(curbox[y][x]) map[y + cury][x + curx] = curbox[y][x]; } int clear() { /*清除掉满行*/ /*这个函数实际上效率也很低的,为了简便 它从头到尾作了测试*/ /*具体的算法为: 从第0行开始到最后一行,测试地图点阵是否为满,如果是的话 从当前行算起,之上的地图向下掉一行*/ int x, y; int dx, dy; int fullflag; for(y = 0; y < 25 - 2; y++) { /*最后两行保留行*/ fullflag = 1; /*假设为满*/ for(x = 2; x < 18 - 2; x++) { /*保留列~*/ if(!map[y][x]) { fullflag = 0; break; } } if(fullflag) { /*向下移动一行*/ for(dy = y; dy > 0; dy--) for(dx = 2; dx < 18 - 2; dx++) map[dy][dx] = map[dy - 1][dx]; for(dx = 2; dx < 18 - 2; dx++) map[0][dx] = 0; score+=10; /*并清除掉第一行*/ } } } int nextbox() {int next1,x,y; randomize(); next1=rand()%7; for(y=0;y<5;y++) for(x=0;x<5;x++) if(n==0) curbox[y][x]=box[next1][y][x]; if(n==1) {for(y=0;y<5;y++) for(x=0;x<5;x++) curbox[y][x]=box[next2][y][x];} next2=rand()%7; n=1; for(y=0;y<5;y++) for(x=0;x<5;x++) if(box[next2][y][x]) rectangle(x*12.5+500,y*12.5+50,x*12.5+510,y*12.5+60); } int move(int dir) {int x,y,newx; if(dir) {newx=curx+1; if(test(newx,cury)) curx=newx;} else newx=curx-1; if(test(newx,cury)) curx=newx; } int main() {int x,y,gm=DETECT,gr,h; initgraph(&gm,&gr,"C:\\JMSOFT\\DRV"); //"C:\\JMSOFT\\DRV" 改成在你电脑上的地址 initmap(); newfall(); while(1){ if(!drop()) {rebuidmap(); for(x=3;x<16;x++) if(map[2][x]) {setcolor(RED); settextstyle(0,0,2); cleardevice(); outtextxy(260,240,"GAME OVER"); sleep(2); closegraph(); exit(0); } newfall(); } while(kbhit()) {key=bioskey(0); switch(key){ case right:move(1);break; case leaf:move(0);break; case esc:closegraph();exit(0); case up:rotatebox();break; }} }}