这个游戏需要用到一个图形库。graphics.h
前段时间由于自己VS2019的重装,以前安装的图形库没了,所以又重新安装了一遍,在这顺便给大家分享一下
好多朋友还不知道在哪里下载及安装 EasyX <graphics.h>
关于Easy-x的下载:http://www.easyx.cn/downloads/
下载最新版
找到软件安装位置点击安装
1.创建项目
2.在项目的源文件夹下创建一个文件夹image
3.把下面十二张图片重命名好放进刚创建的文件夹
4.总共需要创建两个文件,一个main.cpp,一个是2048.h。本人使用的编译器是vs2019.
以下是main.cpp的内容
————————————————————————————————————————————————————————————————————————————————
#include"2048.h"
int main() {
loadResource();
initgraph(400, 400);
randIntNum(0);
drawMap();
while (gameOver()) {
keyDown();
drawMap();
}
printf("游戏结束");
system("pause");
}
以下是2048.h的内容
——————————————————————————————————————————————————————————————————————————————————
#pragma once
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
using namespace std;
int map[4][4] = {
0 };
IMAGE img[12];
int imgIndex[12] = {
0,2,4,8,16,32,64,128,256,512,1024,2048 };
void loadResource() {
for (int i = 0; i < 12; i++) {
char fileName[20] = "";
sprintf_s(fileName, "image/%d.bmp", imgIndex[i]);
loadimage(img + i, fileName);
}
}
void randIntNum(int a) {
if (!a) {
int i, j;
srand((unsigned int)time(NULL));
while (1) {
i = rand() % 4;
j = rand() % 4;
if (map[i][j] == 0) {
map[i][