1、C+写的俄罗斯方块源代码+开发文档0 20 40 60 20 40 60 每一点取的是左上角坐标 20,60, 20,40, 20,20, 40,60, RED, 1, 0,60, 20,60, 40,40, 40,60, RED, 2, 20,20, 40,20, 40,40, 40,60, RED, 3, 0,60, 0,40, 20,40, 40,40, RED, 0, 20,60, 20,40, 20,20, 40,20, YELLOW,1, 0,40, 0,60, 20,60, 40,60, YELLOW,2, 20,60, 40,60, 40,40, 40,20, YELLOW,3
2、, 0,40, 20,40, 40,40, 40,60, YELLOW,0, 0,60, 20,60, 20,40, 40,60, GREEN,1, 0,40, 20,60, 20,40, 20,20, GREEN,2, 0,40, 20,40, 20,60, 40,40, GREEN,3, 20,20, 20,40, 20,60, 40,40, GREEN,0, 0,60, 20,60, 20,40, 40,40, WHITE,1, 0,40, 0,20, 20,40, 20,60, WHITE,0, 20,0, 20,20, 20,40, 20,60, LIGHTGRAY,1, 0,40,
3、 20,40, 40,40, 60,40, LIGHTGRAY,0, 20,20, 20,40, 20,60, 20,80, LIGHTGRAY,1, 0,60, 20,60, 40,60, 60,60, LIGHTGRAY,0, 20,40, 20,60, 40,40, 40,60, LIGHTGREEN,1以下是源代码:Game.h 文件#include #include #include #include #define UP w#define DOWN s#define LEFT a#define RIGHT d#define Esc 27int key=NULL;int a=0;st
4、ruct Shape int xy8; int color;struct full /记?录?每?个?方?块的?信?息 bool isfull; int color;Shape shape74= /7种?基本?形?状 四?种?变?形?(不?够?四?种?变?化的?用?相同?的?来填?充?)? 20,60, 20,40, 20,20, 40,60, RED, 0,60, 20,60, 40,40, 40,60, RED, 20,20, 40,20, 40,40, 40,60, RED, 0,60, 0,40, 20,40, 40,40, RED, , 20,60, 20,40, 20,20, 40
5、,20, YELLOW, 0,40, 0,60, 20,60, 40,60, YELLOW, 20,60, 40,60, 40,40, 40,20, YELLOW, 0,40, 20,40, 40,40, 40,60, YELLOW, , 0,60, 20,60, 20,40, 40,60, GREEN, 0,40, 20,60, 20,40, 20,20, GREEN, 0,40, 20,40, 20,60, 40,40, GREEN, 20,20, 20,40, 20,60, 40,40, GREEN, , 0,60, 20,60, 20,40, 40,40, WHITE, 0,40, 0
6、,20, 20,40, 20,60, WHITE, 0,60, 20,60, 20,40, 40,40, WHITE, 0,40, 0,20, 20,40, 20,60, WHITE , 0,40, 20,40, 20,60, 40,60 , BLUE, 0,40, 0,60, 20,40, 20,20 , BLUE, 0,40, 20,40, 20,60, 40,60 , BLUE, 0,40, 0,60, 20,40, 20,20 , BLUE, , 20,0, 20,20, 20,40, 20,60, LIGHTGRAY, 0,40, 20,40, 40,40, 60,40, LIGHT
7、GRAY, 20,0, 20,20, 20,40, 20,60, LIGHTGRAY, 0,40, 20,40, 40,40, 60,40, LIGHTGRAY, , 20,40, 20,60, 40,40, 40,60, LIGHTGREEN, 20,40, 20,60, 40,40, 40,60, LIGHTGREEN, 20,40, 20,60, 40,40, 40,60, LIGHTGREEN, 20,40, 20,60, 40,40, 40,60, LIGHTGREEN, ;class Game int x,y; /x y 表示?方?块的?相对?坐?标 int dir; /方?块移?
8、动方?向 int _shape; /七?种?形?状 int change_shape; /四?种?变?换? int old_shape; /记?录?未变?形?方?块的?形?状 int old_change_shape; bool is_fullline20; /标记?是?否?满行D full isfull2010; /标记?是?否?有D方?块 20行D10列D public : Game(); int shapeX(int i)return (shape_shapechange_shape.xyi+x); /返回?shape形?状第i个?方?块的?的?实际X坐?标 int shapeY(int
9、 i)return (shape_shapechange_shape.xyi+1+y); /返回?shape形?状第i个?方?块的?的?实际Y坐?标 int rowX(int i)return i*20+100; /返回?第i列D的?X坐?标 int columnY(int i)return i*20+50; /返回?第i行D的?Y坐?标 void drawinterface(); /画-界?面? void drawshape(); /画-方?块 void clearshape(); /清?除y方?块 void drawshowshape( ); /画-在预览框中D的?方?块 void cle
10、arshowshape(); /清?除y在预览框中D的?方?块 void getdir(); /取?得?方?块移?动方?向 bool is_move_leftX(); /方?块是?否?能向左移? bool is_move_rightX(); /方?块是?否?能向右移? bool is_moveY(); /方?块是?否?能在 Y 轴方?向移?动 bool is_change(); /是?否?能变?形? (近边?界?时变?形?后不?能出?界?)? void shapemove(); /方?块移?动 void newshape(); /出?新?的?方?块 void fullline(); /扫描满行
11、D void clearfullline(); /清?除y满行D void repaint(); /重?画-(实现?消?行D下?移?)? bool isgameover(); /游?戏是?否?结束?;Game:Game () x=180; y=50; srand(unsigned)time(NULL); _shape=rand()%7; change_shape=rand()%4; for(int i=0;i20;i+) for(int j=0;j10;j+) isfullij.isfull =false; isfullij.color =BLACK; for(int i=0;i20;i+)
12、is_fulllinei=false; drawinterface();void Game:drawinterface () initgraph(640,480); rectangle(345,45,435,135); rectangle(95,45,305,455);void Game:drawshape () setfillstyle(shape_shapechange_shape.color ); for(int i=0;i=6;i+=2) bar(shapeX(i), shapeY(i), shapeX(i)+20, shapeY(i)+20);void Game:clearshape
13、 () setfillstyle(BLACK); for(int i=0;i=6;i+=2) bar(shapeX(i), shapeY(i), shapeX(i)+20, shapeY(i)+20);void Game:drawshowshape () setfillstyle(shapeold_shapeold_change_shape.color ); for(int i=0;i=6;i+=2) bar(shapeold_shapeold_change_shape.xy i+350,shapeold_shapeold_change_shape.xy i+1+50,shapeold_sha
14、peold_change_shape.xy i+350+20,shapeold_shapeold_change_shape.xy i+1+50+20);void Game:clearshowshape () setfillstyle(BLACK ); for(int i=0;i=6;i+=2) bar(shapeold_shapeold_change_shape.xy i+350,shapeold_shapeold_change_shape.xy i+1+50,shapeold_shapeold_change_shape.xy i+350+20,shapeold_shapeold_change
15、_shape.xy i+1+50+20);void Game:getdir() if(kbhit() dir=getch(); if(dir=27) key=dir;bool Game:is_move_leftX() for(int i=0;i=6;i+=2) if(shapeX(i)=100 |isfull(shapeY(i)-50)/20(shapeX(i)-100)/20-1.isfull =true )/ return false; return true;bool Game:is_move_rightX () for(int i=0;i=300|isfull(shapeY(i)-50
16、)/20(shapeX(i)-100)/20+1.isfull =true) / return false; return true;bool Game:is_moveY () for(int i=0;i=430 | isfull(shapeY(i)-50)/20+1(shapeX(i)-100)/20.isfull =true) / setcolor(RED); / rectangle(shapeX(i)-100)/20-1)*20+100,(shapeY(i)-50)/20+1)*20+50,(shapeX(i)-100)/20-1)*20+100+20,(shapeY(i)-50)/20
17、+1)*20+50+20); return false; return true;bool Game:is_change () for(int i=0;i=6;i+) if(change_shape3) if(shape_shapechange_shape+1.xy i+x300) /变?形?后超?出?边?界?不?能变? return false; if(isfull(shape_shapechange_shape+1.xyi+1+y-50)/20-1(shape_shapechange_shape+1.xyi+x-100)/20-1.isfull =true) /变?形?后碰?到?方?块 r
18、eturn false; else if(change_shape=3) if(shape_shape0.xy i+x300) return false; if(isfull(shape_shape0.xyi+1+y-50)/20-1(shape_shape0.xyi+x-100)/20-1.isfull =true) return false; return true;void Game:shapemove() if(a=0) old_shape=_shape; old_change_shape=change_shape; drawshowshape(); a=1; for(int i=0;
19、i20;i+) /把?一?秒?钟分?成20份Y (加快左右移?动的?速度)? drawshape(); getdir(); Sleep(50); clearshape(); switch(dir) case LEFT: if(is_move_leftX() ) x-=20; break; case RIGHT: if(is_move_rightX() ) x+=20; break; case DOWN: if(is_moveY() y+=20; break; case UP: if(is_change() change_shape+; if(change_shape=4) change_sha
20、pe=0; break; dir=NULL; if(is_moveY() y+=20; void Game:newshape () srand(unsigned)time(NULL); if(!is_moveY() clearshowshape(); a=0; drawshape(); for(int i=0;i=0;i-) for(int j=0;j10;j+) if(isfullij.isfull =true) count+; if(count=10) is_fulllinei=true; else is_fulllinei=false; count=0; void Game:clearf
21、ullline () for(int a=0;a=0;k-) if(is_fulllinek=true) for(int i=k;i0;i-) for(int j=0;j=0;i-) for(int j=0;j10;j+) setfillstyle(isfullij.color ); bar(j*20+100,i*20+50,j*20+120,i*20+70); bool Game:isgameover() for(int i=0;i=6;i+) if(shapeY(i)=50) return true; return false;Main.cpp 文件#include Game.hextern int key;Game game;int main() setwritemode(R2_XORPEN); while(key!=Esc) game.newshape(); game.shapemove (); if(game.isgameover () break; /game.repaint (); outtextxy(320,240,LGameOver); getch(); exit(0); closegraph(); return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1