会随机消除某几个数,增加游戏难度~(其实是找不到bug
记得按easyx插件
#include#include #include #include #include #include #include #include #define MAX_GRTD 4 //每行格子 #define GRID_WIDTH 100 //格子宽度 #define INTERVAL 15 //间隔 using namespace std; 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 }; int map[4][4]; int num[12] = { 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 }; POINT pos[4][4];//保存每个格子左上交坐标 bool flag = false; int twoorfour(){ if (rand() % 10 == 1){ return 4; } else{ return 2; } } void CreatNumber(){ while (1){ int x = rand() % 4, y = rand() % 4; if (map[x][y] == 0){ map[x][y] = twoorfour(); break; } } } void Gameinit(){ srand(GetTickCount()); for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++){ pos[i][j].x = j*GRID_WIDTH + (j + 1)*INTERVAL; pos[i][j].y = i*GRID_WIDTH + (i + 1)*INTERVAL; } } CreatNumber(); CreatNumber(); } void Gamedraw(){ setbkcolor(back); cleardevice(); for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++){ for (int q = 0; q < 12; q++){ if (map[i][j] == num[q]){ setfillcolor(arr[q]); solidrectangle(pos[i][j].x, pos[i][j].y, pos[i][j].x + GRID_WIDTH, pos[i][j].y + GRID_WIDTH); if (map[i][j] != 0){ char number[5] = " "; settextcolor(RGB(119, 110, 100)); settextstyle(50, 0, "黑体"); setbkmode(TRANSPARENT); sprintf(number, "%d",map[i][j]); int temp = textwidth(number); temp = GRID_WIDTH / 2 - temp / 2; int temp1 = textheight(number); temp1 = GRID_WIDTH / 2 - temp1 / 2; outtextxy(pos[i][j].x+temp, pos[i][j].y+temp1, number); } } } } } } void moveup(){ for (int i = 0; i < 4; i++){ int temp = 0; for (int b = 1; b < 4; b++){ if (map[b][i]!=0){ if (map[temp][i] == 0){ map[temp][i] = map[b][i]; map[b][i] = 0; } else if(map[temp][i]==map[b][i]){ map[temp][i] += map[b][i]; map[b][i] = 0; } else{ map[temp+1][i] = map[b][i]; if (temp + 1 != b){ map[b][i] = 0; } } temp++; flag = true; } } } } void movedown(){ for (int i = 0; i < 4; i++){ int temp = 3; for (int b = 2; b >=0; b--){ if (map[b][i] != 0){ if (map[temp][i] == 0){ map[temp][i] = map[b][i]; map[b][i] = 0; } else if (map[temp][i] == map[b][i]){ map[temp][i] += map[b][i]; map[b][i] = 0; } else{ map[temp - 1][i] = map[b][i]; if (temp - 1 != b){ map[b][i] = 0; } } temp--; flag = true; } } } } void moveleft(){ for (int i = 0; i < 4; i++){ int temp = 0; for (int b = 1; b < 4; b++){ if (map[i][b] != 0){ if (map[i][temp] == 0){ map[i][temp] = map[i][b]; map[i][b] = 0; } else if (map[i][temp] == map[i][b]){ map[i][temp] += map[i][b]; map[i][b] = 0; } else{ map[i][temp+1] = map[i][b]; if (temp + 1 != b){ map[i][b] = 0; } } temp++; flag = true; } } } } void moveright(){ for (int i = 0; i < 4; i++){ int temp = 3; for (int b = 2; b >=0; b--){ if (map[i][b] != 0){ if (map[i][temp] == 0){ map[i][temp] = map[i][b]; map[i][b] = 0; } else if (map[i][temp] == map[i][b]){ map[i][temp] += map[i][b]; map[i][b] = 0; } else{ map[i][temp - 1] = map[i][b]; if (temp - 1 != b){ map[i][b] = 0; } } temp--; flag = true; } } } } void panduan(){ if (flag){ CreatNumber(); flag = false; } } void GameControl(){ char key = _getch(); if (key == 'w' || key == 'W' || key == 72) moveup(); if (key == 's' || key == 'S' || key == 80) movedown(); if (key == 'a' || key == 'A' || key == 75) moveleft(); if (key == 'd' || key == 'D' || key == 77) moveright(); } int main(){ initgraph(4 * 100 + 5 * 15, 400 + 5 * 15); Gameinit(); while (1){ Gamedraw(); GameControl(); panduan(); } getchar(); return 0; }



