c++easyx换图游戏
时间: 2023-12-18 11:03:57 浏览: 90
以下是一个使用C++和EasyX图形库实现的换图游戏的示例代码:
```cpp
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 4
#define COL 4
#define SIZE 100
IMAGE img[11];
int map[ROW][COL] = {0};
int score = 0;
void init();
void draw();
void update();
void addRandom();
bool isGameOver();
bool canMove();
void moveLeft();
void moveRight();
void moveUp();
void moveDown();
void swap(int& a, int& b);
int main()
{
initgraph(4 * SIZE, 4 * SIZE);
init();
draw();
while (!isGameOver())
{
if (canMove())
{
update();
draw();
}
else
{
break;
}
}
_getch();
closegraph();
return 0;
}
void init()
{
srand((unsigned)time(NULL));
loadimage(&img[0], L"0.jpg", SIZE, SIZE);
loadimage(&img[2], L"2.jpg", SIZE, SIZE);
loadimage(&img[4], L"4.jpg", SIZE, SIZE);
loadimage(&img[8], L"8.jpg", SIZE, SIZE);
loadimage(&img[16], L"16.jpg", SIZE, SIZE);
loadimage(&img[32], L"32.jpg", SIZE, SIZE);
loadimage(&img[64], L"64.jpg", SIZE, SIZE);
loadimage(&img[128], L"128.jpg", SIZE, SIZE);
loadimage(&img[256], L"256.jpg", SIZE, SIZE);
loadimage(&img[512], L"512.jpg", SIZE, SIZE);
loadimage(&img[1024], L"1024.jpg", SIZE, SIZE);
loadimage(&img[2048], L"2048.jpg", SIZE, SIZE);
addRandom();
addRandom();
}
void draw()
{
cleardevice();
setbkcolor(WHITE);
settextcolor(BLACK);
settextstyle(30, 0, L"Consolas");
wchar_t str[10];
swprintf_s(str, L"Score: %d", score);
outtextxy(0, 0, str);
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
int x = j * SIZE;
int y = i * SIZE + 50;
int num = map[i][j];
if (num == 0)
{
putimage(x, y, &img[0]);
}
else
{
putimage(x, y, &img[num]);
}
}
}
}
void update()
{
int dir = _getch();
switch (dir)
{
case 'a':
moveLeft();
break;
case 'd':
moveRight();
break;
case 'w':
moveUp();
break;
case 's':
moveDown();
break;
default:
break;
}
addRandom();
}
void addRandom()
{
int count = 0;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j] == 0)
{
count++;
}
}
}
if (count == 0)
{
return;
}
int index = rand() % count;
int num = rand() % 2 == 0 ? 2 : 4;
count = 0;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j] == 0)
{
if (count == index)
{
map[i][j] = num;
return;
}
count++;
}
}
}
}
bool isGameOver()
{
if (canMove())
{
return false;
}
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j] == 2048)
{
return false;
}
}
}
return true;
}
bool canMove()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j] == 0)
{
return true;
}
if (j < COL - 1 && map[i][j] == map[i][j + 1])
{
return true;
}
if (i < ROW - 1 && map[i][j] == map[i + 1][j])
{
return true;
}
}
}
return false;
}
void moveLeft()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL - 1; j++)
{
for (int k = j + 1; k < COL; k++)
{
if (map[i][k] > 0)
{
if (map[i][j] == 0)
{
swap(map[i][j], map[i][k]);
}
else if (map[i][j] == map[i][k])
{
map[i][j] *= 2;
score += map[i][j];
map[i][k] = 0;
}
break;
}
}
}
}
}
void moveRight()
{
for (int i = 0; i < ROW; i++)
{
for (int j = COL - 1; j > 0; j--)
{
for (int k = j - 1; k >= 0; k--)
{
if (map[i][k] > 0)
{
if (map[i][j] == 0)
{
swap(map[i][j], map[i][k]);
}
else if (map[i][j] == map[i][k])
{
map[i][j] *= 2;
score += map[i][j];
map[i][k] = 0;
}
break;
}
}
}
}
}
void moveUp()
{
for (int j = 0; j < COL; j++)
{
for (int i = 0; i < ROW - 1; i++)
{
for (int k = i + 1; k < ROW; k++)
{
if (map[k][j] > 0)
{
if (map[i][j] == 0)
{
swap(map[i][j], map[k][j]);
}
else if (map[i][j] == map[k][j])
{
map[i][j] *= 2;
score += map[i][j];
map[k][j] = 0;
}
break;
}
}
}
}
}
void moveDown()
{
for (int j = 0; j < COL; j++)
{
for (int i = ROW - 1; i > 0; i--)
{
for (int k = i - 1; k >= 0; k--)
{
if (map[k][j] > 0)
{
if (map[i][j] == 0)
{
swap(map[i][j], map[k][j]);
}
else if (map[i][j] == map[k][j])
{
map[i][j] *= 2;
score += map[i][j];
map[k][j] = 0;
}
break;
}
}
}
}
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
```
阅读全文