2048源代码

跟着B站老师 做的,但最后老师没做完,我完善了一下(实现核心功能以及游戏成功、失败的弹窗界面)。【C/C++】如何快速用C语言写一个2048游戏?从零到成功,只需2小时,你还在担心学不会编程吗_哔哩哔哩_bilibili

#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<iostream>
#include<time.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include <WINDOWS.H>

using namespace std;

#define TARGET 2048
#define SIZE 4
#define INTERVAL 15
#define GRID_W 100

enum Color //枚举格子颜色
{   
    zero     = RGB(205, 193, 180), //0的颜色
    twoTo1   = RGB(238, 228, 218), //2的颜色
    twoTo2   = RGB(237, 224, 200), //4的颜色
    twoTo3   = RGB(242, 177, 121), //8的颜色
    twoTo4   = RGB(245, 149,  99), //16的颜色
    twoTo5   = RGB(246, 124,  95), //32的颜色
    twoTo6   = RGB(246,  94,  59), //64的颜色
    twoTo7   = RGB(242, 177, 121), //128的颜色
    twoTo8   = RGB(237, 204,  97), //256的颜色
    twoTo9   = RGB(255,   0, 128), //512的颜色
    twoTo10  = RGB(145,  0,  72), //1024的颜色
    twoTo11  = RGB(242, 17, 158), //2048的颜色
    back = RGB(187, 173, 160), //背景颜色
     
};
Color arr[13] = {zero,twoTo1,twoTo2,twoTo3,twoTo4,twoTo5,twoTo6,twoTo7,twoTo8,twoTo9,twoTo10,twoTo11, back};

bool isLose;
int map[SIZE][SIZE];
bool flag;//添加flag判断是否发生了移动。若无移动则不执行gameInit(b);
bool isWin;

void upMove(){
    for (int j = 0; j < SIZE; j++)
    {
        int temp = 0;
        for (int i = 1; i < SIZE; i++)
        {
            if ( map[i][j] )
            {
                if(map[temp][j] == 0){
                    map[temp][j] = map[i][j];
                    map[i][j] = 0;
                    flag =true;
                }
                else if(map[temp][j] == map[i][j]){
                    map[temp][j] += map[i][j];
                    map[i][j] = 0;
                    if(map[temp][j] == TARGET){
                        isWin =true;
                        break;
                    }
                    temp++;
                    flag =true;
                }
                else {
                    map[temp + 1][j] = map[i][j];
                    if(temp +1 != i){
                        map[i][j] =0 ;
                        flag =true;
                    }

                    temp++;
                }

            }
                
        }

    }  

}

void rightMove(){
    for (int i = SIZE -1; i >= 0; i--)
    {
        int temp = SIZE-1;
        for (int j = SIZE -2; j >= 0; j--)
        {
            if ( map[i][j] )
            {
                if(map[i][temp] == 0){
                    map[i][temp] = map[i][j];
                    map[i][j] = 0;
                    flag =true;
                }
                else if(map[i][temp] == map[i][j]){
                    map[i][temp] += map[i][j];
                    map[i][j] = 0;
                    if(map[i][temp] ==TARGET){
                        isWin =true;
                        break;
                    }
                    flag =true;
                    temp--;
                }
                else {
                    map[i][temp -1] = map[i][j];
                    if(temp -1 != j){
                        map[i][j] =0 ;
                        flag =true;
                    }

                    temp--;
                }

            }
                
        }

    }  

}

void downMove(){
    for (int j = SIZE -1; j >= 0; j--)
    {
        int temp = SIZE-1;
        for (int i = SIZE -2; i >= 0; i--)
        {
            if ( map[i][j] )
            {
                if(map[temp][j] == 0){
                    map[temp][j] = map[i][j];
                    map[i][j] = 0;
                    flag =true;
                }
                else if(map[temp][j] == map[i][j]){
                    map[temp][j] += map[i][j];
                    map[i][j] = 0;
                    if(map[temp][j] ==TARGET){
                        isWin =true;
                        break;
                    }
                    flag =true;
                    temp--;
                }
                else {
                    map[temp - 1][j] = map[i][j];
                    if(temp -1 != i){
                        map[i][j] =0 ;
                        flag =true;
                    }

                    temp--;
                }

            }
                
        }

    }  

}

