C语言实战俄罗斯方块子函数.docx

上传人:b****7 文档编号:26183453 上传时间:2023-06-17 格式:DOCX 页数:23 大小:18.45KB
下载 相关 举报
C语言实战俄罗斯方块子函数.docx_第1页
第1页 / 共23页
C语言实战俄罗斯方块子函数.docx_第2页
第2页 / 共23页
C语言实战俄罗斯方块子函数.docx_第3页
第3页 / 共23页
C语言实战俄罗斯方块子函数.docx_第4页
第4页 / 共23页
C语言实战俄罗斯方块子函数.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

C语言实战俄罗斯方块子函数.docx

《C语言实战俄罗斯方块子函数.docx》由会员分享,可在线阅读,更多相关《C语言实战俄罗斯方块子函数.docx(23页珍藏版)》请在冰豆网上搜索。

C语言实战俄罗斯方块子函数.docx

C语言实战俄罗斯方块子函数

#include"elsfkhead.h"

//背景数组

charg_arrBackGroud[20][10]={0};

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

intg_nSqareID=-1;

intg_nLine=-1;

intg_nList=-1;

intg_nScore=0;

voidonpaint(HDChdc)//函数实现

{

//创建兼容性DC

HDChMemDC=CreateCompatibleDC(hdc);//HMemDC一张纸的编号

HBITMAPhbitmapback=CreateCompatibleBitmap(hdc,500,600);//创建一张纸

SelectObject(hMemDC,hbitmapback);//将编号与纸关联起来

paintsqare(hMemDC);ShowSqare2(hMemDC);

ShowScore(hMemDC);

BitBlt(hdc,0,0,500,600,hMemDC,0,0,SRCCOPY);//传递

DeleteObject(hbitmapback);//释放位图

DeleteDC(hMemDC);//释放DC

}

voidShowScore(HDChMemDC)

{

charstrScore[10]={0};

Rectangle(hMemDC,300,0,500,600);

itoa(g_nScore,strScore,10);

TextOut(hMemDC,400,10,"得分",strlen("得分"));

TextOut(hMemDC,400,32,strScore,strlen(strScore));

}

voidonCreate()

{

srand((unsignedint)time(NULL));

CreateRandomSqare();

CopySqareToBack();

}

voidpaintsqare(HDChMemDC)

{

inti=0,

j=0;

Rectangle(hMemDC,0,0,300,600);//窗口上的内容,大方块

for(i=0;i<20;i++)

{

for(j=0;j<10;j++)

{

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

{

Rectangle(hMemDC,j*30,i*30,j*30+30,i*30+30);//画小格子

}

}

}

}

intCreateRandomSqare()//创建随机快

{

intnInedx=rand()%7;

switch(nInedx)

{

case0:

g_arrsqare[0][0]=1,g_arrsqare[0][1]=1,g_arrsqare[0][2]=0,g_arrsqare[0][3]=0;

g_arrsqare[1][0]=0,g_arrsqare[1][1]=1,g_arrsqare[1][2]=1,g_arrsqare[1][3]=0;

g_nLine=0;

g_nList=3;

break;

case1:

g_arrsqare[0][0]=0,g_arrsqare[0][1]=1,g_arrsqare[0][2]=1,g_arrsqare[0][3]=0;

g_arrsqare[1][0]=1,g_arrsqare[1][1]=1,g_arrsqare[1][2]=0,g_arrsqare[1][3]=0;

g_nLine=0;

g_nList=3;

break;

case2:

g_arrsqare[0][0]=1,g_arrsqare[0][1]=0,g_arrsqare[0][2]=0,g_arrsqare[0][3]=0;

g_arrsqare[1][0]=1,g_arrsqare[1][1]=1,g_arrsqare[1][2]=1,g_arrsqare[1][3]=0;

g_nLine=0;

g_nList=3;

break;

case3:

g_arrsqare[0][0]=0,g_arrsqare[0][1]=0,g_arrsqare[0][2]=1,g_arrsqare[0][3]=0;

g_arrsqare[1][0]=1,g_arrsqare[1][1]=1,g_arrsqare[1][2]=1,g_arrsqare[1][3]=0;

g_nLine=0;

g_nList=3;

break;

case4:

g_arrsqare[0][0]=0,g_arrsqare[0][1]=1,g_arrsqare[0][2]=0,g_arrsqare[0][3]=0;

g_arrsqare[1][0]=1,g_arrsqare[1][1]=1,g_arrsqare[1][2]=1,g_arrsqare[1][3]=0;

g_nLine=0;

g_nList=3;

break;

case5:

g_arrsqare[0][0]=0,g_arrsqare[0][1]=1,g_arrsqare[0][2]=1,g_arrsqare[0][3]=0;

g_arrsqare[1][0]=0,g_arrsqare[1][1]=1,g_arrsqare[1][2]=1,g_arrsqare[1][3]=0;

g_nLine=0;

g_nList=4;

break;

case6:

g_arrsqare[0][0]=1,g_arrsqare[0][1]=1,g_arrsqare[0][2]=1,g_arrsqare[0][3]=0;

g_arrsqare[1][0]=0,g_arrsqare[1][1]=0,g_arrsqare[1][2]=0,g_arrsqare[1][3]=0;

g_nLine=0;

g_nList=3;

break;

}

g_nSqareID=nInedx;

returnnInedx;

}

