俄罗斯方块代码.docx

上传人:小飞机 文档编号:3276665 上传时间:2023-03-12 格式:DOCX 页数:24 大小:40.56KB
返回 下载 相关 举报
俄罗斯方块代码.docx_第1页
第1页 / 共24页
俄罗斯方块代码.docx_第2页
第2页 / 共24页
俄罗斯方块代码.docx_第3页
第3页 / 共24页
俄罗斯方块代码.docx_第4页
第4页 / 共24页
俄罗斯方块代码.docx_第5页
第5页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《俄罗斯方块代码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块代码.docx(24页珍藏版)》请在三一办公上搜索。

1、俄罗斯方块代码俄罗斯方块,有你的名字:#include <stdio.h> #include <dos.h> #include <conio.h> #include <graphics.h> #include <stdlib.h> #include <time.h>#include <math.h>#ifdef _cplusplus #define _CPPARGS . #else #define _CPPARGS #endif #define MINBOXSIZE 23 /* 最小方块的尺寸 */ #define BGCOLOR 7 /

2、* 背景着色 */ #define GX 200 #define GY 10 #define SJNUM 1000 /* 每当玩家打到一千分等级加一级*/ /* 按键码*/ #define VK_LEFT 0x4b00 #define VK_RIGHT 0x4d00 #define VK_DOWN 0x5000 #define VK_UP 0x4800 #define VK_HOME 0x4700 #define VK_END 0x4f00 #define VK_SPACE 0x3920 #define VK_ESC 0x011b #define VK_ENTER 0x1c0d /* 定义俄罗

3、斯方块的方向*/ #define F_DONG 0 #define F_NAN 1 #define F_XI 2 #define F_BEI 3 #define NEXTCOL 20 /* 要出的下一个方块的纵坐标*/ #define NEXTROW 12 /* 要出的下一个方块的横从标*/ #define MAXROW 14 /* 游戏屏幕大小*/ #define MAXCOL 20 #define SCCOL -16 /*游戏屏幕大显示器上的相对位置*/ #define SCROW -16 int gril2216; /* 游戏屏幕坐标*/ int col=1,row=7; /* 当前方块

