ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:18.86KB ,
资源ID:11863186      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11863186.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(小游戏源代码.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

小游戏源代码.docx

1、小游戏源代码/* Desc: 俄罗斯方块游戏 * By: hoodlum1980 * Email * Date: 22:30 */#include #include #include #include #include #include #define true 1#define false 0#define BoardWidth 12#define BoardHeight 23#define _INNER_HELPER /*inner helper method */*Scan Codes Define*/enum KEYCODES K_ESC =0x011b, K_UP =0x4800,

2、/* upward arrow */ K_LEFT =0x4b00, K_DOWN =0x5000, K_RIGHT =0x4d00, K_SPACE =0x3920, K_P =0x1970;/* the data structure of the block */typedef struct tagBlock char c44; /* cell fill info array, 0-empty, 1-filled */ int x; /* block position cx 0,BoardWidht -1 */ int y; /* block position cy -4,BoardHei

3、ght-1 */ char color; /* block color */ char size; /* block max size in width or height */ char name; /* block name (the blocks shape) */ Block;/* games global info */int FrameTime= 1300;int CellSize= 18;int BoardLeft= 30;int BoardTop= 30;/* next block grid */int NBBoardLeft= 300;int NBBoardTop= 30;i

4、nt NBCellSize= 10;/* score board position */int ScoreBoardLeft= 300;int ScoreBoardTop=100;int ScoreBoardWidth=200;int ScoreBoardHeight=35;int ScoreColor=LIGHTCYAN;/* infor text postion */int InfoLeft=300;int InfoTop=200;int InfoColor=YELLOW;int BorderColor=DARKGRAY;int BkGndColor=BLACK;int GameRunni

5、ng=true;int TopLine=BoardHeight-1; /* top empty line */int TotalScore=100;char info_score20;char info_help255;char info_common255;/* our board, Boardxy0-isFilled, Boardxy1-fillColor */unsigned char BoardBoardWidthBoardHeight2;char BufferCells44; /* used to judge if can rotate block */Block curBlock;

6、 /* current moving block */Block nextBlock; /* next Block to appear */* function list */int GetKeyCode();int CanMove(int dx,int dy);int CanRotate();int RotateBlock(Block *block);int MoveBlock(Block *block,int dx,int dy);void DrawBlock(Block *block,int,int,int);void EraseBlock(Block *block,int,int,in

7、t);void DisplayScore();void DisplayInfo(char* text);void GenerateBlock(Block *block);void NextBlock();void InitGame();int PauseGame();void QuitGame();/*Get Key Code */int _INNER_HELPER GetKeyCode() int key=0; if(bioskey(1) key=bioskey(0); return key;/* display text! */void _INNER_HELPER DisplayInfo(

8、char *text) setcolor(BkGndColor); outtextxy(InfoLeft,InfoTop,info_common); strcpy(info_common,text); setcolor(InfoColor); outtextxy(InfoLeft,InfoTop,info_common);/* create a new block by key number,* the block anchor to the top-left corner of 4*4 cells*/void _INNER_HELPER GenerateBlock(Block *block)

9、 int key=(random(13)*random(17)+random(1000)+random(3000)%7; block-size=3;/* because most blocks size=3 */ memset(block-c,0,16); switch(key) case 0: block-name=T; block-color=RED; block-c10=1; block-c11=1, block-c21=1; block-c12=1; break; case 1: block-name=L; block-color=YELLOW; block-c10=1; block-

10、c11=1; block-c12=1, block-c22=1; break; case 2: block-name=J; block-color=LIGHTGRAY; block-c10=1; block-c11=1; block-c12=1, block-c02=1; break; case 3: block-name=z; block-color=CYAN; block-c00=1, block-c10=1; block-c11=1, block-c21=1; break; case 4: block-name=5; block-color=LIGHTBLUE; block-c10=1,

11、 block-c20=1; block-c01=1, block-c11=1; break; case 5: block-name=o; block-color=BLUE; block-size=2; block-c00=1, block-c10=1; block-c01=1, block-c11=1; break; case 6: block-name=I; block-color=GREEN; block-size=4; block-c10=1; block-c11=1; block-c12=1; block-c13=1; break; /* get next block! */void

12、NextBlock() /* copy the nextBlock to curBlock */ =; =; =(BoardWidth-4)/2; =; memcpy,16); /* generate nextBlock and show it */ EraseBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize); GenerateBlock(&nextBlock); =1,=0; DrawBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);/* rotate the block, updat

13、e the block struct data */int _INNER_HELPER RotateCells(char c44,char blockSize) char temp,i,j; switch(blockSize) case 3: temp=c00; c00=c20, c20=c22, c22=c02, c02=temp; temp=c01; c01=c10, c10=c21, c21=c12, c12=temp; break; case 4: /* only I block arived here! */ c10=1-c10, c12=1-c12, c13=1-c13; c01=

