2048小程序

本文介绍了一个突发奇想用C语言编写的2048小程序,玩家通过wasd键控制方块移动。文章附带了源代码。

2048小程序

突发奇想做的,wasd控制上下左右移动,中间移动方块部分的函数其实还可以简化,贴一下代码

源代码:

//encoding:UTF-8
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>
#include "conio.h"

using namespace std;

/*
WELCOME UI:
*************************************
*                                   *
*              WELCOME!             *
*                                   *
*                                   *
*                2048               *
*                                   *
*                                   *
*  press 'e' to start the game      *
*  press 'q' to quit the game       *
*  press 'h' to check the high-score*
*************************************
*/
char welcome[12][40] = {
   
   
"*************************************\n",
"*                                   *\n",
"*              WELCOME!             *\n",
"*                                   *\n",
"*                                   *\n",
"*                2048               *\n",
"*                                   *\n",
"*                                   *\n",
"*  press 'e' to start the game      *\n",
"*  press 'q' to quit the game       *\n",
"*  press 'h' to check the high-score*\n",
"*************************************\n"
};


/*
UI design:
    _____________________________
s0  |      |      |      |      |
    | 2048 | 2048 | 2048 | 2048 |
s1  |______|______|______|______|
d0  |      |      |      |      |
    | 2048 | 2048 | 2048 | 2048 |
s1  |______|______|______|______|
d0  |      |      |      |      |
    | 2048 | 2048 | 2048 | 2048 |
s1  |______|______|______|______|
d0  |      |      |      |      |
    | 2048 | 2048 | 2048 | 2048 |
s1  |______|______|______|______|

*/
char s0[] = {
   
   "_____________________________\n|      |      |      |      |\n"};
char s1[] = {
   
   "|______|______|______|______|\n"};
char d0[] = {
   
   "|      |      |      |      |\n"};
//建立界面数组并赋值为0
int num[4][4], score, highscore;
FILE *f1;
bool null_file;

void creat_game(int a[4][4]);
bool creat_random_number(int a[4][4]);

//欢迎界面
void _welcome()
{
   
   
    int n = 0;
    while(n <= 11)
        printf("%s", welcome[n++]);
}

//菜单选择
int chose()
{
   
   
    char first = getch();
    while(1){
   
   
        while(first != 'e' && first != 'q' && first != 'h'){
   
   
            first = getch();
        }
        if(first == 'q')
            exit(0);
        else if(first == 'h'){
   
   
            if(null_file)
                printf("No high score!\n");
            else{
   
    
                fscanf(f1,"%d", &highscore);
                if(highscore == EOF)
                    printf("No high score!\n");
                else
                    printf("The high score is: %d\n", highscore);
            }
        }
        else
            return 0;
        first = getch();
    }
}

//打印出当前UI
void show_UI(int a[4][4])
{
   
   
    printf("%s", s0);
    for(int i = 0; i <= 3; i++){
   
   
        for(int j = 0; j <= 3; j++)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值