C++基于EasyX图形库实现2048小游戏

C++ 和 EasyX 图形库,实现2048小游戏,供大家参考,具体内容如下

MainGame2048.cpp

/** Name: Game2048CoreClass*/
#include<iostream>
#include<graphics.h>
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdio.h>
#include"Game2048.h"
#define BLOCK_SIZE 60
#define SIZE_COL 10
#define SIZE_ROW 10
using namespace std;
void DisplayMap(Game2048& mygame);
int GetMove();
int main(int argc, char * argv[])
{
 HWND hwnd=initgraph(SIZE_COL*BLOCK_SIZE, SIZE_ROW*BLOCK_SIZE);
 setbkmode(TRANSPARENT);
 setbkcolor(RGB(180,180,180));
 settextcolor(RGB(0,180,80));
 cleardevice();
 while (1)
 {
 Game2048 mygame(SIZE_ROW, SIZE_COL);
 while (1)
 {
 DisplayMap(mygame);
 int mov = GetMove();
 cout << mov << endl;
 if (!mygame.Run(mov))
 break;
 }
 if (MessageBox(hwnd, "游戏结束,是否重玩?", "Tips", MB_YESNO) == IDNO)
 break;
 }

 return 0;
}
int GetMove()
{
 char move = '\0';
 move = getch();
 if (move == 'w' || move == '8' || move == 'W')
 return MOV_UP;
 if (move == 's' || move == '5' || move == 'S')
 return MOV_DOWN;
 if (move == 'a' || move == '4' || move == 'A')
 return MOV_LEFT;
 if (move == 'd' || move == '6' || move == 'D')
 return MOV_RIGHT;
 if (move == '*')
 return MOV_NULL;
 return 0;
}
void DisplayMap(Game2048& mygame)
{
 BeginBatchDraw();
 cleardevice();
 char temp[20] = { 0 };
 system("cls");
 cout << mygame.GetScore() << " " << mygame.GetStep() << " " << mygame.GetUsedTime()<<" " << mygame.GetMaxNum() << endl;
 for (int i = 0; i<mygame.GetLines(); i++)
 {
 for (int j = 0; j<mygame.GetCols(); j++)
 {
 cout.width(4);
 cout << mygame.MapAt(i,j);
 if (mygame.MapAt(i, j) == 0)
 {
 setfillcolor(RGB(220,220,220));
 solidcircle(j*BLOCK_SIZE + BLOCK_SIZE / 2, i*BLOCK_SIZE + BLOCK_SIZE / 2, BLOCK_SIZE / 2);
 }
 else
 {
 int size=mygame.MapAt(i, j);
 sprintf(temp, "%d\0",size );
 size /= 2;
 setfillcolor(RGB(200, 255-size*20, 0+size*20));
 solidcircle(j*BLOCK_SIZE + BLOCK_SIZE / 2, i*BLOCK_SIZE + BLOCK_SIZE / 2, BLOCK_SIZE / 2);
 outtextxy(j*BLOCK_SIZE, i*BLOCK_SIZE,temp);
 }

 }
 printf("\n");
 }
 cout << "-------" << endl;
 sprintf(temp,"Score : %d\0",mygame.GetScore());
 outtextxy(0, 1*BLOCK_SIZE/3, temp);
 sprintf(temp, "Step : %d\0", mygame.GetStep());
 outtextxy(0, 2 * BLOCK_SIZE / 3, temp);
 sprintf(temp, "Time : %d\0", mygame.GetUsedTime());
 outtextxy(0, 3 * BLOCK_SIZE / 3, temp);
 sprintf(temp, "MAX : %d\0", mygame.GetMaxNum());
 outtextxy(0, 4 * BLOCK_SIZE / 3, temp);
 EndBatchDraw();
}

Game2048.h

