俄罗斯方块(C++)
2014-11-21 13:57
148 查看
主函数
头文件blocks.h
struct Shape
{
//0代表方形,1代表L型,2代表反L型,3代表长条型,4代表T型,5代表Z型,6代表反Z型
int type;
int x,y;
int x1,y1;
int x2,y2;
int x3,y3;
int state;
bool moveDown(int map[22][10])
{
bool can=true;
int X[4];X[0]=x;X[1]=x1;X[2]=x2;X[3]=x3;
int Y[4];Y[0]=y;Y[1]=y1;Y[2]=y2;Y[3]=y3;
for(int i=0;i<4;i++)
{
if(X[i]==0)
{
return false;
}
if(map[X[i]-1][Y[i]]!=0)
{
bool overlap=false;
for(int j=0;j<4;j++)
{
if(X[i]-1==X[j]&&Y[i]==Y[j])
{
overlap=true;break;
}
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<4;i++)
{
map[X[i]-1][Y[i]]=1;
}
x--;x1--;x2--;x3--;
return true;
}
else
{
return false;
}
}
bool moveLeft(int map[22][10])
{
bool can=true;
int X[4];X[0]=x;X[1]=x1;X[2]=x2;X[3]=x3;
int Y[4];Y[0]=y;Y[1]=y1;Y[2]=y2;Y[3]=y3;
for(int i=0;i<4;i++)
{
if(Y[i]==0)
{
return false;
}
if(map[X[i]][Y[i]-1]!=0)
{
bool overlap=false;
for(int j=0;j<4;j++)
{
if(X[i]==X[j]&&Y[i]-1==Y[j])
{
overlap=true;break;
}
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]-1]=1;
}
y--;y1--;y2--;y3--;
return true;
}
else
{
return false;
}
}
bool moveRight(int map[22][10])
{
bool can=true;
int X[4];X[0]=x;X[1]=x1;X[2]=x2;X[3]=x3;
int Y[4];Y[0]=y;Y[1]=y1;Y[2]=y2;Y[3]=y3;
for(int i=0;i<4;i++)
{
if(Y[i]==9)
{
return false;;
}
if(map[X[i]][Y[i]+1]!=0)
{
bool overlap=false;
for(int j=0;j<4;j++)
{
if(X[i]==X[j]&&Y[i]+1==Y[j])
{
overlap=true;break;
}
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]+1]=1;
}
y++;y1++;y2++;y3++;
return true;
}
else
{
return false;
}
}
bool rotate(int map[22][10])
{
bool can=true;
int X[3];X[0]=x1;X[1]=x2;X[2]=x3;
int Y[3];Y[0]=y1;Y[1]=y2;Y[2]=y3;
for(int i=0;i<3;i++)
{
int p=X[i]-x,q=Y[i]-y;
if(x-q<0||y+p<0||y+p>9)
return false;
if(map[x-q][y+p]!=0)
{
bool overlap=false;
for(int j=0;j<3;j++)
{
if(x-q==X[j]&&y+p==Y[j])
overlap=true;
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<3;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<3;i++)
{
int p=X[i]-x,q=Y[i]-y;
map[x-q][y+p]=1;
}
int p,q;
p=x1-x;q=y1-y;x1=x-q;y1=y+p;
p=x2-x;q=y2-y;x2=x-q;y2=y+p;
p=x3-x;q=y3-y;x3=x-q;y3=y+p;
}
else
return false;
}
};
有空再详细分析
#include<Windows.h> #include"blocks.h" #include <cstdlib> #include<ctime> #define window_width 250 #define window_height 400 HDC g_hdc=NULL,g_mdc=NULL,g_bufdc=NULL;//全局设备句柄 HBITMAP g_background,pic_block; DWORD g_tPre=0,g_tNow=0; int map[22][10];//前一个代表行,后一个代表列 Shape shape; bool gameover=false; int speed; LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam ); bool Game_Init(HWND hwnd);//资源初始化 void Game_Paint(HWND hwnd);//绘图代码 bool Game_CleanUp(HWND hwnd);//资源清理 bool createBlock();//创建一个新的方格 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { WNDCLASSEX wndClass={0}; wndClass.cbSize=sizeof(WNDCLASSEX); wndClass.style=CS_HREDRAW|CS_VREDRAW; wndClass.lpfnWndProc=WndProc; wndClass.cbClsExtra=0; wndClass.cbWndExtra=0; wndClass.hInstance=hInstance; wndClass.hIcon=(HICON)::LoadImage(NULL,L"microsoft.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);//图标 wndClass.hCursor=LoadCursor(NULL,IDC_ARROW); wndClass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);//背景 wndClass.lpszMenuName=NULL; wndClass.lpszClassName=L"crw";//窗口类的名字 if(!RegisterClassEx(&wndClass))return -1;//注册窗口 HWND hwnd=CreateWindow(L"crw",L"Russian Block",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,window_width,window_height,NULL,NULL,hInstance,NULL);//窗口正式创建 MoveWindow(hwnd,200,50,window_width,window_height,true);//显示窗口的位置 ShowWindow(hwnd,nShowCmd); UpdateWindow(hwnd); if(!Game_Init(hwnd)) { MessageBox(hwnd,L"资源初始化失败",L"消息窗口",0); return FALSE; } //消息循环过程 MSG msg={0}; while(msg.message!=WM_QUIT) { if(PeekMessage(&msg,0,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { g_tNow=GetTickCount(); if(g_tNow-g_tPre>=1000/speed&&!gameover)Game_Paint(hwnd); } } //窗口类的注销 UnregisterClass(L"ForTheDreamOfDevelop",wndClass.hInstance); return 0; } //窗口过程函数WndProc LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_DESTROY: Game_CleanUp(hwnd); PostQuitMessage(0); break; case WM_KEYDOWN: switch(wParam) { case VK_RIGHT: shape.moveRight(map); break; case VK_LEFT: shape.moveLeft(map); break; case VK_UP: if(shape.type!=0) shape.rotate(map); break; case VK_DOWN: speed=10; break; }break; case WM_KEYUP: speed=5; break; default: return DefWindowProc(hwnd,message,wParam,lParam); } return 0; } //初始化函数 bool Game_Init(HWND hwnd) { HBITMAP bmp; g_hdc=GetDC(hwnd);//获取设备环境句柄 g_mdc=CreateCompatibleDC(g_hdc); g_bufdc=CreateCompatibleDC(g_hdc); bmp=CreateCompatibleBitmap(g_hdc,window_width,window_height); SelectObject(g_mdc,bmp); g_background=(HBITMAP)LoadImage(NULL,L"blue.bmp",IMAGE_BITMAP,window_width,window_height,LR_LOADFROMFILE); pic_block=(HBITMAP)LoadImage(NULL,L"block.bmp",IMAGE_BITMAP,20,20,LR_LOADFROMFILE); //地图初始化 for(int i=0;i<22;i++) { for(int j=0;j<10;j++) { map[i][j]=0; } } //时间种子初始化 srand(time(NULL)); createBlock(); speed=5; return true; } //绘制函数 void Game_Paint(HWND hwnd) { SelectObject(g_bufdc,g_background); BitBlt(g_mdc,0,0,window_width,window_height,g_bufdc,0,0,SRCCOPY); SelectObject(g_bufdc,pic_block); for(int i=0;i<17;i++) { for(int j=0;j<10;j++) { if(map[i][j]!=0) BitBlt(g_mdc,j*20,(17-i)*20,20,20,g_bufdc,0,0,SRCCOPY); } } gameover=false; if(!shape.moveDown(map)) { for(int i=0;i<17;i++) { bool erase=true; for(int j=0;j<10;j++) { if(map[i][j]==0) erase=false; } if(erase) { for(int j=0;j<10;j++) { map[i][j]=0; } } } for(int k=0;k<4;k++) { bool exist=false; for(int i=0;i<17;i++) { bool erase=true; for(int j=0;j<10;j++) { if(map[i][j]==1) erase=false; } if(erase) { for(int j=0;j<10;j++) { map[i][j]=map[i+1][j]; map[i+1][j]=0; } } } } if(!createBlock()) gameover=true; } if(gameover) MessageBox(0,L"game over!",L"message",MB_OK); //一起画到g_hdc上 BitBlt(g_hdc,0,0,window_width,window_height,g_mdc,0,0,SRCCOPY); g_tPre=GetTickCount(); } //资源清理函数 bool Game_CleanUp(HWND hwnd) { DeleteObject(g_background); DeleteDC(g_bufdc); DeleteDC(g_mdc); ReleaseDC(hwnd,g_hdc); return true; } bool createBlock() { //0代表方形,1代表L型,2代表反L型,3代表长条型,4代表T型,5代表Z型,6代表反Z型 int type=0; type=rand()%6; switch(type) { //正方形 case 0: shape.type=0; shape.y=rand()%9;shape.x=18; shape.x1=shape.x;shape.y1=shape.y+1; shape.x2=shape.x+1;shape.y2=shape.y+1; shape.x3=shape.x+1;shape.y3=shape.y; break; case 1: shape.type=1; shape.y=rand()%9;shape.x=19;shape.state=0; shape.x1=shape.x-1;shape.y1=shape.y; shape.x2=shape.x-1;shape.y2=shape.y+1; shape.x3=shape.x+1;shape.y3=shape.y; break; case 2: shape.type=2;shape.y=rand()%9+1;shape.x=19;shape.state=0; shape.x1=shape.x+1;shape.y1=shape.y; shape.x2=shape.x-1;shape.y2=shape.y-1; shape.x3=shape.x-1;shape.y3=shape.y; break; case 3: shape.type=3;shape.y=rand()%10;shape.x=20;shape.state=0; shape.x1=shape.x+1;shape.y1=shape.y; shape.x2=shape.x-2;shape.y2=shape.y; shape.x3=shape.x-1;shape.y3=shape.y; break; case 4: shape.type=4;shape.y=1+rand()%8;shape.x=19;shape.state=0; shape.x1=shape.x;shape.y1=shape.y+1; shape.x2=shape.x+1;shape.y2=shape.y; shape.x3=shape.x;shape.y3=shape.y-1; break; case 5: shape.type=5;shape.y=rand()%9;shape.x=19;shape.state=0; shape.x1=shape.x-1;shape.y1=shape.y+1; shape.x2=shape.x;shape.y2=shape.y+1; shape.x3=shape.x+1;shape.y3=shape.y; break; case 6: shape.type=6;shape.y=rand()%9;shape.x=19;shape.state=0; shape.x1=shape.x-1;shape.y1=shape.y; shape.x2=shape.x;shape.y2=shape.y+1; shape.x3=shape.x+1;shape.y3=shape.y+1; break; } if(map[shape.x][shape.y]==1||map[shape.x1][shape.y1]==1||map[shape.x2][shape.y2]==1||map[shape.x3][shape.y3]==1) return false; else map[shape.x][shape.y]=1;map[shape.x1][shape.y1]=1;map[shape.x2][shape.y2]=1;map[shape.x3][shape.y3]=1; return true; }
头文件blocks.h
struct Shape
{
//0代表方形,1代表L型,2代表反L型,3代表长条型,4代表T型,5代表Z型,6代表反Z型
int type;
int x,y;
int x1,y1;
int x2,y2;
int x3,y3;
int state;
bool moveDown(int map[22][10])
{
bool can=true;
int X[4];X[0]=x;X[1]=x1;X[2]=x2;X[3]=x3;
int Y[4];Y[0]=y;Y[1]=y1;Y[2]=y2;Y[3]=y3;
for(int i=0;i<4;i++)
{
if(X[i]==0)
{
return false;
}
if(map[X[i]-1][Y[i]]!=0)
{
bool overlap=false;
for(int j=0;j<4;j++)
{
if(X[i]-1==X[j]&&Y[i]==Y[j])
{
overlap=true;break;
}
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<4;i++)
{
map[X[i]-1][Y[i]]=1;
}
x--;x1--;x2--;x3--;
return true;
}
else
{
return false;
}
}
bool moveLeft(int map[22][10])
{
bool can=true;
int X[4];X[0]=x;X[1]=x1;X[2]=x2;X[3]=x3;
int Y[4];Y[0]=y;Y[1]=y1;Y[2]=y2;Y[3]=y3;
for(int i=0;i<4;i++)
{
if(Y[i]==0)
{
return false;
}
if(map[X[i]][Y[i]-1]!=0)
{
bool overlap=false;
for(int j=0;j<4;j++)
{
if(X[i]==X[j]&&Y[i]-1==Y[j])
{
overlap=true;break;
}
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]-1]=1;
}
y--;y1--;y2--;y3--;
return true;
}
else
{
return false;
}
}
bool moveRight(int map[22][10])
{
bool can=true;
int X[4];X[0]=x;X[1]=x1;X[2]=x2;X[3]=x3;
int Y[4];Y[0]=y;Y[1]=y1;Y[2]=y2;Y[3]=y3;
for(int i=0;i<4;i++)
{
if(Y[i]==9)
{
return false;;
}
if(map[X[i]][Y[i]+1]!=0)
{
bool overlap=false;
for(int j=0;j<4;j++)
{
if(X[i]==X[j]&&Y[i]+1==Y[j])
{
overlap=true;break;
}
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<4;i++)
{
map[X[i]][Y[i]+1]=1;
}
y++;y1++;y2++;y3++;
return true;
}
else
{
return false;
}
}
bool rotate(int map[22][10])
{
bool can=true;
int X[3];X[0]=x1;X[1]=x2;X[2]=x3;
int Y[3];Y[0]=y1;Y[1]=y2;Y[2]=y3;
for(int i=0;i<3;i++)
{
int p=X[i]-x,q=Y[i]-y;
if(x-q<0||y+p<0||y+p>9)
return false;
if(map[x-q][y+p]!=0)
{
bool overlap=false;
for(int j=0;j<3;j++)
{
if(x-q==X[j]&&y+p==Y[j])
overlap=true;
}
if(!overlap)
can=false;
}
}
if(can)
{
for(int i=0;i<3;i++)
{
map[X[i]][Y[i]]=0;
}
for(int i=0;i<3;i++)
{
int p=X[i]-x,q=Y[i]-y;
map[x-q][y+p]=1;
}
int p,q;
p=x1-x;q=y1-y;x1=x-q;y1=y+p;
p=x2-x;q=y2-y;x2=x-q;y2=y+p;
p=x3-x;q=y3-y;x3=x-q;y3=y+p;
}
else
return false;
}
};
有空再详细分析
相关文章推荐
- JavaScript--js写的俄罗斯方块
- [转] 不错的俄罗斯方块程序代码(VC++版)
- 俄罗斯方块总结
- JAVA游戏编程之三----j2me 手机游戏入门开发--俄罗斯方块_3_增加成绩记录
- 俄罗斯方块javascript版本
- 一个MIDP俄罗斯方块游戏的设计和实现
- c++写的基于openGL的立体俄罗斯方块
- 俄罗斯方块代码(C,有BUG)
- 自己动手写俄罗斯方块(一)
- 俄罗斯方块AI
- Actionscript3.0 俄罗斯方块源码
- 发布一个Java写的俄罗斯方块源码 算法简单(300行) 注释详细
- 俄罗斯方块之三——Block类
- 终端下的俄罗斯方块游戏tetris
- 120行的俄罗斯方块(Javascript)
- 多库俄罗斯方块之五(JavaScript)
- 俄罗斯方块的构思
- 俄罗斯方块旋转算法研究
- C + API俄罗斯方块