俄罗斯方块C语言源代码.pdf
《俄罗斯方块C语言源代码.pdf》由会员分享,可在线阅读,更多相关《俄罗斯方块C语言源代码.pdf(95页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、俄罗斯方块C语言源代码俄罗斯方块C语言源代码.txt逆风的方向,更适合飞翔。我不怕万人阻挡,只怕自己投降。你发怒一分钟,便失去6 0分钟的幸福。忙碌是一种幸福,让我们没时间体会痛苦;奔波是一种快乐,让我们真实地感受生活;疲惫是一种享受,让我们无暇空虚。生活就像”呼吸呼是为出一 口气,吸是为争一 口气。#include#include#include ttinclude ttinclude#ifdef _ cplusplusS define C P P A R G S .#elsedefine _C P P A R G S#endifS define M I N B O X S I Z E 1
2、5 /*最小方块的尺寸*/ttdefine B G C O L O R 7 /*背景着色*/ttdefine G X 2 0 0#define G Y 1 0#define S J N U M 1 0 0 0 0 /*每当玩家打到一万分等级加一级*/*按键码*/#define V K _L E F T 0 x4 b0 0tfdefine V K _R I G H T 0 x4 d0 0#define V K _D 0 W N 0 x5 0 0 0#define V K U P 0 x4 8 0 0#define V K _H O M E 0 x4 7 0 0#define V K _E N D
3、 0 x4 f0 0S define V K S P A C E 0 x3 9 2 0S define V K _E S C 0 x0 1 1 bttdefine V K _E N TE R O xlcO d/*定义俄罗斯方块的方向(我定义他为4种)*/define F _D O N G 0#define F _N A N 1ttdefine F _X I 2ttdefine F _B E I 3ttdefine N E X TC O L 2 0 /*要出的下一个方块的纵坐标*/S define N E X TR O W 1 2 /*要出的下一个方块的横从标*/ttdefine M A X R
4、 O W 1 4 /*游戏屏幕大小*/tfdefine M A X C O L 2 0ttdefine S C C O L 1 0 0 /*游戏屏幕大显示器上的相对位置*/#define S C R O W 6 0 int gril 2 2 1 6;/*游戏屏幕坐标*/int col=l,row=7;/*当前方块的横纵坐标*/int boxfx=0,boxgs=0;/*当前寺块的形壮和方向*/int nextboxfx=0,nextboxgs=0,maxcolW Z;/*下一个方块的形壮和方向*/intminboxcolor,nextminboxcolor;int num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0,0,0,0;/*随机得到当前方块和下一个方块的形状和方向*/void boxrad()minboxcolor=nextminboxcolor;boxgs=nextboxgs;boxfx=nextboxfx;nextminboxcolor=random(1 4)+1;if(nextminboxcolor-4 1|nextminboxcolor-7|n
9、extminboxcolor-8)nextminboxcolor=9;next boxfx=F _D 0 N G;nextboxgs=random(7);/*初始化图形模试*/void init(int gdrive,int gmode)int errorcode;initgraph(&gdrive,&gmode,D:tc );errorcode=graphresult();i f(errorcode!=grO k)printf(error of:%s,z,grapherrormsg(errorcode);exit(1);)/*在图形模式下的清屏*/void cis()(setfillstyl
10、e(S O L I D F I L L,0);setcolor(0);bar(0,0,6 4 0,4 8 0);)/*在图形模式下的高级清屏*/void clscr(int a,int b,int c,int d,int color)setfillstyle(S O L I D F I L L,color);setcolor(color);bar(a,b,c,d);/*最小方块的绘制*/void minbox(int asc,int bsc,int color,int bdcolor)int a=0,b=0;a=S C C O L+asc;b二S C R O W+bsc;clscr(a+l,b
11、+1,a-l+M I N B O X S I Z E,b-l+M I N B O X S I Z E,color);if(color!=B G C O L O R)setcolor(bdcolor);line(a+l,b+1,a-l+M I N B O X S I Z E,b+1);line(a+1,b+1,a+1,b-l+M I N B O X S I Z E);line(a-l+M I N B O X S I Z E,b+1,a-l+M lN B O X S I Z E,b-l+M I N B O X S I Z E);line(a+1,b-1+M I N B O X S I Z E,a-
12、l+M I N B O X S I Z E,b-l+M I N B O X S I Z E);)/*游戏中出现的文字*/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(int a,int b,int c,int d,int bgcolor,intbordercolor)clscr(a,b,c,d,bgcolor);setcolor(bordercolor);line(a,b,c,b);l
13、ine(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 boxz 4 4;for(i=0;i 1 6;i+)boxz i/4 i%4=boxstr boxgs boxfx i;for(i=0;i 4;i+)for(j=0;j 4;j+)if(boxz i j=l)minbox(j+row+a)*M I N B O X S I Z E,(i+col+b)*M I N B O X S I Z E,color,bdcolor);/*下一个
14、方块的绘制*/void nextfunbox(int a,int b,int color,int bdcolor)int i,j;int boxz 4 4;for(i=0;i 1 6;i+)boxz i/4 i%4=boxstr nextboxgs nextboxfx i;for(i=0;i 4;i+)for(j=0;jl;i-)f or(j=l;j15;j+)m in b ox(j*MINBOX SIZE,i*MINBOX SIZE,BG C OL OR,BG C OL OR);gr il i j=gr il i-1 j;if (gr il i j =Dm in b ox(j*M!NBOX
15、SIZE,i*M!NBOX SIZE,m in b ox c ol or,0);)/*消掉所有都有最小方块的行*/v oid d e l e t e ()in t i,j,ze r o,d e l gx=0;c ha r *n m=00000;f or(i=l;i21;i+)ze r o=0;f or(j=l;j15;j+)if(gr il i j=O)ze r o=l;if(ze r o=0)d e l c ol (i);d e l gx+;)n u m=n u m+d e l gx*d e l gx*10;d j=n u m/10000;s p r in t f (n m,刎,n u m)
16、;c l s c r(456,173,500,200,4);t x t (456,173,“Nu m b e r:1,15);t x t (456,193,n m,1,15);)/*时间中断结束*/v oid Kil l T im e r()d is a b l e O;s e t v e c t(T IMER,ol d ha n d l e r);e n a b l e ();/*测试当前方块是否可以向下落*/in t d ow n ok()in t i,j,k=l,a 4 4;f or (i=0;i16;i+)a i/4 i%4=b ox s t r b ox gs b ox f x i;
17、f or(i=0;i4;i+)f or(j=0;j4;j+)if(a i j&gr il Ec ol+i+1 r ow+j)k=0;r e t u r n (k);)/*测试当前方块是否可以向左行*/in t l e f t ok()in t i,j,k=l,a 4 4;f or(i=0;i16;i+)a i/4 i%4=b ox s t r b ox gs b ox f x i;f or(i=0;i4;i+)f or(j=0;j4;j+)if(a i j&gr il c ol+i r ow+j-1)k=0;r e t u r n (k);)/*测试当前方块是否可以向右行*/in t r ig
18、ht ok()in t i,j,k=l,a 4 4;f or (i=0;i16;i+)a i/4 i%4=b ox s t r b ox gs b ox f x i;f or(i=0;i4;i+)f or(j=0;j4;j+)if(a i j&gr il c ol+i r ow+j+l )k=0;r e t u r n (k);/*测试当前方块是否可以变形*/in t u p ok ()in t i,j,k=l,a 4 4;f or(i=0;i4;i+)f or (i=0;i=0;i-)f or(j=3;j=0;j)if(a i j&gr il c ol+i r ow+j)k=0;r e t
19、u r n (k);/*当前方块落下之后,给屏幕坐标作标记*/v oid s e t gr il()in t i,j,a4 4;f u n b ox(0,0,m in b ox c ol or,0);f or(i=0;i16;i+)a i/4 i%4=b ox s t r b ox gs b ox f x i;f or(i=0;i4;i+)f or (j=0;j0;i-)f or (j=l;j3)b ox f x=0;f u n b ox(0,0,m in b ox c ol or,0);b r e a k;c a s e V K L EF T:/*左方向键,纵坐标减一*/if (l e f
20、t ok ()r ow ;f u n b ox(0,0,m in b ox c ol or,0);b r e a k;)c a s e V K_ R1G HT:/*右方向键,纵坐标加一*/if (r ight ok O)r ow+;f u n b ox(0,0,m in b ox c ol or,0);b r e a k;)c a s e V K SPAC E:/*空格键,直接落到最后可以落到的们置*/w hil e(d ow n ok O)c ol+;f u n b ox(0,0,m in b ox c ol or,0);s e t gr il ();n e x t f u n b ox(N
21、EX T C OL,NEX T ROW,4,4);b ox r a d ();n e x t f u n b ox(NEX T C OL,NEX T ROW,n e x t m in b ox c ol or,0);d e l e t e ();b r e a k;d e f a u l t:t x t(423,53,“w or n g k e y!”,1,4);t x t(428,80,z,Pl e s e En t e r An l y Ke y AG!”,1,4);ge t c hO;c l s c r(420,50,622,97,BG C OL OR);/*时间中断开始*/v oid
22、t im e zd(v oid)in t k e y;Se t T im e r(n e w ha n d l e r);b ox r a d ();n e x t f u n b ox(NEX T C OL,NEX T ROW,n e x t m in b ox c ol or,0);f or (;)if(b ios k e y(l)k e y=b ios k e y(0);f u n b ox(0,0,BG C OL OR,BG C OL OR);if(k e y=V K_ ESC)b r e a k;c a l l _ k e y(k e y);)if(T im e r C ou n t
23、 e r ga m e d jd j)T im e r C ou n t e r=0;if (d ow n ok O)f u n b ox(0,0,BG C OL OR,BG C OL OR);c ol+;f u n b ox(0,0,m in b ox c ol or,0);)e l s e if(c ol=l)ga m e ov e r ();ge t c hO;b r e a k;s e t gr il ();d e l e t e ();f u n b ox(0,0,m in b ox c ol or,0);c ol=l;r ow=7;f u n b ox(0,0,BG C OL OR
24、,BG C OL OR);n e x t f u n b ox(NEX T C OL,NEX T ROW,4,4);b ox r a d ();n e x t f u n b ox(NEX T C OL,NEX T ROW,n e x t m in b ox c ol or,0);)/*主程序开始*/v oid m a in(v oid)in t i,j;c ha r *n m=OOOOO”;in it(V G A,V G AHI);c l s();/*屏幕坐标初始化*/f or(i=0;i=MAX C 0L+l;i+)f or(j=0;j=MAX ROW+l;j+)gr il i j=O;f
25、 or(i=0;i=MAX C 0L+l;i+)gr il i 0=l;gr il i 15=1;f or(j=l;j=MAX ROW;j+)gr il 0 j=l;gr il 21 j=l;)c l s c r(0,0,640,480,15);w in d,1,639,479,4,15);w in(SC C 0L+MINB0X SIZE-2,SC ROW+MINBOX SIZE-2,SC C 0L+15*MINB0X SIZE+2,SC R0W+21*MINB0X SIZE+2,BG C OL OR,0);n e x t b ox gs=r a n d om(8);n e x t b ox
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 俄罗斯方块 语言 源代码
限制150内