4、的横纵坐标*/ int boxfx=0,boxgs=0; /* 当前寺块的形壮和方向*/ int nextboxfx=0,nextboxgs=0,maxcol=22;/*下一个方块的形壮和方向*/ int minboxcolor=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0,0,1,0,0,0,0,0,0 ; /* 随机得到当前方块和下一个方块的形状和

8、方向*/ void boxrad minboxcolor=nextminboxcolor; boxgs=nextboxgs; boxfx=nextboxfx; srand(time(0);nextminboxcolor=rand%14+1; if(nextminboxcolor=1|nextminboxcolor=7|nextminboxcolor=8) nextminboxcolor=14; nextboxfx=F_DONG; srand(time(0);nextboxgs=rand%7; /*初始化图形模试*/ void init(int gdrive,int gmode) int err

9、orcode; initgraph(&gdrive,&gmode,.bgi); errorcode=graphresult; if(errorcode!=grOk) printf(error of: %s,grapherrormsg(errorcode); exit(1); void f24(int x,int y)static int flag=0;int i,j,m,k;int n;int a266=0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x30,0x00,0x00,0x30,0x00,0x00,0x30,0x00,0x00,0x30,0xF0,0x04,0

10、x30,0x78,0x0C,0x30,0x18,0x0C,0x31,0x00,0x0C,0x31,0x80,0x0C,0x33,0x80,0x0C,0x13,0x00,0x08,0x07,0x00,0x00,0x06,0x00,0x00,0x0C,0x00,0x00,0x18,0x00,0x00,0x38,0x00,0x00,0x60,0x00,0x01,0xC0,0x00,0x03,0x80,0x00,0x0E,0x00,0x00,0x38,0x00,0x00,0x01,0x80,0x00,0x03,0x80,0x00,0x07,0x06,0x00,0x2E,0x1F,0x00,0x38,0

11、x76,0x00,0x33,0x66,0x1E,0x13,0x7F,0xFE,0x13,0x76,0xC6,0x13,0x64,0xC6,0x13,0x7C,0x86,0x17,0x60,0xBC,0x3F,0x6C,0x9C,0x33,0x6E,0x8C,0x32,0x7E,0x80,0x06,0xF0,0x80,0x06,0x60,0x80,0x0C,0x00,0x80,0x0C,0x00,0x80,0x18,0x00,0x80,0x10,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80 ;flag=(+flag)%2;for(i=0;i<66;i+=3)

12、for(k=i;k<i+3;k+)for(j=0;j<8;j+)n=pow(2,7-j);m=aflagk/n;aflagk=aflagk%n;if(m&(j+(k-i)*8)!=24&(j+(k-i)*8)!=23&(j+(k-i)*8)!=0)putpixel(x+j+(k-i)*8,y+i/3,1);/* 在图形模式下的清屏 */ void cls setfillstyle(SOLID_FILL,0); setcolor(0); bar(0,0,640,480); /*在图形模式下的高级清屏*/ void clscr(int a,int b,int c,int d,int col

13、or) setfillstyle(SOLID_FILL,color); setcolor(color); bar(a,b,c,d); /*最小方块的绘制*/ void minbox(int asc,int bsc,int color,int bdcolor) int a=0,b=0; a=SCCOL+asc; b=SCROW+bsc; clscr(a+1,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE,color); if(color!=BGCOLOR) f24(a,b+1);setcolor(bdcolor); line(a+1,b+1,a-1+MINBOXSIZE,b+

14、1); line(a+1,b+1,a+1,b-1+MINBOXSIZE); line(a-1+MINBOXSIZE,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE); line(a+1,b-1+MINBOXSIZE,a-1+MINBOXSIZE,b-1+MINBOXSIZE); /*游戏中出现的文字*/ void txt(int a,int b,char *txt,int font,int color) setcolor(color); settextstyle(0,0,font); outtextxy(a,b,txt); /*windows 绘制*/ void win(i

15、nt a,int b,int c,int d,int bgcolor,int bordercolor) clscr(a,b,c,d,bgcolor); setcolor(bordercolor); line(a,b,c,b); line(a,b,a,d); line(a,d,c,d); line(c,b,c,d); /* 当前方块的绘制*/ void funbox(int a,int b,int color,int bdcolor) int i,j; int boxz44; for(i=0;i<16;i+) boxzi/4i%4=boxstrboxgsboxfxi; for(i=0;i&l

16、t4;i+) for(j=0;j<4;j+) if(boxzij=1) minbox(j+row+a)*MINBOXSIZE,(i+col+b)*MINBOXSIZE,color,bdcolor); /*下一个方块的绘制 */void nextfunbox(int a,int b,int color,int bdcolor) int i,j; int boxz44; for(i=0;i<16;i+) boxzi/4i%4=boxstrnextboxgsnextboxfxi; clscr(a-1)*MINBOXSIZE,(b-1)*MINBOXSIZE,(3+a)*MINBOXSIZE,

17、(4+b)*MINBOXSIZE,2);for(i=0;i<4;i+) for(j=0;j<4;j+) if(boxzij=1) minbox(j+a)*MINBOXSIZE,(i+b)*MINBOXSIZE,color,bdcolor); /*时间中断定义*/ #define TIMER 0x1c int TimerCounter=0; void interrupt ( *oldhandler)(_CPPARGS); void interrupt newhandler(_CPPARGS) TimerCounter+; oldhandler; void SetTimer(void in

18、terrupt (*IntProc)(_CPPARGS) oldhandler=getvect(TIMER); disable; setvect(TIMER,IntProc); enable; /*由于游戏的规则,消掉都有最小方块的一行*/ void delcol(int a) int i,j; for(i=a;i>1;i-) for(j=1;j<15;j+) minbox(j*MINBOXSIZE,i*MINBOXSIZE,BGCOLOR,BGCOLOR); grilij=grili-1j; if(grilij=1) minbox(j*MINBOXSIZE,i*MINBOXSIZE,

19、minboxcolor,0); /*消掉所有都有最小方块的行*/ void delete int i,j,zero,delgx=0; char *nm=00000; for(i=1;i<21;i+) zero=0; for(j=1;j<15;j+) if(grilij=0) zero=1; if(zero=0) delcol(i); delgx+; num=num+delgx*delgx*10; dj=num/10000; sprintf(nm,%d,num); clscr(456,173,500,200,2); txt(456,173,Score :,1,4);txt(456,193

20、,nm,1,4); /*时间中断结束*/ void KillTimer disable; setvect(TIMER,oldhandler); enable; /* 测试当前方块是否可以向下落*/ int downok int i,j,k=1,a44; for(i=0;i<16;i+) ai/4i%4=boxstrboxgsboxfxi; for(i=0;i<4;i+) for(j=0;j<4;j+) if(aij & grilcol+i+1row+j) k=0; return(k); /* 测试当前方块是否可以向左行*/ int leftok int i,j,k=1,a44; f

