C++win32實現俄羅斯方塊
頭文件
源文件:
背景類的源碼
.h文件
#pragma once
class bg
{
public:
bg(void);
int getmark();
int addmark(int num);
public:
int background[20][10];
private:
int mark ;
};
.cpp文件
#include "StdAfx.h"
#include "bg.h"
bg::bg()
{
for(int i =0;i<20;i++)
{
for(int j = 0;j<10;j++)
{
background[i][j] = 0;
}
}
mark = 0;
}
int bg::addmark(int num)
{
return this->mark = this->mark + num;
}
int bg::getmark()
{
return this->mark;
}
父類Block.h
#pragma once
#include <windows.h>
#include "bg.h"
class Block
{
public:
void Init();
int createBlock(Block *p);
void Fix();
virtual void printblock(HDC &Hdc)=0 ;
void printFix(HDC &Hdc);
void fall();
void MoveLeft();
void MoveRight();
void del();
void Zchange();
void change_background();
virtual void Zchangesquare(const bg *ground) = 0;
void Zchangesquare1();
void Zchangesquare2();
void Zchangesquare3();
void Zchangesquare4();
int Zchangesquarecan();
void Zchangesquare6();
int Zchangesquare6can();
bool isDown();
bool isDown_collide();
bool isGameOver(HWND hWnd);
bool isLeft();
bool isRight();
bool isLeft_collide();
bool isRright_collide();
bool isFull(int a);
int getSquarenum();
int getmark();
void showScore(HDC & Hdc);
void wirteDcu(HWND hwnd,int mark);
int ReadDcu();
void releaseDB();
protected:
int squarenum;
int squareline;
int squarelist;
protected:
POINT point[8];//記錄方塊座標 用於畫圖
int block[2][4]; //記錄方塊狀態
int num ;
};
父類的實現block.cpp
#include "stdafx.h"
#include "Block.h"
#include <cstring>
#include "Z.h"
bg *ground = new bg;
//隨機產生方塊
int Block::createBlock(Block *p)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
ground->background[i][j + 3] = p->block[i][j];
}
}
return 0;
}
//畫固定方塊
void Block::printFix(HDC & Hdc)
{
HGDIOBJ oldBrush;
HGDIOBJ newBrush = CreateSolidBrush(RGB(0, 255, 127));
oldBrush = SelectObject(Hdc, newBrush);
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
if (2 == ground->background[i][j])
{
Rectangle(Hdc, j * 20, i * 20, j * 20 + 19, i * 20 + 19);
}
}
}
newBrush = SelectObject(Hdc, oldBrush);
DeleteObject(newBrush);
}
//固定
void Block::Fix()
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
if (1 == ground->background[i][j])
{
ground->background[i][j] = 2;
num = 0;
}
}
}
}
//是否能下落
bool Block::isDown()
{
for (int i = 0; i < 10; i++)
{
if (1 == ground->background[19][i])
{
return false;
}
}
return true;
}
//下落障礙碰撞
bool Block::isDown_collide()
{
for (int i = 19; i >= 0; i--)
{
for (int j = 0; j < 10; j++)
{
if (1 == ground->background[i][j])
{
if (2 == ground->background[i + 1][j])
{
return false;
}
}
}
}
return true;
}
bool Block::isLeft()
{
for (int i = 0; i < 20; i++)
{
if (1 == ground->background[i][0])
{
return false;
}
}
return true;
}
//左移障礙碰撞
bool Block::isLeft_collide()
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
if (1 == ground->background[i][j])
{
if (2 == ground->background[i][j - 1])
{
return false;
}
}
}
}
return true;
}
bool Block::isRight()
{
for (int i = 0; i < 20; i++)
{
if (1 == ground->background[i][9])
{
return false;
}
}
return true;
}
//右移障礙碰撞
bool Block::isRright_collide()
{
for (int i = 0; i < 20; i++)
{
for (int j = 9; j >= 0; j--)
{
if (1 == ground->background[i][j])
{
if (2 == ground->background[i][j + 1])
{
return false;
}
}
}
}
return true;
}
//下落
void Block::fall()
{
for(int i =0;i < sizeof(point)/sizeof(POINT); i++)
{
point[i].y += 20;
}
for (int i = 19; i >= 0; i--)
{
for (int j = 0; j < 10; j++)
{
if (1 == ground->background[i][j])
{
ground->background[i + 1][j] = ground->background[i][j];
ground->background[i][j] = 0;
}
}
}
squareline++;
}
//左移
void Block::MoveLeft()
{
for(int i =0;i<sizeof(point)/sizeof(POINT);i++)
{
point[i].x-=20;
}
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
if (1 == ground->background[i][j])
{
ground->background[i][j - 1] = ground->background[i][j];
ground->background[i][j] = 0;
}
}
}
squarelist--;
}
//右移
void Block::MoveRight()
{
for(int i =0;i<sizeof(point)/sizeof(POINT);i++)
{
point[i].x+=20;
}
for (int i = 0; i < 20; i++)
{
for (int j = 9; j >= 0; j--)
{
if (1 == ground->background[i][j])
{
ground->background[i][j + 1] = ground->background[i][j];
ground->background[i][j] = 0;
}
}
}
squarelist++;
}
//判斷是否滿行
bool Block::isFull(int a)
{
int n = 0;
for(int j = 0;j<10;j++)
{
if(ground->background[a][j] == 2 )
{
n++;
}
}
if(n == 10)
{
ground->addmark(10);
return true;
}
return false;
}
void Block::del()//消行
{
for(int i = 0;i<20;i++)
{
if(isFull(i))
{
for(int n=i;n>=0;n--)
{
for(int j = 0;j<10;j++)
{
ground->background[n][j] = ground->background[n-1][j];
}
}
for(int i2 = 0;i2<10;i2++)
{
ground->background[0][i2] = 0;
}
}
}
}
int Block::getSquarenum()
{
return this->squarenum;
}
int Block::getmark()
{
return ground->getmark();
}
//該方法計劃用於數據存儲,基本內容已經實現,有些小細節沒有完成
void Block::wirteDcu(HWND hwnd,int mark)
{
TCHAR writeFile[] = _T("d://a.txt");
HANDLE wFile = CreateFile(writeFile, //創建文件的名稱。
GENERIC_WRITE, // GENERIC_WRITE | GENERIC_READ, 寫和讀文件。
0, // 不共享。
NULL, // 缺省安全屬性
OPEN_ALWAYS, // CREATE_ALWAYS 覆蓋文件(不存在則創建) OPEN_EXISTING 打開文件(不存在則報錯)
FILE_ATTRIBUTE_NORMAL, // 一般的文件。
NULL); // 模板文件爲空。
GetLastError();
DWORD dwreturnsize;
wchar_t p[5];
memset(p,0,sizeof(p));
_itoa(mark,(char*) p, 10);
p[4]='\0';
WriteFile(wFile, p, sizeof(p), &dwreturnsize, NULL);
//WriteFile(wFile, &s, 1, &dwreturnsize, NULL);
CloseHandle(wFile);//關閉文件
}
int Block::ReadDcu()
{
TCHAR szFile[] = _T("d://a.txt");
HANDLE hFile = CreateFile(szFile, //創建文件的名稱。
GENERIC_READ, // GENERIC_WRITE | GENERIC_READ, 寫和讀文件。
FILE_SHARE_READ, // 共享讀。
NULL, // 缺省安全屬性
OPEN_EXISTING, // CREATE_ALWAYS 覆蓋文件(不存在則創建) OPEN_EXISTING 打開文件(不存在則報錯)
FILE_ATTRIBUTE_NORMAL, // 一般的文件。
NULL); // 模板文件爲空。
if (hFile == INVALID_HANDLE_VALUE) //無效值
{
OutputDebugString(TEXT("CreateFile fail!\r\n"));
return 0;
}
DWORD dwSize = GetFileSize(hFile, NULL); //讀取文件大小
//cout << "dwSize=" << dwSize << endl;
char cBuf[5]; //在堆上開闢緩衝區,等待讀入文件數據
memset(cBuf,0,sizeof(cBuf));
//SetFilePointer(hFile, 10, NULL, FILE_BEGIN);
DWORD haveReadByte;
ReadFile(hFile, cBuf, 4, &haveReadByte, 0);
int num = MultiByteToWideChar(0,0,cBuf,-1,NULL,0);
MessageBox(NULL,cBuf, TEXT("最佳紀錄"), MB_OK);
CloseHandle(hFile);
return 0;
}
void Block::releaseDB()
{
if(ground != NULL)
{
delete ground;
ground = NULL;
}
}
功能以及分數展示
void Block::showScore(HDC & Hdc)
{
Rectangle(Hdc, 200, 0, 350, 400);
char strScore[10];
memset(strScore,0,sizeof(strScore));
_itoa(ground->getmark(),(char*)strScore, 10);
TextOut(Hdc, 250, 80, TEXT("分數"), strlen("分數"));
TextOut(Hdc, 270, 100, strScore, strlen(strScore));
TextOut(Hdc,210, 195, TEXT("俄羅斯方塊遊戲介紹"), strlen(TEXT("俄羅斯方塊遊戲介紹")));
TextOut(Hdc,235, 240, TEXT("按上變形"), strlen("按上變形"));
TextOut(Hdc, 235, 260, TEXT("按下加速"), strlen("按下加速"));
TextOut(Hdc, 235, 280, TEXT("按左向左移"), strlen("按左向左移"));
TextOut(Hdc, 235, 300, TEXT("按右向右移"), strlen("按右向右移"));
}
遊戲結束的判斷
bool Block::isGameOver(HWND hWnd)
{
for (int i = 0; i < 10; i++)
{
if (2 == ground->background[1][i] || 1 == ground->background[1][i]|| 2 == ground->background[0][i]||1 == ground->background[0][i] )
{
TCHAR szBuffer[1024];
LPCTSTR str = TEXT("恭喜你獲得: %d分");
wsprintf(szBuffer, str,ground->getmark() );
wirteDcu(hWnd,ground->getmark());
MessageBox(NULL, szBuffer , TEXT("gameover"), MB_RETRYCANCEL);
releaseDB();
return false;
}
}
return true;
}
旋轉的實現
void Block::Zchange()
{
switch(squarenum)
{
case 0:
if(1==Zchangesquarecan())
{
Zchangesquare(ground);
}
else
{
return ;
}
break;
case 1:
if(1==Zchangesquarecan())
{
Zchangesquare(ground);
}
else
{
return ;
}
break;
case 2:
if(1==Zchangesquarecan())
{
Zchangesquare(ground);
}
else
{
return ;
}
break;
case 3:
if(1==Zchangesquarecan())
{
Zchangesquare(ground);
}
else
{
return ;
}
break;
case 4:
if(1==Zchangesquarecan())
{
Zchangesquare(ground);
}
else
{
return ;
}
break;
case 5:
return;
case 6:
if(1==Zchangesquare6can())
{
Zchangesquare6();
}
else
{
return;
}
break;
}
}
void Block::change_background()
{
int x = 0;
int y = 0;
int twoline = 2;
int Zremblock[3][3]={0};
for(x = 0 ;x < 3;x++)
{
for(y = 0;y <3 ;y++)
{
Zremblock[x][y] = ground->background[squareline+x][squarelist+y];
}
}
for( x = 0;x < 3;x++)
{
for(y = 0;y < 3;y++)
{
ground->background[squareline+x][squarelist+y] = Zremblock[twoline][x];
twoline--;
}
twoline=2;
}
}
int Block::Zchangesquarecan()
{
int x = 0;
int y = 0;
for(x=0;x<3;x++)
{
for(y=0;y<3;y++)
{
if(2==ground->background[squareline+x][squarelist+y])
{
return 0;
}
}
}
if( squareline >= 17)
{
return 0;
}
if( squarelist<=-1 ||squarelist>=8)
{
return 0;
}
return 1;
}
void Block::Zchangesquare6()
{
if(1 == ground->background[squareline][squarelist-1])
{
if(2 == ground->background[squareline+1][squarelist]||2==ground->background[squareline+2][squarelist])
{
return;
}
else
{
ground->background[squareline][squarelist-1]=0;
ground->background[squareline][squarelist+1]=0;
ground->background[squareline][squarelist+2]=0;
ground->background[squareline-1][squarelist]=1;
ground->background[squareline+1][squarelist]=1;
ground->background[squareline+2][squarelist]=1;
point[0].y = 20*(squareline-1);point[0].x = 20*(squarelist);
point[1].y = 20*(squareline-1)+20-1;point[1].x = 20*(squarelist)+20-1;
point[4].y = 20*(squareline+1);point[4].x = 20*(squarelist);
point[5].y = 20*(squareline+1)+20-1;point[5].x = 20*(squarelist)+20-1;
point[6].y = 20*(squareline+2);point[6].x = 20*(squarelist);
point[7].y = 20*(squareline+2)+20-1;point[7].x = 20*squarelist+20-1;
}
}
else
{
if(2 == ground->background[squareline][squarelist+1] || 2 == ground->background[squareline][squarelist+2] || 2 == ground->background[squareline][squarelist-1])
{
return;
}
else
{
point[0].y = 20*squareline; point[0].x = 20*(squarelist-1);
point[1].y = (20*squareline)+20-1; point[1].x = 20*(squarelist-1)+20-1;
point[4].y = 20*squareline; point[4].x = 20*(squarelist+1);
point[5].y = (20*squareline)+20-1; point[5].x = 20*(squarelist+1)+20-1;
point[6].y = 20*squareline; point[6].x = 20*(squarelist+2);
point[7].y = (20*squareline)+20-1; point[7].x = 20*(squarelist+2)+20-1;
ground->background[squareline][squarelist-1]=1;
ground->background[squareline][squarelist+1]=1;
ground->background[squareline][squarelist+2]=1;
ground->background[squareline-1][squarelist]=0;
ground->background[squareline+1][squarelist]=0;
ground->background[squareline+2][squarelist]=0;
}
}
}
int Block::Zchangesquare6can()
{
if(squareline<18 && squareline>0 && squarelist>0 && squarelist<8)//邊界不可變
{
return 1;
}
return 0;
}
子類的實現
#include "StdAfx.h"
#include "L.h"
L::L()
{
// ■
// ■■■
point[0].x = 60;point[0].y = 0;
point[1].x = 79;point[1].y = 19;
point[2].x = 60;point[2].y = 20;
point[3].x = 79;point[3].y = 39;
point[6].x = 80;point[6].y = 20;
point[7].x = 99;point[7].y = 39;
point[4].x = 100;point[4].y = 20;
point[5].x = 119;point[5].y = 39;
block[0][0] = 1,block[0][1] = 0,block[0][2] = 0,block[0][3] = 0;
block[1][0] = 1,block[1][1] = 1,block[1][2] = 1,block[1][3] = 0;
squareline = 0;
squarelist = 3;
squarenum = 3;
}
void L::printblock(HDC &Hdc)
{
Rectangle(Hdc, 0, 0, 200, 400);
HBRUSH oldBrush;
HBRUSH newBrush = CreateSolidBrush(RGB(141, 238, 238));//將畫筆改成紅色
oldBrush = (HBRUSH)SelectObject(Hdc, newBrush);//保存之前的畫筆顏色
for(int i = 0;i < sizeof(point)/sizeof(POINT);i = i + 2)
{
Rectangle(Hdc,point[i].x,point[i].y,point[i+1].x,point[i+1].y);
}
newBrush = (HBRUSH)SelectObject(Hdc, oldBrush);
DeleteObject(newBrush);
}
void L::Zchangesquare(const bg *ground)
{
if(1 == ground->background[squareline][squarelist])//0
{
change_background();
point[0].x = 20*(squarelist+2);point[0].y = 20*(squareline);
point[1].x = 20*(squarelist+2+1)-1;point[1].y = 20*(squareline+1)-1;
point[2].x = 20*(squarelist+1);point[2].y = 20*(squareline);
point[3].x = 20*(squarelist+1+1)-1;point[3].y = 20*(squareline+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+1);point[6].y = 20*(squareline+2);
point[7].x = 20*(squarelist+1+1)-1;point[7].y = 20*(squareline+2+1)-1;
}
else if(1 == ground->background[squareline][squarelist+2])//1
{
change_background();
point[0].x = 20*(squarelist+2);point[0].y = 20*(squareline+2);
point[1].x = 20*(squarelist+2+1)-1;point[1].y = 20*(squareline+2+1)-1;
point[2].x = 20*(squarelist+2);point[2].y = 20*(squareline+1);
point[3].x = 20*(squarelist+2+1)-1;point[3].y = 20*(squareline+1+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist);point[6].y = 20*(squareline+1);
point[7].x = 20*(squarelist+1)-1;point[7].y = 20*(squareline+1+1)-1;
}
else if(1 == ground->background[squareline+2][squarelist+2])//2
{
change_background();
point[0].x = 20*(squarelist);point[0].y = 20*(squareline+2);
point[1].x = 20*(squarelist+1)-1;point[1].y = 20*(squareline+2+1)-1;
point[2].x = 20*(squarelist+1);point[2].y = 20*(squareline+2);
point[3].x = 20*(squarelist+1+1)-1;point[3].y = 20*(squareline+2+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+1);point[6].y = 20*(squareline);
point[7].x = 20*(squarelist+1+1)-1;point[7].y = 20*(squareline+1)-1;
}
else if(1 == ground->background[squareline+2][squarelist])//3
{
change_background();
point[0].x = 20*(squarelist);point[0].y = 20*(squareline);
point[1].x = 20*(squarelist+1)-1;point[1].y = 20*(squareline+1)-1;
point[2].x = 20*(squarelist);point[2].y = 20*(squareline+1);
point[3].x = 20*(squarelist+1)-1;point[3].y = 20*(squareline+1+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+2);point[6].y = 20*(squareline+1);
point[7].x = 20*(squarelist+2+1)-1;point[7].y = 20*(squareline+1+1)-1;
}
}
#include "StdAfx.h"
#include "LZ.h"
LZ::LZ()
{
point[0].x = 100;point[0].y = 0;
point[1].x = 119;point[1].y = 19;
point[2].x = 60;point[2].y = 20;
point[3].x = 79;point[3].y = 39;
point[6].x = 80;point[6].y = 20;
point[7].x = 99;point[7].y = 39;
point[4].x = 100;point[4].y = 20;
point[5].x = 119;point[5].y = 39;
block[0][0] = 0,block[0][1] = 0,block[0][2] = 1,block[0][3] = 0;
block[1][0] = 1,block[1][1] = 1,block[1][2] = 1,block[1][3] = 0;
squareline = 0;
squarelist = 3;//旋轉
squarenum = 4;
}
void LZ::printblock(HDC &Hdc)
{
Rectangle(Hdc, 0, 0, 200, 400);
HBRUSH oldBrush;
HBRUSH newBrush = CreateSolidBrush(RGB(141, 238, 238));//將畫筆改成紅色
oldBrush = (HBRUSH)SelectObject(Hdc, newBrush);//保存之前的畫筆顏色
for(int i = 0;i < sizeof(point)/sizeof(POINT);i = i + 2)
{
Rectangle(Hdc,point[i].x,point[i].y,point[i+1].x,point[i+1].y);
}
newBrush = (HBRUSH)SelectObject(Hdc, oldBrush);
DeleteObject(newBrush);
}
void LZ::Zchangesquare(const bg *ground)
{
if(1 == ground->background[squareline][squarelist+2])//0
{
change_background();
point[0].x = 20*(squarelist+2);point[0].y = 20*(squareline+2);
point[1].x = 20*(squarelist+2+1)-1;point[1].y = 20*(squareline+2+1)-1;
point[2].x = 20*(squarelist+1);point[2].y = 20*(squareline+2);
point[3].x = 20*(squarelist+1+1)-1;point[3].y = 20*(squareline+2+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+1);point[6].y = 20*(squareline);
point[7].x = 20*(squarelist+1+1)-1;point[7].y = 20*(squareline+1)-1;
}
else if(1 == ground->background[squareline+2][squarelist+2])//1
{
change_background();
point[0].x = 20*(squarelist);point[0].y = 20*(squareline+2);
point[1].x = 20*(squarelist+1)-1;point[1].y = 20*(squareline+2+1)-1;
point[2].x = 20*(squarelist);point[2].y = 20*(squareline+1);
point[3].x = 20*(squarelist+1)-1;point[3].y = 20*(squareline+1+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+2);point[6].y = 20*(squareline+1);
point[7].x = 20*(squarelist+2+1)-1;point[7].y = 20*(squareline+1+1)-1;
}
else if(1 == ground->background[squareline+2][squarelist])//2
{
change_background();
point[0].x = 20*(squarelist);point[0].y = 20*(squareline);
point[1].x = 20*(squarelist+1)-1;point[1].y = 20*(squareline+1)-1;
point[2].x = 20*(squarelist+1);point[2].y = 20*(squareline);
point[3].x = 20*(squarelist+1+1)-1;point[3].y = 20*(squareline+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+1);point[6].y = 20*(squareline+2);
point[7].x = 20*(squarelist+1+1)-1;point[7].y = 20*(squareline+2+1)-1;
}
else if(1 == ground->background[squareline][squarelist])//3
{
change_background();
point[0].x = 20*(squarelist+2);point[0].y = 20*(squareline);
point[1].x = 20*(squarelist+2+1)-1;point[1].y = 20*(squareline+1)-1;
point[2].x = 20*(squarelist+2);point[2].y = 20*(squareline+1);
point[3].x = 20*(squarelist+2+1)-1;point[3].y = 20*(squareline+1+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist);point[6].y = 20*(squareline+1);
point[7].x = 20*(squarelist+1)-1;point[7].y = 20*(squareline+1+1)-1;
}
}
#include "StdAfx.h"
#include "Rct.h"
Rct::Rct()
{
point[0].x = 60;point[0].y = 0;
point[1].x = 79;point[1].y = 19;
point[2].x = 80;point[2].y = 0;
point[3].x = 99;point[3].y = 19;
point[4].x = 100;point[4].y = 0;
point[5].x = 119;point[5].y = 19;
point[6].x = 120;point[6].y = 0;
point[7].x = 139;point[7].y = 19;
block[0][0] = 1,block[0][1] = 1,block[0][2] = 1,block[0][3] = 1;
block[1][0] = 0,block[1][1] = 0,block[1][2] = 0,block[1][3] = 0;
squareline = 0;
squarelist = 4;
squarenum = 6;
}
void Rct::printblock(HDC &Hdc)
{
Rectangle(Hdc, 0, 0, 200, 400);
HBRUSH oldBrush;
HBRUSH newBrush = CreateSolidBrush(RGB(141, 238, 238));//將畫筆改成紅色
oldBrush = (HBRUSH)SelectObject(Hdc, newBrush);//保存之前的畫筆顏色
for(int i = 0;i < sizeof(point)/sizeof(POINT);i = i + 2)
{
Rectangle(Hdc,point[i].x,point[i].y,point[i+1].x,point[i+1].y);
}
newBrush = (HBRUSH)SelectObject(Hdc, oldBrush);
DeleteObject(newBrush);
}
#include "StdAfx.h"
#include "Tian.h"
Tian::Tian()
{
// ■■
// ■■
point[0].x = 60;point[0].y = 0;
point[1].x = 79;point[1].y = 19;
point[2].x = 80;point[2].y = 0;
point[3].x = 99;point[3].y = 19;
point[4].x = 60;point[4].y = 20;
point[5].x = 79;point[5].y = 39;
point[6].x = 80;point[6].y = 20;
point[7].x = 99;point[7].y = 39;
block[0][0] = 1,block[0][1] = 1,block[0][2] = 0,block[0][3] = 0;
block[1][0] = 1,block[1][1] = 1,block[1][2] = 0,block[1][3] = 0;
squareline = 0;
squarelist = 0;
squarenum = 5;//刪除無影響
}
void Tian::printblock(HDC &Hdc)
{
Rectangle(Hdc, 0, 0, 200, 400);
HBRUSH oldBrush;
HBRUSH newBrush = CreateSolidBrush(RGB(141, 238, 238));//將畫筆改成紅色
oldBrush = (HBRUSH)SelectObject(Hdc, newBrush);//保存之前的畫筆顏色
for(int i = 0;i < sizeof(point)/sizeof(POINT);i = i + 2)
{
Rectangle(Hdc,point[i].x,point[i].y,point[i+1].x,point[i+1].y);
}
newBrush = (HBRUSH)SelectObject(Hdc, oldBrush);
DeleteObject(newBrush);
}
void Tian::Zchangesquare(const bg *ground)
{
return ;
}
#include "StdAfx.h"
#include "Z.h"
Z::Z()
{
//■■
// ■■
point[6].x = 100;point[6].y = 0;
point[7].x = 119;point[7].y = 19;
point[2].x = 80;point[2].y = 0;
point[3].x = 99;point[3].y = 19;
point[4].x = 60;point[4].y = 20;
point[5].x = 79;point[5].y = 39;
point[0].x = 80;point[0].y = 20;
point[1].x = 99;point[1].y = 39;
block[0][0] = 0, block[0][1] = 1, block[0][2] = 1, block[0][3] = 0;
block[1][0] = 1, block[1][1] = 1, block[1][2] = 0, block[1][3] = 0;
squareline = 0;
squarelist = 3;
squarenum = 0;
}
void Z::printblock(HDC &Hdc)
{
Rectangle(Hdc, 0, 0, 200, 400);
HBRUSH oldBrush;
HBRUSH newBrush = CreateSolidBrush(RGB(141, 238, 238));//將畫筆改成紅色
oldBrush = (HBRUSH)SelectObject(Hdc, newBrush);//保存之前的畫筆顏色
for(int i = 0;i < sizeof(point)/sizeof(POINT);i = i + 2)
{
Rectangle(Hdc,point[i].x,point[i].y,point[i+1].x,point[i+1].y);
}
newBrush = (HBRUSH)SelectObject(Hdc, oldBrush);
DeleteObject(newBrush);
}
void Z::Zchangesquare(const bg *ground)
{
if(1 == ground->background[squareline][squarelist+2])//0
{
change_background();
point[0].x = 20*(squarelist+2);point[0].y = 20*(squareline+2);
point[1].x = 20*(squarelist+2+1)-1;point[1].y = 20*(squareline+2+1)-1;
point[2].x = 20*(squarelist+2);point[2].y = 20*(squareline+1);
point[3].x = 20*(squarelist+2+1)-1;point[3].y = 20*(squareline+1+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+1);point[6].y = 20*(squareline);
point[7].x = 20*(squarelist+1+1)-1;point[7].y = 20*(squareline+1)-1;
}
else if(1 == ground->background[squareline+2][squarelist+2])//1
{
change_background();
point[0].x = 20*(squarelist+2);point[0].y = 20*(squareline+1);
point[1].x = 20*(squarelist+2+1)-1;point[1].y = 20*(squareline+1+1)-1;
point[2].x = 20*(squarelist+1);point[2].y = 20*(squareline+1);
point[3].x = 20*(squarelist+1+1)-1;point[3].y = 20*(squareline+1+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1+1)-1;
point[6].x = 20*(squarelist);point[6].y = 20*(squareline+1+1);
point[7].x = 20*(squarelist+1)-1;point[7].y = 20*(squareline+1+1+1)-1;
}
else if(1 == ground->background[squareline+2][squarelist])//2
{
change_background();
point[0].x = 20*(squarelist);point[0].y = 20*(squareline);
point[1].x = 20*(squarelist+1)-1;point[1].y = 20*(squareline+1)-1;
point[2].x = 20*(squarelist);point[2].y = 20*(squareline+1);
point[3].x = 20*(squarelist+1)-1;point[3].y = 20*(squareline+1+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist+1);point[6].y = 20*(squareline+2);
point[7].x = 20*(squarelist+1+1)-1;point[7].y = 20*(squareline+2+1)-1;
}
else if(1 == ground->background[squareline][squarelist])//3
{
change_background();
point[0].x = 20*(squarelist+2);point[0].y = 20*(squareline);
point[1].x = 20*(squarelist+2+1)-1;point[1].y = 20*(squareline+1)-1;
point[2].x = 20*(squarelist+1);point[2].y = 20*(squareline);
point[3].x = 20*(squarelist+1+1)-1;point[3].y = 20*(squareline+1)-1;
point[4].x = 20*(squarelist+1);point[4].y = 20*(squareline+1);
point[5].x = 20*(squarelist+1+1)-1;point[5].y = 20*(squareline+1+1)-1;
point[6].x = 20*(squarelist);point[6].y = 20*(squareline+1);
point[7].x = 20*(squarelist+1)-1;point[7].y = 20*(squareline+1+1)-1;
}
}
timer類的控制
#include "StdAfx.h"
#include "Timer.h"
void Timer::Init()
{
//block->Init();
//Random();
//block.draw();
//block.createBlock();
block = NULL;
srand((int)time(0));
int n = rand() % 7; // 總共很多種方塊類型
switch(n)
{
case 0:
block = new Z;
break;
case 1:
block = new ZZ;
break;
case 2:
block = new Shan;
break;
case 3:
block = new L;
break;
case 4:
block = new LZ;
break;
case 5:
block = new Tian;
break;
case 6:
block = new Rct;
break;
}
block->createBlock(block);
}
void Timer::onPrint(HDC hdc)
{
HDC Hdc = CreateCompatibleDC(hdc);
HBITMAP hbitmapback = CreateCompatibleBitmap(hdc, 200, 400);
SelectObject(Hdc, hbitmapback);
// 畫方塊
block->printblock(Hdc);
block->printFix(Hdc);
block->showScore(hdc);
BitBlt(hdc, 0, 0, 200, 400, Hdc, 0, 0, SRCCOPY);
DeleteObject(hbitmapback);
DeleteDC(Hdc);
}
void Timer::onReturn(HWND hWnd,int speed)
{
if(block->getmark()<50)
{
SetTimer(hWnd, DEF_TIME_ID, speed, NULL);
}
else if(block->getmark()>=50||block->getmark()<100)
{
SetTimer(hWnd, DEF_TIME_ID, speed-100, NULL);
}
else if(block->getmark()>=100||block->getmark()<200)
{
SetTimer(hWnd, DEF_TIME_ID, speed-200, NULL);
}
else{
SetTimer(hWnd, DEF_TIME_ID, 50, NULL);
}
}
void Timer::onTimer(HWND hWnd)
{
HDC hdc = GetDC(hWnd);
if ((1 == block->isDown() && 1 == block->isDown_collide()))
{
block->fall();
onPrint(hdc);
}
else
{
block->Fix();
block->del();
if (false == block->isGameOver(hWnd))
{
onClose(hWnd);
//Init();
return ;
}
else{
if(block!=NULL)
{
delete block;
block = NULL;
Init();
onPrint(hdc);
}
}
//block->createBlock(block);
//Init();
}
ReleaseDC(hWnd, hdc);
}
void Timer::onLeft(HWND hWnd)
{
if (block->isLeft() &&block->isLeft_collide())
{
HDC hdc = GetDC(hWnd);
block->MoveLeft();
onPrint(hdc);
ReleaseDC(hWnd, hdc);
}
}
void Timer::onRight(HWND hWnd)
{
if (block->isRight() && block->isRright_collide())
{
HDC hdc = GetDC(hWnd);
block->MoveRight();
onPrint(hdc);
ReleaseDC(hWnd, hdc);
}
}
void Timer::onDown(HWND hWnd)
{
onTimer(hWnd);
}
void Timer::onUp(HWND hWnd)
{
HDC hdc = GetDC(hWnd);
block->Zchange();
onPrint(hdc);
ReleaseDC(hWnd, hdc);
}
void Timer::onClose(HWND hWnd)
{
KillTimer(hWnd, DEF_TIME_ID);
block->releaseDB();
if(block!=NULL)
{
delete block;
block = NULL;
}
}
void Timer::getMax()
{
block->ReadDcu();
}
爲窗口實現事件監聽
switch (message)
{
case WM_CREATE:
timer.Init();
timer.onReturn(hWnd,500);
break;
case WM_TIMER:
timer.onTimer(hWnd);
break;
case WM_PAINT:
{
PAINTSTRUCT pt;
HDC hdc = BeginPaint(hWnd, &pt);
timer.onPrint(hdc);
EndPaint(hWnd, &pt);
break;
}
case WM_KEYDOWN:
switch(wParam)
{
case VK_SPACE:
case VK_RETURN:
timer.onReturn(hWnd,500);
break;
case VK_LEFT:
timer.onLeft(hWnd);
break;
case VK_RIGHT:
timer.onRight (hWnd);
break;
case VK_DOWN:
//timer.onReturn(hWnd);
timer.onDown(hWnd);
break;
case VK_UP:
timer.onUp(hWnd);
break;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_easy:
timer.onReturn(hWnd,500);
break;
case IDR_common:
timer.onReturn(hWnd,300);
break;
case IDC_diff:
timer.onReturn(hWnd,200);
break;
case IDC_VIP:
timer.getMax();
break;
}
}
break;
case WM_CLOSE:
case WM_DESTROY:
timer.onClose(hWnd);
PostQuitMessage(0);
break;
}
項目是基於C++win32的配置,類的調用
//
// 函數: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 處理主窗口的消息。
//
// WM_COMMAND - 處理應用程序菜單
// WM_PAINT - 繪製主窗口
// WM_DESTROY - 發送退出消息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static Timer timer;
switch (message)
{
case WM_CREATE:
timer.Init();
timer.onReturn(hWnd,500);
break;
case WM_TIMER:
timer.onTimer(hWnd);
break;
case WM_PAINT:
{
PAINTSTRUCT pt;
HDC hdc = BeginPaint(hWnd, &pt);
timer.onPrint(hdc);
EndPaint(hWnd, &pt);
break;
}
case WM_KEYDOWN:
switch(wParam)
{
case VK_SPACE:
case VK_RETURN:
timer.onReturn(hWnd,500);
break;
case VK_LEFT:
timer.onLeft(hWnd);
break;
case VK_RIGHT:
timer.onRight (hWnd);
break;
case VK_DOWN:
//timer.onReturn(hWnd);
timer.onDown(hWnd);
break;
case VK_UP:
timer.onUp(hWnd);
break;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_easy:
timer.onReturn(hWnd,500);
break;
case IDR_common:
timer.onReturn(hWnd,300);
break;
case IDC_diff:
timer.onReturn(hWnd,200);
break;
case IDC_VIP:
timer.getMax();
break;
}
}
break;
case WM_CLOSE:
case WM_DESTROY:
timer.onClose(hWnd);
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
實現的效果圖:
部分最佳記錄功能有瑕疵,望包涵!
源碼
關注我持續更新!