#ifndef _GAME2048_H_
#define _GAME2048_H_
/*For example:
Game2048 mygame(10,10);
int main(int argc, char * argv[])
{
 while (1)
 {
 DisplayMap();
 int mov = GetMove();
 cout << mov << endl;
 if (!mygame.Run(mov))
 break;
}
system("pause");
return 0;
}
*/
#include<stdlib.h>
#include<time.h>
typedef int MoveDirect;
#define MOV_NULL 0
#define MOV_UP 8
#define MOV_DOWN 5
#define MOV_LEFT 4
#define MOV_RIGHT 6
class Game2048
{
public:
 Game2048(int line=5,int col=5);
 ~Game2048();
 bool Run(MoveDirect mov);
 int GetCols();
 int GetLines();
 int MapAt(int rindex, int cindex);//return >=0 is true,-1 is bad index,0 mean space,other mean number.
 int GetStep();
 int GetScore();
 int GetUsedTime();
 int GetMaxNum();
 void clear();
private:
 void CreateNew();
 void MoveAndResult(MoveDirect mov);
 bool IsDead();
 //运行图和运行时环境
 int * Map;
 int lines;
 int cols;
 int step;
 int core;
 long runtime;
 int usetime;
 int maxnum;

};

#endif // _GAME2048_H_

Game2048.cpp

