使用 EasyX制作的小游戏,游戏思想很简单每一步棋,就判断当前游戏是否结束,判断的规则就是横、竖、斜角是否五子连珠 . . .
游戏素材准备:
一个背景、一首歌,其它的线、棋子,都是通过Api 画出来的 . . .
.
游戏效果如下所示:
小游戏制作五子棋
代码如下所示:
#include <iostream>
#include <cmath>
#include <graphics.h>
#include <mmsystem.h>
#pragma comment (lib,"winmm")
const int WIDTH = 640;
const int HEIGHT = 640;
MOUSEMSG msg;
void Init(); //音乐 背景 大小
class Gobang
{
private:
int fd_flag = 0;
const static int ROW = 16;
const static int COL = 16;
int arr[ROW][COL] = { 0 };
public:
void DrawLine(); //画线
void PlayChess(); //下棋
};
int main()
{
Init();
Gobang gobang;
gobang.DrawLine();
while (1)
gobang.PlayChess();
return 0;
}
void Init()
{
initgraph(WIDTH, HEIGHT);
loadimage(nullptr, "bj.jpg", WIDTH, HEIGHT);
mciSendString("open bj.mp3", 0, 0, 0);
mciSendString("play bj.mp3 repeat", 0, 0, 0);
std::cin.get();
}
void Gobang::DrawLine()
{
setlinecolor(BLACK);
for (int i = 1; i < 16; i++)
{
line(40 * i, 40, 40 * i, WIDTH - 40);
line(40, 40 * i, WIDTH - 40, 40 * i);
}
setlinecolor(RED);
line(39, 40, 39, WIDTH - 40);
line(40 * 15 + 1, 40, 40 * 15 + 1, WIDTH - 40);
line(38, 40, 38, WIDTH - 40);
line(40 * 15 + 2, 40, 40 * 15 + 2, WIDTH - 40);
line(40, 39, WIDTH - 40, 39);
line(40, 40 * 15 + 1, WIDTH - 40, 40 * 15 + 1);
line(38, 40, 38, WIDTH - 40);
line(40 * 15 + 2, 40, 40 * 15 + 2, WIDTH - 40);
setfillcolor(BLACK);
fillcircle(WIDTH / 2, HEIGHT / 2, 4);
fillcircle(WIDTH / 2 + 80, HEIGHT / 2 - 80, 4);
fillcircle(WIDTH / 2 + 80, HEIGHT / 2 + 80, 4);
fillcircle(WIDTH / 2 - 80, HEIGHT / 2 - 80, 4);
fillcircle(WIDTH / 2 - 80, HEIGHT / 2 + 80, 4);
}
void Gobang::PlayChess()
{
msg = GetMouseMsg();
if (msg.mkLButton && msg.x > 20 && msg.x < WIDTH - 20 && msg.y > 20 && msg.y < WIDTH - 20)
{
for (int i = 1; i < ROW; i++)
{
for (int j = 1; j < COL; j++)
{
if (abs(msg.x - i * 40) < 15 && abs(msg.y - j * 40) < 15 && arr[i][j] == 0)
{
if (++fd_flag % 2)
setfillcolor(WHITE);
else
setfillcolor(BLACK);
int x = i * 40;
int y = j * 40;
solidcircle(x, y, 10);
arr[i][j] = fd_flag % 2 + 1;
for (int k = i - 4; k <= i; k++)
{
if (k < 0 || k > 11)
continue;
if (arr[k][j] == 2 && arr[k + 1][j] == 2 && arr[k + 2][j] == 2 && arr[k + 3][j] == 2 && arr[k + 4][j] == 2)
{
MessageBox(nullptr, " 游戏结束! 白子胜利!", "五子棋", MB_OK);
exit(0);
}
if (arr[k][j] == 1 && arr[k + 1][j] == 1 && arr[k + 2][j] == 1 && arr[k + 3][j] == 1 && arr[k + 4][j] == 1)
{
MessageBox(nullptr, " 游戏结束! 黑子胜利!", "五子棋", MB_OK);
exit(0);
}
}
for (int k = j - 4; k <= j; k++)
{
if (k < 0 || k > 11)
continue;
if (arr[i][k] == 2 && arr[i][k + 1] == 2 && arr[i][k + 2] == 2 && arr[i][k + 3] == 2 && arr[i][k + 4] == 2)
{
MessageBox(nullptr, " 游戏结束! 白子胜利!", "五子棋", MB_OK);
exit(0);
}
if (arr[i][k] == 1 && arr[i][k + 1] == 1 && arr[i][k + 2] == 1 && arr[i][k + 3] == 1 && arr[i][k + 4] == 1)
{
MessageBox(nullptr, " 游戏结束! 黑子胜利!", "五子棋", MB_OK);
exit(0);
}
}
int m = i - 4;
int n = j - 4;
for (int k = 0; k < 5; k++)
{
if (m < 0 || m > 11 || n < 0 || n > 11)
{
m++; n++;
continue;
}
if (arr[m][n] == 2 && arr[m + 1][n + 1] == 2 && arr[m + 2][n + 2] == 2 && arr[m + 3][n + 3] == 2 && arr[m + 4][n + 4] == 2)
{
MessageBox(nullptr, " 游戏结束! 白子胜利!", "五子棋", MB_OK);
exit(0);
}
if (arr[m][n] == 1 && arr[m + 1][n + 1] == 1 && arr[m + 2][n + 2] == 1 && arr[m + 3][n + 3] == 1 && arr[m + 4][n + 4] == 1)
{
MessageBox(nullptr, " 游戏结束! 黑子胜利!", "五子棋", MB_OK);
exit(0);
}
m++; n++;
}
m = i - 4;
n = j + 4;
for (int k = 0; k < 5; k++)
{
if (m < 0 || m > 11 || n < 0 || n > 11)
{
m++; n--;
continue;
}
if (arr[m][n] == 2 && arr[m + 1][n - 1] == 2 && arr[m + 2][n - 2] == 2 && arr[m + 3][n - 3] == 2 && arr[m + 4][n - 4] == 2)
{
MessageBox(nullptr, " 游戏结束! 白子胜利!", "五子棋", MB_OK);
exit(0);
}
if (arr[m][n] == 1 && arr[m + 1][n - 1] == 1 && arr[m + 2][n - 2] == 1 && arr[m + 3][n - 3] == 1 && arr[m + 4][n - 4] == 1)
{
MessageBox(nullptr, " 游戏结束! 黑子胜利!", "五子棋", MB_OK);
exit(0);
}
m++; n--;
}
}
}
}
}
}