void leftMove(){
    for (int i = 0; i < SIZE; i++)
    {
        int temp = 0;
        for (int j = 1; j < SIZE; j++)
        {
            if ( map[i][j] )
            {
                if(map[i][temp] == 0){
                    map[i][temp] = map[i][j];
                    map[i][j] = 0;
                    flag =true;
                }
                else if(map[i][temp] == map[i][j]){
                    map[i][temp] += map[i][j];
                    map[i][j] = 0;
                    if(map[i][temp] ==TARGET){
                        isWin =true;
                        break;
                    }
                    flag =true;
                    temp++;
                }
                else {
                    map[i][temp + 1] = map[i][j];
                    if(temp +1 != j){
                        map[i][j] =0 ;
                        flag =true;
                    }

                    temp++;
                }

            }
                
        }

    }  

}


void keyDeal(){
    char key =_getch();
    flag = false;
    isWin=false;
    switch (key)
    {
    case 'w':
    case 'W':
        upMove();
        break;
    case 'S':
    case 's':
        downMove();
        break;
    case 'A':
    case 'a':
        leftMove();
        break;
    case 'd':
    case 'D':
        rightMove();
        break;
    
    default:
        break;
    }

}


int create_num(){
   //srand((int)time(NULL));
    int num = rand()%10;
    if(num >2){
        return 2;

    }

    else return 4;
}

//产生随机数(2或4)
void gameInit(int num){
    srand((int)time(NULL));
    
    for (int i = 0; i < num; )
    {
        int x = rand()%SIZE;
        int y = rand()%SIZE;
        if(map[x][y] ==0){
            map[x][y] = create_num();
            i++;
        }
        
    }
}

void judge_lose(){

    isLose = true;

    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE-1; ++j) {
            if (map[i][j] == map[i][j + 1] || map[j][i] == map[j + 1][i]) {
                isLose = false;
                return;
            }
        }
    }

}

void draw(){
    
    setbkcolor(RGB(187,173,160));
    cleardevice();

    for(int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            
                int m = (j+1)*INTERVAL + j*GRID_W;
                int n = (i+1)*INTERVAL + i*GRID_W;
                COLORREF color = arr[(int) log2( map[i][j] )];
               // cout<<(int) log2( map[i][j] )<<endl;
               // cout<<arr[(int) log2( map[i][j] )]<<endl;
                setfillcolor(color);
                solidrectangle(m,n,m+GRID_W,n+GRID_W);
                

            if(map[i][j]){  
                settextstyle(50,0,_T("黑体"));
                settextcolor(RGB(119,110,101));
                setbkmode(TRANSPARENT);  
                char str[10]="";
                sprintf_s(str,"%d",map[i][j]);

                
                int tw = textwidth(str);
                int th = textheight(str);
                int tm = (GRID_W - tw) /2;
                int tn = (GRID_W - th) /2;


                //SetTextColor(HDC,RGB(119,110,101));
                
                outtextxy(m+tm,n+tn,str);
                // sprintf_s


            }
            
            
        }
        
        /* code */
    }
    

}

int main(int argc, char** argv)
{
    initgraph(SIZE*GRID_W+INTERVAL*(SIZE+1),SIZE*GRID_W+INTERVAL*(SIZE+1),TRUE);
 
    int a =2;
    int b =1;
    gameInit(a);
    


    while (!isWin && !isLose){
        
        draw(); 
        keyDeal();
        judge_lose();
        
       // cout<<flag<<endl;
        if (flag)
        {
            gameInit(b);
        }
        
        
    };
    draw();
    if(isWin){
        MessageBox(NULL,TEXT("CONGRATULATIONS!"),TEXT("GAME OVER"),MB_OK);
    }
    
    if(isLose){
        MessageBox(NULL,TEXT("YOU LOSE!"),TEXT("GAME OVER"),MB_OK);
    }
    

    closegraph();



  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值