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++)

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

被折叠的 条评论
为什么被折叠?



