C++编写的俄罗斯方块源码

把做工程过程中经常用到的代码片段记录起来,下边资料是关于C++编写的俄罗斯方块的代码,应该对各位有所好处。

#ifndef DATA_H_

#define DATA_H_

#include<windows.h>

static const int TYPE1=1;

static const int TYPE2=2;

static const int TYPE3=3;

static const int TYPE4=4;

static const int TYPE5=5;

static const int TYPE6=6;

static const int TYPE7=7;

static const int RTYPE1=1;

static const int RTYPE2=2;

static const int RTYPE3=4;

static int rTypeNext;

static int rTypeDown;

static const int index[KINDS][COLS]={

  {0,1,0,1,0,0,-1,-1,TYPE1,RTYPE1},

  {-1,0,1,2,0,0,0,0,TYPE2,RTYPE2},

  {0,0,1,1,1,0,0,-1,TYPE3,RTYPE2},

  {0,0,1,1,-1,0,0,1,TYPE4,RTYPE2},

  {-1,0,0,1,0,0,1,0,TYPE5,RTYPE3},

  {-1,0,1,1,0,0,0,-1,TYPE6,RTYPE3},

  {-1,0,1,1,0,0,0,1,TYPE7,RTYPE3}

};

static const int TIMER=1;

static int CURRENTLEVEL=600;

static int level=1;

static const int CTN=4;

typedef struct {

int x;

int y;

}sCord;

sCord sDown[CTN],sNext[CTN];

static RECT rectNext[CTN];

static RECT rectDown[CTN];

static const int cxSize=25;

static const int cySize=35;

static int offsetx;

static int offsety;

static int offsetxNext;

static int offsetyNext;

static const int MS_DOWN=10001;

static bool go=true;

static bool startGame=false;

static bool gameOver=false;

static int score;

RECT rt={326,81,425,455};

static const int pelsSize=13;

static const POINT area[]={0,455,326,455,326,0};

static int fillArea[cySize+1][cxSize+1];

HBRUSH hBrush2=CreateSolidBrush(RGB(255,255,255));

#include"Data.h"

#include<iostream>

#include<cstdlib>

using namespace std;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int main()

{

    HINSTANCE hInstance=NULL;

static TCHAR szAppName[]=TEXT("ELS");

HWND hwnd;

MSG msg;

WNDCLASS wndclass;

wndclass.lpfnWndProc=WndProc;

wndclass.cbClsExtra=0;

wndclass.cbWndExtra=0;

wndclass.hInstance=hInstance;

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);

wndclass.lpszMenuName= NULL;

wndclass.lpszClassName= szAppName;

if(!RegisterClass(&wndclass))

{

  MessageBox(NULL,TEXT("REGISTER ERROR"),szAppName,MB_ICONERROR);

  return 0;

}

  300,100,429,480,

  NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,SW_SHOWNORMAL);

UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))

{

  TranslateMessage(&msg);

  DispatchMessage(&msg);

}

    return msg.wParam;

}

void getRandom()

{

int k=rand()%KINDS+1;

for(int i=0;i<KINDS;i++)

{

  if(index[i][COLS-2]==k)

  {

  for(int j=0;j<CTN;j++)

  {

    sNext[j].x=index[i][j];

    sNext[j].y=index[i][j+4];

  }

  break;

  }

}

rotate(sNext,rTypeNext,rand()%rTypeNext+1,true);

int min_y=0;

for (int t = 0; t < CTN; t++)

  min_y = min_y > sNext[t].y ? sNext[t].y : min_y;

}

{

int tempx;

int tempy;

    int temp;

int tx=(offsetx-(int)(pelsSize/2))/pelsSize;

int ty=(offsety-(int)(pelsSize/2))/pelsSize;

bool ra=true;

switch(rType)

{

case RTYPE1:

  ra=false;

  break;

case RTYPE2:

{

  if(rNumber%2!=0)

  {

  for (int j = 0; j < CTN; j++)

  {

    tempx=-lpsCord->y+tx;

    tempy=lpsCord->x+ty;

    lpsCord++;

    if(!firstRotate&&(fillArea[tempx][tempy]>0||tempx>24||tempx<0||tempy<0||tempy>34))

    {

    ra=false;

    }

    }

  lpsCord-=4;

  }

    if(ra)

    {

  if (rNumber % 2 != 0)

    for (int k = 0; k < CTN; k++)

    {

    temp = -lpsCord->x;

    lpsCord->x = lpsCord->y;

    lpsCord->y = temp;

    lpsCord++;

    }

    }

}

  break;

case RTYPE3:

  for(int k=0;k<rNumber;k++)

  {

  for(int l=0;l<CTN;l++)

  {

    tempx=lpsCord->y+tx;

    tempy=(-lpsCord->x)+ty;

    lpsCord++;

    if(!firstRotate&&(fillArea[tempx][tempy]>0||tempx>24||tempx<0||tempy<0||tempy>34))

    {

    ra = false;

    }

  }

  lpsCord-=4;

  }

if(ra)

  for (int i = 0; i < rNumber; i++)

  {

  for (int j = 0; j < CTN; j++)

  {

    temp = -lpsCord->x;

    lpsCord->x = lpsCord->y;

    lpsCord->y = temp;

    lpsCord++;

  }

  lpsCord=lpsCord-4;

  }

  break;

}

return ra;

}

{

rTypeDown=rTypeNext;

offsetx=offsetxNext;

offsety=offsetyNext;

for(int i=0;i<CTN;i++)

{

  targ->x=sur->x;

  targ->y=sur->y;

  sur++;

  targ++;

}

  getRandom();

}

{

HDC hdc=GetDC(hwnd);

SelectObject(hdc,hBrush);

SelectObject(hdc,hPen1);

for(int i=0;i<CTN;i++)

{

  shape++;

}

ReleaseDC(hwnd,hdc);

}

void start()

{

if(!startGame)

{

  for (int i = 0; i < cySize + 1; i++)

  for (int j = 0; j < cxSize + 1; j++)

    fillArea[i][j] = 0;

  startGame=true;

  go=true;

  score=0;

}

}

bool downAble()

{

bool da=true;

int x=(offsetx-(int)(pelsSize/2))/pelsSize;

int y=(offsety-(int)(pelsSize/2))/pelsSize;

int xtemp;

int ytemp;

for(int i=0;i<CTN;i++)

{

  xtemp=sDown[i].x+x;

  ytemp=sDown[i].y+y+1;

  if(fillArea[ytemp][xtemp]>0||ytemp>34)

  {

  da=false;

  break;

  }

}

if (!da)

{

  for (int k = 0; k < CTN; k++)

  {

  xtemp = sDown[k].x + x;

  ytemp = sDown[k].y + y;

  fillArea[ytemp][xtemp] = 1;

  fillArea[ytemp][cxSize]++;

  fillArea[cySize][xtemp]++;

  }

}

return da;

}

bool leftAble()

{

bool la = true;

int x = (offsetx - (int) (pelsSize / 2)) / pelsSize;

int y = (offsety - (int) (pelsSize / 2)) / pelsSize;

int xtemp;

int ytemp;

for (int i = 0; i < CTN; i++)

{

  xtemp = sDown[i].x + x-1;

  ytemp = sDown[i].y + y;

  if (fillArea[ytemp][xtemp] > 0 || xtemp <0)

  {

  la = false;

  break;

  }

}

return la;

}

bool rightAble()

{

bool ra = true;

int x = (offsetx - (int) (pelsSize / 2)) / pelsSize;

int y = (offsety - (int) (pelsSize / 2)) / pelsSize ;

int xtemp;

int ytemp;

for (int i = 0; i < CTN; i++)

{

  xtemp = sDown[i].x + x+1;

  ytemp = sDown[i].y + y;

  if (fillArea[ytemp][xtemp] > 0 || xtemp > 24)

  {

  ra = false;

  break;

  }

}

return ra;

}

bool disRows(HWND hwnd)

