原創不易,請勿抄襲
作者聯系方式 : QQ:993678929
文章目錄
- 0.游戲界面展示
- 1. 撰寫思路
- 2. 界面繪制
- 3.鍵盤控制
- 4.數字的生成與合并
- 5.游戲結束判定
- #.完整代碼
0.游戲界面展示


1. 撰寫思路
動手寫代碼之前,先設計好要做哪些部分,下面所述的是我個人的思路,如果有更好的思路歡迎探討,
我們肯定能想到的是游戲界面的繪制,游戲的鍵盤控制,游戲邏輯,
游戲邏輯中包括數字的生成,合并,分數計算,以及游戲結束的判斷,
棋盤是 4 x 4 的,且只需要顯示數字,這可以用一個int陣列來表示,
游戲程序中用戶移動之后生成數字,需要注意這個次序
合并的順序是按移動的逆方向來合并,而且一次移動只會合并一次,
比如有一行是4 2 2 2
那么向右劃(移動)的結果是0 4 2 4
向左劃的結果是4 4 2 0
不同的數字可以使用不同的顏色來顯示方便辨識,提高玩家的游戲體驗
后續還可以推出聯網天梯排行榜
2. 界面繪制
3.鍵盤控制
4.數字的生成與合并
5.游戲結束判定
#.完整代碼
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
const int W = 4;
const int H = 4;
int board[H][W]; //H:高 W:寬 board[2][1] 表示第二行第二個
int score;
const int keymap[4] = {72,80,75,77}; //依次為上下左右
const int dx[4] = { -1,1,0,0 }; //依次對應上下左右
const int dy[4] = { 0,0,-1,1 };
//2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 (超過這個數字的從頭開始)
const int color[] = {15,8,6,14, 4, 2,12 ,5, 13, 3, 10, 11, 1, 9, };
/* 顏色說明:
* 0 = 黑色 8 = 灰色
* 1 = 藍色 9 = 淡藍色
* 2 = 綠色 10 = 淡綠色
* 3 = 淺綠色 11 = 淡淺綠色
* 4 = 紅色 12 = 淡紅色
* 5 = 紫色 13 = 淡紫色
* 6 = 黃色 14 = 淡黃色
* 7 = 白色 15 = 亮白色
*/
bool judge() //0:ok 1:gameover
{
bool flag = true;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (board[i][j] == 0)
{
flag = false;
}
return flag;
}
void Set_Color(int color)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
}
void display()
{
system("cls");
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (board[i][j] == 0) printf(" ");
else {
Set_Color(color[ (int)log2(board[i][j])-1 ]); //根據數字設定顏色
printf("%-5d", board[i][j]);
Set_Color(7);
}if (j < 3) putchar('|');
}
putchar('\n');
for (int r = 0; r < 23; r++) putchar('-');
putchar('\n');
}
printf("當前得分: %d",score);
}
void move(int dir)
{
int x=0,y=0;
if (dy[dir]) //水平移動
{
for (int i = 0; i < H; i++) //遍歷每一行
{
y = (dy[dir] == 1) ? 3 : 0;
int j = y;
int top = y;
while (abs(j - y) < 3) {
j -= dy[dir];
if (board[i][top] == 0 &&board[i][j]!=0)
{
board[i][top] = board[i][j];
board[i][j] = 0;
}
else if (board[i][top]!=0 && board[i][j]==board[i][top])
{
board[i][top] *= 2;
score += board[i][top];
board[i][j] = 0;
}
else if (board[i][top] * board[i][j] != 0 && board[i][top] != board[i][j])
{
top -= dy[dir];
if (j != top)
{
board[i][top] = board[i][j];
board[i][j] = 0;
}
}
}
}
}
else if (dx[dir]) //垂直移動
{
for (int j = 0; j < W; j++) //遍歷每一列
{
x = (dx[dir] == 1) ? 3 : 0;
int i = x;
int top = x;
while (abs(i - x) < 3) {
i -= dx[dir];
if (board[top][j] == 0 && board[i][j] != 0)
{
board[top][j] = board[i][j];
board[i][j] = 0;
}
else if (board[top][j] != 0 && board[i][j] == board[top][j])
{
board[top][j] *= 2;
score += board[top][j];
board[i][j] = 0;
}
else if (board[top][j] * board[i][j] != 0 && board[top][j] != board[i][j])
{
top -= dx[dir];
if (i != top)
{
board[top][j] = board[i][j];
board[i][j] = 0;
}
}
}
}
}
}
void newNum()
{
int flag = 0;
int n = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (board[i][j] == 0 || board[i][j] == 2)
n++;
int end = rand() % n;
int t = 0;
for (int i = 0; i < 4 && !flag; i++)
for (int j = 0; j < 4 && !flag; j++)
{
if (board[i][j] == 0)
{
if (t == end) {
board[i][j] += 2;
flag = 1;
}
else t++;
}
}
}
void play()
{
newNum();
display();
while (!judge())
{
int ch = _getch();
for (int i = 0; i < 4; i++)
if (ch == keymap[i]) {
move(i);
newNum();
display();
}
Sleep(10); //防止連按
}
system("cls");
printf("\n\n\tGAME OVER!");
printf("\n\n\tSCORE:%d",score);
}
void init()
{
system("mode con:cols=24 lines=10");
memset(board, 0, sizeof(board));
score = 0;
}
int main()
{
init();
play();
getchar();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/256692.html
標籤:其他
上一篇:JavaScript_數字時鐘
