很久以前就打算做俄罗斯方块,这次终于做成功了,界面不是很好看,现在的技术也就只能到这里了。 一下是重要的部分代码 方块类 #pragma once class FangKuai { private: int x; int y; int Style; int direct; int color; public: //是否停止 bool Is_Stop; //是否被消除 bool Is_Live; FangKuai(void); ~FangKuai(void); void SetX(int xx); void SetY(int yy); int GetX()const; int GetY()const; void SetDirect(int d); int GetDirect()const; int GetStyle()const; int GetColor()const { return color; } void Change(); void ChangeBack(); void MoveLeft(); void MoveRight(); void MoveDown(); }; #include "stdafx.h" #include "FangKuai.h" FangKuai::FangKuai(void) { Is_Live=true; Is_Stop=false; x=100; y=0; srand((unsigned)time(NULL)); direct=rand()%4; Style=rand()%4; } FangKuai::~FangKuai(void) { } void FangKuai::SetX(int xx) { x=xx; } void FangKuai::SetY(int yy) { y=yy; } int FangKuai::GetX()const { return x; } int FangKuai::GetY()const { return y; } int FangKuai::GetStyle()const { return Style; } void FangKuai::SetDirect(int d) { direct=d; } int FangKuai::GetDirect()const { return direct; } void FangKuai::Change() { switch(Style) { case 0: if(direct==0) direct=1; else if(direct==1) direct=2; else if(direct==2) direct=3; else if(direct==3) direct=0; break; case 1: if(direct==0) direct=1; else if(direct==1) { if(x-50>=0&&x+50<=400) direct=2; } else if(direct==2) direct=3; else if(direct==3) { if(x-50>=0&&x+50<=400) direct=0; } break; case 2: if(direct==0) { direct=1; } else if(direct==1) { if(x-50>=0) direct=2; } else if(direct==2) { direct=3; } else if(direct==3) { if(x+50<=400) direct=0; } break; case 3: if(direct==0) { direct=1; } else if(direct==1) { if(x-50>=0&&x+50<=400) direct=2; } else if(direct==2) { direct=3; } else if(direct==3) { if(x-50>=0&&x+50<=400) direct=0; } break; } } void FangKuai::ChangeBack() { if(direct==0) direct=3; else if(direct==1) direct=0; else if(direct==2) direct=1; else if(direct==3) direct=2; } void FangKuai::MoveLeft() { switch(Style) { case 0: if(x>=50) x-=50; break; case 1: if(direct==0||direct==2||direct==3) { if(x-50>=50) x-=50; } else if(direct==1) { if(x>=50) x-=50; } break; case 2: if(direct==0||direct==2||direct==3) { if(x-50>=50) x-=50; } else if(direct==1) { if(x>=50) x-=50; } break; case 3: if(direct==0||direct==2) { if(x-50>=50) x-=50; } else if(direct==1||direct==3) { if(x>=50) x-=50; } break; } } void FangKuai::MoveRight() { switch(Style) { case 0: if(x+100<=400-50) x+=50; break; case 1: if(direct==0||direct==2||direct==1) { if(x+100<=400-50) x+=50; } else if(direct==3) { if(x+50<=400-50) x+=50; } break; case 2: if(direct==0||direct==1||direct==2) { if(x+100<=400-50) x+=50; } else if(direct==3) { if(x+50<=400-50) x+=50; } break; case 3: if(direct==0||direct==2) { if(x+100<=400-50) x+=50; } else if(direct==1||direct==3) { if(x+50<=400-50) x+=50; } break; } } void FangKuai::MoveDown() { y+=50; } // 俄罗斯方块(自制单机版)View.h : C俄罗斯方块(自制单机版)View 类的接口 // #pragma once #include"FangKuai.h" #include<vector> using std::vector; #define ALL 0 #define TIME 1 class C俄罗斯方块(自制单机版)View : public CView { protected: // 仅从序列化创建 C俄罗斯方块(自制单机版)View(); DECLARE_DYNCREATE(C俄罗斯方块(自制单机版)View) // 特性 public: C俄罗斯方块(自制单机版)Doc* GetDocument() const; // 操作 public: //是否开始游戏 bool Start; //定义分数 int m_Scores; //定义方块地图 enum S{Blank,Box}; int F_Map[20][20]; //定义方块类向量 FangKuai *Move_F; //时间显示 int time; //方块落下的速度 enum F_Spend{Slow=900,Normal=500,Fast=200}; int F_DownSpend; //画方块 void DrawMapF(CDC* pDC); //画移动方块 void DrawMoveF(CDC *pDC); //让方块能随时间下落 void F_Down(); //判断方块是否可以下落 bool DownOk(); //判断方块是否能向左 bool LeftOk(); //判断方块是否可以向右 bool RightOk(); //画地图 void DrawMap(CDC *pDC); //画时间 void DrawTime(CDC *pDC); //判断是否满一行 然后消行 void CutCol(); //画分数 void DrawScore(CDC* pDC); //判断游戏结束 bool GameOver(); //画开始界面 void DrawStart(CDC* pDC); // 重写 public: virtual void OnDraw(CDC* pDC); // 重写以绘制该视图 virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); // 实现 public: virtual ~C俄罗斯方块(自制单机版)View(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // 生成的消息映射函数 protected: DECLARE_MESSAGE_MAP() public: afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); afx_msg void OnGameStart(); afx_msg void OnEasy(); afx_msg void OnNormal(); afx_msg void OnHard(); }; #ifndef _DEBUG // 俄罗斯方块(自制单机版)View.cpp 中的调试版本 inline C俄罗斯方块(自制单机版)Doc* C俄罗斯方块(自制单机版)View::GetDocument() const { return reinterpret_cast<C俄罗斯方块(自制单机版)Doc*>(m_pDocument); } #endif // 俄罗斯方块(自制单机版)View.cpp : C俄罗斯方块(自制单机版)View 类的实现 // #include "stdafx.h" // SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的 // ATL 项目中进行定义,并允许与该项目共享文档代码。 #ifndef SHARED_HANDLERS #include "俄罗斯方块(自制单机版).h" #endif #include "俄罗斯方块(自制单机版)Doc.h" #include "俄罗斯方块(自制单机版)View.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // C俄罗斯方块(自制单机版)View IMPLEMENT_DYNCREATE(C俄罗斯方块(自制单机版)View, CView) BEGIN_MESSAGE_MAP(C俄罗斯方块(自制单机版)View, CView) // 标准打印命令 ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview) ON_WM_TIMER() ON_WM_KEYDOWN() ON_COMMAND(ID_GAME_START, &C俄罗斯方块(自制单机版)View::OnGameStart) ON_COMMAND(ID_EASY, &C俄罗斯方块(自制单机版)View::OnEasy) ON_COMMAND(ID_NORMAL, &C俄罗斯方块(自制单机版)View::OnNormal) ON_COMMAND(ID_HARD, &C俄罗斯方块(自制单机版)View::OnHard) END_MESSAGE_MAP() // C俄罗斯方块(自制单机版)View 构造/析构 C俄罗斯方块(自制单机版)View::C俄罗斯方块(自制单机版)View() { // TODO: 在此处添加构造代码 F_DownSpend=Slow; Move_F=new FangKuai(); for(int i=0;i<=15;i++) { for(int j=0;j<=8;j++) F_Map[i][j]=Blank; } Start=false; m_Scores=0; } C俄罗斯方块(自制单机版)View::~C俄罗斯方块(自制单机版)View() { } BOOL C俄罗斯方块(自制单机版)View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此处通过修改 // CREATESTRUCT cs 来修改窗口类或样式 return CView::PreCreateWindow(cs); } // C俄罗斯方块(自制单机版)View 绘制 //画移动方块 void C俄罗斯方块(自制单机版)View::DrawMoveF(CDC *pDC) { int s,d,x,y; s=Move_F->GetStyle(); d=Move_F->GetDirect(); x=Move_F->GetX(); y=Move_F->GetY(); //间隔 int t=1; switch(s) { case 0: pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y+50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y+50,50-t,50-t,RGB(0,255,0)); break; case 1: if(d==0) { pDC->FillSolidRect(x-50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x-50,y-50,50-t,50-t,RGB(0,255,0)); } else if(d==1) { pDC->FillSolidRect(x,y-50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y-50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y+50,50-t,50-t,RGB(0,255,0)); } else if(d==2) { pDC->FillSolidRect(x-50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y+50,50-t,50-t,RGB(0,255,0)); } else if(d==3) { pDC->FillSolidRect(x,y-50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y+50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x-50,y+50,50-t,50-t,RGB(0,255,0)); } break; case 2: if(d==0) { pDC->FillSolidRect(x,y-50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x-50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y,50-t,50-t,RGB(0,255,0)); } else if(d==1) { pDC->FillSolidRect(x,y-50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y+50,50-t,50-t,RGB(0,255,0)); } else if(d==2) { pDC->FillSolidRect(x-50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y+50,50-t,50-t,RGB(0,255,0)); } else if(d==3) { pDC->FillSolidRect(x,y-50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x-50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y+50,50-t,50-t,RGB(0,255,0)); } break; case 3: if(d==0||d==2) { pDC->FillSolidRect(x-50,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x+50,y,50-t,50-t,RGB(0,255,0)); } else if(d==1||d==3) { pDC->FillSolidRect(x,y-50,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y,50-t,50-t,RGB(0,255,0)); pDC->FillSolidRect(x,y+50,50-t,50-t,RGB(0,255,0)); } break; } } void C俄罗斯方块(自制单机版)View::DrawMapF(CDC* pDC) { } //画地图 void C俄罗斯方块(自制单机版)View::DrawMap(CDC *pDC) { for(int i=0;i<=15;i++) { for(int j=0;j<=8;j++) { if(F_Map[i][j]==Box) pDC->FillSolidRect(j*50,i*50,49,49,RGB(0,255,0)); } } } //画时间 int T=0; int TT[6]={0}; void C俄罗斯方块(自制单机版)View::DrawTime(CDC *pDC) { int m_time=T; for(int i=0;i<4;i++) { TT[i]=m_time%10; m_time/=10; } CFont font,*pOldFont=NULL; font.CreatePointFont(500,_T("宋体"),pDC); pOldFont=pDC->SelectObject(&font); pDC->SetTextColor(RGB(255,0,0)); pDC->TextOutW(450,10,_T("时间")); pDC->SelectObject(pOldFont); font.DeleteObject(); pDC->SetTextColor(RGB(255,0,0)); int x,y; int ti=0; for(int i=0;i<4;i++,ti+=20) { x=700-i*40-ti; y=100; switch(TT[i]) { case 0: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,100,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 1: pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 2: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,50+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 3: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 4: pDC->FillSolidRect(x-100,y,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 5: pDC->FillSolidRect(x-100,y,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y+50,8,50+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 6: pDC->FillSolidRect(x-100,y,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,100,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y+50,8,50+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 7: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 8: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,100,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 9: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; } } } //画分数 void C俄罗斯方块(自制单机版)View::DrawScore(CDC* pDC) { int S=m_Scores; int d[5]; for(int i=0;i<4;i++) { d[i]=S%10; S/=10; } //画分数 CFont font,*pOldFont=NULL; font.CreatePointFont(500,_T("宋体"),pDC); pOldFont=pDC->SelectObject(&font); pDC->SetTextColor(RGB(0,255,0)); pDC->TextOutW(450,300,_T("分数")); pDC->SelectObject(pOldFont); font.DeleteObject(); int x,y; int ti=0; for(int i=0;i<4;i++,ti+=20) { x=700-i*40-ti; y=400; switch(d[i]) { case 0: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,100,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 1: pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 2: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,50+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 3: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 4: pDC->FillSolidRect(x-100,y,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 5: pDC->FillSolidRect(x-100,y,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y+50,8,50+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 6: pDC->FillSolidRect(x-100,y,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,100,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50+8,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y+50,8,50+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 7: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); break; case 8: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,100,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; case 9: pDC->FillSolidRect(x-100,y,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y,8,50,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+100,50,8,RGB(255,0,0)); pDC->FillSolidRect(x-100+50,y,8,100+8,RGB(255,0,0)); pDC->FillSolidRect(x-100,y+50,50,8,RGB(255,0,0)); break; } } } //画开始界面 void C俄罗斯方块(自制单机版)View::DrawStart(CDC* pDC) { pDC->SetTextColor(RGB(0,255,0)); pDC->TextOutW(50,400,_T("游戏说明:")); pDC->TextOutW(50,420,_T("本游戏简单暴力,仅供智商超高级别的人群耍耍,智商低者勿玩,后果自负!")); CFont font,*pOldFont=NULL; font.CreatePointFont(500,_T("宋体"),pDC); pOldFont=pDC->SelectObject(&font); pDC->SetTextColor(RGB(255,0,0)); pDC->TextOutW(150,50,_T("俄罗斯方块")); pDC->TextOutW(300,150,_T("---美辰制作")); pDC->SelectObject(pOldFont); font.DeleteObject(); } void C俄罗斯方块(自制单机版)View::OnDraw(CDC* pDC) { C俄罗斯方块(自制单机版)Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此处为本机数据添加绘制代码 CRect rect; GetClientRect(&rect); pDC->FillSolidRect(rect,RGB(0,0,0)); if(Start==false) { DrawStart(pDC); } else { pDC->FillSolidRect(400,0,2,750,RGB(0,0,255)); DrawMoveF(pDC); DrawMap(pDC); DrawTime(pDC); DrawScore(pDC); //判断游戏结束 if(GameOver()) { KillTimer(ALL); KillTimer(TIME); MessageBox(_T("你输了!游戏结束!"),_T("再接再厉!")); } } } // C俄罗斯方块(自制单机版)View 打印 BOOL C俄罗斯方块(自制单机版)View::OnPreparePrinting(CPrintInfo* pInfo) { // 默认准备 return DoPreparePrinting(pInfo); } void C俄罗斯方块(自制单机版)View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: 添加额外的打印前进行的初始化过程 } void C俄罗斯方块(自制单机版)View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: 添加打印后进行的清理过程 } // C俄罗斯方块(自制单机版)View 诊断 #ifdef _DEBUG void C俄罗斯方块(自制单机版)View::AssertValid() const { CView::AssertValid(); } void C俄罗斯方块(自制单机版)View::Dump(CDumpContext& dc) const { CView::Dump(dc); } C俄罗斯方块(自制单机版)Doc* C俄罗斯方块(自制单机版)View::GetDocument() const // 非调试版本是内联的 { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(C俄罗斯方块(自制单机版)Doc))); return (C俄罗斯方块(自制单机版)Doc*)m_pDocument; } #endif //_DEBUG // C俄罗斯方块(自制单机版)View 消息处理程序 //判断方块是否可以下落 bool C俄罗斯方块(自制单机版)View::DownOk() { int x=Move_F->GetX(); int y=Move_F->GetY(); int d=Move_F->GetDirect(); int s=Move_F->GetStyle(); x/=50; y/=50; int h=600; switch(s) { case 0: if((F_Map[y+2][x]==Blank&&F_Map[y+2][x+1]==Blank)&&(y*50)+50+50<=h) return true; break; case 1: if(d==0) { if((F_Map[y+1][x]==Blank&&F_Map[y+1][x+1]==Blank&&F_Map[y+1][x-1]==Blank)&&(y*50)+50<=h) return true; } else if(d==2) { if((F_Map[y+1][x]==Blank&&F_Map[y+2][x+1]==Blank&&F_Map[y+1][x-1]==Blank)&&(y*50)+100<=h) return true; } else if(d==1) { if((F_Map[y+2][x]==Blank&&F_Map[y][x+1]==Blank)&&(y*50)+100<=h) return true; } else if(d==3) { if((F_Map[y+2][x]==Blank&&F_Map[y][x-1]==Blank)&&(y*50)+100<=h) return true; } break; case 2: if(d==0) { if((F_Map[y+1][x-1]==Blank&&F_Map[y+1][x]==Blank&&F_Map[y+1][x+1]==Blank)&&(y*50)+50<=h) return true; } else if(d==1) { if((F_Map[y+2][x]==Blank&&F_Map[y+1][x+1]==Blank)&&(y*50)+100<=h) return true; } else if(d==2) { if((F_Map[y+2][x]==Blank&&F_Map[y+1][x-1]==Blank&&F_Map[y+1][x+1]==Blank)&&(y*50)+100<=h) return true; } else if(d==3) { if((F_Map[y+2][x]==Blank&&F_Map[y+1][x-1]==Blank)&&(y*50)+100<=h) return true; } break; case 3: if(d==0||d==2) { if((F_Map[y+1][x-1]==Blank&&F_Map[y+1][x]==Blank&&F_Map[y+1][x+1]==Blank)&&(y*50)+50<=h) return true; } else if(d==1||d==3) { if((F_Map[y+2][x]==Blank)&&(y*50)+100<=h) return true; } break; } return false; } //判断是否满一行 然后消行 void C俄罗斯方块(自制单机版)View::CutCol() { int mark[20]; int k=0; //记录要消行的行 for(int i=0;i<=15;i++) { int count=0; for(int j=0;j<=8;j++) { if(F_Map[i][j]==Box) count++; } if(count==8) { mark[k]=i; k++; } } //判断分数叠加多少 int t=5; for(int i=1;i<=k;i++) { m_Scores+=10+t; t+=5*i; } //消行 for(int l=0;l<k;l++) { for(int i=mark[l];i>=1;i--) { for(int j=0;j<=8;j++) { F_Map[i][j]=F_Map[i-1][j]; } } for(int i=0;i<=8;i++) F_Map[0][i]=Blank; } } //让方块能随时间下落 bool Is_Cut=false; void C俄罗斯方块(自制单机版)View::F_Down() { int x=Move_F->GetX(); int y=Move_F->GetY(); int s=Move_F->GetStyle(); int d=Move_F->GetDirect(); if(DownOk()) { Move_F->MoveDown(); Is_Cut=false; } else { Is_Cut=true; //加入地图中 x=x/50; y=y/50; switch(s) { case 0: F_Map[y][x]=Box; F_Map[y][x+1]=Box; F_Map[y+1][x]=Box; F_Map[y+1][x+1]=Box; break; case 1: if(d==0) { F_Map[y-1][x-1]=Box; F_Map[y][x-1]=Box; F_Map[y][x]=Box; F_Map[y][x+1]=Box; } else if(d==1) { F_Map[y-1][x]=Box; F_Map[y-1][x+1]=Box; F_Map[y][x]=Box; F_Map[y+1][x]=Box; } else if(d==2) { F_Map[y][x-1]=Box; F_Map[y][x]=Box; F_Map[y][x+1]=Box; F_Map[y+1][x+1]=Box; } else if(d==3) { F_Map[y-1][x]=Box; F_Map[y][x]=Box; F_Map[y+1][x]=Box; F_Map[y+1][x-1]=Box; } break; case 2: if(d==0) { F_Map[y][x-1]=Box; F_Map[y][x]=Box; F_Map[y][x+1]=Box; F_Map[y-1][x]=Box; } else if(d==1) { F_Map[y-1][x]=Box; F_Map[y][x+1]=Box; F_Map[y+1][x]=Box; F_Map[y][x]=Box; } else if(d==2) { F_Map[y][x-1]=Box; F_Map[y][x]=Box; F_Map[y][x+1]=Box; F_Map[y+1][x]=Box; } else if(d==3) { F_Map[y][x-1]=Box; F_Map[y][x]=Box; F_Map[y+1][x]=Box; F_Map[y-1][x]=Box; } break; case 3: if(d==0||d==2) { F_Map[y][x]=Box; F_Map[y][x-1]=Box; F_Map[y][x+1]=Box; } else if(d==1||d==3) { F_Map[y][x]=Box; F_Map[y-1][x]=Box; F_Map[y+1][x]=Box; } break; } //新生成一个方块 delete Move_F; Move_F=new FangKuai(); } } //判断游戏结束 bool C俄罗斯方块(自制单机版)View::GameOver() { for(int i=0;i<=8;i++) if(F_Map[0][i]==Box) return true; return false; } void C俄罗斯方块(自制单机版)View::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if(nIDEvent==0) { //方块下落 F_Down(); CDC* pDC=GetDC(); OnDraw(pDC); if(Is_Cut) { //消行 CutCol(); OnDraw(pDC); Is_Cut=false; } } else if(nIDEvent==1) { //时间刷新 T++; CDC* pDC=GetDC(); OnDraw(pDC); } CView::OnTimer(nIDEvent); } //判断方块是否能向左 bool C俄罗斯方块(自制单机版)View::LeftOk() { int x=Move_F->GetX(); int y=Move_F->GetY(); int d=Move_F->GetDirect(); int s=Move_F->GetStyle(); x/=50; y/=50; int h=600; switch(s) { case 0: if(F_Map[y][x-1]==Blank&&F_Map[y+1][x-1]==Blank) return true; break; case 1: if(d==0) { if(F_Map[y-1][x-2]==Blank&&F_Map[y][x-2]==Blank) return true; } else if(d==1) { if(F_Map[y-1][x-1]==Blank&&F_Map[y][x-1]==Blank&&F_Map[y+1][x-1]==Blank) return true; } else if(d==2) { if(F_Map[y][x-2]==Blank&&F_Map[y+1][x]==Blank) return true; } else if(d==3) { if(F_Map[y-1][x-1]==Blank&&F_Map[y][x-1]==Blank&&F_Map[y+1][x-2]==Blank) return true; } break; case 2: if(d==0) { if(F_Map[y-1][x-1]==Blank&&F_Map[y][x-2]==Blank) return true; } else if(d==1) { if(F_Map[y-1][x-1]==Blank&&F_Map[y][x-1]==Blank&&F_Map[y+1][x-1]==Blank) return true; } else if(d==2) { if(F_Map[y][x-2]==Blank&&F_Map[y+1][x-1]==Blank) return true; } else if(d==3) { if(F_Map[y-1][x-1]==Blank&&F_Map[y][x-2]==Blank&&F_Map[y+1][x-2]==Blank) return true; } break; case 3: if(d==0||d==2) { if(F_Map[y][x-1]==Blank) return true; } else if(d==1||d==3) { if(F_Map[y-1][x-1]==Blank&&F_Map[y][x-1]==Blank&&F_Map[y+1][x-1]==Blank) return true; } break; } return false; } //判断方块是否可以向右 bool C俄罗斯方块(自制单机版)View::RightOk() { int x=Move_F->GetX(); int y=Move_F->GetY(); int d=Move_F->GetDirect(); int s=Move_F->GetStyle(); x/=50; y/=50; int h=600; switch(s) { case 0: if(F_Map[y][x+2]==Blank&&F_Map[y+1][x+2]==Blank) return true; break; case 1: if(d==0) { if(F_Map[y-1][x]==Blank&&F_Map[y][x+2]==Blank) return true; } else if(d==1) { if(F_Map[y-1][x+2]==Blank&&F_Map[y][x+1]==Blank&&F_Map[y+1][x+1]==Blank) return true; } else if(d==2) { if(F_Map[y][x+2]==Blank&&F_Map[y+1][x+2]==Blank) return true; } else if(d==3) { if(F_Map[y-1][x+1]==Blank&&F_Map[y][x+1]==Blank&&F_Map[y+1][x+1]==Blank) return true; } break; case 2: if(d==0) { if(F_Map[y-1][x+1]==Blank&&F_Map[y][x+2]==Blank) return true; } else if(d==1) { if(F_Map[y-1][x+1]==Blank&&F_Map[y][x+2]==Blank&&F_Map[y+1][x+1]==Blank) return true; } else if(d==2) { if(F_Map[y][x+2]==Blank&&F_Map[y+1][x+1]==Blank) return true; } else if(d==3) { if(F_Map[y-1][x+1]==Blank&&F_Map[y][x+1]==Blank&&F_Map[y+1][x+1]==Blank) return true; } break; case 3: if(d==0||d==2) { if(F_Map[y][x+1]==Blank) return true; } else if(d==1||d==3) { if(F_Map[y-1][x+1]==Blank&&F_Map[y][x+1]==Blank&&F_Map[y+1][x+1]==Blank) return true; } break; } return false; } void C俄罗斯方块(自制单机版)View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if(GetAsyncKeyState(VK_UP)) { Move_F->Change(); } else if(GetAsyncKeyState(VK_DOWN)) { if(DownOk()) Move_F->MoveDown(); } else if(GetAsyncKeyState(VK_LEFT)) { if(LeftOk()) Move_F->MoveLeft(); } else if(GetAsyncKeyState(VK_RIGHT)) { if(RightOk()) Move_F->MoveRight(); } CDC *pDC=GetDC(); OnDraw(pDC); CView::OnKeyDown(nChar, nRepCnt, nFlags); } void C俄罗斯方块(自制单机版)View::OnGameStart() { // TODO: 在此添加命令处理程序代码 Start=true; SetTimer(ALL,F_DownSpend,NULL); SetTimer(TIME,1000,NULL); time=0; } void C俄罗斯方块(自制单机版)View::OnEasy() { // TODO: 在此添加命令处理程序代码 F_DownSpend=Slow; } void C俄罗斯方块(自制单机版)View::OnNormal() { // TODO: 在此添加命令处理程序代码 F_DownSpend=Normal; } void C俄罗斯方块(自制单机版)View::OnHard() { // TODO: 在此添加命令处理程序代码 F_DownSpend=Fast; } |