C.C++语言-俄罗斯方块源码

合集下载

俄罗斯方块 C语言程序

俄罗斯方块 C语言程序

#include<stdio.h>#include<graphics.h>#include<conio.h>#include<time.h>#include<dos.h>#include<stdlib.h>#include<math.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bintBox_x[5],Box_y[5],Box_xy[15][10]={0},num,life=1,score,mx,my,key,x[5],y[5],size,g rade=0;int *buf;void Init();void my_ad_1();void my_ad_2();void my_ad_3();void play();void jiang();void De_B_xy();void Draw_Box();void Ad_Box_y();void Mi_Box_x();void Ad_Box_x();void ch_B_xy();void rebuild();void Draw_New();void close();void delay_n(int n);void main(){Init();my_ad_1();my_ad_2();my_ad_3();play();close();}void Init(){int driver=DETECT,mode;initgraph(&driver,&mode,"C:\\Program Files\\CYuYan\\bin");mx=getmaxx();my=getmaxy();setbkcolor(BLUE);size=imagesize(0,0,32,32);if(size!=-1)buf=malloc(size);getimage(0,0,32,32,buf);}void my_ad_1(){intch1[]={100,120,100,300,120,300,120,200,200,200,200,280,180,280,180,300,220,30 0,220,180,120,180,120,140,300,140,300,100,280,120,100,120},ch2[]={240,100,280,300,300,300,260,100,240,100},ch3[]={280,160,264,280,284,280,300,160,280,160},j1[]={300,120,340,160,360,160,320,100,300,120},j2[]={300,180,340,220,360,220,320,160,300,180},j3[]={300,300,340,300,380,220,360,220,300,300},j4[]={380,120,380,140,420,140,420,280,360,280,360,300,500,300,500,280,440,280, 440,140,480,140,480,120,380,120};settextjustify(CENTER_TEXT,CENTER_TEXT);settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);setcolor(WHITE);sleep(2);outtextxy(320,200,"C Program Flash Creation");settextstyle(3,HORIZ_DIR,3);sleep(1);outtextxy(320,300,"Cheng Jiang,Electronic Class 1,Physic Department, GDUT");sleep(1);cleardevice();fillpoly(16,ch1);fillpoly(5,ch2);fillpoly(5,ch3);fillpoly(5,j1);fillpoly(5,j2);fillpoly(5,j3);fillpoly(12,j4);sleep(2);cleardevice();}void my_ad_2(){setfillstyle(SOLID_FILL,LIGHTBLUE);bar(0,0,150,my);bar(450,0,mx,my);setviewport(150,0,450,my,1);}void my_ad_3()int i,j;outtextxy(150,30,"Waitting...");setlinestyle(1,1,1);rectangle(0,60,450,90);setlinestyle(SOLID_LINE,1,THICK_WIDTH); jiang();for(i=0;i<10;i++){bar(30*i,60,30*(i+1),90);rectangle(30*i,60,30*(i+1),90);sleep(1);}clearviewport();outtextxy(150,240,"OK! Press [Enter]");getch();clearviewport();}void play(){int i,j;outtextxy(150,460,"grade:0 score: 0 ");while(1){randomize();num=random(19);De_B_xy();while(1){while(kbhit()){key=bioskey(0);Draw_Box();switch(key){case UP:getch();break;case DOWN:Ad_Box_y();break; case RIGHT:Ad_Box_x();break; case LEFT:Mi_Box_x();break; case ESC:close();break;default:ch_B_xy();break;}if(check()) break;}Draw_Box();if(check()) break;for(i=1;i<5;i++)Box_y[i]+=1;if(!kbhit())delay_n(10-grade);}rebuild();Draw_New();if(!life) break;}}void Draw_Box(){int i;for(i=1;i<5;i++)putimage(x[i]*30-1,30*y[i]-1,buf,COPY_PUT);if(!(y[1]&&y[2]&&y[3]&&y[0]))for(i=4;i<8;i++)putimage(30*i-1,0,buf,COPY_PUT);for(i=1;i<5;i++){x[i]=Box_x[i];y[i]=Box_y[i];bar(Box_x[i]*30, Box_y[i]*30, (Box_x[i]+1)*30, (Box_y[i]+1)*30);rectangle(Box_x[i]*30, Box_y[i]*30, (Box_x[i]+1)*30, (Box_y[i]+1)*30); }}void De_B_xy(){int i;switch(num){case 0: {Box_x[1]=Box_x[2]=Box_x[3]=Box_x[4]=0;Box_y[1]=0;Box_y[2]=1;Box_y[3]=2;Box_y[4]=3;} break;case 1: {Box_y[1]=Box_y[2]=Box_y[3]=Box_y[4]=0;Box_x[1]=0;Box_x[2]=1;Box_x[3]=2;Box_x[4]=3;} break;case 2: {Box_x[1]=0;Box_x[2]=1; Box_x[3]=0;Box_x[4]=1;Box_y[1]=0;Box_y[2]=0; Box_y[3]=1;Box_y[4]=1;} break;case 3: {Box_x[1]=0;Box_x[2]=0; Box_x[3]=0;Box_x[4]=1;Box_y[1]=0;Box_y[2]=1; Box_y[3]=2;Box_y[4]=1;} break;case 4: {Box_x[1]=0;Box_x[2]=1; Box_x[3]=1;Box_x[4]=2;Box_y[1]=2;Box_y[2]=1; Box_y[3]=2;Box_y[4]=2;} break;case 5: {Box_x[1]=0;Box_x[2]=1; Box_x[3]=1;Box_x[4]=1;Box_y[1]=1;Box_y[2]=0; Box_y[3]=1;Box_y[4]=2;} break;case 6: {Box_x[1]=0;Box_x[2]=1; Box_x[3]=1;Box_x[4]=2; Box_y[1]=1;Box_y[2]=1; Box_y[3]=2;Box_y[4]=1;} break; case 7: {Box_x[1]=0;Box_x[2]=0; Box_x[3]=1;Box_x[4]=1;Box_y[1]=0;Box_y[2]=1; Box_y[3]=1;Box_y[4]=2;} break; case 8: {Box_x[1]=0;Box_x[2]=1; Box_x[3]=1;Box_x[4]=2; Box_y[1]=1;Box_y[2]=0; Box_y[3]=1;Box_y[4]=0;} break; case 9: {Box_x[1]=0;Box_x[2]=0; Box_x[3]=1;Box_x[4]=1; Box_y[1]=1;Box_y[2]=2; Box_y[3]=0;Box_y[4]=1;} break; case 10:{Box_x[1]=0;Box_x[2]=1; Box_x[3]=1;Box_x[4]=2; Box_y[1]=1;Box_y[2]=1; Box_y[3]=2;Box_y[4]=2;} break; case 11:{Box_x[1]=0;Box_x[2]=0; Box_x[3]=0;Box_x[4]=1; Box_y[1]=0;Box_y[2]=1; Box_y[3]=2;Box_y[4]=2;} break; case 12:{Box_x[1]=0;Box_x[2]=1; Box_x[3]=2;Box_x[4]=2; Box_y[1]=1;Box_y[2]=1; Box_y[3]=0;Box_y[4]=1;} break; case 13:{Box_x[1]=0;Box_x[2]=1; Box_x[3]=1;Box_x[4]=1; Box_y[1]=0;Box_y[2]=0; Box_y[3]=1;Box_y[4]=2;} break; case 14:{Box_x[1]=0;Box_x[2]=0; Box_x[3]=1;Box_x[4]=2; Box_y[1]=0;Box_y[2]=1; Box_y[3]=0;Box_y[4]=0;} break; case 15:{Box_x[1]=0;Box_x[2]=1; Box_x[3]=1;Box_x[4]=1; Box_y[1]=2;Box_y[2]=0; Box_y[3]=1;Box_y[4]=2;} break; case 16:{Box_x[1]=0;Box_x[2]=1; Box_x[3]=2;Box_x[4]=2; Box_y[1]=0;Box_y[2]=0; Box_y[3]=0;Box_y[4]=1;} break; case 17:{Box_x[1]=0;Box_x[2]=0; Box_x[3]=0;Box_x[4]=1; Box_y[1]=0;Box_y[2]=1; Box_y[3]=2;Box_y[4]=0;} break; case 18:{Box_x[1]=0;Box_x[2]=0; Box_x[3]=1;Box_x[4]=2; Box_y[1]=0;Box_y[2]=1; Box_y[3]=1;Box_y[4]=1;} break; }for(i=1;i<5;i++)Box_x[i]+=4;}int check(){int i,p=0;for(i=1;i<5;i++)if(Box_xy[Box_y[i]+1][Box_x[i]]||Box_y[i]>13) p=1;return(p);}void Ad_Box_y(){int i,p=1;for(i=1;i<5;i++)if(Box_xy[Box_y[i]+1][Box_x[i]]||Box_y[i]>13) p=0;if(p)for(i=1;i<5;i++)Box_y[i]+=1;}void Ad_Box_x(){int i,p=1;for(i=1;i<5;i++)if(Box_xy[Box_y[i]][ Box_x[i]+1]||Box_x[i]>8) p=0;if(p)for(i=1;i<5;i++)Box_x[i]+=1;}void Mi_Box_x(){int i,p=1;for(i=1;i<5;i++)if(Box_xy[Box_y[i]][ Box_x[i]-1]||Box_x[i]<1) p=0;if(p)for(i=1;i<5;i++)Box_x[i]-=1;}void ch_B_xy(){int i,j,t,maxx=Box_x[1],maxy=Box_y[1];for(i=2;i<5;i++){if(maxx<Box_x[i])maxx=Box_x[i];if(maxy<Box_y[i])maxy=Box_y[i];}switch(num){case 0:num=num+1;break;case 1:num=num-1;case 2: ;break;case 3:num=num+1;break;case 4:num=num+1;break;case 5:num=num+1;break;case 6:num=num-3;break;case 7:num=num+1;break;case 8:num=num-1;break;case 9:num=num+1;break;case 10:num=num-1;break;case 11:num=num+1;break;case 12:num=num+1;break;case 13:num=num+1;break;case 14:num=num-3;break;case 15:num=num+1;break;case 16:num=num+1;break;case 17:num=num+1;break;case 18:num=num-3;break;}De_B_xy();for(i=1;i<5;i++){Box_x[i]=Box_x[i]+maxx-Box_x[4]; Box_y[i]=Box_y[i]+maxy-2;}}void rebuild(){int i,j,p,t[4]={0},k=0;for(i=1;i<5;i++)Box_xy[Box_y[i]][Box_x[i]]=1;for(i=0;i<15;i++){p=1;for(j=0;j<10;j++)if(!Box_xy[i][j])p=0;if(p)t[k]=i;k++;score+=10*k;}}for(k=0;k<4;k++){if(t[k]){for(i=t[k];i>0;i--)for(j=0;j<10;j++)Box_xy[i][j]=Box_xy[i-1][j]; }}for(i=0;i<10;i++)if(Box_xy[0][i])life=0;setfillstyle(1,4);for(i=0;i<4;i++)if(t[i])bar(0,30*t[i],300,30*(t[i]+1)); setfillstyle(1,9);for(i=1;i<5;i++){x[i]=0;y[i]=0;}grade=(score-score%100)/100 ; delay(30000);void Draw_New(){int i,j;char text[20];clearviewport();for(i=0;i<15;i++)for(j=0;j<10;j++)if(Box_xy[i][j]){bar(30*j,30*i,30*j+30,30*i+30);rectangle(30*j,30*i,30*j+30,30*i+30);}sprintf(text,"grade: %d score: %d",grade,score); gotoxy(100,460);outtextxy(150,460,text);}void close(){int i,j;for(i=14;i>=0;i--){for(j=0;j<10;j++){bar(30*j,30*i,30*(j+1),30*(i+1));rectangle(30*j,30*i,30*(j+1),30*(i+1));}delay(20000);}clearviewport();jiang();outtextxy(150,60,"game over");getch();closegraph();}void delay_n( int n){int i;for(i=0;i<n;i++) delay(50000);}void jiang(){int jx=0,jy=4;for(jx=0;jx<3;jx++){bar(30*jx,30*jy,30*(jx+1),30*(jy+1));rectangle(30*jx,30*jy,30*(jx+1),30*(jy+1));bar(30*jx,30*(jy+3),30*(jx+1),30*(jy+4));rectangle(30*jx,30*(jy+3),30*(jx+1),30*(jy+4)); jy++;}jy=14;for(jx=0;jx<4;jx++){bar(30*jx,30*jy,30*(jx+1),30*(jy+1));rectangle(30*jx,30*jy,30*(jx+1),30*(jy+1));jy--;}jy=4;for(jx=4;jx<9;jx++){bar(30*jx,30*jy,30*(jx+1),30*(jy+1));rectangle(30*jx,30*jy,30*(jx+1),30*(jy+1)); }jy=14;for(jx=3;jx<11;jx++){bar(30*jx,30*jy,30*(jx+1),30*(jy+1));rectangle(30*jx,30*jy,30*(jx+1),30*(jy+1)); }jx=6;for(jy=5;jy<15;jy++){bar(30*jx,30*jy,30*(jx+1),30*(jy+1));rectangle(30*jx,30*jy,30*(jx+1),30*(jy+1)); }}。

