计算机之家


 
标题: sdk下的俄罗斯方块源码
  本主题由 cao 于 2008-5-18 14:37 设置高亮 
530761333
技术精英
Rank: 11Rank: 11Rank: 11Rank: 11


分享大使   技术精英组  
UID 38326
精华 0
积分 1730
帖子 617
威望 1730
现金 437 币币
存款 1000 币币
阅读权限 11
注册 2007-8-18
来自 网上
状态 离线
 
发表于 2008-5-8 13:28  资料  个人空间  短消息  加为好友  QQ           
sdk下的俄罗斯方块源码

windows程序设计的实习练手之物
[attach]7075[/attach]
代码文件主要有两个,一个是主程序,一个是方块阵列.
MonyerRect.cpp
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "rect.h"

#define RectRow 10//列数
#define RectLine 20//行数
#define RectWidth 30//每方格宽度
#define RectLeft 100//距左边距离
#define RectTop 50//距右边距离

int BoxEnd = 0;//本次移动是否结束1
int BoxTimes = 0;//下落次数
int BoxMove = 3;//左右距离
int RectRandom = rand()%7;//小方格随机
int RectOldRandom = rand()%7;//主方格随机
int RectArray[RectLine][RectRow];//显示数组
int RectStatic[RectLine][RectRow];//静态方格数组
int RectSmall[4][4];//小方格数组
int Line,Row,i,j;
int OldBoxMove;//移动前左右距离
int BoxTrack;//最后操作方向,0为横向,1为纵向
int RectKill=0;//已满格的行
int sharp=0;//变换形状
int RectKillTimes = 0;//一次消除方块行数
int Score=0;//分数
POINT p1,p2;//绘制时用到
char ScoreShow[] = "您目前的分数为:" ;

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int MoveRectBox(HWND);
int doRectKill();

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hDC;
HBRUSH hBrush;
HPEN hPen;
PAINTSTRUCT PtStr;
char str[10];

