C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx

上传人:b****6 文档编号:18988342 上传时间:2023-01-02 格式:DOCX 页数:25 大小:20.24KB
下载 相关 举报
C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx_第1页
第1页 / 共25页
C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx_第2页
第2页 / 共25页
C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx_第3页
第3页 / 共25页
C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx_第4页
第4页 / 共25页
C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx

《C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx(25页珍藏版)》请在冰豆网上搜索。

C语言课程设计俄罗斯方块源代码剖析Word文档下载推荐.docx

voiddraw_table();

//(5)其他功能函数

voidkey_down(WPARAMwParam);

voidresize();

voidinitialize();

voidfinalize();

//(6)回调函数,用来处理Windows消息

LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);

//源代码

//1.文件包含

#include<

windows.h>

time.h>

stdio.h>

#include"

tetris.h"

//2.常量定义

#defineAPP_NAME"

TETRIS"

#defineAPP_TITLE"

TetrisGame"

#defineGAMEOVER"

GAMEOVER"

#defineSHAPE_COUNT7

#defineBLOCK_COUNT4

#defineMAX_SPEED5

#defineCOLUMS10

#defineROWS20

#defineREDRGB(255,0,0)

#defineYELLOWRGB(255,255,0)

#defineGRAYRGB(128,128,128)

#defineBLACKRGB(0,0,0)

#defineWHITERGB(255,255,255)

#defineSTONERGB(192,192,192)

#defineCHARS_IN_LINE14

#defineSCORE"

SCORE%4d"

//3.全局变量定义

//

(1)

charscore_char[CHARS_IN_LINE]={0};

//

(2)

char*press_enter="

PressEnterkey..."

;

//(3)帮助提示信息

char*help[]=

"

pressspaceorupkeytotransformshape."

"

Pressleftorrightkeytomovershape."

Pressdownkeytospeedup."

Pressenterkeytopausegame."

Enjoyit.:

-)"

0

};

//(4)枚举游戏的状态

enumgame_state

game_start,

game_run,

game_pause,

game_over,

}state=game_start;

//(5)定义方块的颜色

COLORREFshape_color[]=

RGB(255,0,0),

RGB(0,255,0),

RGB(0,0,255),

RGB(255,255,0),

RGB(0,255,255),

RGB(255,0,255),

RGB(255,255,255)

//(6)方块的7中类型

intshape_coordinate[SHAPE_COUNT][BLOCK_COUNT][2]=

{{0,1},{0,0},{-1,0},{-1,1}},

{{0,-1},{0,0},{1,0},{1,1}},

{{0,-1},{0,0},{0,1},{0,2}},

{{-1,0},{0,0},{1,0},{0,1}},

{{0,0},{1,0},{0,1},{1,1}},

{{-1,-1},{0,-1},{0,0},{0,1}},

{{1,-1},{0,-1},{0,0},{0,1}}

//(7)得分

intscore=0;

//(8)下一个方块

shapenext=0;

//(9)当前方块

shapecurrent=0;

//(10)当前方块的每一部分坐标

intcurrent_coordinate[4][2]={0};

//(11)游戏桌面

inttable[ROWS][COLUMS]={0};

//(12)当前方块的x坐标

intshapex=0;

//(13)当前方块的\y坐标

intshapey=0;

//(14)方块下移速度

intspeed=0;

//(15)每一帧开始时间

clock_tstart=0;

//(16)每一帧结束时间

clock_tfinish=0;

//(17)windows绘图用变量

HWNDgameWND;

HBITMAPmemBM;

HBITMAPmemBMOld;

HDCmemDC;

RECTclientRC;

HBRUSHblackBrush;

HBRUSHstoneBrush;

HBRUSHshapeBrush[SHAPE_COUNT];

HPENgrayPen;

HFONTbigFont;

HFONTsmallFont;

//4.主要处理函数

//

(1)取最大坐标

intmaxX()

inti=0;

intx=current_coordinate[i][0];

intm=x;

for(i=1;

i<

BLOCK_COUNT;

i++)

{

x=current_coordinate[i][0];

if(m<

x)

{

m=x;

}

}

returnm;

}

//

(2)取最小坐标

intminX()