voidCopySqareToBack()

{

inti=0,

j=0;

for(i=0;i<2;i++)

{

for(j=0;j<4;j++)

{

g_arrBackGroud[i][j+3]=g_arrsqare[i][j];

}

}

}

voidonReturn(HWNDhwnd)

{

SetTimer(hwnd,DEF_TIMER1,500,NULL);

}

voidsqareDwon()

{

inti=0,

j=0;

for(i=19;i>=0;i--)

{

for(j=0;j<10;j++)

{

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

{

g_arrBackGroud[i+1][j]=g_arrBackGroud[i][j];

g_arrBackGroud[i][j]=0;

}

}

}

}

//定时器响应函数

voidOnTimer(HWNDhwnd)

{

HDChdc=GetDC(hwnd);//内核对象

if(1==CanSqaerDown()&&1==CanSqaerDown2())

{

sqareDwon();//调用下落

g_nLine++;

}

else

{

Change1To2();//1变2

DestroyOneLineSqare();

if(0==CanGameOver())

{

//结束

KillTimer(hwnd,DEF_TIMER1);

return;

}

CreateRandomSqare();//产生随机快

CopySqareToBack();//复制到背景

}

//调用显示

paintsqare(hdc);

onpaint(hdc);

ReleaseDC(hwnd,hdc);//内核对象释放

}

intCanGameOver()

{

inti=0;

for(i=0;i<10;i++)

{

if(

2==g_arrBackGroud[0][i])

{

MessageBox(NULL,"GameOver","提示",MB_OK);

return0;

}

}

return1;

}

intCanSqaerDown()//方块停在最底层,返回0不可以下落,1可以下落

{

inti=0;

for(i=0;i<10;i++)

{

if(1==g_arrBackGroud[19][i])

{

return0;

}

}

return1;

}

voidChange1To2()//1变2

{

inti=0,

j=0;

for(i=0;i<20;i++)

{

for(j=0;j<10;j++)

{

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

{

g_arrBackGroud[i][j]=2;

}

}

}

}

//显示2

voidShowSqare2(HDChMemDC)

{

inti=0,

j=0;

HBRUSHhOldBrush;

HBRUSHhNewBrush=CreateSolidBrush(RGB(244,38,2));

hOldBrush=(HBRUSH)SelectObject(hMemDC,hNewBrush);

for(i=0;i<20;i++)

{

for(j=0;j<10;j++)

{

if(2==g_arrBackGroud[i][j])

{

Rectangle(hMemDC,j*30,i*30,j*30+30,i*30+30);//画小格子

}

}

}

hNewBrush=(HBRUSH)SelectObject(hMemDC,hOldBrush);

DeleteObject(hNewBrush);

}

intCanSqaerDown2()//解决方块下落都停在最底下的问题

{

inti=0,

j=0;

for(i=19;i>0;i--)

{

for(j=0;j<10;j++)

{

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

{

if(2==g_arrBackGroud[i+1][j])

{

return0;

}

}

}

}

return1;

}

voidOnLeft(HWNDhwnd)

{

if(1==CanSqaerLeft()&&1==CanSqaerLeft2())

{

HDChdc=GetDC(hwnd);

//方块左移

sqareLeft();

g_nList--;

//显示方块

onpaint(hdc);

ReleaseDC(hwnd,hdc);

}

}

voidsqareLeft()

{

inti=0,

j=0;

for(i=0;i<20;i++)

{

for(j=0;j<10;j++)

{

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

{

g_arrBackGroud[i][j-1]=g_arrBackGroud[i][j];

g_arrBackGroud[i][j]=0;

}

}

}

}

intCanSqaerLeft()

{

inti=0;

for(i=0;i<20;i++)

{

if(1==g_arrBackGroud[i][0])

{

return0;

}

}

return1;

}

intCanSqaerLeft2()

{

inti=0,

j=0;

for(i=0;i<20;i++)

{

for(j=0;j<10;j++)

{

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

{

if(2==g_arrBackGroud[i][j-1])

{

return0;

}

}

}

}

return1;

}

voidOnRight(HWNDhwnd)

{

if(1==CanSqaerRight()&&1==CanSqaerRight2())

{

HDChdc=GetDC(hwnd);

//方块右移

g_nList++;

sqareRight();

onpaint(hdc);

ReleaseDC(hwnd,hdc);

}

}