14、1-c01, c21=1-c21, c31=1-c31; break; /* judge if the block can move toward the direction */int CanMove(int dx,int dy) int i,j,tempX,tempY; for(i=0;i;i+) for(j=0;j;j+) ifij) /* cannot move leftward or rightward */ tempX = + i + dx; if(tempX(BoardWidth-1) return false; /* make sure x is valid! */ /* ca

15、nnot move downward */ tempY = + j + dy; if(tempY(BoardHeight-1) return false; /* y is only checked lower bound, maybe negative! */ /* the cell already filled, we must check Ys upper bound before check cell ! */ if(tempY=0 & BoardtempXtempY0) return false; return true;/* judge if the block can rotate

16、 */int CanRotate() int i,j,tempX,tempY; /* update buffer */ memcpy(BufferCells, , 16); RotateCells(BufferCells,; for(i=0;i;i+) for(j=0;j;j+) if(BufferCellsij) tempX=+i; tempY=+j; if(tempX(BoardWidth-1) return false; if(tempY(BoardHeight-1) return false; if(tempY=0 & BoardtempXtempY0) return false; r

17、eturn true;/* draw the block */void _INNER_HELPER DrawBlock(Block *block,int bdLeft,int bdTop,int cellSize) int i,j; setfillstyle(SOLID_FILL,block-color); for(i=0;isize;i+) for(j=0;jsize;j+) if(block-cij & (block-y+j)=0) floodfill( bdLeft+cellSize*(i+block-x)+cellSize/2, bdTop+cellSize*(j+block-y)+c

18、ellSize/2, BorderColor); /* Rotate the block, if success, return true */int RotateBlock(Block *block) char temp,i,j; int b_success; if(block-size=2) return true; if( b_success=CanRotate() EraseBlock(block,BoardLeft,BoardTop,CellSize); memcpy,BufferCells,16); DrawBlock(block,BoardLeft,BoardTop,CellSi

19、ze); return b_success;/* erase a block, only fill the filled cell with background color */void _INNER_HELPER EraseBlock(Block *block,int bdLeft,int bdTop,int cellSize) int i,j; setfillstyle(SOLID_FILL,BkGndColor); for(i=0;isize;i+) for(j=0;jsize;j+) if(block-cij & (block-y+j=0) floodfill( bdLeft+cel

20、lSize*(i+block-x)+cellSize/2, bdTop+cellSize*(j+block-y)+cellSize/2, BorderColor); /* move by the direction if can, donothing if cannot* return value: true - success, false - cannot move toward this direction*/int MoveBlock(Block *block,int dx,int dy) int b_canmove=CanMove(dx,dy); if(b_canmove) Eras

21、eBlock(block,BoardLeft,BoardTop,CellSize); +=dx; +=dy; DrawBlock(block,BoardLeft,BoardTop,CellSize); return b_canmove;/* drop the block to the bottom! */int DropBlock(Block *block) EraseBlock(block,BoardLeft,BoardTop,CellSize); while(CanMove(0,1) +; DrawBlock(block,BoardLeft,BoardTop,CellSize); retu

22、rn 0;/* return value is assign to the blocks alive */* init the graphics mode, draw the board grid */void InitGame() int i,j,gdriver=DETECT,gmode; struct time sysTime; /* draw board cells */ memset(Board,0,BoardWidth*BoardHeight*2); memset,0,16); strcpy(info_help,P: Pause Game. -by hoodlum1980); ini

23、tgraph(&gdriver,&gmode,); setcolor(BorderColor); for(i=0;i=BoardWidth;i+) line(BoardLeft+i*CellSize, BoardTop, BoardLeft+i*CellSize, BoardTop+ BoardHeight*CellSize); for(i=0;i=BoardHeight;i+) line(BoardLeft, BoardTop+i*CellSize, BoardLeft+BoardWidth*CellSize, BoardTop+ i*CellSize); /* draw board out

24、er border rect */ rectangle(BoardLeft-CellSize/4, BoardTop-CellSize/4, BoardLeft+BoardWidth*CellSize+CellSize/4, BoardTop+BoardHeight*CellSize+CellSize/4); /* draw next block grids */ for(i=0;i=4;i+) line(NBBoardLeft+i*NBCellSize, NBBoardTop, NBBoardLeft+i*NBCellSize, NBBoardTop+4*NBCellSize); line(

25、NBBoardLeft, NBBoardTop+i*NBCellSize, NBBoardLeft+4*NBCellSize, NBBoardTop+ i*NBCellSize); /* draw score rect */ rectangle(ScoreBoardLeft,ScoreBoardTop,ScoreBoardLeft+ScoreBoardWidth,ScoreBoardTop+ScoreBoardHeight); DisplayScore(); /* set new seed! */ gettime(&sysTime); srand*3600+*60+; GenerateBloc

26、k(&nextBlock); NextBlock(); /* create first block */ setcolor(DARKGRAY); outtextxy(InfoLeft,InfoTop+20,Up -rotate Space-drop); outtextxy(InfoLeft,InfoTop+35,Left-left Right-right); outtextxy(InfoLeft,InfoTop+50,Esc -exit); DisplayInfo(info_help);/* set the isFilled and fillcolor data to the board */

27、void _INNER_HELPER FillBoardData() int i,j; for(i=0;i;i+) for(j=0;j=0) Board+i+j0=1; Board+i+j1=; /* draw one line of the board */void _INNER_HELPER PaintBoard() int i,j,fillcolor; for(j=max(TopLine-4),0);jBoardHeight;j+) for(i=0;iBoardWidth;i+) fillcolor=Boardij0? Boardij1:BkGndColor; setfillstyle(

28、SOLID_FILL,fillcolor); floodfill(BoardLeft+i*CellSize+CellSize/2,BoardTop+j*CellSize+CellSize/2,BorderColor); /* check if one line if filled full and increase the totalScore! */void _INNER_HELPER CheckBoard() int i,j,k,score=10,sum=0,topy,lines=0; /* we find the top empty line! */ j=topy=BoardHeight-1; do sum=0; for(i=0;i

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1