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