C语言实现俄罗斯方块(转)

C语言实现俄罗斯方块(转)

C语⾔实现俄罗斯⽅块(转)原⽂地址如下:在图书馆看到⼀本书有俄罗斯⽅块的源程序⾃⼰当年也是俄罗斯⽅块的爱好者便想分析分析这个⼩游戏的源代码这⼏天有空就看了看发现读源码对编程领悟很有帮助读完深深的感觉到程序的确是好的数据结构加上好的算法这段程序定义了两个数据结构分别是//游戏底板结构,表⽰每个⼩⽅块所具有的属性struct BOARD{//当前状态,只有0或1,1表⽰次⼩⽅块已被占⽤int var;//⼩⽅块的颜⾊int color;}Table_board[Vertical_boxs][Horizontal_boxs];//游戏跳出的⽅块结构struct SHAPE{//⼀个字节等于8位,每四位来表⽰⼀个游戏⽅块的⼀⾏//如box[0]="0x88",box[1]="oxc0"表⽰的是://1000//1000//1100//0000//以此来表⽰游戏⽅块的各种形状char box[2];//游戏⽅块的颜⾊int color;//下⼀个游戏⽅块的编号int next;//这个next设计的也是相当妙啊};该源码的算法更确切的说是游戏的逻辑读完也是很有体会把握好⼤的系统强⼤的逻辑能⼒是必要的平时⾃⼰还是要再数据结构和算法这些⽅⾯加强学习、锻炼这段程序还有就是中断不是很理解还需要时间领悟⼀下下⾯贴⼀下我的程序注释这个游戏的层次描述:1预览功能2控制功能俄罗斯⽅块 3显⽰更新功能4分数更新功能5游戏帮助功能下⾯是源码:(该程序⽤到了TC的图形库,所以VC下是运⾏不了的,不过VC做代码编辑阅读⽐较⽅便,所以代码注释采⽤了VC下的//)//加载头⽂件#include <stdio.h>#include <stdlib.h>#include <dos.h>#include <graphics.h>//加载tc图形库//定义按键码#define VK_LEFT 0x4b00#define VK_RIGHT 0x4d00#define VK_DOWN 0x5000#define VK_UP 0x4800#define VK_ESC 0x011b//设置中断号 1c:时钟中断时得到控制权,若要固定时间发⽣事件,可以通过修改1c中断来得到#define TIMER 0x1c//共有19种形态的游戏⽅块#define MAX_BOX 19//⽅块的边长为20#define BSIZE 20//显⽰游戏界⾯的左上⾓x坐标#define Sys_x 160//显⽰游戏界⾯的左上⾓y坐标#define Sys_y 25//⽔平⽅向的⽅块数量#define Horizontal_boxs 10//垂直⽅向的⽅块数量#define Vertical_boxs 15//第⼀个游戏⽅块的产⽣起始位置#define Begin_boxs_x Horizontal_boxs/2//前景⾊#define FgColor 3//背景⾊#define BgColor 0//右边状态栏的x坐标#define LeftWin_x Sys_x+Horizontal_boxs*BSIZE+46#define false 0#define true 1//移动的⽅向#define MoveLeft 1#define MoveRight 2#define MoveDown 3#define MoveRoll 4//保存当前游戏⽅块编号int current_box_numb;//保存游戏⽅块的当前坐标int Curbox_x=Sys_x+Begin_boxs_x*BSIZE,Curbox_y=Sys_y;//是否要产⽣新游戏⽅块int flag_newbox=false;//下落速度int speed=1;//游戏得分int score=0;//每等级所需要分数int speed_step=30;//指向原来时钟中断处理过程⼊⼝的中断处理函数指针关键字interrupt指定⼀个函数应该被看成⼀个中断函数void interrupt(*oldtimer)(void);//游戏底板结构,表⽰每个⼩⽅块所具有的属性struct BOARD{//当前状态,只有0或1,1表⽰次⼩⽅块已被占⽤int var;//⼩⽅块的颜⾊int color;}Table_board[Vertical_boxs][Horizontal_boxs];//游戏跳出的⽅块结构struct SHAPE{//⼀个字节等于8位,每四位来表⽰⼀个游戏⽅块的⼀⾏//如box[0]="0x88",box[1]="oxc0"表⽰的是://1000//1000//1100//0000//以此来表⽰游戏⽅块的各种形状char box[2];//游戏⽅块的颜⾊int color;//下⼀个游戏⽅块的编号int next;};//初始化游戏⽅块的内容,包括形状颜⾊和下⼀个游戏⽅块编号struct SHAPE shapes[MAX_BOX]={{0x88,0xc0,CYAN,1},{0xe8,0x0,CYAN,2},{0xc4,0x40,CYAN,3},{0x2e,0x0,CYAN,0},{0x44,0xc0,MAGENTA,5},{0x8e,0x0,MAGENTA,6},{0xc8,0x80,MAGENTA,7},{0xe2,0x0,MAGENTA,4},{0x8c,0x40,YELLOW,9},{0x6c,0x0,YELLOW,8},{0x4c,0x80,BROWN,11},{0xc6,0x0,BROWN,10},{0x4e,0x0,WHITE,13},{0x8c,0x80,WHITE,14},{0xe4,0x0,WHITE,15},{0x4c,0x40,WHITE,12},{0x88,0x88,RED,17},{0xf0,0x0,RED,16},{0xcc,0x0,BLUE,18},};//定时计数器变量unsigned int TimerCounter=0;//以下为函数声明void initialize(int,int,int,int);void interrupt newtimer(void);void SetTimer(void interrupt(*IntProc)(void));void KillTimer(void);void ShowScore(int);void ShowSpeed(int);void show_help(int,int);void setFullRow(int);int DelFullRow(int);void show_box(int,int,int,int);void EraseBox(int,int,int);void ErasePreBox(int,int,int);int MkNextBox(int);int MoveAble(int,int,int,int);//主函数void main(){int GameOver=0;int key,nextbox;int Currentaction=0;int gd=VGA,gm=VGAHI,errorcode;initgraph(&gd,&gm,"");errorcode=graphresult();if(errorcode!=grOk){printf("\nNotice:Graphics error: %s\n",grapherrormsg(errorcode));printf("Press any key to quit!");getch();exit(1);}setbkcolor(BgColor);setcolor(FgColor);randomize();SetTimer(newtimer);initialize(Sys_x,Sys_y,Horizontal_boxs,Vertical_boxs);//初始化nextbox=MkNextBox(-1);show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color); show_box(LeftWin_x,Curbox_y+320,nextbox,shapes[nextbox].color);show_help(Sys_x,Curbox_y+320);getch();while (1){Currentaction=0;flag_newbox=false;//int bioskey(int cmd)//完成直接键盘操作,cmd的值决定执⾏什么操作//cmd=0,返回下⼀个键盘键⼊的值//cmd=1,查询是否按下⼀个键,若按下⼀个键返回⾮零值,否则返回0if(bioskey(1)){key=bioskey(0);}else{key=0;}switch(key){case VK_LEFT:if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveLeft)){EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_x-=BSIZE;Currentaction=MoveLeft;}break;case VK_RIGHT:if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveRight)){EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_x+=BSIZE;Currentaction=MoveRight;}break;case VK_DOWN:if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown)){EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_y+=BSIZE;Currentaction=MoveDown;}elseflag_newbox=true;break;case VK_UP:if(MoveAble(Curbox_x,Curbox_y,shapes[current_box_numb].next,MoveRoll)){EraseBox(Curbox_x,Curbox_y,current_box_numb);current_box_numb=shapes[current_box_numb].next;Currentaction=MoveLeft;}break;case VK_ESC:GameOver=1;break;default:break;}if (Currentaction)//当前有动作就执⾏{show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color); Currentaction=0;}if(flag_newbox)//按了向下键,但不能下移就产⽣新的游戏⽅块{ErasePreBox(LeftWin_x,Sys_y+200,nextbox);nextbox=MkNextBox(nextbox);show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);if(!MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown)){show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color); GameOver=1;}else{flag_newbox=false;}Currentaction=0;}else//⾃由下落{if(Currentaction==MoveDown||TimerCounter>(20-speed*2)){if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown)){EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_y+=BSIZE;show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color); }TimerCounter=0;}}if(GameOver){printf("game over,thank you! your score is %d",score);getch();break;}}getch();KillTimer();closegraph();}//******************************************************************** //显⽰帮助,提⽰⽤户如何进⾏游戏的相关操作//void show_help(int xs,int ys){char stemp[50];setcolor(15);//void far rectangle(int x0,int y0,int x1,int y1)//以(x0,y0)为左上⾓,(x1,x2)为右下⾓画⼀个矩形框rectangle(xs,ys,xs+239,ys+100);//sprintf()//将字串格式化到字符串sprintf(stemp," -Roll -Downwards");stemp[0]=24;stemp[8]=25;setcolor(14);//void far outtextxy(int x,int y,char far *text)//将⽂本输出到指定位置outtextxy(xs+40,ys+30,stemp);sprintf(stemp," -Turn Left -Turn Right");stemp[0]=27;stemp[13]=26;outtextxy(xs+40,ys+45,stemp);outtextxy(xs+40,ys+60,"Esc-Exit");setcolor(FgColor);}//******************************************************************* //对游戏界⾯的初始化void initialize(int x,int y,int m,int n){int i,j,oldx;oldx=x;for(j=0;j<n;j++){for(i=0;i<m;i++){Table_board[j][i].var=0;Table_board[j][i].color=BgColor;line(x,y,x+BSIZE,y);line(x,y,x,y+BSIZE);line(x,y+BSIZE,x+BSIZE,y+BSIZE);line(x+BSIZE,y,x+BSIZE,y+BSIZE);x+=BSIZE;}y+=BSIZE;x=oldx;}Curbox_x=x;Curbox_y=y;flag_newbox=false;speed=1;score=0;ShowScore(score);ShowSpeed(speed);}//*************************************************************//显⽰当前⽤户的成绩void ShowScore(int score){int x,y;char score_str[5];setfillstyle(SOLID_FILL,BgColor);y=100;//确定⼀个以(x0,y0)为左上⾓,(x1,x2)为右下⾓的矩形窗⼝,再按规定图形和颜⾊填充bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);sprintf(score_str,"%3d",score);outtextxy(x,y,"SCORE");outtextxy(x,y+10,score_str);}//**********************************************************//显⽰当前下落速度void ShowSpeed(int speed){int x,y;char speed_str[5];setfillstyle(SOLID_FILL,BgColor);x=LeftWin_x;y=150;bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);sprintf(speed_str,"%3d",speed);outtextxy(x,y,"Level");outtextxy(x,y+10,speed_str);outtextxy(x,y+50,"Nextbox");}//**********************************************************//定义新的时钟中断处理函数void interrupt newtimer(void){//调⽤原来的例程(*oldtimer)();//全局计数器变量加1TimerCounter++;}//********************************************************//设置新的时钟中断处理void SetTimer(void interrupt (*IntProc)(void)){//获取中断号为TIMER的中断处理函数的⼊⼝地址oldtimer=getvect(TIMER);//设置新的时钟中断处理过程时,禁⽌所有中断disable();//将中断号为TIMER的中断处理函数⼊⼝地址改为IntProc()函数的⼊⼝地址setvect(TIMER,IntProc);//开启中断enable();}//*********************************************************//恢复原有的时钟中断处理过程void KillTimer(){disable();setvect(TIMER,oldtimer);enable();}//********************************************************//在(x,y)位置开始,⽤指定的颜⾊显⽰编号为box_num的游戏⽅块void show_box(int x,int y,int box_num,int color){int i,ii,ls_x=x;//指定的游戏⽅块不存在if(box_num<0||box_num>=MAX_BOX)box_num=MAX_BOX/2;//void far setfillstyle(int pattern,int color)//以pattern为填充模式以color为填充颜⾊对指定图形进⾏填充setfillstyle(SOLID_FILL,color);{int mask=128;//掩码,⽤于位运算//单个⽅块填充for(i=0;i<8;i++){if(i%4==0&&i!=0)//表⽰转到游戏⽅块的下⼀⾏了{y+=BSIZE;x=ls_x;}if((shapes[box_num].box[ii])&mask){bar(x,y,x+BSIZE,y+BSIZE);line(x,y,x+BSIZE,y);line(x,y,x,y+BSIZE);line(x,y+BSIZE,x+BSIZE,y+BSIZE);line(x+BSIZE,y,x+BSIZE,y+BSIZE);}x+=BSIZE;mask/=2;}y+=BSIZE;x=ls_x;}}//***************************************************************//清除(x,y)位置开始的编号为box_num的boxvoid EraseBox(int x,int y,int box_num){int mask=128,t_boardx,t_boardy,n,m;setfillstyle(SOLID_FILL,BgColor);for(n=0;n<4;n++){for (m=0;m<4;m++){if(((shapes[box_num].box[n/2])&mask)){bar(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);line(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE);line(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE,y+n*BSIZE+BSIZE);line(x+m*BSIZE,y+n*BSIZE+BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE); line(x+m*BSIZE+BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE); }mask/=2;if(mask==0)mask=128;}}}//*******************************************************//同EraseBox()void ErasePreBox(int x,int y,int box_numb){int mask=128,t_boardx,t_boardy,n,m;setfillstyle(SOLID_FILL,BgColor);for(n=0;n<4;n++){for(m=0;m<4;m++){if(((shapes[box_numb].box[n/2])&mask)){bar(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);}mask/=2;if(mask==0)mask=128;}}}//***************************************************************//将新图形的游戏⽅块放置在游戏板上,并返回此游戏⽅块号int MkNextBox(int box_numb){int mask=128,t_boardx,t_boardy,n,m;t_boardx=(Curbox_x-Sys_x)/BSIZE;t_boardy=(Curbox_y-Sys_y)/BSIZE;for(n=0;n<4;n++){for(m=0;m<4;m++){if(((shapes[current_box_numb].box[n/2])&mask)){//设置游戏⽅块Table_board[t_boardy+n][t_boardx+m].var=1;Table_board[t_boardy+n][t_boardx+m].color=shapes[current_box_numb].color;}mask=mask/2;if(mask==0)mask=128;}}setFullRow(t_boardy);//初始化坐标Curbox_x=Sys_x+Begin_boxs_x*BSIZE;Curbox_y=Sys_y;if(box_numb==-1)box_numb=rand()%MAX_BOX;//随机产⽣游戏⽅块current_box_numb=box_numb;flag_newbox=false;return (rand()%MAX_BOX);}//***********************************************************//⽤于找到满⾏,参数t_boardy表⽰当前的游戏⽅块号void setFullRow(int t_boardy){int n,full_numb=0,top=0;//top保存当前游戏主板在消除满⾏后的最⾼点,⽤于游戏主板的重绘register m;//寄存器类型,存取速度快for(n=t_boardy+3;n>=t_boardy;n--){if(n<0||n>=Vertical_boxs)continue;for(m=0;m<Horizontal_boxs;m++){if(!Table_board[n+full_numb][m].var)//发现有⼀个是空的就跳过break;}if(m==Horizontal_boxs)//找到满⾏{if(n==t_boardy+3)top=DelFullRow(n+full_numb);//清除该⾏,并保存最⾼点elseDelFullRow(n+full_numb);full_numb++;//保存满⾏的⾏数}}if(full_numb)//存在满⾏{int oldx,x=Sys_x,y=BSIZE*top+Sys_y;oldx=x;score=score+full_numb*10;for(n=top;n<t_boardy+4;n++){if(n>=Vertical_boxs)continue;//重绘游戏主板for(m=0;m<Horizontal_boxs;m++){if(Table_board[n][m].var){setfillstyle(SOLID_FILL,Table_board[n][m].color);}elsesetfillstyle(SOLID_FILL,BgColor);bar(x,y,x+BSIZE,y+BSIZE);line(x,y,x+BSIZE,y);line(x,y,x,y+BSIZE);line(x,y+BSIZE,x+BSIZE,y+BSIZE);line(x+BSIZE,y,x+BSIZE,y+BSIZE);x+=BSIZE;}y+=BSIZE;x=oldx;}ShowScore(score);if(speed!=score/speed_step){speed=score/speed_step;ShowSpeed(speed);}elseShowSpeed(speed);}}//************************************************************//处理删除⾏,参数y指明具体哪⼀⾏为满⾏int DelFullRow(int y){int n,top=0;register m,totoal;for (n=y;n>=0;n--){totoal=0;for(m=0;m<Horizontal_boxs;m++){if(!Table_board[n][m].var)totoal++;//没有⽅格,对计数器加1//上⾏不等于下⾏就把上⾏传给下⾏,此处为程序优化部分,也可不优化 if(Table_board[n][m].var!=Table_board[n-1][m].var){Table_board[n][m].var=Table_board[n-1][m].var;Table_board[n][m].color=Table_board[n-1][m].color;}}//发现上⾯有连续的空⾏,提前结束if (totoal==Horizontal_boxs){top=n;break;}}return top;//返回最⾼点}//********************************************************8//判断⽅块是否可以移动,(x,y)为当前游戏⽅块位置,box_numb为游戏⽅块号,direction为动作标识int MoveAble(int x,int y,int box_numb,int direction){int n,m,t_boardx,t_boardy;int mask;if(direction==MoveLeft)//如果向左移动{mask=128;x-=BSIZE;t_boardx=(x-Sys_x)/BSIZE;t_boardy=(y-Sys_y)/BSIZE;for(n=0;n<4;n++){for(m=0;m<4;m++){if((shapes[box_numb].box[n/2])&mask){if((x+BSIZE*m)<Sys_x)//碰到最左边return false;else if(Table_board[t_boardy+n][t_boardx+m].var)//左移⼀个单位后,与游戏主板冲突return false;}mask/=2;if(mask==0)mask=128;}}return true;}else if(direction==MoveRight)//右移动{x+=BSIZE;t_boardx=(x-Sys_x)/BSIZE;t_boardy=(y-Sys_y)/BSIZE;mask=128;for(n=0;n<4;n++){for(m=0;m<4;m++){if((shapes[box_numb].box[n/2])&mask){if((x+BSIZE*m)>=(Sys_x+BSIZE*Horizontal_boxs))//碰到最右边return false;else if(Table_board[t_boardy+n][t_boardx+m].var)//与游戏主板冲突return false;}mask/=2;if(mask==0)mask=128;}}return true;}else if(direction==MoveDown)//下移动{mask=128;y+=BSIZE;t_boardx=(x-Sys_x)/BSIZE;t_boardy=(y-Sys_y)/BSIZE;for(n=0;n<4;n++){for(m=0;m<4;m++){if((shapes[box_numb].box[n/2])&mask){if((y+BSIZE*n)>=(Sys_y+BSIZE*Vertical_boxs)||Table_board[t_boardy+n][t_boardx+m].var)//碰到最下边或向下有冲突 {flag_newbox=true;break;}}mask/=2;if(mask==0)mask=128;}}if(flag_newbox)return false;elsereturn true;}else if(direction==MoveRoll)//旋转{mask=128;t_boardx=(x-Sys_x)/BSIZE;t_boardy=(y-Sys_y)/BSIZE;for(n=0;n<4;n++){for(m=0;m<4;m++){if((shapes[box_numb].box[n/2])&mask){if((y+BSIZE*n)>=(Sys_y+BSIZE*Vertical_boxs))//碰到最下边return false;if((x+BSIZE*n)>=(Sys_x+BSIZE*Horizontal_boxs))//碰到最左边return false;if((x+BSIZE*m)>=(Sys_x+BSIZE*Horizontal_boxs))//碰到最右边return false;else if (Table_board[t_boardy+n][t_boardx+m].var)//向下有冲突{return false;}}mask/=2;if(mask==0)mask=128;}}return true;}elsereturn false;}。

