欢迎访问 如意编程网!

如意编程网

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

2048小游戏 c++

发布时间:2024/5/15 c/c++ 4 豆豆
如意编程网 收集整理的这篇文章主要介绍了 2048小游戏 c++ 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

这个游戏需要用到一个图形库。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][j] = (rand() % 2 + 1) * 2;break;}}} }void drawMap() {int x, y, k;for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){x = 100 * j;y = 100 * i;for ( k = 0; k <12; k++){if (map[i][j] == imgIndex[k]) {break;}}putimage(x, y, img + k);}}}int gameOver() {int flag1=0, flag2=0;for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {if (!map[i][j]) {flag1 = 1;}}}for (int i = 0; i < 3; i++) {for (int j = 0; j < 4; j++) {if (map[j][i] == map[j][i + 1]||map[i][j]==map[i+1][j]) {flag2 = 1;}}}if (flag1!=1&&flag2==1) {return 0;}return 1;}void merge_Num(int a1,int a2,int b1,int b2){map[a1][a2] += map[b1][b2];map[b1][b2] = 0;}int move_d() {int flag = 0;for (int i = 0; i < 4; i++) {for (int j = 3; j >= 0; j--) {int curKey = map[i][j]; //记录当前数字if (curKey) {int k = j - 1;while (k >= 0) {int curNext = map[i][k]; //记录当前数字的后一个if (curNext != 0) {if (curKey == curNext) { //如果相等则相加flag = 1; //标记置为1merge_Num(i, j, i, k); //相加}k = -1; //如果不相等,退出进行下个数字的检验break;}k--;}}}}for (int i = 0; i < 4; i++) {for (int j = 3; j >= 0; j--) {int curKey = map[i][j];if (!curKey) {int k = j - 1;while (k >= 0) {int curNext = map[i][k];if (curNext != 0) {flag = 1;merge_Num(i, j, i, k);k = -1;break;}k--;}}}}if (flag) {return 0;}else {return 1;}}int move_s() {int flag = 0;for (int i = 0; i < 4; i++) {for (int j = 3; j >= 0; j--) {int curKey = map[j][i];if (curKey) {int k = j - 1;while (k >= 0) {int curNext = map[k][i];if (curNext != 0) {if (curKey == curNext) {flag = 1;merge_Num(j, i, k, i);}k = -1;break;}k--;}}}}for (int i = 0; i < 4; i++) {for (int j = 3; j >= 0; j--) {int curKey = map[j][i];if (!curKey) {int k = j - 1;while (k >= 0) {int curNext = map[k][i];if (curNext != 0) {flag = 1;merge_Num(j, i, k, i);k = -1;break;}k--;}}}}if (flag) {return 0;}else {return 1;} }int move_a() {int flag = 0;for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {int curKey = map[i][j];if (curKey) {int k = j + 1;while (k <= 3) {int curNext = map[i][k];if (curNext != 0) {if (curKey == curNext) {flag = 1;merge_Num(i, j, i, k);}k = -1;break;}k++;}}}}for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {int curKey = map[i][j];if (!curKey) {int k = j + 1;while (k <= 3) {int curNext = map[i][k];if (curNext != 0) {flag = 1;merge_Num(i, j, i, k);k = -1;break;}k++;}}}}if (flag) {return 0;}else {return 1;} }int move_w() {int flag = 0;for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {int curKey = map[j][i];if (curKey) {int k = j + 1;while (k <= 3) {int curNext = map[k][i];if (curNext != 0) {if (curKey == curNext) {flag = 1;merge_Num(j, i, k, i);}k = -1;break;}k++;}}}}for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {int curKey = map[j][i];if (!curKey) {int k = j + 1;while (k <= 3) {int curNext = map[k][i];if (curNext != 0) {flag = 1;merge_Num(j, i, k, i);k = -1;break;}k++;}}}}if (flag) {return 0;}else {return 1;} }void keyDown() {char key = _getch();switch (key){case 'w':case 'W':case 72:randIntNum(move_w());break;case's':case'S':case 80:randIntNum(move_s());break;case'a':case'A':case 75:randIntNum(move_a());break;case'd':case'D':case 77:randIntNum(move_d());break;}}

总结

以上是如意编程网为你收集整理的2048小游戏 c++的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得如意编程网网站内容还不错,欢迎将如意编程网推荐给好友。