elsHead.h
#pragma once
#ifndef DEF_ELS_HEAD
#define DEF_ELS_HEAD
#define DEF_TIMER1 1234
#define WIDTH 700
#define HEIGHT 655
#include <time.h>
#include <windows.h>
void onPaint(HDC hdc);
void onCreat();
// 显示方块
void paintSqare(HDC hdc);
// 产生随机块
int createRandSqare();
// 随机块贴近背景
void copySqareToBack();
// 按键
void onReturn(HWND hwnd);
void onTimer(HWND hwnd);
void onUp(HWND hwnd);
void onLeft(HWND hwnd);
void onRight(HWND hwnd);
// 方块移动
void sqareDown();
void sqareLeft();
void sqareRight();
// 方块停在最底下
int canSqareDown();
// 把1变成2
void change();
// 方块停止下落
int sqareStop();
// 方块左右移动
int canSqareRight();
int canSqareLeft();
// 方块停止左右移动
int sqareLeftStop();
int sqareRightStop();
// 消除完整行
void destroyOneLine();
// 方块变形
void sqareChange();
#endif
elsHead.cpp
#include "elsHead.h"
// 背景数组
char g_arrBackGround[20][16] = { 0 };
char g_arrSqare[2][4] = { 0 };
char g_;
int g_row = 0;
int g_list = 0;
int g_sqareID;
void onPaint(HDC hdc) {
// 兼容性DC
HDC comdc = CreateCompatibleDC(hdc);
// 创建一张纸
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, WIDTH, HEIGHT);
// 关联起来
SelectObject(comdc, hBitmap);
paintSqare(comdc);
// 传递
BitBlt(hdc, // 目标DC
5, // 起始x
5, // 起始y
480, // 目标区域x
600, // 目标区域y
comdc, // 源DC
0, 0, SRCCOPY);
// 释放DC
DeleteObject(hBitmap);
DeleteDC(comdc);
}
void onCreat()
{
srand((unsigned int)time(NULL));
createRandSqare();
copySqareToBack();
}
// 显示方块
void paintSqare(HDC comdc)
{
int i = 0;
int j = 0;
// 遍历
for (i = 0;i < 20;i++)
{
for (j = 0;j < 16;j++)
{
// 如果是1或2 画出方块
if(g_arrBackGround[i][j] == 1 || g_arrBackGround[i][j] == 2)
Rectangle(comdc, 30 * j , 30 * i, 30 + 30 * j, 30 + 30 * i);
}
}
}
// 产生随机块
int createRandSqare()
{
int n;
n = rand() % 7 + 1;
g_sqareID = n;
// 方块类型
switch (n)
{
// 正方形
case 1:
g_arrSqare[0][0] = 1;g_arrSqare[0][1] = 1;g_arrSqare[0][2] = 0;g_arrSqare[0][3] = 0;
g_arrSqare[1][0] = 1;g_arrSqare[1][1] = 1;g_arrSqare[1][2] = 0;g_arrSqare[1][3] = 0;
break;