俄罗斯方块 变形 c语言

俄罗斯方块 变形 c语言

俄罗斯方块变形c语言在C语言中实现俄罗斯方块游戏是一项复杂的任务,涉及到图形界面、游戏逻辑和事件处理等多个方面。

以下是一个简化版的俄罗斯方块游戏示例,使用字符在控制台中显示游戏界面。

c复制代码#include<stdio.h>#include<stdlib.h>#include<unistd.h>#define WIDTH 20#define HEIGHT 10typedef enum {BLOCK_EMPTY,BLOCK_I,BLOCK_J,BLOCK_L,BLOCK_O,BLOCK_S,BLOCK_Z} BlockType;typedef struct {BlockType type;int x;int y;} Block;Block board[WIDTH][HEIGHT];BlockType currentBlock = BLOCK_I;int rotation = 0;int x = WIDTH / 2;int y = HEIGHT - 1;void drawBoard() {for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { char ch = ' ';switch (board[i][j].type) {case BLOCK_EMPTY:ch = '.';break;case BLOCK_I:ch = 'I';break;case BLOCK_J:ch = 'J';break;case BLOCK_L:ch = 'L';break;case BLOCK_O:ch = 'O';break;case BLOCK_S:ch = 'S';break;case BLOCK_Z:ch = 'Z';break;}printf("%c", ch);}printf("\n");}}void updateBoard() {for (int i = 0; i < WIDTH; i++) {for (int j = 0; j < HEIGHT; j++) {if (board[i][j].type != BLOCK_EMPTY) {board[i][j].y--; // Move block down one row.} else { // Place new block.switch (currentBlock) { // Place based on current block type.case BLOCK_I: // Place full I-block.board[i][j].type = BLOCK_I; // Column.j y row.i X -- column.j+1 y row.i X -- column.j y row.i+1 X -- column.j+1 y row.i+1 X -- column.j y row.i X -- column.j+1 y row.i X -- column.j y row.i+1 X -- column.j+1 y row.i+1 X -- column.j y row.i X -- column.j+1 y row.i X -- column.j y row.i+1 X -- column.j+1 y row.i+1 X -- column.j y row.i X -- column.j+1 y row.i X -- column.j y row.i+1 X -- column.j+1 y row.i+1 X -- column.j y row.i X -- column.j+1 y row.i X -- column.j y row.i+1 X -- column.j+1 y row.i+1 X -- column.j y row.i X -- column.j+1 y row.i X -- column.j y row.i+1 X -- column.j+1 y row.i+1 X -- column.j y row.i X -- column.j+1 y row.i X -- column.j y row.i+1 X -- column j Y n Row Y j Columns n - j 1 -- i 1 i - i j Row i Row i - 1 i Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column Column n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i - - - - - - - - - - - - - - -。

