收藏 分享(赏)

小游戏源代码.doc

上传人:tangtianxu2 文档编号:2861142 上传时间:2018-09-28 格式:DOC 页数:14 大小:92.50KB
下载 相关 举报
小游戏源代码.doc_第1页
第1页 / 共14页
小游戏源代码.doc_第2页
第2页 / 共14页
小游戏源代码.doc_第3页
第3页 / 共14页
小游戏源代码.doc_第4页
第4页 / 共14页
小游戏源代码.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

1、/* Desc: 俄罗斯方块游戏 * By: hoodlum1980 * Email: * Date: 2008.03.12 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 KEYCODESK_ESC =0x011b,K_UP =0x4

2、800, /* 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 tagBlockchar c44; /* cell fill info array, 0-empty, 1-filled */int x; /* block position cx 0,BoardWidht -1 */int y; /* block position cy -4,BoardHeight

3、-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;int NBC

4、ellSize= 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 GameRunning=tru

5、e;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; /* cu

6、rrent 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,int);voi

7、d 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(char *text

8、)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)int key=(random

9、(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-c11=1;block-c12=1, block-c22=1;

10、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, block-c20=1;block-c01=1, block-c11=1;break;case 5

11、: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 NextBlock()/* copy the nextBlock to curBlock */curBlock.size=nextBloc

12、k.size;curBlock.color=nextBlock.color;curBlock.x=(BoardWidth-4)/2;curBlock.y=-curBlock.size;memcpy(curBlock.c,nextBlock.c,16);/* generate nextBlock and show it */EraseBlock(GenerateBlock(nextBlock.x=1,nextBlock.y=0;DrawBlock(/* rotate the block, update the block struct data */int _INNER_HELPER Rotat

13、eCells(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=1-c01, c21=1-c21, c31=1-c31;break;/* judge if the block can

14、move toward the direction */int CanMove(int dx,int dy)int i,j,tempX,tempY;for(i=0;i(BoardWidth-1) return false; /* make sure x is valid! */* cannot move downward */tempY = curBlock.y + j + dy;if(tempY(BoardHeight-1) return false; /* y is only checked lower bound, maybe negative! */* the cell already

15、 filled, we must check Ys upper bound before check cell ! */if(tempY=0 return true;/* judge if the block can rotate */int CanRotate()int i,j,tempX,tempY;/* update buffer */memcpy(BufferCells, curBlock.c, 16);RotateCells(BufferCells,curBlock.size);for(i=0;i(BoardWidth-1)return false;if(tempY(BoardHei

16、ght-1)return false;if(tempY=0 return 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 /* Rotate the block, if success, return true */int RotateBlock(Block *

17、block)char temp,i,j;int b_success;if(block-size=2)return true;if( b_success=CanRotate()EraseBlock(block,BoardLeft,BoardTop,CellSize);memcpy(curBlock.c,BufferCells,16);DrawBlock(block,BoardLeft,BoardTop,CellSize);return b_success;/* erase a block, only fill the filled cell with background color */voi

18、d _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 /* move by the direction if can, donothing if cannot* return value: true - success, false - cannot move toward this direction*/int Move

19、Block(Block *block,int dx,int dy)int b_canmove=CanMove(dx,dy);if(b_canmove)EraseBlock(block,BoardLeft,BoardTop,CellSize);curBlock.x+=dx;curBlock.y+=dy;DrawBlock(block,BoardLeft,BoardTop,CellSize);return b_canmove;/* drop the block to the bottom! */int DropBlock(Block *block)EraseBlock(block,BoardLef

20、t,BoardTop,CellSize);while(CanMove(0,1)curBlock.y+;DrawBlock(block,BoardLeft,BoardTop,CellSize);return 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,

21、0,BoardWidth*BoardHeight*2);memset(nextBlock.c,0,16);strcpy(info_help,“P: Pause Game. -by hoodlum1980“);initgraph(setcolor(BorderColor);for(i=0;i=0)BoardcurBlock.x+icurBlock.y+j0=1;BoardcurBlock.x+icurBlock.y+j1=curBlock.color;/* draw one line of the board */void _INNER_HELPER PaintBoard()int i,j,fi

22、llcolor;for(j=max(TopLine-4),0);j0 /* remove the full filled line (max remove lines count = 4) */dosum=0;for(i=0;i topy;k-)for(i=0;i0 NextBlock();if(!CanMove(0,1)goto GameOver;tick=0;delay(100);switch(key)case K_LEFT:MoveBlock(break;case K_RIGHT:MoveBlock(break;case K_DOWN:MoveBlock(break;case K_UP:

23、RotateBlock(break;case K_SPACE:DropBlock(break;case K_P:PauseGame();break;GameOver:DisplayInfo(“GAME OVER! Press any key to exit!“);getch(); /* wait the user Press any key. */QuitGame();基本思想:定义基本的 7 种方块(含四种状态 )二维数组变量.Terial74. 用数组下标索引方块的种类和状态.定义方块运行的区域(二维数组变量 ) 如(高 23,宽 10)用它存储方块下落后的状态.左移和右移判断是方块是否左右溢出、是否会叠在运行区的格子上。同样改变状态(变形)也得判断是否会出现左右溢出、是否会叠在运行区格子上。下落后,对运行区的空格子进行赋值。

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

当前位置:首页 > 高等教育 > 专业基础教材

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报