21、or(i=0;i<16;i+) ai/4i%4=boxstrboxgsboxfxi; for(i=0;i<4;i+) for(j=0;j<4;j+) if(aij & grilcol+irow+j-1) k=0; return(k); /* 测试当前方块是否可以向右行*/ int rightok int i,j,k=1,a44; for(i=0;i<16;i+) ai/4i%4=boxstrboxgsboxfxi; for(i=0;i<4;i+) for(j=0;j<4;j+) if(aij & grilcol+irow+j+1) k=0; return(k); /*

22、测试当前方块是否可以变形*/ int upok int i,j,k=1,a44; for(i=0;i<4;i+) for(i=0;i<16;i+) ai/4i%4=boxstrboxgsboxfx+1i; for(i=3;i>=0;i-) for(j=3;j>=0;j-) if(aij & grilcol+irow+j) k=0; return(k); /*当前方块落下之后,给屏幕坐标作标记*/ void setgril int i,j,a44; funbox(0,0,minboxcolor,0); for(i=0;i<16;i+) ai/4i%4=boxstrboxgsb

23、oxfxi; for(i=0;i<4;i+) for(j=0;j<4;j+) if(aij) grilcol+irow+j=1; col=1;row=7; /*游戏结束*/ void gameover int i,j; for(i=20;i>0;i-) for(j=1;j<15;j+) minbox(j*MINBOXSIZE,i*MINBOXSIZE,2,0); txt(56,206,Game Over,4,0);txt(53,203,Game Over,4,4); /*按键的设置*/ void call_key(int keyx) switch(keyx) case VK_D

24、OWN: /*下方向键,横坐标加一。*/ if(downok) col+; funbox(0,0,minboxcolor,0); else funbox(0,0,minboxcolor,0); setgril; nextfunbox(NEXTCOL,NEXTROW,4,4); boxrad; nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0); delete; break; case VK_UP: /*上方向键,方向形状旋转90度*/ if(upok) boxfx+; if(boxfx>3) boxfx=0; funbox(0,0,minboxcol

25、or,0); break; case VK_LEFT: /*左方向键,纵坐标减一*/ if(leftok) row-; funbox(0,0,minboxcolor,0); break; case VK_RIGHT: /*右方向键,纵坐标加一*/ if(rightok) row+; funbox(0,0,minboxcolor,0); break; case VK_SPACE: /*空格键,直接落到最后可以落到的们置*/ while(downok) col+; funbox(0,0,minboxcolor,0); setgril; nextfunbox(NEXTCOL,NEXTROW,4,4)

26、; boxrad; nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0); delete; break; default: txt(423,53,worng key!,1,4); txt(428,80,Plese Enter Anly Key AG!,1,4); getch;clscr(420,50,622,97,2); /*时间中断开始*/ void timezd(void) int key; SetTimer(newhandler); boxrad; nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0); for(

27、;) if(bioskey(1) key=bioskey(0); funbox(0,0,BGCOLOR,BGCOLOR); if(key=VK_ESC) break; call_key(key); if(TimerCounter>gamedjdj) TimerCounter=0; if(downok) funbox(0,0,BGCOLOR,BGCOLOR); col+; funbox(0,0,minboxcolor,0); else if(col=1) gameover; getch; break; setgril; delete; funbox(0,0,minboxcolor,0); c

28、ol=1;row=7; funbox(0,0,BGCOLOR,BGCOLOR); nextfunbox(NEXTCOL,NEXTROW,4,4); boxrad; nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0); /*主程序开始*/ void main(void) int i,j; char *nm=00000; init(VGA,VGAHI); cls; /*屏幕坐标初始化*/ for(i=0;i<=MAXCOL+1;i+) for(j=0;j<=MAXROW+1;j+) grilij=0; for(i=0;i<=MAXCOL+1;i+

29、) grili0=1; grili15=1; for(j=1;j<=MAXROW;j+) gril0j=1; gril21j=1; clscr(0,0,640,480,15); win(1,1,639,479,2,15); win(SCCOL+MINBOXSIZE-2,SCROW+MINBOXSIZE-2,SCCOL+15*MINBOXSIZE+2,SCROW+21*MINBOXSIZE+2,BGCOLOR,0); srand(time(0);nextboxgs=rand%7; nextboxfx=rand%4; sprintf(nm,%d,num); txt(456,173,Score :,1,4);txt(456,193,nm,1,4); txt(456,243,Next Box:,1,4); timezd;KillTimer; closegraph;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号