{

HDC hdc=GetDC(hwnd);

bool da=false;

for (int ii = 0; ii < CTN; ii++)

  row[ii] = 0;

static int levelScore;

SelectObject(hdc,hPen1);

for (int i = 0; i < cySize; i++)

{

  if (fillArea[i][cxSize] == cxSize)

  row[number++] = i;

}

{

  da=true;

  cout<<"levelScore:"<<levelScore<<endl;

  {

  levelScore=0;

  SetTimer(hwnd,TIMER,CURRENTLEVEL,NULL);

  cout<<"currentlevel:"<<CURRENTLEVEL<<endl;

  for(int i=0;i<15;i++)

  {

    {

    level=i+2;

    cout<<"level"<<i+2<<endl;

    break;

    }

  }

  }

  InvalidateRect(hwnd,&rt,true);

  for (int k = 0; k < number; k++)

  {

  for(int i=row[k];i>0;i--)

  {

    for(int j=0;j<cxSize+1;j++)

    {

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

    }

  }

  }

  InvalidateRect(hwnd,NULL,true);

}

ReleaseDC(hwnd,hdc);

return da;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

HDC hdc;

PAINTSTRUCT ps;

RECT r;

static bool down=false;

bool isSend=false;

switch(message)

{

case WM_CREATE:

{

  SetTimer(hwnd,TIMER,CURRENTLEVEL,NULL);

  cout<<"level1"<<endl;

  return 0;

}

case WM_SIZE:

  return 0;

case WM_TIMER:

{

  if(startGame&&go)

  {

  if (down)

  {

    {

    cout<<"can not down"<<endl;

    down=false;

    disRows(hwnd);

    if(!isSend)

    {

      SendMessage(hwnd,MS_DOWN,0,0);

      isSend=true;

    }

    }

    else

    {

    draw(hwnd, sDown, rectDown,(HBRUSH)GetStockObject(WHITE_BRUSH), offsetx, offsety);

    offsety += pelsSize;

    draw(hwnd, sDown, rectDown,hBrush1, offsetx, offsety);

    }

  }

  }

  return 0;

}

case MS_DOWN:

{

  draw(hwnd,sNext,rectNext,(HBRUSH)GetStockObject(WHITE_BRUSH),369,44);

  getNext(sDown,sNext);

  draw(hwnd,sNext,rectNext,hBrush1,369,44);

  draw(hwnd,sDown,rectDown,hBrush1,offsetx,offsety);

  offsety-=pelsSize;

  if(downAble())

  {

  offsety+=pelsSize;

  isSend=false;

  down=true;

  }

  else

  {

  cout<<"game over!"<<endl;

  startGame=false;

  gameOver=true;

  InvalidateRect(hwnd,&rt,true);

  }

}

case WM_PAINT:

{

  hdc = BeginPaint(hwnd, &ps);

  GetClientRect(hwnd, &r);

  SelectObject(hdc,hPen1);

  SelectObject(hdc,hBrush1);

  for(int i=1;i<cxSize+8;i++)

  {

  }

  for(int j=1;j<cySize;j++)

  {

  }

  SaveDC(hdc);

  for(int t=0;t<cySize;t++)

  for(int k=0;k<cxSize;k++)

  {

    if(fillArea[t][k]>0)

    {

    }

  }

  if(startGame)

  {

  draw(hwnd,sNext,rectNext,hBrush1,369,44);

  draw(hwnd,sDown,rectDown,hBrush1,offsetx,offsety);

  }

  FillRect(hdc,&rt,hBrush2);

  char ss[20];

  char ll[20];

  wsprintf(ss,"score:%d",score);

  wsprintf(ll,"level:%d",level);

  TextOut(hdc,330,300,ll,lstrlen(ll));

  TextOut(hdc,330,320,ss,lstrlen(ss));

  if(gameOver)

  {

  char g[]="Game Over!!";

  TextOut(hdc,330,200,g,lstrlen(g));

  }

  SelectObject(hdc, GetStockObject(BLACK_PEN));

  MoveToEx(hdc, 325, 80, NULL);

  LineTo(hdc, 425, 80);

  EndPaint(hwnd, &ps);

  return 0;

}

case WM_KEYDOWN:

  switch(wParam)

  {

  case VK_UP:

  {

  if(go&&startGame)

  {

    down=false;

    draw(hwnd,sDown,rectDown,(HBRUSH)GetStockObject(WHITE_BRUSH),offsetx,offsety);

    rotate(sDown,rTypeDown,1,false);

    draw(hwnd,sDown,rectDown,hBrush1,offsetx,offsety);

  }

    }

  return 0;

  case VK_DOWN:

  {

  if(go&&startGame)

  {

  down=false;

  draw(hwnd,sDown,rectDown,(HBRUSH)GetStockObject(WHITE_BRUSH),offsetx,offsety);

  int k=(offsety-(int)(pelsSize)/2)/pelsSize;

  while(k<cySize)

  {

    if(downAble())

    {

    offsety+=pelsSize;

    }

    else

    break;

  }

  draw(hwnd,sDown,rectDown,hBrush1,offsetx,offsety);

  disRows(hwnd);

  SendMessage(hwnd,MS_DOWN,0,0);

  }

  return 0;

  }

  case VK_LEFT:

  {

  if(leftAble()&&go&&startGame)

  {

    down=false;

    draw(hwnd,sDown,rectDown,(HBRUSH)GetStockObject(WHITE_BRUSH),offsetx,offsety);

    offsetx-=pelsSize;

    draw(hwnd,sDown,rectDown,hBrush1,offsetx,offsety);

  }

  return 0;

  }

  case VK_RIGHT:

  {

  if(rightAble()&&go&&startGame)

  {

    down=false;

    draw(hwnd, sDown, rectDown, (HBRUSH) GetStockObject(WHITE_BRUSH),

        offsetx, offsety);

      offsetx+=pelsSize;

      draw(hwnd, sDown, rectDown, hBrush1,

        offsetx, offsety);

  }

  return 0;

  }

  case VK_SPACE:

  {

  go=!go;

  return 0;

  }

  case VK_RETURN:

  {

  if(!startGame&&!gameOver)

  {

    cout<<"startGame"<<endl;

    gameOver=false;

    start();

    getRandom();

    SendMessage(hwnd,MS_DOWN,0,0);

  }

  if(!startGame&&gameOver)

  {

    cout<<"RestartGame!"<<endl;

    gameOver=false;

    start();

    level=1;

    InvalidateRect(hwnd,NULL,true);

    getRandom();

    SendMessage(hwnd,MS_DOWN,0,0);

  }

  return 0;

  }

  }

  return 0;

case WM_KEYUP:

  switch(wParam)

  {

  case VK_UP:

  if(go)

    down=true;

  return 0;

  case VK_LEFT:

  if(go)

    down=true;

  return 0;

  case VK_RIGHT:

  if(go)

    down=true;

  return 0;

  }

  return 0;

case WM_DESTROY:

  DeleteObject(hBrush1);

  DeleteObject(hBrush2);

  DeleteObject(hPen1);

  KillTimer(hwnd,TIMER);

  PostQuitMessage(0);

  return 0;

}

return DefWindowProc(hwnd,message,wParam,lParam);

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容