C语言俄罗斯方块游戏源代码

C语言俄罗斯方块游戏源代码

/*学无止境*/ #include <stdlib.h>#include <stdio.h>#include <graphics.h>#define ESC 27#define UP 328#define DOWN 336#define LEFT 331#define RIGHT 333#define BLANK 32#define BOTTOM 2#define CANNOT 1#define CAN 0#define MAX 30#define F1 315#define ADD 43#define EQUAL 61#define DEC 45#define SOUNDs 115#define SOUNDS 83#define PAUSEP 80#define PAUSEp 112void Init();void Down();void GoOn();void ksdown();void Display(int color);void Give();int Touch(int x,int y,int dx,int dy);int GeyKey();void Select();void DetectFill();void GetScores();void Fail();void Help();void Quit();void DrawBox(int x,int y,int Color);void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch);void DrawNext(int Color);int Heng=12,Shu=20; /*横竖*/int Position[MAX][MAX];int middle[MAX][MAX];int ActH,ActS;int Act,Staus;int i,j,k;int Wid=10;int NoPass=CAN;float Delays=15000;int BeginH=250,BeginS=7;float Seconds=0;int Scores=0;int flag=1;int Sounds=CAN;int PreAct,NextAct;int a[8][4][4][4]={{{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0}, {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},{1,1,1,1,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},{1,1,0,0,1,1,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},{1,1,0,0,1,1,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,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0}}, {{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}, {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}, {1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}, {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}}, {{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}, {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}, {1,1,0,0,0,1,1,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}, {1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0}, {1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}}, {{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}, {1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0}, {1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0}}, {{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}; int b[4][4];main(int argc,char *argv[]){if (argc!=1){if (argv[1]!="")Heng=atoi(argv[1]);if (argv[2]!="")Shu=atoi(argv[2]);}Init(); /*初始化界面*/PreAct=random(8); /*取得当前的方块*/for(;;) /*以下是游戏流程*/{NextAct=random(8); /*取得下一个方块*/ DrawNext(1); /*画出下一个方块*/Act=PreAct;if (Heng%2==0) ActH=Heng/2;else ActH=(Heng-1)/2;ActS=0; /*方块开始从游戏空间的中间下落*/ Staus=0; /*取开始的状态*/NoPass=CAN; /*物体可以下落*/Give(); /*取得当前的方块*/Display(Act+1); /*显示当前的方块,每种方块的颜色不同*/ GoOn(); /*游戏的算法精髓所在*/PreAct=NextAct; /*方块下落完毕,取得下一个方块*/ DrawNext(0);}}void Init(){int GraphDriver=DETECT,GraphMode;registerbgidriver(EGAVGA_driver);initgraph(&GraphDriver,&GraphMode,"");if (kbhit()) Sounds=CANNOT;setcolor(1);OutTextXY(10,10,"Tetris");OutTextXY(30,30,"Version 2.0");OutTextXY(10,120,"Help:");OutTextXY(20,140,"+ :Faster");OutTextXY(20,160,"- :Slower");OutTextXY(20,180,"Esc :Quit");OutTextXY(20,200,"F1 :Help");OutTextXY(10,320,"By Mr. Unique");outtextxy(10,250,"Score: 00000");rectangle(BeginH-3,BeginS-3,BeginH+Heng*(Wid+2)+2,BeginS+Sh u*(Wid+2)+2);rectangle(BeginH-5,BeginS-5,BeginH+Heng*(Wid+2)+4,BeginS+Sh u*(Wid+2)+4);rectangle(BeginH+(Heng+4)*(Wid+2)-2,BeginS+10,BeginH+(Heng+ 8)*(Wid+2)+2,BeginS+12+4*(Wid+2));for (i=0;i<MAX;i++)for (j=0;j<MAX;j++){Position[i][j]=1;middle[i][j]=-1;}for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)Position[i][j]=0;for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)DrawBox(i,j,0);randomize();}void GoOn(){for(;;){Seconds+=0.2; /*控制方块的下落速度*/ if (Seconds>=Delays){Down();Seconds=0;if (NoPass==BOTTOM){DetectFill();middle[ActH][ActS]=Act;if (ActS==0)Fail();return;}}if (kbhit())Select();}}void Down() /*方块下降*/{Display(0);if (Touch(ActH,ActS,0,1)==CAN)ActS++;elsemiddle[ActH][ActS]=Act;Display(Staus+1);}int Touch(int x,int y,int dx,int dy) {NoPass=CAN;for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+dx+i][y+dy+j]+=b[i][j]; for (i=0;i<MAX;i++)for (j=0;j<MAX;j++)if (Position[i][j]>1)NoPass=CANNOT;for (i=0;i<4;i++)for (j=0;j<4;j++){Position[x+dx+i][y+dy+j]-=b[i][j]; middle[x+dx+i][y+dy+j]=Act;}if (NoPass==CANNOT && dx==0 && dy==1) {for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+i][y+j]+=b[i][j];NoPass=BOTTOM;}return NoPass;}int GetKey(void){int Ch,Low,Hig;Ch=bioskey(0);Low=Ch&0x00ff;Hig=(Ch&0xff00)>>8;return(Low==0Hig+256:Low);}void Select(){int OldStaus,acts=ActS;switch(GetKey()){case ESC :Quit();break;case DOWN :Seconds+=14500;break;case LEFT :Display(0);if (ActH>0 && Touch(ActH,ActS,-1,0)==CAN) { ActH--;}Display(Act+1);break;case RIGHT :Display(0);if (ActH<Heng && Touch(ActH,ActS,1,0)==CAN) { ActH++;}Display(Act+1);break;case BLANK : Display(0);ksdown();Display(Act+1);break;case F1 :Help();break;case EQUAL :case ADD :if (Delays>300) Delays-=100;break; case DEC :if (Delays<3000) Delays+=100;break; case PAUSEP :case PAUSEp :getch();break;case SOUNDS :case SOUNDs :if (Sounds==CAN)Sounds=CANNOT;elseSounds=CAN;break;case UP :if(Act==7){while(acts<Shu-1&&Position[ActH][acts]!=1)acts++;Position[ActH][acts]=0;DrawBox(ActH,acts,0);acts=ActS;break;}else{Display(0);OldStaus=Staus;switch(Act){case 0:case 3:case 4:if (Staus==1) Staus=0;else Staus=1;break; case 1:break;case 2:case 5:case 6:if (Staus==3) Staus=0;else Staus++;break; }Give();if (Touch(ActH,ActS,0,0)==CANNOT){Staus=OldStaus;Give();}Display(Act+1);break;}}}void ksdown(){while(flag){if(Touch(ActH,ActS,0,0)==CAN){ActS++;}else {ActS--;flag=0;}}flag=1;}void Quit(){int ch,TopScore;FILE *fp;if ((fp=fopen("Russian.scr","r+"))!=NULL) {fscanf(fp,"%d",&TopScore);if (Scores>TopScore){setcolor(1);outtextxy(470,80,"Hello !");outtextxy(470,100,"In all the players,"); outtextxy(470,120,"You are the First !"); outtextxy(470,140,"And your score will"); outtextxy(470,160,"be the NEW RECORD !"); fseek(fp,0L,0);fprintf(fp,"%d",Scores);}fclose(fp);}setcolor(1);OutTextXY(470,220,"Are You Sure (Yes/no)"); ch=getch();if (ch=='y'||ch=='Y'){closegraph();delay(20);exit(0);}setcolor(0);outtextxy(470,220,"Are You Sure (Yes/no)"); }void OutTextXY(int x,int y,char *String) {int i=0;char a[2];moveto(x,y);a[1]='\0';while (*(String+i)!='\0'){a[0]=*(String+i);outtext(a);if (Sounds==CAN && a[0]!=' '){sound(3000);delay(50);nosound();}i++;}}void Help(){unsigned Save;void *Buf;Save=imagesize(160,120,500,360); Buf=malloc(Save);getimage(160,120,500,360,Buf); setfillstyle(1,1);bar(160,120,500,280);setcolor(0);OutTextXY(170,130," About & Help");OutTextXY(170,150," # # # ########## # # # "); OutTextXY(170,160," # ## # # # # # # ###### ### "); OutTextXY(170,170," ########## ########## ## # # "); OutTextXY(170,180," # # # # # # # ## #### "); OutTextXY(170,190," # ## # #### ## # # # "); OutTextXY(170,200," # ## # # # # # ## # # # "); OutTextXY(170,210," # # # ## ## # ###### # # # "); OutTextXY(170,220," ## # ## # ## # # # # "); OutTextXY(170,230," # ## # #### # ## # "); OutTextXY(170,260," Good Luckly to You !!! ");getch();putimage(160,120,Buf,0);free(Buf);}void GetScores(){int Sec10000,Sec1000,Sec100,Sec10,Sec1;setfillstyle(0,1);bar(60,250,109,260);Sec1=Scores%10;Sec10=(Scores%100-Scores%10)/10;Sec100=(Scores%1000-Scores%100)/100;Sec1000=(Scores%10000-Scores%1000)/1000; Sec10000=(Scores%100000-Scores%10000)/10000; DispScore(60,250,'0'+Sec10000);DispScore(70,250,'0'+Sec1000);DispScore(80,250,'0'+Sec100);DispScore(90,250,'0'+Sec10);DispScore(100,250,'0'+Sec1);DispScore(110,250,'0');DispScore(120,250,'0');}void DispScore(int x,int y,char Ch){char a[2];a[1]='\0';a[0]=Ch;outtextxy(x,y,a);}void Give(){for (i=0;i<4;i++)for (j=0;j<4;j++)b[i][j]=a[Act][Staus][i][j];}void Display(int color){for (i=0;i<4;i++)for (j=0;j<4;j++)if (b[i][j]==1) DrawBox(ActH+i,ActS+j,color); }void DrawBox(int x,int y,int Color){x=BeginH+x*(Wid+2);y=BeginS+y*(Wid+2);setfillstyle(1,Color);bar(x+2,y+2,x+Wid-1,y+Wid-1);if (Color==0)setcolor(9);elsesetcolor(Act+1);rectangle(x+4,y+4,x+Wid-4,y+Wid-4);}void DrawNext(int Color){for (i=0;i<4;i++)for (j=0;j<4;j++)if (a[NextAct][0][i][j]==1) DrawBox(Heng+4+i,1+j,Color); }void DetectFill(){int Number,Fall,FallTime=0;for (i=Shu-1;i>=0;i--){Number=0;for (j=0;j<Heng;j++)if (Position[j][i]==1) Number++;if (Number==Heng){FallTime++;if (Sounds==CAN){sound(500);delay(500);nosound();}for (Fall=i;Fall>0;Fall--)for (j=0;j<Heng;j++)Position[j][Fall]=Position[j][Fall-1]; middle[j][Fall]=middle[j][Fall-1];if (Position[j][Fall]==0) DrawBox(j,Fall,0); else DrawBox(j,Fall,middle[j][Fall]+1);}i++;}}switch(FallTime){case 0:break;case 1:Scores+=1;break;case 2:Scores+=3;break;case 3:Scores+=6;break;case 4:Scores+=10;break;}if (FallTime!=0){GetScores();if (Scores%100==0) Delays-=100;}void Fail(){if (Sounds==CAN){for (k=0;k<3;k++){sound(300);delay(200);nosound();}}setcolor(1);OutTextXY(440,200,"Game over!"); Quit();closegraph();exit(0);}。