voidsqareRight()

{

inti=0,

j=0;

for(i=0;i<20;i++)

{

for(j=9;j>=0;j--)

{

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

{

g_arrBackGroud[i][j+1]=g_arrBackGroud[i][j];

g_arrBackGroud[i][j]=0;

}

}

}

}

intCanSqaerRight()

{

inti=0;

for(i=0;i<20;i++)

{

if(1==g_arrBackGroud[i][9])

{

return0;

}

}

return1;

}

intCanSqaerRight2()

{

inti=0,

j=0;

for(i=0;i<20;i++)

{

for(j=9;j>=0;j--)

{

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

if(2==g_arrBackGroud[i][j+1])

{

return0;

}

}

}

return1;

}

voidOnDown(HWNDhwnd)

{

OnTimer(hwnd);

}

voidOnChangeSqare(HWNDhwnd)

{

HDChdc=GetDC(hwnd);

switch(g_nSqareID)

{

case0:

case1:

case2:

case3:

case4:

//普通变形

if(1==CanSqaerChangeShape())

{

ChangeSqare();

}

else

{

return;

}

break;

case5:

//正方形

return;

case6:

//长条

//变形

if(CanLineSqaerChange())

{

ChangeLineSqare();

}

break;

}

onpaint(hdc);

ReleaseDC(hwnd,hdc);

}

voidChangeSqare()

{

inti=0,

j=0;

intnTemp=2;

chararrSqare[3][3]={0};

//背景块复制出来

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

arrSqare[i][j]=g_arrBackGroud[g_nLine+i][g_nList+j];//将背景当中的方块传递到小口袋

}

}

//变形后的方块数组复制回去

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

g_arrBackGroud[g_nLine+i][g_nList+j]=arrSqare[nTemp][i];

nTemp--;

}

nTemp=2;

}

}

intCanSqaerChangeShape()

{

inti=0,

j=0;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

if(2==g_arrBackGroud[g_nLine+i][g_nList+j])

{

return0;

}

}

}

//if(g_nList<0||g_nList+2>9)

//{

//return0;

//}

if(g_nList<0)

{

g_nList=0;

}

elseif(g_nList+2>9)

{

g_nList=7;

}

return1;

}

voidChangeLineSqare()

{

if(1==g_arrBackGroud[g_nLine][g_nList-1])

{//清零

g_arrBackGroud[g_nLine][g_nList-1]=0;

g_arrBackGroud[g_nLine][g_nList+1]=0;

g_arrBackGroud[g_nLine][g_nList+2]=0;

if(2==g_arrBackGroud[g_nLine+1][g_nList])

{

g_arrBackGroud[g_nLine-1][g_nList]=1;

g_arrBackGroud[g_nLine-2][g_nList]=1;

g_arrBackGroud[g_nLine-3][g_nList]=1;

}

elseif(2==g_arrBackGroud[g_nLine+2][g_nList])

{

g_arrBackGroud[g_nLine+1][g_nList]=1;

g_arrBackGroud[g_nLine-1][g_nList]=1;

g_arrBackGroud[g_nLine-2][g_nList]=1;

}

else

{

//元素赋值

g_arrBackGroud[g_nLine-1][g_nList]=1;

g_arrBackGroud[g_nLine+1][g_nList]=1;

g_arrBackGroud[g_nLine+2][g_nList]=1;

}

}

else

{

g_arrBackGroud[g_nLine-1][g_nList]=0;

g_arrBackGroud[g_nLine+1][g_nList]=0;

g_arrBackGroud[g_nLine+2][g_nList]=0;

if(2==g_arrBackGroud[g_nLine][g_nList+1]||9==g_nList)

{

g_arrBackGroud[g_nLine][g_nList-1]=1;

g_arrBackGroud[g_nLine][g_nList-2]=1;

g_arrBackGroud[g_nLine][g_nList-3]=1;

g_nList=g_nList-2;

}

elseif(2==g_arrBackGroud[g_nLine][g_nList+2]||8==g_nList)

{

g_arrBackGroud[g_nLine][g_nList+1]=1;

g_arrBackGroud[g_nLine][g_nList-1]=1;

g_arrBackGroud[g_nLine][g_nList-2]=1;

g_nList=g_nList-1;

}

elseif(2==g_arrBackGroud[g_nLine][g_nList-1]||0==g_nList)

{

g_arrBackGroud[g_nLine][g_nList+1]=1;

g_arrBackGroud[g_nLine][g_nList+2]=1;

g_arrBackGroud[g_nLine][g_nList+3]=1;

g_nList=g_nList+1;

}

else

{

//元素复制

g_arrBackGroud[g_nLine][g_nList-1]=1;

g_arrBackGroud[g_nLine][g_nList+1]=1;

g_arrBack

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

当前位置:首页 > 幼儿教育 > 家庭教育

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

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