switch(message)
{
case WM_TIMER:
  
   if(BoxEnd==1)//若本次方块已落下
   {
    BoxEnd = 0;
    BoxTimes = 0;//下落距离清零
    sharp = 0;
    BoxMove = 3;
    RectOldRandom = RectRandom;
    RectRandom = rand()%7;//随机方块
    doRectKill();
   }
   else//本次方块下落中
   {
    BoxTimes++;//增加下落距离
    BoxTrack = 1;//纵向
   }
  
   InvalidateRect(hWnd,NULL,0);
   break;
case WM_PAINT:
   hDC = BeginPaint(hWnd,&PtStr);
  
   //初始化主面板数据为0
   for(Line=0;Line<RectLine;Line++)
   {
    for(int Row=0;Row<RectRow;Row++)
    {
     RectArray[Line][Row] = 0;
    }
   }

   //初始化小面板数据为0
   for(Line=0;Line<4;Line++)
   {
    for(int Row=0;Row<4;Row++)
    {
     RectSmall[Line][Row] = 0;
    }
   }

   //显示分数
   TextOut(hDC,0,0,ScoreShow,lstrlen(ScoreShow) );
   itoa(Score,str,10);
   TextOut(hDC,150,0,str,lstrlen(str) );
//**************************************************************************************  
  
   //为RectArray赋值
   for(Line=0;Line<RectLine;Line++)//将RectStatic赋给RectArray
   {
    for(int Row=0;Row<RectRow;Row++)
    {
     RectArray[Line][Row] = RectStatic[Line][Row];
    }
   }

   for(i=0;i<4;i++)//左右边界限定
   {
    Line = Rect_Box[RectOldRandom][sharp][0]+BoxTimes;//获取Box行位置
    Row = Rect_Box[RectOldRandom][sharp][1]+BoxMove;//获取Box列位置

    if(Row<0)//左边界限制
    {
     BoxMove++;
     Row++;
     break;
    }
    if(Row>RectRow-1)//右边界限制
    {
     BoxMove--;
     Row--;
     break;
    }

    if(RectArray[Line][Row]==1)//发生碰撞?
    {
     if(!BoxTrack)//横向
     {
      BoxMove = OldBoxMove;
      break;
     }
    }

   }
   for(i=0;i<4;i++)//下边界限定
   {  
    Line = Rect_Box[RectOldRandom][sharp][0]+BoxTimes;//获取Box行位置
    Row = Rect_Box[RectOldRandom][sharp][1]+BoxMove;//获取Box列位置

    if(Line>RectLine-1)//如果下落超过底
    {
     BoxTimes--;//BoxTimes回滚
     Line--;
     BoxEnd = 1;
     break;
    }

    if(RectArray[Line][Row]==1)//如果即将下落位置已经拥有
    {
     if(BoxTrack)//纵向
     {
      BoxTimes--;
      BoxEnd = 1;//本次方块移动结束

      if(BoxTimes==-1)//Game Over本次游戏结束
      {
       for(Line=0;Line<RectLine;Line++)//重新开始
       {
        for(int Row=0;Row<RectRow;Row++)
        {
         RectStatic[Line][Row] = 0;
        }
       }
       Score = 0;
       BoxEnd = 1;
       return 0;
      }
      break;
     }
    }
   }

   if(BoxEnd)//如果方块停止,将Box存入RectStatic
   {
    for(j=0;j<4;j++)
    {
     RectStatic[
      Rect_Box[RectOldRandom][sharp][j][0]+BoxTimes
      ][
      Rect_Box[RectOldRandom][sharp][j][1]+BoxMove
      ] = 1;
    }
    return 0;
   }
  
   for(i=0;i<4;i++)//将Box赋给RectArray
   {
    Line = Rect_Box[RectOldRandom][sharp][0]+BoxTimes;//获取Box行位置
    Row = Rect_Box[RectOldRandom][sharp][1]+BoxMove;//获取Box列位置
    RectArray[Line][Row]=1;
   }

//*************************************************************************************  
   //为RectSmall赋值
   for(i=0;i<4;i++)
   {
    RectSmall[Rect_Box[RectRandom][0][0]][Rect_Box[RectRandom][0][1]]=1;
   }

   /*通过RectArray数组显示主面板*/
   for(Line=0;Line<RectLine;Line++)
   {
    for(Row=0;Row<RectRow;Row++)
    {
     p1.x = RectLeft + Row * RectWidth;
     p1.y = RectTop + Line * RectWidth;
     p2.x = p1.x + RectWidth;
     p2.y = p1.y + RectWidth;
     if(RectArray[Line][Row]==0)
     {
      hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
      hPen = (HPEN)GetStockObject(BLACK_PEN);
     }
     else
     {
      hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
      hPen = (HPEN)GetStockObject(WHITE_PEN);
     }
     SelectObject(hDC,hPen);
     SelectObject(hDC,hBrush);
     Rectangle(hDC,p1.x,p1.y,p2.x,p2.y);
    }
   }

   //通过RectSmall数据显示小面板
   for(Line=0;Line<4;Line++)
   {
    for(Row=0;Row<4;Row++)
    {
     if(RectSmall[Line][Row]==0)
     {
      hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
      hPen = (HPEN)GetStockObject(BLACK_PEN);
     }
     else
     {
      hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
      hPen = (HPEN)GetStockObject(WHITE_PEN);
     }
     SelectObject(hDC,hPen);
     SelectObject(hDC,hBrush);
     Rectangle(
      hDC,
      RectRow * RectWidth + 4*RectWidth + Row * RectWidth/3,
      RectTop + Line * RectWidth/3,
      RectRow * RectWidth + 4*RectWidth + Row * RectWidth/3 + RectWidth/3,
      RectTop + Line * RectWidth/3 + RectWidth/3
     );
    }
   }

   EndPaint(hWnd,&PtStr);
   break;

case WM_KEYDOWN:
   {
    switch(wParam)
    {
    case VK_UP:
     if(sharp<3) sharp++;
     else sharp=0;
     InvalidateRect(hWnd,NULL,0);
     break;
    case VK_DOWN:
     BoxTimes++;
     BoxTrack = 1;//纵向
     InvalidateRect(hWnd,NULL,0);
     break;
    case VK_LEFT:
     OldBoxMove = BoxMove;
     BoxMove--;
     BoxTrack = 0;//横向
     InvalidateRect(hWnd,NULL,0);
     break;
    case VK_RIGHT:
     OldBoxMove = BoxMove;
     BoxMove++;
     BoxTrack = 0;//横向
     InvalidateRect(hWnd,NULL,0);
     break;
    default:
     return DefWindowProc(hWnd,message,wParam,lParam);
    }
   }

case WM_CREATE:
  
   break;

case WM_DESTROY:
   PostQuitMessage(0);
   return 0;

default:
   return DefWindowProc(hWnd,message,wParam,lParam);

}
return 0;
}

int MoveRectBox(HWND hWnd)
{
InvalidateRect(hWnd,NULL,0);
return 0;
}

int doRectKill()
{
RectKillTimes = 0;
for(Line=0;Line<RectLine;Line++)//检查RectStatic,消去行
{
   for(int Row=0;Row<RectRow;Row++)
   {
    if(RectStatic[Line][Row]==0)//如果存在空块,则退出
    {
     RectKill = 0;
     break;
    }
    else
    {
     RectKill = Line;
    }
   }
  
   if(RectKill!=0)//发现满行,消除
   {
    for(i=RectKill;i>1;i--)
    {
     for(j=0;j<RectRow;j++)
     {
      RectStatic[j] = RectStatic[i-1][j];
     }
    }
    Score += 100 + RectKillTimes * 25;//一次消除多行,加分
    RectKillTimes++;
   }
}
return 0;
}

/*******************************************WinMain*******************************************/
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
HWND hWnd;
WNDCLASS WndCls;
MSG msg;

WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndCls.hCursor = LoadCursor(NULL,IDC_ARROW);
WndCls.hIcon = LoadIcon(NULL,IDI_APPLICATION);
WndCls.hInstance = hInstance;
WndCls.lpfnWndProc = WndProc;
WndCls.lpszClassName = "Monyer Rect";
WndCls.lpszMenuName = NULL;
WndCls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndCls);

hWnd = CreateWindow
   (
   "Monyer Rect",
   "Monyer Rect",
   WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT,
   CW_USEDEFAULT,
   800,
   800,
   NULL,
   NULL,
   hInstance,
   NULL
   );
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);

srand((unsigned)time(NULL));
SetTimer(hWnd,1,500,NULL);
for(Line=0;Line<RectLine;Line++)
{
   for(int Row=0;Row<RectRow;Row++)
   {
    RectStatic[Line][Row] = 0;
   }
}

while(GetMessage(&msg,NULL,0,0))
{
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}
return msg.wParam;
}

rect.h
int Rect_Box[7][4][4][2] =
{
   {//A//Rect_Box[0]
    {//A0//Rect_Box[0][0]
     {0,1},{1,1},{2,1},{3,1}
    },
    {//A1//Rect_Box[0][1]
     {1,0},{1,1},{1,2},{1,3}
    },
    {//A2//Rect_Box[0][2]
     {0,1},{1,1},{2,1},{3,1}
    },
    {//A3//Rect_Box[0][3]
     {1,0},{1,1},{1,2},{1,3}
    }
   },
   {//B//Rect_Box[1]
    {//B0//Rect_Box[1][0]
     {0,1},{1,1},{2,1},{1,2}
    },
    {//B1//Rect_Box[1][1]
     {1,0},{0,1},{1,1},{2,1}
    },
    {//B2//Rect_Box[1][2]
     {1,0},{1,1},{1,2},{0,1}
    },
    {//B3//Rect_Box[1][3]
     {1,0},{1,1},{1,2},{2,1}
    }
   },
   {//C//Rect_Box[2]
    {//C0//Rect_Box[2][0]
     {0,0},{0,1},{1,0},{1,1}
    },
    {//C1//Rect_Box[2][1]
     {0,0},{0,1},{1,0},{1,1}
    },
    {//C2//Rect_Box[2][2]
     {0,0},{0,1},{1,0},{1,1}
    },
    {//C3//Rect_Box[2][3]
     {0,0},{0,1},{1,0},{1,1}
    }
   },
   {//D//Rect_Box[3]
    {//D0//Rect_Box[3][0]
     {0,1},{1,1},{2,1},{2,2}
    },
    {//D1//Rect_Box[3][1]
     {0,0},{0,1},{1,1},{2,1}
    },
    {//D2//Rect_Box[3][2]
     {2,0},{2,1},{2,2},{1,2}
    },
    {//D3//Rect_Box[3][3]
     {1,0},{1,1},{1,2},{2,0}
    }
   },
   {//E//Rect_Box[4]
    {//E0//Rect_Box[4][0]
     {0,2},{1,2},{2,2},{2,1}
    },
    {//E1//Rect_Box[4][1]
     {1,1},{1,2},{1,3},{2,3}
    },
    {//E2//Rect_Box[4][2]
     {0,2},{0,3},{1,2},{2,2}
    },
    {//E3//Rect_Box[4][3]
     {0,1},{1,1},{1,2},{1,3}
    }
   },
   {//F//Rect_Box[5]
    {//F0//Rect_Box[5][0]
     {0,1},{1,1},{1,2},{2,2}
    },
    {//F1//Rect_Box[5][1]
     {1,2},{1,3},{2,1},{2,2}
    },
    {//F2//Rect_Box[5][2]
     {0,1},{1,1},{1,2},{2,2}
    },
    {//F3//Rect_Box[5][3]
     {1,2},{1,3},{2,1},{2,2}
    }
   },
   {//G//Rect_Box[6]
    {//G0//Rect_Box[6][0]
     {0,2},{1,1},{1,2},{2,1}
    },
    {//G1//Rect_Box[6][1]
     {1,0},{1,1},{2,1},{2,2}
    },
    {//G2//Rect_Box[6][2]
     {0,2},{1,1},{1,2},{2,1}
    },
    {//G3//Rect_Box[6][3]
     {1,0},{1,1},{2,1},{2,2}
    }
   }
};

vc6.0下编译通过。



 附件: 您所在的用户组无法下载或查看附件
本帖最近评分记录
cao   2008-5-18 14:37  威望  +10   谢谢分享
cao   2008-5-18 14:37  现金  +10   谢谢分享
顶部
jie_china
新手上路
Rank: 1


UID 23629
精华 0
积分 3
帖子 47
威望 3
现金 97 币币
存款 0 币币
阅读权限 1
注册 2007-5-5
状态 离线
 
发表于 2008-7-28 19:52  资料  个人空间  短消息  加为好友 
不错,不错,收藏了

顶部
 

 
 
当前时区 GMT+8, 现在时间是 2009-1-9 12:29