俄罗斯方块c语言源代码

俄罗斯方块c语言源代码

俄罗斯方块c语言源代码俄罗斯方块游戏是一款非常受欢迎的游戏,使用C语言编写源代码实现其功能。

下面是俄罗斯方块游戏的C语言源代码:1. 创建窗口函数: // 创建窗口函数 void CreateWindow(int width, int height) { // 使用SDL库创建窗口 SDL_Init(SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow("Tetris",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,width, height, 0); // 设置刷新时间SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"); }2. 创建游戏函数: // 创建游戏函数 void CreateGame() { // 设置随机数种子srand((unsigned int)time(NULL)); // 加载游戏资源 LoadResources(); // 初始化游戏数据InitGameData(); // 初始化游戏界面InitGameUI(); // 开始游戏循环 GameLoop(); // 清理游戏资源 CleanupGame(); }3. 绘图函数: // 绘图函数 void Draw(int x, inty, Color color) { // 使用SDL库在指定位置绘制指定颜色的矩形 SDL_Rect rect; rect.x = x;rect.y = y; rect.w = BLOCK_SIZE; rect.h = BLOCK_SIZE; SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);SDL_RenderFillRect(renderer, &rect); }。

俄罗斯方块源代码

俄罗斯方块源代码

C语言俄罗斯方块源代码Vc6.0编译通过#include<windows.h>#include<stdio.h>#include<time.h>#include<stdlib.h>#include<malloc.h>#include<conio.h>#define MAP_WIDTH10#define MAP_HEIGHT20#define BLOCKM"■"#define BKBLOCK"□"#define OUTSTD GetStdHandle(STD_OUTPUT_HANDLE)typedef int(*PFUN)(void *pData);void ShowMapArray(int map[MAP_HEIGHT][MAP_WIDTH]);//生成方块int xyIsInarrBlock(int arrBlock[4][2], int x, int y) //有返回1 没有返回0 {int i;for (i = 0;i<4;i++)if (arrBlock[i][0] == x && arrBlock[i][1] == y)return 1;return 0;}void GetTransBlocks(int arrBlock[4][2])//坐标模式4*4方块{int nTmp, x, y;int nCount = 1;int i;int nMinx = 0, nMiny = 0;memset(arrBlock, 0, 8 * sizeof(int));while (nCount < 4){nTmp = rand() % nCount;x = arrBlock[nTmp][0];y = arrBlock[nTmp][1];nTmp = rand() % 4;switch (nTmp){case 0:x--;break;case 1:y--;break;case 2:x++;break;case 3:y++;break;}if (xyIsInarrBlock(arrBlock, x, y))continue;arrBlock[nCount][0] = x;arrBlock[nCount][1] = y;if (nMinx > x)nMinx = x;if (nMiny > y)nMiny = y;nCount++;}for (i = 0;i<4;i++){if (nMinx < 0)arrBlock[i][0] -= nMinx;if (nMiny < 0)arrBlock[i][1] -= nMiny;}}//旋转void Ratat(int arrBlock[4][2], int Direct) // driect 1 顺时针方向旋转,-1 逆时针方向旋转{int i;int nMinx, nMiny;int nTmp;for (i = 0;i<4;i++){nTmp = arrBlock[i][0];arrBlock[i][0] = arrBlock[i][1] * (-1)*Direct;arrBlock[i][1] = nTmp*Direct;if (i == 0){nMinx = arrBlock[i][0];nMiny = arrBlock[i][1];}else{if (nMinx > arrBlock[i][0])nMinx = arrBlock[i][0];if (nMiny > arrBlock[i][1])nMiny = arrBlock[i][1];}}for (i = 0;i<4;i++){if (nMinx < 0)arrBlock[i][0] -= nMinx;if (nMiny < 0)arrBlock[i][1] -= nMiny;}}void gotoxy(int x, int y){COORD pos = { x,y };SetConsoleCursorPosition(OUTSTD, pos);}void showxy(int x, int y, int bShow){COORD pos = { x * 2 + 2,y + 2 };SetConsoleCursorPosition(OUTSTD, pos);if (bShow)printf(BLOCKM);elseprintf(BKBLOCK);}void DisShowCursor(){CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(OUTSTD, &cci);cci.bVisible = FALSE;SetConsoleCursorInfo(OUTSTD, &cci);}int CheckBlockPlace(int map[MAP_HEIGHT][MAP_WIDTH], int x, int y, int block[4][2], int bShow) //判断位置是否可用{int i;if (x < 0 || y < 0 || x >= MAP_WIDTH || y >= MAP_HEIGHT)return 0;for (i = 0;i<4;i++){if (map[y + block[i][1]][x + block[i][0]] == 1 && bShow)return 0;if (y + block[i][1] >= MAP_HEIGHT || x + block[i][0] >= MAP_WIDTH)return 0;}return 1;}int ShowBlock(int x, int y, int block[4][2], int bShow){int i;for (i = 0;i<4;i++)showxy(block[i][0] + x, block[i][1] + y, bShow);return 1;}void LoadMap(int map[MAP_HEIGHT][MAP_WIDTH]){int i, j;DisShowCursor();system("cls");printf("----------------俄罗斯方块v0.1--------------");printf("\n\n");for (i = 0;i<MAP_HEIGHT;i++){printf(" ");for (j = 0;j<MAP_WIDTH;j++){if (map[i][j])printf(BLOCKM);elseprintf(BKBLOCK);}printf("\n");}gotoxy(MAP_WIDTH * 2 + 6, 4);printf("按s开始\n");gotoxy(MAP_WIDTH * 2 + 6, 5);printf("Next:");gotoxy(MAP_WIDTH * 2 + 6, 12);printf("分数:");}int gameDown(int map[MAP_HEIGHT][MAP_WIDTH], int blockxy[4][2], int nSec, PFUN OnFun, void *pOnData){int i, j, k;int nSelect;int x = 3, y = 0;static int maxy = 20;int missrow = 0;int xsum = 0;while (1){nSelect = OnFun(pOnData);if (nSelect){switch (nSelect){case 75:{if (CheckBlockPlace(map, x - 1, y, blockxy, 1))x--;}break;case 72:{Ratat(blockxy, 1);if (!CheckBlockPlace(map, x, y, blockxy, 1)){Ratat(blockxy, -1);}}break;case 77:{if (CheckBlockPlace(map, x + 1, y, blockxy, 1))x++;}break;}}else{if (CheckBlockPlace(map, x, y, blockxy, 1)){ShowBlock(x, y, blockxy, 1);Sleep(nSec);if (CheckBlockPlace(map, x, y + 1, blockxy, 1)){ShowBlock(x, y, blockxy, 0);y++;}else{for (i = 0;i<4;i++){map[y + blockxy[i][1]][x + blockxy[i][0]] = 1;}if (y < maxy)maxy = y;break;}}elsereturn -1;}}for (i = maxy;i<MAP_HEIGHT;i++){xsum = 0;for (j = 0;j<MAP_WIDTH;j++){xsum += map[i][j];}if (xsum == MAP_WIDTH){for (k = i;k >= maxy;k--)for (j = 0;j<MAP_WIDTH;j++)map[k][j] = map[k - 1][j];missrow++;LoadMap(map);}}return missrow;}// help functionvoid ShowMapArray(int map[MAP_HEIGHT][MAP_WIDTH]){int i, j;for (i = 0;i<MAP_HEIGHT;i++){COORD pos = { MAP_WIDTH * 2,i };SetConsoleCursorPosition(OUTSTD, pos);for (j = 0;j<MAP_WIDTH;j++){printf("%d", map[i][j]);}}}int GetInfo(void *pData){while (kbhit()){char ch1 = getch();if (ch1 < 0){ch1 = getch();}return ch1;}while (kbhit())getch();return 0;}int main(){int map[MAP_HEIGHT][MAP_WIDTH] = { 0 };int blockarrnow[4][2] = { 0 }, blockarrnext[4][2] = { 0 };int ch, nRe, i, j, nScro = 0, nSpeed = 300;BOOL bRun = TRUE;LoadMap(map);srand((unsigned)time(NULL));while (bRun){if (kbhit()){ch = getch();}if (ch == 's' || ch == 'S'){GetTransBlocks(blockarrnow);while (bRun){GetTransBlocks(blockarrnext);ShowBlock(MAP_WIDTH + 2, 5, blockarrnext, 1);nRe = gameDown(map, blockarrnow, nSpeed, GetInfo, NULL);for (i = 0;i<4;i++){blockarrnow[i][0] = blockarrnext[i][0];blockarrnow[i][1] = blockarrnext[i][1];}for (i = 0;i <= 4;i++)for (j = 0;j <= 4;j++){gotoxy(MAP_WIDTH * 2 + 4 + j * 2, 7 + i);printf(" ");}if (nRe < 0){bRun = FALSE;break;}else{nScro += (nRe * 100);gotoxy(MAP_WIDTH * 2 + 11, 12);printf("%d", nScro);}}}}return 0;}Vs2015 编译运行配图。

