百度空间 | 百度首页 
               
 
查看文章
 
sdk下的俄罗斯方块源码
2008-05-05 17:36

windows程序设计的实习练手之物

代码文件主要有两个,一个是主程序,一个是方块阵列.

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][i][0]+BoxTimes;//获取Box行位置
    Row = Rect_Box[RectOldRandom][sharp][i][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][i][0]+BoxTimes;//获取Box行位置
    Row = Rect_Box[RectOldRandom][sharp][i][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][i][0]+BoxTimes;//获取Box行位置
    Row = Rect_Box[RectOldRandom][sharp][i][1]+BoxMove;//获取Box列位置
    RectArray[Line][Row]=1;
   }

//*************************************************************************************  
   //为RectSmall赋值
   for(i=0;i<4;i++)
   {
    RectSmall[Rect_Box[RectRandom][0][i][0]][Rect_Box[RectRandom][0][i][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[i][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下编译通过。


类别:编程源码 | 添加到搜藏 | 浏览() | 评论 (14)
 
最近读者:
 
网友评论:
1
2008-05-05 17:40 | 回复
嘿嘿 沙发一下
 
2
2008-05-05 17:40 | 回复
不好!!!
 
3
2008-05-05 17:43 | 回复
喜欢玩叠方块
 
4
2008-05-05 18:18 | 回复
半年前还真以为不会再回来了。。 经历那事以后必然已是炼狱重生,曾经想给你留言的。 后来还是算了,我不太喜欢说伤感的事,多少是因为自己经过一些,会想起一些自己的事。 001over
 
5
2008-05-05 18:22 | 回复
回复楼上的匿名者: 您指的不好是出来的效果不好,还是代码的运行效率不高?还是代码的可读性太差? 如果是后两者,那么monyer希望您能留名,monyer要虚心向您请教. 但如果你是指不好看或不好玩,那的确如您所见,不好。因为这个东西根本就不是用来玩的,呵呵!
 
6
2008-05-05 18:23 | 回复
下载下研究研究 好长这么有耐心。。
 
7
2008-05-05 20:42 | 回复
反正我写不出来,哈哈! 我喜欢却做不到的就只能佩服了^_^
 
8
2008-05-05 23:50 | 回复
MONYER做的小游戏吗?呃。。看看。。看看。
 
9
2008-05-05 23:58 | 回复
囧,连用什么打开都不知道。
 
10
2008-05-06 13:05 | 回复
玩这个我可是高手呢……高手中的高高手呢!!!~~~ 我有差不多的很小的软件 你要不要看看 我一个人能打两个人的俄罗斯呢!!!!!!!!!!~~~~~ PS对了,为什么不能插入表情啊???
 
11
2008-05-12 15:43 | 回复
汗呀...没看懂
 
12
2008-05-12 15:52 | 回复
对了...是VB吗?
 
13
2008-05-12 18:07 | 回复
vc
 
14
2008-05-15 19:18 | 回复
好牛啊,上次看到一个用Turbo C编写的,代码400多行。
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu