通过本文主要向大家介绍了c语言控制台,c语言控制台界面,c语言控制台程序,c语言控制台颜色,c语言控制台输入等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
效果不好,见谅,没事就写了一个!!!
/**
* @author Routh
* @main.c
* @date 2014, 4, 26
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
// console width
#define CONSOLE_WIDTH 80
#define BOX_WIDTH 10
int BOX[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
// the console output handle
HANDLE c_handle;
void setCursorPosition(short x, short y) {
static COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(c_handle, c);
}
void drawTheGameBox() {
printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
}
/**
* get a random position: R
* the x : 0xff & (R >> 4)
* the y : 0x0f & R
*/
int random() {
int i = 0, j = 0, _index = 0, rrr = 0;
int rand_arr[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
srand((unsigned)time(NULL));
// rand()
for(i = 0; i < 4; i ++) {
for(j = 0; j < 4; j ++) {
if(BOX[i][j] == 0) {
rand_arr[_index ++] = (0xff & i << 4) | (0xf & j);
}
}
}
if(_index == 0) {
return -1;
}
return rand_arr[rand() % _index];
}
/** the clean array.**/
int* alogs(int item[]) {
int i = 0, j = 0;
int tep[4] = {0, 0, 0, 0}, tmp[4] = {0, 0, 0, 0};
for(i = 0; i < 4; i ++) {
if(item[i] != 0) {
tep[j ++] = item[i];
}
}
for(i = 0; i < 3; i ++) {
if(tep[0] == 0) break;
if(tep[i] == tep[i + 1]) {
tep[i] *= 2;
tep[i + 1] = 0;
}
}
j = 0;
for(i = 0; i < 4; i ++) {
if(tep[i] != 0) {
tmp[j ++] = tep[i];
}
}
return (int *)(&tmp);
}
/** BOX can move.*/
int validate(int item[]) {
int i = 0;
for(i = 0; i < 3; i ++) {
if(item[i] != 0 && item[i] == item[i + 1]) return 1;
if(item[i] == 0 && item[i + 1] != 0) return 1;
}
return 0;
}
int keydownControlx(int key) {
int i = 0, j = 0;
int *p;
int tp[4] = {0, 0, 0, 0};
switch(key) {
case 72: // up
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[0][i];
tp[1] = BOX[1][i];
tp[2] = BOX[2][i];
tp[3] = BOX[3][i];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[0][i] = p[0];
BOX[1][i] = p[1];
BOX[2][i] = p[2];
BOX[3][i] = p[3];
}
return j != 4;
case 80: // down
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[3][i];
tp[1] = BOX[2][i];
tp[2] = BOX[1][i];
tp[3] = BOX[0][i];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[3][i] = p[0];
BOX[2][i] = p[1];
BOX[1][i] = p[2];
BOX[0][i] = p[3];
}
return j != 4;
case 75: // left
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[i][0];
tp[1] = BOX[i][1];
tp[2] = BOX[i][2];
tp[3] = BOX[i][3];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[i][0] = p[0];
BOX[i][1] = p[1];
BOX[i][2] = p[2];
BOX[i][3] = p[3];
}
return j != 4;
case 77: // right
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[i][3];
tp[1] = BOX[i][2];
tp[2] = BOX[i][1];
tp[3] = BOX[i][0];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[i][3] = p[0];
BOX[i][2] = p[1];
BOX[i][1] = p[2];
BOX[i][0] = p[3];
}
return j != 4;
default: return 0;
}
return 0;
}
int main() {
int i = 0, j = 0, r = 0;
/** set the cursor, visible or invisible.*/
CONSOLE_CURSOR_INFO cci = {1, 0};
/** get the console output handle.*/
c_handle = GetStdHandle(STD_OUTPUT_HANDLE);
// hide the cursor.
SetConsoleCursorInfo(c_handle, &cci);
//
SetConsoleTextAttribute(c_handle, 0x3);
//system("color 30");
drawTheGameBox();
r = random();
if(rand() % 2 == 0) {
BOX[0xff & ( r >> 4)][0xf & r] = 2;
} else {
BOX[0xff & ( r >> 4)][0xf & r] = 4;
}
for(i = 0; i < 4; i ++) {
for(j = 0; j < 4; j ++) {
if(BOX[i][j] == 0) continue;
setCursorPosition(17 + j * 8 + 2 + (j * 2), i * 4 + (i + 2));
//SetConsoleTextAttribute(c_handle, BOX[i][j]);
printf("%d", BOX[i][j]);
}
}
// begin
while(1) {
Sleep(100);
// key down.
while (_kbhit())
{
// the key down fun.
if(!keydownControlx(_getch())) continue;
// clear the console and redraw.
system("cls");
SetConsoleTextAttribute(c_handle, 0x3);
drawTheGameBox();
for(i = 0; i < 4; i ++) {
for(j = 0; j < 4; j ++) {
if(BOX[i][j] == 0) continue;
setCursorPosition(17 + j * 8 + 2 + (j * 2), i * 4 + (i + 2));
//SetConsoleTextAttribute(c_handle, BOX[i][j]);
printf("%d", BOX[i][j]);
}
}
r = random();
if( r == -1 ) { // game over
//SetConsoleTextAttribute(c_handle, 0xff0000);
setCursorPosition(27, 10);
printf("GAME ORVER!!!!!!!");
}
if(rand() % 2 == 0) {
BOX[0xff & ( r >> 4)][0xf & r] = 2;
} else {
BOX[0xff & ( r >> 4)][0xf & r] = 4;
}
Sleep(100);
setCursorPosition(17 + (0xf & r) * 8 + 2 + ((0xf & r) * 2), (0xff & ( r >> 4)) * 4 + ((0xff & ( r >> 4)) + 2));
//SetConsoleTextAttribute(c_handle, BOX[0xff & ( r >> 4)][0xf & r]);
printf("%d", BOX[0xff & ( r >> 4)][0xf & r]);
}
}
return 0;
}
</div>
附上另外一个小伙伴的代码
/* * Copyright (C) Judge Young * E-mail: yjjtc@126.com * Version: 1.0 */ #include <stdio.h> #include <time.h> /* 包含设定随机数种子所需要的time()函数 */ #include <conio.h> /* 包含Windows平台上完成输入字符不带回显和回车确认的getch()函数 */ #include <windows.h> /* 包含Windows平台上完成设定输出光标位置达到清屏功能的函数 */ void start_game(); /* 开始游戏 */ void reset_game(); /* 重置游戏 */ /* 往左右上下四个方向移动 */ void move_left(); void move_right(); void move_up(); void move_down(); void refresh_show(); /* 刷新界面显示 */ void add_rand_num(); /* 生成随机数,本程序中仅生成2或4,概率之比设为2:1 */ void check_game_over(); /* 检测是否输掉游戏,设定游戏结束标志 */ int get_null_count(); /* 获取游戏面板上空位置数量 */ int board[4][4]; /* 游戏数字面板,抽象为二维数组 */ int score; /* 游戏的分 */ int best; /* 游戏最高分 */ int if_need_add_num; /* 是否需要生成随机数标志,1表示需要,0表示不需要 *

