安装方法
不多说,只要在终端输入以下代码即可安装
sudo apt install wget gcc
wget https://raw.githubusercontent.com/mevdschee/2048.c/master/2048.c
gcc -o 2048 2048.c
sudo cp 2048 /usr/sbin/2048
rm 2048*
安装结果:
解读代码
解读一下上方提供的代码,
sudo apt install wget gcc
这行代码的意思是安装下载程序以及C编辑器,如果系统已经有安装或者有更好的工具,可以忽略(如已安装aria2就不需要安装wget)
wget https://raw.githubusercontent.com/mevdschee/2048.c/master/2048.c
这行代码的意思是下载2048程序源码,你可以把连接https://raw.githubusercontent.com/mevdschee/2048.c/master/2048.c
复制到浏览器或者其他下载工具打开
gcc -o 2048 2048.c
这行代码的意思是编译下载下来的2048源码,可以使用其他编译器编译
sudo cp 2048 /usr/sbin/2048
这行代码的意思是将编译好的2048程序复制到/usr/sbin
目录下,就不需要先定位到2048程序所在位置,然后再使用./2048
目录运行2048程序
rm 2048*
这行代码的意思是删除刚刚下载以及编译的有关文件(也可以叫做删除安装临时文件),这行可有可无。
懒人安装器
对于一些懒人,我自己制作了安装器,源码如下:
#include <iostream>
using namespace std;
int main()
{
// 程序开始!
cout<<"开始安装,在此过程中可能需要输入root用户密码\n";
cout<<"安装所需程序……\n";
system("sudo apt install wget gcc -y"); // 安装对应的程序
cout<<"下载所需文件……\n";
system("wget https://raw.githubusercontent.com/mevdschee/2048.c/master/2048.c"); // 下载2048程序源码
cout<<"编译2048游戏……\n";
system("gcc -o 2048 2048.c"); // 编译程序源码
cout<<"复制快捷方式……\n";
system("sudo cp 2048 /usr/sbin/2048"); // 将程序复制到指定目录,以后可以直接输入命令打开游戏
cout<<"删除安装时的临时文件……\n";
system("rm 2048.c 2048.c* 2048"); // 删除源码、2048执行文件
cout<<"\n安装成功,你可以输入命令“2048”开始游戏!\n";
return 0; // 程序结束
}
源码可以直接复制然后用g++编译,或者在以下链接下载:https://download.csdn.net/download/weixin_46403483/12691450
欣赏2048源码
这是用C语言写的,代码如下:
/*
============================================================================
Name : 2048.c
Author : Maurits van der Schee
Description : Console version of the game "2048" for GNU/Linux
============================================================================
*/
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <signal.h>
#define SIZE 4
uint32_t score=0;
uint8_t scheme=0;
void getColor(uint8_t value, char *color, size_t length) {
uint8_t original[] = {
8,255,1,255,2,255,3,255,4,255,5,255,6,255,7,255,9,0,10,0,11,0,12,0,13,0,14,0,255,0,255,0};
uint8_t blackwhite[] = {
232,255,234,255,236,255,238,255,240,255,242,255,244,255,246,0,248,0,249,0,250,0,251,0,252,0,253,0,254,0,255,0};
uint8_t bluered[] = {
235,255,63,255,57,255,93,255,129,255,165,255,201,255,200,255,199,255,198,255,197,255,196,255,196,255,196,255,196,255,196,255};
uint8_t *schemes[] = {
original,blackwhite,bluered};
uint8_t *background = schemes[scheme]+0;
uint8_t *foreground = schemes[scheme]+1;
if (value > 0) while (value--) {
if (background+2<schemes[scheme]+sizeof(original)) {
background+=2;
foreground+=2;
}
}
snprintf(color,length,"\033[38;5;%d;48;5;%dm",*foreground,*background);
}
void drawBoa