基于EasyX的2048创作
code:
#include <graphics.h>
#include <bits/stdc++.h>
#include <conio.h>
#define SPACE 11 //小矩形间距
#define SIDEL 130 //小矩形边长
#define BKLEN 575 //大矩形边长
#define X 200
#define Y 100
//font consolas
//需要用到的颜色
enum Color {
bk1 = RGB(46, 40, 14),
bk2 = RGB(63, 54, 45),
bk3 = RGB(58, 48, 37),
two1 = RGB(50, 37, 23),
two2 = RGB(63, 48, 21),
two3 = RGB(120, 62, 12),
two4 = RGB(136, 52, 9),
two5 = RGB(139, 33, 8),
two6 = RGB(161, 36, 7),
two7 = RGB(120, 95, 15),
two8 = RGB(131, 104, 15),
two9 = RGB(93, 93, 22),
two10 = RGB(15, 28, 24),
two11 = RGB(130, 130, 101),
font = RGB(171, 178, 191),
};
//方向
enum Dirction { up = 'w', down = 's', left = 'a', right = 'd' };
//keyCode是38,40,37和39
class My2048 {
public:
//constructor
My2048() :_matrix{} {}
//初始化游戏界面
void init() {
bk2();
setFont();
}
//在空位产生2或者4
void newNumber(bool judge) {
if (judge)
{
srand((unsigned int)time(0));
int x, y;
int a = rand() % 100;
//找到一个空位
do {
x = rand() % 4;
y = rand() % 4;
} while (_matrix[x][y] != 0);
_matrix[x][y] = (a > 80) ? 2 : 1;
}
}
//在对应的位置上画出小方块
void drawSmallRect() {
int x, y;
y = Y + SPACE;
for (int xj = 0; xj < 4; xj++)
{
x = X + SPACE;
for (int yj = 0; yj < 4; yj++)
{
switch (_matrix[xj][yj])//设置小方块颜色
{
case 1:
setfillcolor(Color::two1);
break;
case 2:
setfillcolor(Color::two2);
break;
case 3:
setfillcolor(Color::two3);
break;
case 4:
setfillcolor(Color::two4);
break;
case 5:
setfillcolor(Color::two5);
break;
case 6:
setfillcolor(Color::two6);
break;
case 7:
setfillcolor(Color::two7);
break;
case 8:
setfillcolor(Color::two8);
break;
case 9:
setfillcolor(Color::two9);
break;
case 10:
setfillcolor(Color::two10);
break;
case 11:
setfillcolor(Color::two11);
break;
default:
setfillcolor(Color::bk3);
break;
}//
solidroundrect(x, y, x + SIDEL, y + SIDEL, 7, 7);
//设置字体
LOGFONT f;
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY; //抗锯齿
_tcscpy_s(f.lfFaceName, TEXT("consolas"));//字体类型
setbkmode(TRANSPARENT);
settextcolor(Color::font);
if (_matrix[xj][yj] < 4) {
f.lfHeight = 90; //字体高度
settextstyle(&f);
switch (_matrix[xj][yj])
{
case 1:
outtextxy(x + 45, y + 20, TEXT("2"));
break;
case 2:
outtextxy(x + 45, y + 20, TEXT("4"));
break;
case 3:
outtextxy(x + 45, y + 20, TEXT("8"));
break;
}
}
else if (_matrix[xj][yj] < 7) {
f.lfHeight = 90; //字体高度
settextstyle(&f);
switch (_matrix[xj][yj])
{
case 4:
outtextxy(x + 25, y + 15, TEXT("16"));
break;
case 5:
outtextxy(x + 25, y + 15, TEXT("32"));
break;
case 6:
outtextxy(x + 25, y + 15, TEXT("64"));
break;
}
}
else if (_matrix[xj][yj] < 10) {
f.lfHeight = 80; //字体高度
settextstyle(&f);
switch (_matrix[xj][yj])
{
case 7:
outtextxy(x + 10, y + 20, TEXT("128"));
break;
case 8:
outtextxy(x + 10, y + 20, TEXT("256"));
break;
case 9:
outtextxy(x + 10, y + 20, TEXT("512"));
break;
}
}
else if (_matrix[xj][yj] < 10) {
f.lfHeight = 90; //字体高度
settextstyle(&f);
switch (_matrix[xj][yj])
{
case 10:
outtextxy(x, y, TEXT("1024"));
break;
case 11:
outtextxy(x, y, TEXT("2048"));
break;
}
}
x += SIDEL + SPACE;
}
y += SIDEL + SPACE;
}
}
bool moveRect(int di) {//移动方块到指定位置
bool isMove = false;
switch (di)
{
case Dirction::up:
isMove = up();
break;
case Dirction::down:
isMove = down();
break;
case Dirction::left:
isMove = left();
break;
case Dirction::right:
isMove = right();
break;
}
return isMove;
}
bool isWon() {
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (_matrix[i][j] == 2048)
{
return true;
}
}
}
return false;
}
bool isFail() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++) {
// 横向和纵向比较挨着的两个元素是否相等,若有相等则游戏不结束
if (_matrix[i][j] == _matrix[i][j + 1] || _matrix[j][i] == _matrix[j + 1][i])
return false;
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (_matrix[i][j] == 0)
return false;
}
}
return true;
}
char overPict() {
LOGFONT f;
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
f.lfHeight = 100; //字体高度
_tcscpy_s(f.lfFaceName, TEXT("黑体"));//字体类型
settextstyle(&f);
Sleep(200);
cleardevice();
outtextxy(X + SPACE + SIDEL + 50, 200, TEXT("你失败了"));
f.lfHeight = 50;
settextstyle(&f);
outtextxy(X + SPACE + SIDEL + 50, 300, TEXT("输入y再来一次"));
return (char)_getch();
}
private:
bool up() {
bool ret = false;
for (int x = 0; x < 4; x++) //逐列处理
{
for (int i = 0, j = 1; j < 4; j++) //从头开始查找是否能合并
{
if (_matrix[j][x] != 0) {
if (_matrix[j][x] == _matrix[i][x])//相等
{
_matrix[i][x] ++; ret = true;
_matrix[j][x] = 0;
i++;
}
else if (_matrix[i][x] == 0) { //移动
_matrix[i][x] = _matrix[j][x]; ret = true;
_matrix[j][x] = 0;
}
else { //碰撞
_matrix[i + 1][x] = _matrix[j][x];
if ((i + 1) != j)
{
_matrix[j][x] = 0; ret = true;
}
i++;
}
}
}
}
return ret;
}
bool down() {
bool ret = false;
for (int x = 0; x < 4; x++) //逐列处理
{
for (int i = 3, j = 2; j >= 0; j--) //从头开始查找是否能合并
{
if (_matrix[j][x] != 0) {
if (_matrix[j][x] == _matrix[i][x])//相等
{
_matrix[i][x] ++; ret = true;
_matrix[j][x] = 0;
i--;
}
else if (_matrix[i][x] == 0) { //移动
_matrix[i][x] = _matrix[j][x]; ret = true;
_matrix[j][x] = 0;
}
else { //碰撞
_matrix[i - 1][x] = _matrix[j][x];
if ((i - 1) != j)
{
_matrix[j][x] = 0; ret = true;
}
i--;
}
}
}
}
return ret;
}
bool left() {
bool ret = false;
for (int y = 0; y < 4; y++) //逐行处理
{
for (int i = 0, j = 1; j < 4; j++) //从头开始查找是否能合并
{
if (_matrix[y][j] != 0) {
if (_matrix[y][j] == _matrix[y][i])//相等
{
_matrix[y][i] ++; ret = true;
_matrix[y][j] = 0;
i++;
}
else if (_matrix[y][i] == 0) { //移动
_matrix[y][i] = _matrix[y][j]; ret = true;
_matrix[y][j] = 0;
}
else { //碰撞
_matrix[y][i + 1] = _matrix[y][j];
if ((i + 1) != j)
{
_matrix[y][j] = 0; ret = true;
}
i++;
}
}
}
}
return ret;
}
bool right() {
bool ret = false;
for (int y = 0; y < 4; y++) //逐行处理
{
for (int i = 3, j = 2; j >= 0; j--) //从头开始查找是否能合并
{
if (_matrix[y][j] != 0) {
if (_matrix[y][j] == _matrix[y][i])//相等
{
_matrix[y][i] ++; ret = true;
_matrix[y][j] = 0;
i--;
}
else if (_matrix[y][i] == 0) { //移动
_matrix[y][i] = _matrix[y][j]; ret = true;
_matrix[y][j] = 0;
}
else { //碰撞
_matrix[y][i - 1] = _matrix[y][j];
if ((i - 1) != j)
{
_matrix[y][j] = 0; ret = true;
}
i--;
}
}
}
}
return ret;
}
void bk2() {//设置背景
setfillcolor(Color::bk2);
solidroundrect(X, Y, X + BKLEN, Y + BKLEN, 20, 20);
int x, y;
y = Y + SPACE;
for (int i = 0; i < 4; i++)
{
x = X + SPACE;
for (int j = 0; j < 4; j++)
{
setfillcolor(Color::bk3);
solidroundrect(x, y, x + SIDEL, y + SIDEL, 7, 7);
x += SIDEL + SPACE;
}
y += SIDEL + SPACE;
}
}
void setFont() {
LOGFONT f;
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
f.lfHeight = 100; //字体高度
_tcscpy_s(f.lfFaceName, TEXT("黑体"));//字体类型
settextstyle(&f);
outtextxy(X + SPACE + SIDEL + 50, 0, TEXT("2048"));
f.lfHeight = 20;
settextstyle(&f);
outtextxy(0, 0, TEXT("请用wasd操作 //汪汪出品"));
}
int _matrix[4][4];
};
int main()
{
// 初始化图形窗口
initgraph(1000, 700);
setbkcolor(Color::bk1);
cleardevice();
//画出游戏主界面
A:
My2048 a;
a.init();
//游戏循环
a.newNumber(true);
a.newNumber(true);
a.drawSmallRect();
bool judge;
int ctr;
while ((ctr = _getch()) == Dirction::up || ctr == Dirction::down
|| ctr == Dirction::left || ctr == Dirction::right)
{
judge = a.moveRect(ctr);
a.drawSmallRect();
Sleep(150);
a.newNumber(judge);
a.drawSmallRect();
if (a.isWon()) {
ShellExecute(NULL, TEXT("ww出品"), TEXT("you're win"), NULL, NULL, MB_OK);
Sleep(3000);
break;
}
if (a.isFail()) {
Sleep(1000);
char e = a.overPict();
if (e == 'y')
{
goto A;
}
else {
cleardevice();
outtextxy(X + SPACE + SIDEL + 50, 200, TEXT("What a pity"));
Sleep(2000);
break;
}
}
Sleep(200);
}
closegraph();
}