main.c
上传用户:fhtm20
上传日期:2022-06-30
资源大小:28k
文件大小:11k
- #include <windows.h>
- #define TIMER 1
- LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
- INT checkleft();
- INT checkright();
- VOID buildfram(HWND);
- VOID restart1(HWND);
- VOID restart2(HWND);
- VOID drawrect(HWND,int,int,COLORREF);
- VOID drawrect2(HWND,int,int,int,COLORREF);
- VOID change(int);
- VOID godown(int,HWND);
- VOID clean(int,HWND);
- VOID gameover(HWND);
- const int rx[28][4]={{-1,0,1,0},{0,0,0,1},{-1,0,1,0},{0,0,0,-1},// _|_
- {0,0,0,1},{-1,0,1,-1},{-1,0,0,0},{-1,0,1,1},// __|
-
- {0,0,0,-1},{-1,0,1,-1},{1,0,0,0},{-1,0,1,1},// |__
-
- {0,0,1,1},{0,0,1,1},{0,0,1,1},{0,0,1,1},// 田
-
- {-1,0,1,2},{0,0,0,0},{-1,0,1,2},{0,0,0,0},// ___
-
- {-1,0,0,1},{0,0,-1,-1},{-1,0,0,1},{0,0,-1,-1},// -|_
-
- {1,0,0,-1},{0,0,1,1},{1,0,0,-1},{0,0,1,1}},// _|-
- //the x of rect ↑ the y of rect ↓
- ry[28][4]={{0,0,0,1},{1,0,-1,0},{0,0,0,-1},{1,0,-1,0},
- {1,0,-1,-1},{0,0,0,-1},{1,1,0,-1},{0,0,0,1},
- {1,0,-1,-1},{0,0,0,1},{1,1,0,-1},{0,0,0,-1},
- {1,0,1,0},{1,0,1,0},{1,0,1,0},{1,0,1,0},
- {0,0,0,0},{1,0,-1,-2},{0,0,0,0},{1,0,-1,-2},
- {0,0,-1,-1},{1,0,0,-1},{0,0,-1,-1},{1,0,0,-1},
- {0,0,-1,-1},{1,0,0,-1},{0,0,-1,-1},{1,0,0,-1}},
- rlen[7]={4,4,4,4,4,4,4};
- const COLORREF rcolor[7]={RGB(150,0,0),RGB(0,150,0),RGB(0,0,150),
- RGB(150,150,0),RGB(150,0,150),RGB(0,150,150),
- RGB(200,200,100)},
- bcolor=RGB(200,200,240);//colorm of background
- const int sx=20, //start x
- sy=320, //start y
- slen=20, //side length
- w=12, //wide of fram
- h=15; //high of fram
- int nrect=0,prect,x=7,y=15,map[15][20],linenum[20],score2,level2;
- long int interval=1000;
- //prect(prepare) is the next one of nrect(now);
- char szClassName[ ] = "WindowsApp";
- int WINAPI
- WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,
- LPSTR lpszArgument, int nFunsterStil)
- {
- HWND hwnd;
- MSG messages;
- WNDCLASSEX wincl;
-
- srand((unsigned)time(NULL));
- prect=(rand()%28);
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = szClassName;
- wincl.lpfnWndProc = WindowProcedure;
- wincl.style = CS_DBLCLKS;
- wincl.cbSize = sizeof (WNDCLASSEX);
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL;
- wincl.cbClsExtra = 0;
- wincl.cbWndExtra = 0;
- wincl.hbrBackground = (HBRUSH)CreateSolidBrush(bcolor);
- if (!RegisterClassEx (&wincl))
- {
- MessageBox(NULL,TEXT("您的RP过高,窗口创建失败,请重试"),TEXT("RP蹦圈错误"),MB_OK);
- return 0;
- }
- hwnd = CreateWindowEx (0,szClassName,"game2_1.01",WS_OVERLAPPEDWINDOW,
- 400,200,400,400,HWND_DESKTOP,NULL,hThisInstance,NULL);
- ShowWindow (hwnd, nFunsterStil);
- UpdateWindow(hwnd);
- while (GetMessage (&messages, NULL, 0, 0))
- {
- TranslateMessage(&messages);
- DispatchMessage(&messages);
- }
- return messages.wParam;
- }
- LRESULT CALLBACK
- WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- RECT rt;
- int i,j;
- switch (message)
- {
- case WM_CREATE:
- for(i=1;i<=w;i++)
- for(j=1;j<=h+4;j++)map[i][j]=-1;
- for(i=0;i<=w+1;i++)map[i][0]=1;
- // for(i=0;i<=w+1;i++)map[i][h+1]=1;
- for(j=0;j<=h+1;j++)map[0][j]=1;
- for(j=0;j<=h+1;j++)map[w+1][j]=1;
- for(j=0;j<=h+1;j++)linenum[j]=0;
- restart2(hwnd);
- restart1(hwnd);
- SetTimer(hwnd,TIMER,interval-level2*100,NULL);
- break;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- buildfram(hwnd);
- restart1(hwnd);
- drawrect2(hwnd,nrect,x,y,rcolor[nrect/4]);
- EndPaint(hwnd, &ps);
- break;
- case WM_DESTROY:
- KillTimer(hwnd,TIMER);
- PostQuitMessage (0);
- break;
- case WM_TIMER:
- // MessageBox(NULL,"!","!",MB_OK);
- godown(nrect,hwnd);
- break;
- case WM_KEYDOWN:
- // drawrect2(hwnd,nrect,x,y,bcolor);
- // nrect=nrect/4*4+(nrect%4+1)%4;
- // drawrect2(hwnd,nrect,x,y,rcolor[nrect/4]);
- switch(wParam)
- {
- case VK_LEFT:
- if(checkleft(nrect))
- {
- drawrect2(hwnd,nrect,x,y,bcolor);
- x--;
- drawrect2(hwnd,nrect,x,y,rcolor[nrect/4]);
- }
- break;
- case VK_RIGHT:
- if(checkright(nrect))
- {
- drawrect2(hwnd,nrect,x,y,bcolor);
- x++;
- drawrect2(hwnd,nrect,x,y,rcolor[nrect/4]);
- }
- break;
- case VK_UP:
- drawrect2(hwnd,nrect,x,y,bcolor);
- change(nrect/4*4+(nrect%4+1)%4);
- drawrect2(hwnd,nrect,x,y,rcolor[nrect/4]);
- break;
- case VK_DOWN:
- KillTimer(hwnd,TIMER);
- godown(nrect,hwnd);
- SetTimer(hwnd,TIMER,interval-level2*100,NULL);
- break;
- }
- break;
- default:
- return DefWindowProc (hwnd, message, wParam, lParam);
- }
- return 0;
- }
- VOID buildfram(HWND hwnd) //初始化框架
- {
- //the map is 12(W240)*15(H300)
- HDC hdc;
- RECT rect;
- HBRUSH hbrush;
- TCHAR *next="NEXT:",*score="SCORE:",*level="LEVEL:",*exp="EXP:",
- *AD="俄罗斯方块1.01版 幻梦帝国荣誉出品";
- hdc=GetDC(hwnd);
- hbrush=(HBRUSH)CreateSolidBrush(RGB(0,0,0));
- SetRect(&rect,18,18,260,20);
- FillRect(hdc,&rect,hbrush);
- SetRect(&rect,18,18,20,320);
- FillRect(hdc,&rect,hbrush);
- SetRect(&rect,18,320,260,322);
- FillRect(hdc,&rect,hbrush);
- SetRect(&rect,260,18,262,322);
- FillRect(hdc,&rect,hbrush);
- SetRect(&rect,290,30,370,50);
- DrawText(hdc,next,lstrlen(next),&rect,DT_LEFT);
- SetRect(&rect,290,150,370,170);
- DrawText(hdc,score,lstrlen(score),&rect,DT_LEFT);
- SetRect(&rect,290,200,370,220);
- DrawText(hdc,level,lstrlen(level),&rect,DT_LEFT);
- SetRect(&rect,290,250,370,270);
- DrawText(hdc,exp,lstrlen(exp),&rect,DT_LEFT);
- SetRect(&rect,20,340,300,360);
- DrawText(hdc,AD,lstrlen(AD),&rect,DT_LEFT);
- DeleteObject(hbrush);
- ReleaseDC(hwnd,hdc);
- }
- VOID restart1(HWND hwnd)
- {
- HDC hdc;
- RECT rect;
- TCHAR s1[20]="",ss[20]="";
- int i,j;
- for(i=1;i<=12;i++)
- for(j=1;j<=15;j++)
- if(map[i][j]>=0)drawrect(hwnd,i,j,rcolor[map[i][j]]);
- else drawrect(hwnd,i,j,bcolor);
- hdc=GetDC(hwnd);
- drawrect2(hwnd,nrect,15,12,bcolor);
- drawrect2(hwnd,prect,15,12,rcolor[prect/4]);
- level2=score2/100;
- itoa(level2,s1,10);
- SetRect(&rect,290,225,370,245);
- DrawText(hdc,s1,lstrlen(s1),&rect,DT_LEFT);
- itoa(score2,s1,10);
- SetRect(&rect,290,175,370,195);
- DrawText(hdc,s1,lstrlen(s1),&rect,DT_LEFT);
- i=level2*100+100;
- itoa(i,ss,10);
- lstrcat(s1,"/");
- lstrcat(s1,ss);
- SetRect(&rect,290,275,370,295);
- DrawText(hdc,s1,lstrlen(s1),&rect,DT_LEFT);
- ReleaseDC(hwnd,hdc);
- }
- VOID restart2(HWND hwnd)
- {
- nrect=prect;
- srand((unsigned)time(NULL));
- prect=(rand()%28);
- prect=(rand()%28);
- x=7;
- y=16;
- }
- VOID drawrect(HWND hwnd,int nx,int ny,COLORREF color)
- {
- HDC hdc;
- RECT rect;
- HBRUSH hbrush;
- hdc=GetDC(hwnd);
- hbrush=(HBRUSH)CreateSolidBrush(color);
- SetRect(&rect,sx+(nx-1)*slen+1,sy-ny*slen+1,sx+(nx)*slen-1,sy-(ny-1)*slen-1);
- FillRect(hdc,&rect,hbrush);
- DeleteObject(hbrush);
- ReleaseDC(hwnd,hdc);
- }
- VOID drawrect2(HWND hwnd,int num,int nx,int ny,COLORREF color)
- {
- int i;
- for(i=0;i<rlen[(int)(num/4)];i++)
- if(ny+ry[num][i]<16)
- drawrect(hwnd,nx+rx[num][i],ny+ry[num][i],color);
- }
- BOOL checkleft(int v)
- {
- int i;
- for(i=0;i<rlen[v/4];i++)
- if(map[x+rx[v][i]-1][y+ry[v][i]]!=-1)return(0);
- return(1);
- }
- BOOL checkright(int v)
- {
- int i;
- for(i=0;i<rlen[v/4];i++)
- if(map[x+rx[v][i]+1][y+ry[v][i]]!=-1)return(0);
- return(1);
- }
- VOID change(int v)
- {
- int bl,br,bb,i; //b means bool,and l,left,r,right,b,bottom
- bl=br=bb=0;
- for(i=0;i<rlen[v/4];i++)
- if(map[x+rx[v][i]][y+ry[v][i]]!=-1)
- {
- if((rx[v][i]<0)&&(rx[v][i]<bl))bl=rx[v][i];
- if((rx[v][i]>0)&&(rx[v][i]>br))br=rx[v][i];
- if(ry[v][i]<0)bb=1;
- }
- if((bl!=0&&br!=0)||bb!=0)return;
- nrect=v;
- if(bl!=0)x-=bl;
- if(br!=0)x-=br;
- }
- VOID godown(int v,HWND hwnd)
- {
- int i,nx,ny,bb,bf; //bf means bool full;
- // TCHAR ss[60]="";
- drawrect2(hwnd,nrect,x,y,bcolor);
- bb=bf=0;
- nx=x;
- ny=y-1;
- for(i=0;i<rlen[v/4];i++)
- if(map[nx+rx[v][i]][ny+ry[v][i]]!=-1)bb=1;
- if(bb)
- {
- for(i=0;i<rlen[v/4];i++)
- if(y+ry[v][i]>15)
- {
- gameover(hwnd);
- return;
- }
-
- for(i=0;i<rlen[nrect/4];i++)
- {
- map[x+rx[nrect][i]][y+ry[nrect][i]]=nrect/4;
- linenum[y+ry[nrect][i]]+=1;
- if(linenum[y+ry[nrect][i]]>11)bf=1;
- }
- if(bf)clean(nrect,hwnd);
- restart2(hwnd);
- restart1(hwnd);
- drawrect2(hwnd,nrect,x,y,rcolor[nrect/4]);
- return;
- }
- y--;
- drawrect2(hwnd,nrect,x,y,rcolor[nrect/4]);
- }
- VOID clean(int v,HWND hwnd)
- {
- int i,j,j2,k,num;
- num=0;
- for(i=0;i<rlen[v/4];i++)
- if(linenum[y+ry[v][i]]>11)
- {
- for(j=1;j<=12;j++)map[j][y+ry[v][i]]=-1;
- linenum[y+ry[v][i]]=0;
- num++;
- }
- restart1(hwnd);
- for(k=0;k<num;k++)
- for(j=1;j<15;j++)
- if(linenum[j]==0)
- {
- for(j2=j;j2<15;j2++)
- {
- for(i=1;i<=12;i++)map[i][j2]=map[i][j2+1];
- linenum[j2]=linenum[j2+1];
- linenum[j2+1]=0;
- }
- }
-
- //score...
- switch(num)
- {
- case 1:
- score2+=10;
- break;
- case 2:
- score2+=30;
- break;
- case 3:
- score2+=50;
- break;
- case 4:
- score2+=90;
- break;
- }
- if(score2>490)
- MessageBox(NULL,TEXT("恭喜您通关"),TEXT("通关成功"),MB_OK);
- }
- VOID gameover(HWND hwnd)
- {
- KillTimer(hwnd,TIMER);
- MessageBox(NULL,"GAMEOVER","GAMEOVER",MB_OK);
- return;
- }