自己用C语言编写的俄罗斯方块小游戏 hello world级

自己用C语言编写的俄罗斯方块小游戏  hello world级
break;
default : return ;
}
}
void clear_small_screen()
{
int i,j;
int x,y;
for(i=0;i<SMALL_CUBSIZE;i++){
for(j=0;j<SMALL_CUBSIZE;j++){
for (x = X_SMALL_START+j*SMALL_UNIT; x <X_SMALL_START+SMALL_UNIT+j*SMALL_UNIT; x++)
break;
case 17:
for(i=0;i<3;i++)
small_cub[0][i]=1;
small_cub[1][1]=1;
break;
case 18:
for(i=0;i<3;i++)
small_cub[i][1]=1;
small_cub[1][0]=1;
break;
case 7:
for(i=0;i<3;i++)
small_cub[i][0]=1;
small_cub[0][1]=1;
break;
case 8:
for(i=0;i<3;i++)
small_cub[0][i]=1;
small_cub[1][2]=1;
{
int i,j;
for(i=0;i<x;i+Байду номын сангаас){
for(j=0;j<y;j++){
if(chosen==1)

C语言编写控制台版俄罗斯方块源码

C语言编写控制台版俄罗斯方块源码
typedef unsigned int uint32;
typedef uint8 bool;
#define APP_WIDTH 20
#define APP_HEIGHT 20
#define APP_MIN_X 8
#define APP_MAX_X (APP_MIN_X + ((APP_WIDTH + 1) * 2))
#define JK_F4 0xFF3E
#define JK_F5 0xFF3F
#define JK_F6 0xFF40
#define JK_F7 0xFF41
#define JK_F8 0xFF42
#define JK_F9 0xFF43
{0xCC00CC00, 0xCC00CC00, 0xCC00CC00, 0xCC00CC00}, //O
{0xE620E400, 0x26E02620, 0x8CE004E0, 0xEC808C80} //T
};
#define PRINT_BOX(bs) printf("%2s", BlockSharps[bs])
#define APP_MIN_Y 3
#define APP_MAX_Y (APP_MIN_Y + ((APP_HEIGHT + 1)))
#define MAP_WIDTH 10
#define MAP_HEIGHT 20
#define MAP_MIN_X (APP_MIN_X + 2)
#define MAP_MAX_X (MAP_MIN_X + (MAP_WIDTH *2))
#define JK_RIGHT 0xE04D
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

注意:本源代码包含<graphics.h>头文件,VC6.0请自行下载库文件包,解决没有库文件的问题环境:WINDOWS7 VC6.0程序清单:库文件MYFILE.H/****************************************************************************** *********************File Name : MYFILE.HCopyright :Module Name :CPU : Intel i7RTOS :Creat Date : 2017/1/13Author : YangAbstract Description: C++、C实用函数******************************************************************************* *********************/#ifndef _MYFILE_#define _MYFILE_#include<stdio.h>#include<iostream.h>void introduce(){printf("欢迎使用!MYFILE.H\n");}/*********************************C++常用类******************************/template<class T_STACK> //栈class STACK{private:int top;T_STACK stackspace[100];public:STACK(){top =-1;}void PUSH(T_STACK x){if(top>=100-1)cout<<"STACK OVER FLOW"<<endl;else{ top++;stackspace[top]=x;}}T_STACK POP(){T_STACK x;if(top<-1){ top = -1; cout<<"STACK EMPTY"<<endl; return stackspace[0];}x = stackspace[top];top--;return x;}};template<class T_List> //队列class LIST{private:int point_in;int point_out;T_List listspace[100];public:LIST(){point_in=-1;point_out=-1;}void INList(T_List x){point_in=(point_in+1)%100;listspace[point_in]=x;}T_List OUTList(){T_List x;point_out=(point_out+1)%100;x = listspace[point_out];return x;}void showList(){int i=0;for(i=point_out+1;i<=point_in;i++)cout<<listspace[i]<<"\t";}};/**************************C语言通用数据结构********************************//***************************C语言控制台显示*********************************/ #include <graphics.h>#define xystep (15)#define IMAGE_X (450)#define IMAGE_Y (450)//初始化UI界面void UI_init(){initgraph(IMAGE_X,IMAGE_Y);settextcolor(LIGHTGREEN);setbkcolor(BLACK);}//display_rectange(坐标轴x,坐标轴y,要显示的字符串,颜色);void display_str(int x,int y,char* s,int color){ x = (x)*xystep;y = (y)*xystep;switch(color){case 1:settextcolor(BLACK);break;case 2:settextcolor(LIGHTBLUE);break;case 3:settextcolor(LIGHTGREEN);break;case 4:settextcolor(LIGHTCYAN);break;case 5:settextcolor(LIGHTRED);break;case 6:settextcolor(LIGHTMAGENTA);break;case 7:settextcolor(YELLOW);break;case 8:settextcolor(WHITE);break;case 9:settextcolor(BROWN);break;case 10:settextcolor(LIGHTGRAY);break;case 11:settextcolor(RED);break;default:settextcolor(WHITE);}outtextxy(x,y,s);}//使用printf在指定位置输出字符串(WIN32API)void WIN32display(int x,int y,char* c) //在x,y显示一个字符串{//RECT rect;COORD pos;HANDLE hOutput;pos.X = x;pos.Y = y;hOutput = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput , pos);printf("%s",c);}#endif//源程序/****************************************************************************** *******************File Name : 俄罗斯方块.cppCopyright :Module Name :CPU : Intel i7RTOS :Creat Date : 2017/1/13Author : YangAbstract Description: 俄罗斯方块游戏编译环境: Visual C++ 6.0绿色版************************************************************************************************/#include <conio.h>#include<iostream.h>#include <myfile.h>#include<math.h>#include<time.h>#include<windows.h>/******************************类与函数的声明******************************/ class shape;void create_new(shape* x);void key_deal();void UI_NEXT(shape* x);void UI();void delay();/******************************自己定义的一些宏******************************/ #define LOCATIONX 9 //砖块初始出现位置#define LOCATIONY 6#define TIME_STEP 30000 //砖块下落的速度/******************************全局变量的声明******************************/static char key_vaule=0;/******************************类的定义******************************/class shape //基类:形状{public:virtual void trans() = 0;int x1,y1,x2,y2,x3,y3,x4,y4; //实时坐标int x12,y12,x22,y22,x32,y32,x42,y42,status2; //坐标,状态备份副本int angle;int status;int color;shape(){x1=0;x2=0;x3=0;x4=0;y1=0;y2=0;y3=0;y4=0;angle=0;color=3;}shape(shape &x) //拷贝构造函数{x1=x.x1;x2=x.x2;x3=x.x3;x4=x.x4;y1=x.y1;y2=x.y2;y3=x.y3;y4=x.y4;color=x.color;}void up(){saved();y1--;y2--;y3--;y4--;}void down(){saved();y1++;y2++;y3++;y4++;}void left(){saved();x1--;x2--;x3--;x4--;}void right(){saved();x1++;x2++;x3++;x4++;}void saved() //坐标备份{ status2=status;x12=x1;y12=y1;x22=x2;y22=y2;x32=x3;y32=y3;x42=x4;y42=y4;}void show(){display_str(x1,y1,"■",color);display_str(x2,y2,"■",color);display_str(x3,y3,"■",color);display_str(x4,y4,"■",color);}void clear(){display_str(x1,y1,"",color);display_str(x2,y2,"",color);display_str(x3,y3,"",color);display_str(x4,y4,"",color);}};class bulid //建筑物类{public:int score;char MAP[30][30]; //BUILDMAP坐标体统一:X: 0 - 29 Y:0 - 29bulid() //建筑体初始化{ int i=0;int j=0;for(i=0;i<30;i++)for(j=0;j<30;j++)MAP[i][j]=0;for(i=5;i<30;i++) //画竖着的杆子MAP[列][行]{MAP[0][i]=1;MAP[1][i]=1;MAP[15][i]=1;MAP[16][i]=1;}for(i=0;i<15;i++) //画底{MAP[i][29]=1;}score=0;}void receive(shape *s) //接纳一个砖头作为建筑物的一部分{MAP[s->x1][s->y1]=1;MAP[s->x2][s->y2]=1;MAP[s->x3][s->y3]=1;MAP[s->x4][s->y4]=1;}void display_mode() //把模板显示出来,方便调试{ int i=0;int j=0;for(i=0;i<30;i++)for(j=0;j<30;j++)display_str(i,j,"□",2);}void display_bulid(int color) //把建筑物显示出来一次{ int i = 0,j=0;for(i=0;i<19;i++) //19:半屏显示for(j=3;j<30;j++)if(MAP[i][j]==1){ if(i==0||i==1||i==15||i==16||j==29)display_str(i,j,"■",8); //容器的颜色elsedisplay_str(i,j,"■",color); //color:已经成了建筑物的砖块的颜色}else if(MAP[i][j]==0)display_str(i,j,"",color);elsedisplay_str(i,j,"☆",color);}int place(shape *s) //是否着陆?{int i=0,j=0;for(i=0;i<30;i++)for(j=0;j<30;j++){if( ((j==(s->y1))&&(i==s->x1)) //坐标与建筑物重合|| ((j==(s->y2))&&(i==s->x2))|| ((j==(s->y3))&&(i==s->x3))|| ((j==(s->y4))&&(i==s->x4)))if(MAP[i][j]==1){if(key_vaule == 'S'||key_vaule == 's') //是向下键{ //可以着陆,那么收录坐标,遴选y最小值s->y1--;s->y2--;s->y3--;s->y4--;receive(s);Score_count();display_bulid(7);delete s; //删除已经变成了建筑物的活体对象的空间,节约内存return 1; //返回1,表示已经着陆}else{ s->status=s->status2;s->x1=s->x12;s->y1=s->y12;s->x2=s->x22;s->y2=s->y22;s->x3=s->x32;s->y3=s->y32;s->x4=s->x42;s->y4=s->y42;}}}return 0;}int END() //是否终止游戏{ int i=0;for(i=2;i<15;i++)if(MAP[i][6]==1){ display_str(20,12,"END THE GAME!!",5);return 1;}return 0;}void Score_count() //计分板{ char SCORE[20];int i,j,count=0;for(i=6;i<29;i++) //从第6行检查到28行{for(j=2;j<15;j++){if(MAP[j][i]==1) //从第2列检查到第14列count++;if(count>=13){ delete_line(i); //删除第i行score++;}}count=0;}//计分显示display_str(22,10,"SCORE",5);itoa(score,SCORE,10);display_str(26,10,SCORE,5);}void delete_line(int Line){int i=0,j=0;for(j=Line;j>0;j--)for(i=2;i<15;i++)if(j-1==5) //第5行实际上是最上方的墙壁了MAP[i][j]=0;elseMAP[i][j]=MAP[i][j-1];}};//-----------------------定义具体的砖块类----------------class BAR:public shape //长条{private:void trans_0() //姿态0{x1-=1;y1+=1;x3+=1;y3-=1;x4+=2;y4-=2;}void trans_1() //姿态1{x1+=1;y1-=1;x3-=1;y3+=1;x4-=2;y4+=2;}public:BAR(){ x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2; y3=y2+1;x4=x2; y4=y2+2;status=0;color=2;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();status = (++status)%2;}};class SQUARE:public shape //正方形{public:SQUARE(){ x2=LOCATIONX;y2=LOCATIONY;x1=x2-1;y1=y2;x3=x2-1;y3=y2+1;x4=x2;y4=y2+1;color = 3;status=0;}void trans(){saved();}};class JBAR:public shape //J形{private:void trans_0(){x1-=1;y1+=1;x3+=1;y3-=1;x4+=2;}void trans_1(){x1+=1;y1+=1;x3-=1;y3-=1;y4-=2;}void trans_2(){x1+=1;y1-=1;x3-=1;y3+=1;x4-=2;}void trans_3(){x1-=1;y1-=1;x3+=1;y3+=1;y4+=2;}public:JBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2; y3=y2+1;x4=x2-1; y4=y2+1;status=0;color=4;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();if(status==2)trans_2();if(status==3)trans_3();status = (++status)%4;}};class LBAR:public shape //L形{private:void trans_0(){x1-=1;y1+=1;x3+=1;y3-=1;y4-=2;}void trans_1(){x1+=1;y1+=1;x3-=1;y3-=1;x4-=2;}void trans_2(){x1+=1;y1-=1;x3-=1;y3+=1;y4+=2;}void trans_3(){x1-=1;y1-=1;x3+=1;y3+=1;x4+=2;}public:LBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2; y3=y2+1;x4=x2+1; y4=y2+1;status=0;color=5;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();if(status==2)trans_2();if(status==3)trans_3();status = (++status)%4;};class ZBAR:public shape{private:void trans_0(){x1+=1;x4+=1;y4-=2;}void trans_1(){x1-=1;x4-=1;y4+=2;}public:ZBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2+1; y3=y2;x4=x2+1; y4=y2+1;status=0;color=6;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();status = (++status)%2;};class SBAR:public shape{private:void trans_0(){x1-=1;y1+=1;x3+=1;y3+=1;x4+=2;}void trans_1(){x1+=1;y1-=1;x3-=1;y3-=1;x4-=2;}public:SBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2; y1=y2-1;x3=x2-1; y3=y2;x4=x2-1; y4=y2+1;status=0;color=11;}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();status = (++status)%2;}};class TBAR:public shape{private:void trans_0(){x1+=1;y1+=1;x3-=1;y3+=1;x4-=1;y4-=1;}void trans_1(){x1+=1;y1-=1;x3+=1;y3+=1;x4-=1;y4+=1;}void trans_2(){x1-=1;y1-=1;x3+=1;y3-=1;x4+=1;y4+=1;}void trans_3(){x1-=1;y1+=1;x3-=1;y3-=1;x4+=1;y4-=1;}public:TBAR(){x2=LOCATIONX;y2=LOCATIONY;x1=x2-1; y1=y2;x3=x2; y3=y2-1;x4=x2+1; y4=y2;status=0;color=10;//}void trans(){ saved();if(status==0)trans_0();if(status==1)trans_1();if(status==2)trans_2();if(status==3)trans_3();status = (++status)%4;}};/******************************自定义的类外的函数******************************/void create_new(shape* x){ int random1,random2;//srand((unsigned)time(0));这个srand()函数只需要执行一次就够了;执行多次会导致生成的数字不那么随机random1=rand()%7+1;//delete x;switch(random1) //随机生成活动对象{case 1: x = new LBAR; break;case 2: x = new SQUARE; break;case 3: x = new BAR; break;case 4: x = new ZBAR; break;case 5: x = new TBAR; break;case 6: x = new SBAR; break;case 7: x = new JBAR; break;default :x =new TBAR; break;}random2=rand()%4+1;switch(random2) //随机生成活动对象的状态{case 1: x->trans();break;case 2: x->trans();x->trans();break;case 3: x->trans();x->trans();x->trans();break;case 4: x->trans();x->trans();x->trans();x->trans();break;default :break;}}void UI() //用户问好界面{ UI_init();display_str(22,10,"SCORE",5);display_str(26,10,"0",5);display_str(1,0,"YWQ:2017/1/18 平台WINDOWS_7 VC6.0 ",3);display_str(1,2,"欢迎各路大神讨论C/C++的学习",4);display_str(20,20,"A:向左移动",8);display_str(20,21,"D:向右移动",8);display_str(20,22,"S:快速落下",8);display_str(20,23,"W:方块变形",8);}void UI_NEXT(shape* x) //把下一个要出现的砖块展示出来{display_str(17,5,"",1);display_str(17,6,"",1);display_str(17,7,"",1);display_str(17,8,"",1);display_str(x->x1+10,x->y1,"■",x->color);display_str(x->x2+10,x->y2,"■",x->color);display_str(x->x3+10,x->y3,"■",x->color);display_str(x->x4+10,x->y4,"■",x->color);}void key_deal(){ int i=0;int time=0;int flag=0;shape* p,*p2;bulid B;p = new LBAR; //当前的方块p2=new LBAR; //下次的方块B.display_bulid(7);delete p;create_new(p); //当前的delete p2;create_new(p2); //下次的UI_NEXT(p2);p->show();while(!B.END()){if(kbhit()) //kbhit():检测当前是否有按键按下非阻塞函数{key_vaule = getch();}else{ time++;key_vaule='U';if(time>TIME_STEP) //自动下降{key_vaule='S';time=0;}}switch(key_vaule){case 'A':case 'a':p->clear();p->left();B.place(p); {p->show();}break;case 'D':case 'd':p->clear();p->right();B.place(p); {p->show();}break;case 'S':case 's':p->clear();p->down(); if(B.place(p)){ p=p2;p2=new LBAR;delete p2;create_new(p2);UI_NEXT(p2);} p->show();break;//不知道为什么,p2一定要先删除一次,再分配一次,数据才是新的case 'w':case 'W':p->clear();p->trans();B.place(p); {p->show();}break;case 'U':break;//U:不做反应default:;}}}void delay(){ int i=0,j=0;for(i=0;i<50000;i++)for(j=0;j<50000;j++);}/******************************主函数****************************************/ void main(){ srand((unsigned)time(0));UI();key_deal();getch();getch();getch();getch();}/******************************此源文件结束******************************/。

相关文档
最新文档