if(m>

//(3)逆时针转动方块

voidturn_left()

intx,y;

for(i=0;

4;

y=current_coordinate[i][1];

current_coordinate[i][0]=y;

current_coordinate[i][1]=-x;

//(4)顺时针旋转方块

voidturn_right()

current_coordinate[i][0]=-y;

current_coordinate[i][1]=x;

//(5)检查方块是否越界

intout_of_table()

x=shapex+current_coordinate[i][0];

y=shapey+current_coordinate[i][1];

if(x<

0||x>

(COLUMS-1)||y>

(ROWS-1))

{

return1;

}

if(table[y][x])

return0;

//(6)旋转方块

voidtransform()

if(current==SquareShape)

return;

turn_right();

if(out_of_table())

turn_left();

//(7)判断方块是否向左移动

intleftable()

=0||table[y][x-1]==1)

return0;

return1;

//(8)判断方块是否向右移动

intrightable()

if(x>

=(COLUMS-1)||table[y][x+1]==1)

//(9)判断方块是否向下移动

intdownable()

if(y>

=(ROWS-1)||table[y+1][x]==1)

//(10)向左移动当前方块

voidmove_left()

if(leftable())

shapex--;

//(11)向右移动当前方块

voidmove_right()

if(rightable())

shapex++;

//(12)向下移动当前方块

voidmove_down()

if(downable())

shapey++;

else

if(add_to_table())

remove_full();

next_shape();

else

state=game_over;

//(13)将当前方块固定到桌面上

intadd_to_table()

if(y<

0||table[y][x]==1)

table[y][x]=1;

//(14)删除填满的行

voidremove_full()

intc=0;

inti,j;

for(i=ROWS-1;

i>

0;

i--)

c=0;

for(j=0;

j<

COLUMS;

j++)

c+=table[i][j];

if(c==COLUMS)

memmove(table[1],table[0],sizeof(int)*COLUMS*i);

memset(table[0],0,sizeof(int)*COLUMS);

score++;

speed=(score/100)%MAX_SPEED;

i++;

elseif(c==0)

break;

//(15)创建新游戏

voidnew_game()

memset(table,0,sizeof(int)*COLUMS*ROWS);

start=clock();

next=random(SHAPE_COUNT);

score=0;

speed=0;

//(16)运行游戏

voidrun_game()

finish=clock();

if((finish-start)>

(MAX_SPEED-speed)*100)

move_down();

start=clock();

InvalidateRect(gameWND,NULL,TRUE);

//(17)操作当前方块

voidnext_shape()

current=next;

memcpy(current_coordinate,shape_coordinate[next],sizeof(int)*BLOCK_COUNT*2);

shapex=(COLUMS-((maxX(current)-minX(current))))/2;

shapey=0;

//(18)取随机数

intrandom(intseed)

if(seed==0)

return0;

srand((unsigned)time(NULL));

return(rand()%seed);

//(19)绘图

voidpaint()

PAINTSTRUCTps;

HDChdc;

draw_table();

hdc=BeginPaint(gameWND,&

ps);

BitBlt(hdc,clientRC.left,clientRC.top,clientRC.right,clientRC.bottom,memDC,0,0,SRCCOPY);

EndPaint(gameWND,&

//(20)绘制游戏桌面

voiddraw_table()

HBRUSHhBrushOld;

HPENhPenOld;

HFONThFontOld;

RECTrc;

intx0,y0,w;

intx,y,i,j;

char*str;

w=clientRC.bottom/(ROWS+2);

x0=y0=w;

FillRect(memDC,&

clientRC,blackBrush);

//如果游戏是开始或结束状态

if(state==game_start||state==game_over)

memcpy(&

rc,&

clientRC,sizeof(RECT));

rc.bottom=rc.bottom/2;

hFontOld=SelectObject(memDC,bigFont);

SetBkColor(memDC,BLACK);

//如果游戏是开始状态,用黄色字显示游戏开始画面

if(state==game_start)

str=APP_TITLE;

SetTextColor(memDC,YELLOW);

//如果游戏是结束状态,用红色字显示GAMEOVER

str=GAMEOVER;

SetTextColor(memDC,RED);

DrawText(memDC,str,strlen(str),&

rc,DT_SINGLELINE|DT_CENTER|DT_BOTTOM);

SelectObject(memDC,hFontOld);

hFontOld=SelectObject(memDC,smallFont);

rc.top=rc.bottom;

rc.bottom=rc.bottom*2;

if(state==game_over)

SetTextColor(memDC,YELLOW);

sprintf(score_char,SCORE,score);

DrawText(memDC,score_char,strlen(score_char),&

rc,DT_SINGLELINE|DT_CENTER|DT_TOP);

SetTextColor(memDC,STONE);

DrawText(memDC,press_enter,strlen(press_enter),&

rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);

return;

//桌面上残留的方块

hBrushOld=SelectObject(memDC,stoneBrush);

for(i=0;

ROWS;

for(j=0;

if(table[i][j]==1)

{

x=x0+j*w;

y=y0+i*w;

Rectangle(memDC,x,y,x+w+1,y+w+1);

}

SelectObject(memDC,hBrushOld);

//画当前的方块

hBrushOld=SelectObject(memDC,shapeBrush[current]);

x=x0+(current_coordinate[i][0]+shapex)*w;

y=y0+(current_coordinate[i][1]+shapey)*w;

if(x<

x0||y<

y0)

continue;

Rectangle(memDC,x,y,x+w+1,y+w+1);

//画桌面上的表格线

hPenOld=SelectObject(memDC,grayPen);

=ROWS;

MoveToEx(memDC,x0,y0+i*w,NULL);

LineTo(memDC,x0+COLUMS*w,y0+i*w);

=COLUMS;

MoveToEx(memDC,x0+i*w,y0,NULL);

LineTo(memDC,x0+i*w,y0+ROWS*w);

SelectObject(memDC,hPenOld);

//画玩家得分

x0=x0+COLUMS*w+3*w;

y0=y0+w;

hFontOld=SelectObject(memDC,smallFont);

SetTextColor(memDC,YELLOW);

sprintf(score_char,SCORE,score);

TextOut(memDC,x0,y0,score_char,strlen(score_char));

//画下一个方块

y0+=w;

SetTextColor(memDC,STONE);

TextOut(memDC,x0,y0,"

NEXT"

4);

x0+=w;

y0+=2*w;

hBrushOld=SelectObject(memDC,shapeBrush[next]);

x=x0+shape_coordinate[next][i][0]*w;

y=y0+shape_coordinate[next][i][1]*w;

//打印帮助信息

x0=(COLUMS+2)*w;

y0+=4*w;

SetTextColor(memDC,GRAY);

i=0;

while(help[i])

TextOut(memDC,x0,y0,help[i],strlen(help[i]));

y0+=w;

SelectObject(memDC,hFontOld);

//(21)处理按键

voidkey_down(WPARAMwParam)

//如果游戏不是运行状态,按下回车键

if(state!

=game_run)

if(wParam==VK_RETURN)

switch(state)

casegame_start:

next_shape();

state=game_run;

break;

casegame_pause:

casegame_over:

new_game();

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

当前位置:首页 > 工程科技 > 兵器核科学

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

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