#include"Game2048.h"
Game2048::Game2048(int line,int col)
{
 this->lines = line;
 this->cols = col;
 this->step=0;
 this->core=0;
 this->runtime=0;
 this->usetime=0;
 this->maxnum=2;
 this->Map=(int *)malloc(sizeof(int)*(this->lines)*(this->cols));
 runtime = time(NULL); //记录开始时间,用于算总时长
 srand((unsigned)time(NULL));
 for (int i = 0; i<this->lines; i++)
 {
 for (int j = 0; j<this->cols; j++)
 {
 Map[i*this->cols+j] = 0;
 }
 }
 CreateNew();
}
Game2048::~Game2048()
{
 free(this->Map);
}
void Game2048::clear()
{
 this->step = 0;
 this->core = 0;
 this->runtime = 0;
 this->usetime = 0;
 this->maxnum = 2;
 runtime = time(NULL); //记录开始时间,用于算总时长
 srand((unsigned)time(NULL));
 for (int i = 0; i<this->lines; i++)
 {
 for (int j = 0; j<this->cols; j++)
 {
 Map[i*this->cols + j] = 0;
 }
 }
 CreateNew();
}
bool Game2048::Run(MoveDirect mov)
{
 CreateNew();
 MoveAndResult(mov);
 if (step > (lines*cols * 2))
 if (IsDead() == 1)
 return false;
 usetime = time(NULL) - runtime;
 return true;
}
int Game2048::GetCols()
{
 return this->cols;
}
int Game2048::GetLines()
{
 return this->lines;
}
int Game2048::MapAt(int rindex, int cindex)
{
 if (rindex<0||cindex<0||rindex >= this->lines || cindex >= this->cols)
 return -1;
 return Map[rindex*this->cols+cindex];
}
int Game2048::GetStep()
{
 return this->step;
}
int Game2048::GetScore()
{
 return this->core;
}
int Game2048::GetUsedTime()
{
 return this->usetime;
}
int Game2048::GetMaxNum()
{
 return this->maxnum;
}
void Game2048::CreateNew()
{
 int hasfull = 1;
 for (int i = 0; i<lines; i++)
 {
 for (int j = 0; j<cols; j++)
 {
 if (Map[i*this->cols+j] == 0)
 hasfull = 0; //判断是否满了,不满才创建
 }
 }
 if (hasfull == 1)
 return;
 int si, sj;
 si = rand() % lines;
 sj = rand() % cols;
 while (Map[si*this->cols+sj] != 0)
 {
 si = rand() % lines;
 sj = rand() % cols;
 }
 Map[si*this->cols+sj] = 2;
}
bool Game2048::IsDead()
{
 for (int i = 0; i<lines; i++)
 {
 for (int j = 0; j<cols; j++)
 {
 if (Map[i*this->lines + j] == 0)
 return false; //如果存在空的格则肯定不结束
 int up, down, right, left;
 up = i - 1;
 down = i + 1;
 right = j + 1;
 left = j - 1; //四个方向进行判定
 while (up >= 0 && Map[up*this->lines + j] == 0)
 up--;
 if (Map[up*this->lines + j] == Map[i*this->lines + j] && up != -1) //只要一个方向可以合并则不结束
 return false;
 while (down<lines&&Map[down*this->lines + j] == 0)
 down--;
 if (Map[down*this->lines + j] == Map[i*this->lines + j] && down != lines)
 return false;
 while (right<cols&&Map[i*this->lines + right] == 0)
 right++;
 if (Map[i*this->lines + right] == Map[i*this->lines + j] && right != cols)
 return false;
 while (left >= 0 && Map[i*this->lines + left] == 0)
 left--;
 if (Map[i*this->lines + left] == Map[i*this->lines + j] && left != -1)
 return false;
 }
 }
 return true; //排除所有情况不结束,肯定结束了
}
void Game2048::MoveAndResult(MoveDirect mov)
{
 if (mov == MOV_NULL)
 return;
 step++; //步数增加
 int ffind, nfind;
 if (mov == MOV_UP)
 {
 for (int i = 0; i<cols; i++)
 {
 ffind = -1;
 nfind = -1;
 for (int j = 0; j<lines; j++)
 {
 int k = j;
 while (k<lines&&Map[k*this->cols+i] == 0)
 k++;
 if (k != lines)
 ffind = k;
 k++;
 while (k<lines&&Map[k*this->lines + i] == 0)
 k++;
 if (k != lines)
 nfind = k; //获取第一个不为零和下一个不为零
 if (ffind != -1 && nfind != -1)
 {
 if (ffind != nfind)
 {
 if (Map[ffind*this->lines + i] == Map[nfind*this->lines + i]) //两个获取相等则叠加
 {
 Map[ffind*this->lines + i] *= 2;
 if (Map[ffind*this->lines + i]>maxnum)
 maxnum = Map[ffind*this->lines + i];
 Map[nfind*this->lines + i] = 0;
 core++;  //分数增加
 }
 }
 }

 }
 int count = 0;
 for (int j = 0; j<lines; j++) //单边对齐
 {
 if (Map[j*this->lines + i] != 0)
 {
 int temp = Map[j*this->lines + i];
 Map[j*this->lines + i] = 0;
 Map[count*this->lines + i] = temp;
 count++;
 }

 }

 }
 }
 else if (mov == MOV_DOWN)
 {
 for (int i = 0; i<cols; i++)
 {
 ffind = -1;
 nfind = -1;
 for (int j = lines; j >= 0; j--)
 {
 int k = j;
 while (k >= 0 && Map[k*this->cols+i] == 0)
 k--;
 if (k != -1)
 ffind = k;
 k--;
 while (k >= 0 && Map[k*this->cols+i] == 0)
 k--;
 if (k != -1)
 nfind = k;
 if (ffind != -1 && nfind != -1)
 {
 if (ffind != nfind)
 {
 if (Map[ffind*this->cols+i] == Map[nfind*this->cols+i])
 {
 Map[ffind*this->cols+i] *= 2;
 if (Map[ffind*this->cols+i]>maxnum)
 maxnum = Map[ffind*this->cols+i];
 Map[nfind*this->cols+i] = 0;
 core++;
 }
 }
 }

 }
 int count = lines - 1;
 for (int j = lines - 1; j >= 0; j--)
 {
 if (Map[j*this->cols+i] != 0)
 {
 int temp = Map[j*this->cols+i];
 Map[j*this->cols+i] = 0;
 Map[count*this->cols+i] = temp;
 count--;
 }

 }
 }
 }
 else if (mov == MOV_LEFT)
 {
 for (int i = 0; i<lines; i++)
 {
 ffind = -1;
 nfind = -1;
 for (int j = 0; j<cols; j++)
 {
 int k = j;
 while (k<cols&&Map[i*this->cols+k] == 0)
 k++;
 if (k != cols)
 ffind = k;
 k++;
 while (k<cols&&Map[i*this->cols+k] == 0)
 k++;
 if (k != cols)
 nfind = k;
 if (ffind != -1 && nfind != -1)
 {
 if (ffind != nfind)
 {
 if (Map[i*this->cols+ffind] == Map[i*this->cols+nfind])
 {
 Map[i*this->cols+ffind] *= 2;
 if (Map[i*this->cols+ffind]>maxnum)
 maxnum = Map[i*this->cols+ffind];
 Map[i*this->cols+nfind] = 0;
 core++;
 }
 }
 }

 }
 int count = 0;
 for (int j = 0; j<cols; j++)
 {
 if (Map[i*this->cols+j] != 0)
 {
 int temp = Map[i*this->cols+j];
 Map[i*this->cols+j] = 0;
 Map[i*this->cols+count] = temp;
 count++;
 }

 }
 }
 }
 else if (mov == MOV_RIGHT)
 {
 for (int i = 0; i<lines; i++)
 {
 ffind = -1;
 nfind = -1;
 for (int j = cols; j >= 0; j--)
 {
 int k = j;
 while (k >= 0 && Map[i*this->cols+k] == 0)
 k--;
 if (k != -1)
 ffind = k;
 k--;
 while (k >= 0 && Map[i*this->cols+k] == 0)
 k--;
 if (k != -1)
 nfind = k;
 if (ffind != -1 && nfind != -1)
 {
 if (ffind != nfind)
 {
 if (Map[i*this->cols+ffind] == Map[i*this->cols+nfind])
 {
 Map[i*this->cols+ffind] *= 2;
 if (Map[i*this->cols+ffind]>maxnum)
 maxnum = Map[i*this->cols+ffind];
 Map[i*this->cols+nfind] = 0;
 core++;
 }
 }
 }

 }
 int count = cols - 1;
 for (int j = cols - 1; j >= 0; j--)
 {
 if (Map[i*this->cols+j] != 0)
 {
 int temp = Map[i*this->cols+j];
 Map[i*this->cols+j] = 0;
 Map[i*this->cols+count] = temp;
 count--;
 }

 }
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C++ 实现2048游戏示例

    这游戏前一段时间传的很火,前几天早上实在太无聊了,就决定把这游戏自己也写一个. 前后写了一个多小时吧,大概300行左右,没什么复杂算法,不过实在懒得去优化了,但估计优化完能控制在200行以下,有兴趣的朋友可以自己优化一下. 说明:我一开始玩的是IOS APP版的TRHEES,后来才玩的2048,两者在滑动的规则上有些区别,本人这个版本是这两者的结合. 最后,祝试玩愉快! 界面丑陋,求不笑. 以下是源代码: 复制代码 代码如下: /*By Reason*/#include<iostream>#i

  • 用VC++6.0的控制台实现2048小游戏的程序

    首先感谢这位大侠的无私分享,仔细学习这个程序以后收获很多,试着添加一些注释 源程序是从开源中国看到的,原作者是 刘地(sir?) 地址为http://www.oschina.net/code/snippet_593413_46040 geek_monkey于2015年3月5日为拜读该程序,受益匪浅 为了方便自己,以及更多初学者阅读,我试着写了写了注释供参考 我是C语言初学者,如有错误希望指正.轻喷 复制代码 代码如下: #include <stdlib.h> #include <stdi

  • C++基于EasyX图形库实现2048小游戏

    C++ 和 EasyX 图形库,实现2048小游戏,供大家参考,具体内容如下 MainGame2048.cpp /** Name: Game2048CoreClass*/ #include<iostream> #include<graphics.h> #include<stdio.h> #include<windows.h> #include<conio.h> #include<stdio.h> #include"Game2

  • C++基于EasyX库实现拼图小游戏

    用C++的EasyX库做的拼图小游戏,供大家参考,具体内容如下   记录一下自己做的第一个项目,还有一些改进空间QWQ,可以支持难度升级,但是通关判断似乎有点小问题肯定不是我菜通不了关 . #pragma once #include <iostream> #include <graphics.h> #include <Windows.h> #include <algorithm> #include <easyx.h> #include <c

  • C++基于easyx图形库实现推箱子游戏

    本文实例为大家分享了C++实现推箱子游戏的具体代码,供大家参考,具体内容如下 头文件: #include<stdio.h> #include<stdlib.h> //#include<Windows.h> #include<conio.h> #include<graphics.h> #include<stdbool.h> //播放音乐需要的头文件 #include <mmsystem.h> #pragma comment(

  • Java完美实现2048小游戏

    完美地模仿了2048游戏,是根据网友的一个2048改的. Block.java import javax.swing.*; import java.awt.*; public class Block extends JLabel { private int value; public Block() { value = 0;//初始化值为0 setFont(new Font("font", Font.PLAIN, 40));//设定字体 setBackground(Color.gray

  • 使用Javascript写的2048小游戏

    最近心血来潮,项目结束了,使用javascript写个小游戏,练练收吧,写的不好还请各位大侠给出批评建议. HTML代码如下 <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <link rel="stylesheet" href="2048.css"/> <scri

  • 用Python写一个无界面的2048小游戏

    以前游戏2048火的时候,正好用其他的语言编写了一个,现在学习python,正好想起来,便决定用python写一个2048,由于没学过python里面的界面编程,所以写了一个极其简单的无界面2048.游戏2048的原理和实现都不难,正好可以拿来练手,要是不知道这游戏的话,可以去网上查一下,或者下载一个到手机来玩一下,我就不在说其原理.我知道不放图的话大家一点兴趣都没,下面首先放一张游戏成型图,然后我们在来讲如何一步步用最基础的知识来实现. 一.生成4*4的矩阵 游戏的第一步便是生成一个4*4的矩

  • jQuery编写网页版2048小游戏

    大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了,但是自己实现起来会遇到各种问题.比如,在最后判断游戏是否结束的时候,我写的语句语法是对的,但就是不执行.最后通过对视频源码的分析对比,发现原作者写的一个setTimeout定时器有额外的意思,本来我以为它就是简单的一个延时动画,其实他是在等待另外一个函数执行完毕.-_-||.最后还是很高兴能写出来,也改进了一些源

  • lua实现的2048小游戏

    lua实现的2048小游戏,只要可以运行lua脚本的环境下都可以玩. 复制代码 代码如下: --[[============================================================================= #     FileName: 2048.lua #         Desc: lua console 2048 #       Author: hanxi #        Email: hanxi.info@gmail.com #    

  • C语言实现2048小游戏

    本文实例为大家分享了C语言实现2048小游戏的具体代码,供大家参考,具体内容如下 具有以下特点: 1.linux下完成 2.非堵塞键盘读取 3.随机生成2和4 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define TTY_PATH "/dev/tty" #define STTY_ON "stty raw -echo -F" #define STTY_O

  • 基于JavaScript+HTML5 实现打地鼠小游戏逻辑流程图文详解(附完整代码)

    随着html5的兴起,那些公司对大型游戏的开发正在慢慢疏远,一.开发周期长:二.运营花费高:他们正找一些能够克服这些缺点的替代品.正好,html5的出现可以改变这些现状,在淘宝.京东等一些大型电商网站.QQ.微信等聊天软件都出现了html5的小游戏,这说明html5越来越受到大家的青睐.接下来我用javascript实现一个小型游戏---打地鼠. 一.游戏简介 打地鼠这个游戏相信大家都不陌生,也是童年时候一款经典的游戏.本次游戏的编写是以html文件形式完成的,并且使用HBulider软件进行编

随机推荐