easyx窗口存在动态画面时无法进行鼠标交互

0

就是easyx有动态画面的时候鼠标交互失效了,想知道怎么解决

Outlook函数是个动态计时器,把它注释掉的时候鼠标交互可用,未注释鼠标交互失效,悬赏,金额不满意可另谈,求解决

以下是源码
#include <stdio.h>
#include <tchar.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
#include<easyx.h>


#define INTERVAL 50  //前面的间隔
#define CHESS_GRID_SIZE 70  //格子宽度
#define ROW  10
#define COL  9
int O = 1, step = 0;
enum PIECES//枚举棋子
{
    車, 馬, 象, 士, 将, 砲, 卒,
    俥, 马, 相, 仕, 帥, 炮, 兵,
    SPACE, BEGIN, END, NONE
};
//红黑方棋子、
int blackChess[] = { 車, 馬, 象, 士, 将, 砲, 卒 };
int redChess[] = { 俥, 马, 相, 仕, 帥, 炮, 兵 };
const char* chessName[] = { "車", "馬", "象", "士", "将", "砲", "卒", "俥", "马", "相", "仕", "帥", "炮", "兵" };
struct Chess//棋子属性
{
    int x;
    int y;
    int id;//哪个棋子
    int type;//是哪一方的棋子 红,黑?
    bool river;//判断小兵是否过了河
};
struct Chess  map[ROW][COL];//结构体数组,用来保存每一个点的信息
POINT begin = { -1, -1 }, end = { -1, -1 };//保存前后两次点击的数组下标
int state = BEGIN;
void visualize() {
    for (int i = 0; i < 10; i++)
    {
        printf("\n");
        for (int j = 0; j < 9; j++) {
            printf("  %d", map[i][j].type);
        }
    }
}
//初始化游戏数据
void GameInit()
{
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            int chessname = SPACE;
            int mcolor = WHITE;
            //黑棋
            if (i <= 4)
            {
                //初始化第一行的棋子,
                if (i == 0)
                {
                    mcolor = BLACK;
                    if (k <= 4)
                    {
                        chessname = blackChess[k];
                    }
                    else
                    {
                        chessname = blackChess[8 - k];
                    }
                }
                //设置炮
                if (i == 2 && (k == 1 || k == 7))
                {
                    chessname = blackChess[5];
                    mcolor = BLACK;
                }
                //设置小兵
                if (i == 3 && k % 2 == 0)
                {
                    chessname = blackChess[6];
                    mcolor = BLACK;
                }

            }
            //紅棋
            else
            {
                //初始化第一行的棋子,
                if (i == 9)
                {
                    mcolor = RED;
                    if (k <= 4)
                    {
                        chessname = redChess[k];
                    }
                    else
                    {
                        chessname = redChess[8 - k];
                    }
                }
                //设置炮
                if (i == 7 && (k == 1 || k == 7))
                {
                    chessname = redChess[5];
                    mcolor = RED;
                }
                //设置小兵
                if (i == 6 && k % 2 == 0)
                {
                    chessname = redChess[6];
                    mcolor = RED;
                }
            }
            map[i][k].id = chessname;
            map[i][k].river = false;
            map[i][k].type = mcolor;
            map[i][k].x = k * CHESS_GRID_SIZE + INTERVAL;
            map[i][k].y = i * CHESS_GRID_SIZE + INTERVAL;
        }
    }
    visualize();
}
void Outlook() {
    settextstyle(50, 0, "Consolas");//时间显示文字的样式
    settextcolor(BLACK);

    char t[100];//计时器部分
    int minutes = 9, seconds = 59;//初始化,参数即为开始倒计时的时间
    while (minutes != 0 || seconds != 0) {//倒计时逻辑
        if (seconds == 0) {
            minutes--; seconds = 59;
        }
        else seconds--;
        Sleep(1000);//1秒
        setbkcolor(RGB(252, 215, 162)); // 设置刷新小矩形背景颜色
        setfillstyle(SOLID_FILL, RGB(252, 215, 162)); // 设置填充样式
        bar(650, 340, 800, 410); // 用背景颜色清除时间文本区域,达到使得时间文本区域透明的效果
        sprintf_s(t, "%02d:%02d", minutes, seconds);//打印时间文字的函数
        outtextxy(650, 340, t);//打印时间文字的函数
        FlushBatchDraw();
        while (minutes == 4 && seconds >= 59)  //五分钟后弹出弹窗
        {
            HWND hand = GetHWnd();    // 获取图形窗口的句柄
            int ResultMB = MessageBox(hand, "随机天罚降临!!!", "温馨提示", MB_OKCANCEL);
            // 点击不同按钮之后的不同操作,省略了取消按钮
            if (IDOK == ResultMB) {
                //printf("弹窗点击了确认");可以不加
            }
            break;
        }
    }
}











//游戏的绘制函数
void GameDraw()
{
    //设置背景颜色 red black 
    setbkcolor(RGB(252, 215, 162));
    IMAGE Pic1;
    IMAGE Pic2;

    loadimage(&Pic1, "wj1.png");//背景图片设置
    loadimage(&Pic2, "wj2.png");

    cleardevice();
    settextcolor(BLACK);
    settextstyle(35, 0, "微软雅黑", 0, 0, 500, false, false, false);
    outtextxy(670, 160, "玩家一");
    settextcolor(BLACK);
    settextstyle(35, 0, "微软雅黑", 0, 0, 500, false, false, false);
    outtextxy(670, 555, "玩家二");
    
    putimage(680, 80, &Pic1);
    putimage(680, 600, &Pic2);


    //绘制棋盘
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 2);
    for (int i = 0; i < 10; i++)
    {
        //画横线
        line(INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, i * CHESS_GRID_SIZE + INTERVAL);
        //画竖线
        if (i < 9)
        {
            line(i * CHESS_GRID_SIZE + INTERVAL, INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
        }
    }
    rectangle(INTERVAL - 5, INTERVAL - 5, 8 * CHESS_GRID_SIZE + INTERVAL + 5, 5 + 9 * CHESS_GRID_SIZE + INTERVAL);
    //楚河汉界显示
    setfillcolor(RGB(252, 215, 162));
    fillrectangle(INTERVAL, 4 * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL);
    //显示文字

    char river[21] = "楚河        汉界";
    settextstyle(50, 0, _T("楷体"));
    settextcolor(BLACK);
    setbkmode(TRANSPARENT);
    outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
    outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
    //画九宫格
        //画上面
    line(3 * CHESS_GRID_SIZE + INTERVAL, INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, INTERVAL);
    //画下面
    line(3 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL);
    //画棋子
    settextstyle(30, 0, _T("楷体"));
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            if (map[i][k].id != SPACE)
            {
                setlinecolor(map[i][k].type);
                fillcircle(map[i][k].x, map[i][k].y, 30);
                fillcircle(map[i][k].x, map[i][k].y, 25);
                settextcolor(map[i][k].type);
                outtextxy(map[i][k].x - 10, map[i][k].y - 10, chessName[map[i][k].id]);
            }

        }
    }
    Outlook();
}
void chessMove() {
    bool canmove = 0;
    int j = 0;
    printf("begin.x=%d\nbegin.y=%d\nend.x=%d\nend.y=%d\n", begin.x, begin.y, end.x, end.y);
    if (!(begin.x == end.x && begin.y == end.y)
        && end.x != -1 && end.y != -1
        && map[begin.x][begin.y].id != SPACE
        && map[begin.x][begin.y].type != map[end.x][end.y].type) {
        printf("CAN");
        switch (map[begin.x][begin.y].id)
        {
        case 車:
        case 俥:
            if (begin.x == end.x) {
                j = 1;
                if (end.y > begin.y) {
                    printf("canmove");
                    for (int i = begin.y + 1; i < end.y; i++) {
                        printf("i=%d\nmap[][]=%d\n", i, map[begin.x][i].id);
                        if (map[begin.x][i].id != SPACE) { j = 0; printf("stop\n"); }
                    }
                }
                else if (end.y < begin.y) {
                    for (int i = end.y + 1; i < begin.y; i++) {
                        if (map[begin.x][i].id != SPACE) j = 0;
                    }
                }
            }
            else if (end.y == begin.y) {
                j = 1;
                if (begin.x < end.x) {
                    for (int i = begin.x + 1; i < end.x; i++) {
                        if (map[i][end.y].id != SPACE) j = 0;
                    }
                }
                else if (begin.x > end.x) {
                    for (int i = end.x + 1; i < begin.x; i++) {
                        if (map[i][end.y].id != SPACE) j = 0;
                    }
                }
            }
            canmove += j;
            break;
        case 馬:
        case 马:
            if (end.y - begin.y == 2 && map[begin.x][begin.y + 1].type == WHITE && abs(end.x - begin.x) == 1)canmove = 1;
            else if (end.y - begin.y == -2 && map[begin.x][begin.y - 1].id == SPACE && abs(end.x - begin.x) == 1)canmove = 1;
            else if (end.x - begin.x == -2 && map[begin.x - 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)canmove = 1;
            else if (end.x - begin.x == 2 && map[begin.x + 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)canmove = 1;
            break;
        case 象:
        case 相:
            if ((begin.x < 5 && end.x < 5) || (begin.x > 4 && end.x > 4)) {
                if (end.y - begin.y == 2 && map[begin.x + 1][begin.y + 1].type == WHITE && end.x - begin.x == 2)canmove = 1;
                else if (end.y - begin.y == -2 && map[begin.x + 1][begin.y - 1].type == WHITE && end.x - begin.x == 2)canmove = 1;
                else if (end.y - begin.y == 2 && map[begin.x - 1][begin.y + 1].type == WHITE && end.x - begin.x == -2)canmove = 1;
                else if (end.y - begin.y == -2 && map[begin.x - 1][begin.y - 1].type == WHITE && end.x - begin.x == -2)canmove = 1;
            }
            break;
        case 士:
        case 仕:
            if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
                if (end.y > 2 && end.y < 6) {
                    if (abs(end.y - begin.y) == 1 && abs(end.x - begin.x) == 1)canmove = 1;
                }
            break;
        case 将:
        case 帥:
            if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
                if (end.y > 2 && end.y < 6) {
                    if (abs(end.y - begin.y) + abs(end.x - begin.x) == 1)canmove = 1;
                }
            break;
        case 砲:
        case 炮:
            if (begin.x == end.x) {
                j = 1;
                if (end.y > begin.y) {
                    printf("canmove");
                    for (int i = begin.y + 1; i < end.y; i++) {
                        if (map[begin.x][i].id != SPACE) { j--; }
                    }
                    if (map[begin.x][end.y].type == WHITE) j--;
                }
                else if (end.y < begin.y) {
                    for (int i = end.y + 1; i < begin.y; i++) {
                        if (map[begin.x][i].id != SPACE) j--;
                    }
                    if (map[end.x][end.y].type == WHITE) j--;
                }
            }
            else if (end.y == begin.y) {
                j = 1;
                if (begin.x < end.x) {
                    for (int i = begin.x + 1; i < end.x; i++) {
                        if (map[i][end.y].id != SPACE) j--;
                    }
                    if (map[end.x][end.y].type == WHITE) j--;
                }
                else if (begin.x > end.x) {
                    for (int i = end.x + 1; i < begin.x; i++) {
                        if (map[i][end.y].id != SPACE) j--;
                    }
                    if (map[end.x][end.y].type == WHITE) j--;
                }
            }
            if (j == 0) canmove = 1;
            break;
        case 卒:
        case 兵:
            if (map[begin.x][begin.y].type == BLACK && end.x - begin.x >= 0) {
                if (end.x - begin.x == 1 && end.y == begin.y) canmove = 1;
                else if (begin.x > 4 && begin.x == end.x && abs(begin.y - end.y) == 1) canmove = 1;
            }
            else if (map[begin.x][begin.y].type == RED && end.x - begin.x <= 0) {
                if (end.x - begin.x == -1 && end.y == begin.y) canmove = 1;
                else if (begin.x < 5 && begin.x == end.x && abs(begin.y - end.y) == 1) canmove = 1;
            }
            break;
        }
        if (canmove) {
            if (map[end.x][end.y].id == 将 || map[end.x][end.y].id == 帥) O = 0;
            map[end.x][end.y].id = map[begin.x][begin.y].id;
            map[end.x][end.y].type = map[begin.x][begin.y].type;
            map[begin.x][begin.y].id = SPACE;
            map[begin.x][begin.y].type = WHITE;

        }
        step += canmove;
        printf("step=%d", step);
    }
}
void MouseControl()
{
    if (MouseHit())
    {
        MOUSEMSG msg = GetMouseMsg();
        if (msg.uMsg == WM_LBUTTONDOWN)
        {
            //获取鼠标点击的数组的下标
            map[-1][-1].type = WHITE;
            int row = (msg.y - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
            int col = (msg.x - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
            if ((step % 4 == 0 && map[row][col].type == RED || step % 4 == 2 && map[row][col].type == BLACK))
            {

                state = END;
                begin.x = row;
                begin.y = col;
                step++;
            }
            else if ((step % 4 == 1 || step % 4 == 3))
            {
                state = BEGIN;
                end.x = row;
                end.y = col;
                chessMove();
            }
            printf("%d,%d  %d\n", row, col, state);
        }
    }
}

int main()
{
    //创建一个图形窗口
    initgraph(800, 800, SHOWCONSOLE);
    GameInit();
    BeginBatchDraw();
    while (O)
    {   MouseControl();
        GameDraw();
        FlushBatchDraw();

        
        
    }printf("GAME OVER");
    return 0;

}
悬赏金额:¥30 元

1. 回答问题后,可以看到提问者联系方式。

2. 如果对问题有不明确的地方,可以在回复中写下你的疑问,就可以看到提问者的联系方式,然后详细沟通。

3. 如果私下沟通解决了问题,建议回答者将解决方案补充到本站,提问者勾选正确答案。每个人回答的问题数量,是能力的象征。

4. 回答问题后,可以通过提问者的联系方式私下协商领取悬赏的方式,本站不做任何干涉,不做担保,不抽取佣金,请双方谨慎操作。

0

#include <stdio.h>
#include <tchar.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
#include<easyx.h>


#define INTERVAL 50  //前面的间隔
#define CHESS_GRID_SIZE 70  //格子宽度
#define ROW  10
#define COL  9
int O = 1, step = 0;
enum PIECES//枚举棋子
{
    車, 馬, 象, 士, 将, 砲, 卒,
    俥, 马, 相, 仕, 帥, 炮, 兵,
    SPACE, BEGIN, END, NONE
};
//红黑方棋子、
int blackChess[] = { 車, 馬, 象, 士, 将, 砲, 卒 };
int redChess[] = { 俥, 马, 相, 仕, 帥, 炮, 兵 };
const wchar_t* chessName[] = { L"車", L"馬", L"象", L"士", L"将", L"砲", L"卒", L"俥", L"马", L"相",L"仕", L"帥", L"炮",L"兵" };
struct Chess//棋子属性
{
    int x;
    int y;
    int id;//哪个棋子
    int type;//是哪一方的棋子 红,黑?
    bool river;//判断小兵是否过了河
};
struct Chess  map[ROW][COL];//结构体数组,用来保存每一个点的信息
POINT begin = { -1, -1 }, end = { -1, -1 };//保存前后两次点击的数组下标
int state = BEGIN;
void visualize() {
    for (int i = 0; i < 10; i++)
    {
        printf("\n");
        for (int j = 0; j < 9; j++) {
            printf("  %d", map[i][j].type);
        }
    }
}
//初始化游戏数据
void GameInit()
{
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            int chessname = SPACE;
            int mcolor = WHITE;
            //黑棋
            if (i <= 4)
            {
                //初始化第一行的棋子,
                if (i == 0)
                {
                    mcolor = BLACK;
                    if (k <= 4)
                    {
                        chessname = blackChess[k];
                    }
                    else
                    {
                        chessname = blackChess[8 - k];
                    }
                }
                //设置炮
                if (i == 2 && (k == 1 || k == 7))
                {
                    chessname = blackChess[5];
                    mcolor = BLACK;
                }
                //设置小兵
                if (i == 3 && k % 2 == 0)
                {
                    chessname = blackChess[6];
                    mcolor = BLACK;
                }

            }
            //紅棋
            else
            {
                //初始化第一行的棋子,
                if (i == 9)
                {
                    mcolor = RED;
                    if (k <= 4)
                    {
                        chessname = redChess[k];
                    }
                    else
                    {
                        chessname = redChess[8 - k];
                    }
                }
                //设置炮
                if (i == 7 && (k == 1 || k == 7))
                {
                    chessname = redChess[5];
                    mcolor = RED;
                }
                //设置小兵
                if (i == 6 && k % 2 == 0)
                {
                    chessname = redChess[6];
                    mcolor = RED;
                }
            }
            map[i][k].id = chessname;
            map[i][k].river = false;
            map[i][k].type = mcolor;
            map[i][k].x = k * CHESS_GRID_SIZE + INTERVAL;
            map[i][k].y = i * CHESS_GRID_SIZE + INTERVAL;
        }
    }
    visualize();
}
void Outlook() {
    settextstyle(50, 0, L"Consolas");//时间显示文字的样式
    settextcolor(BLACK);
    static int a = 1;
    wchar_t t[100];//计时器部分
    int seconds=600;
    static clock_t start = clock(); // 记录第一次运行时刻
    clock_t now = clock(); // 获得当前时刻
    // 计算程序一共运行了多少秒
    int duration = (int((now - start) / CLOCKS_PER_SEC));
    if(a==1)
    seconds = 600 - duration;
    else  seconds = 300 - duration;
     setbkcolor(RGB(252, 215, 162)); // 设置刷新小矩形背景颜色
     setfillstyle(SOLID_FILL, RGB(252, 215, 162)); // 设置填充样式
     bar(650, 340, 800, 410); // 用背景颜色清除时间文本区域,达到使得时间文本区域透明的效果
     _stprintf_s(t, L"%02d:%02d",seconds/60, seconds%60);//打印时间文字的函数
     outtextxy(650, 340, t);//打印时间文字的函数
     FlushBatchDraw();
      while (seconds==300&&a)  //五分钟后弹出弹窗
       {
           HWND hand = GetHWnd();    // 获取图形窗口的句柄
           int ResultMB = MessageBox(hand, L"随机天罚降临!!!", L"温馨提示", MB_OKCANCEL);
           // 点击不同按钮之后的不同操作,省略了取消按钮
           if (IDOK == ResultMB) 
           {
               //printf("弹窗点击了确认");可以不加
               start = clock();
               a = 0;
           }
            break;
        }
}

//游戏的绘制函数
void GameDraw()
{
    //设置背景颜色 red black 
    setbkcolor(RGB(252, 215, 162));
    IMAGE Pic1;
    IMAGE Pic2;

    loadimage(&Pic1, L"wj1.png");//背景图片设置
    loadimage(&Pic2, L"wj2.png");

    cleardevice();
    settextcolor(BLACK);
    settextstyle(35, 0, L"微软雅黑", 0, 0, 500, false, false, false);
    outtextxy(670, 160, L"玩家一");
    settextcolor(BLACK);
    settextstyle(35, 0, L"微软雅黑", 0, 0, 500, false, false, false);
    outtextxy(670, 555, L"玩家二");

    putimage(680, 80, &Pic1);
    putimage(680, 600, &Pic2);


    //绘制棋盘
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 2);
    for (int i = 0; i < 10; i++)
    {
        //画横线
        line(INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, i * CHESS_GRID_SIZE + INTERVAL);
        //画竖线
        if (i < 9)
        {
            line(i * CHESS_GRID_SIZE + INTERVAL, INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
        }
    }
    rectangle(INTERVAL - 5, INTERVAL - 5, 8 * CHESS_GRID_SIZE + INTERVAL + 5, 5 + 9 * CHESS_GRID_SIZE + INTERVAL);
    //楚河汉界显示
    setfillcolor(RGB(252, 215, 162));
    fillrectangle(INTERVAL, 4 * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL);
    //显示文字

    wchar_t river[21] = L"楚河        汉界";
    settextstyle(50, 0, _T("楷体"));
    settextcolor(BLACK);
    setbkmode(TRANSPARENT);
    outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
    outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
    //画九宫格
        //画上面
    line(3 * CHESS_GRID_SIZE + INTERVAL, INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, INTERVAL);
    //画下面
    line(3 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL);
    //画棋子
    settextstyle(30, 0, _T("楷体"));
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            if (map[i][k].id != SPACE)
            {
                setlinecolor(map[i][k].type);
                fillcircle(map[i][k].x, map[i][k].y, 30);
                fillcircle(map[i][k].x, map[i][k].y, 25);
                settextcolor(map[i][k].type);
                outtextxy(map[i][k].x - 10, map[i][k].y - 10, chessName[map[i][k].id]);
            }

        }
    }
    Outlook();
}
void chessMove() {
    bool canmove = 0;
    int j = 0;
    printf("begin.x=%d\nbegin.y=%d\nend.x=%d\nend.y=%d\n", begin.x, begin.y, end.x, end.y);
    if (!(begin.x == end.x && begin.y == end.y)
        && end.x != -1 && end.y != -1
        && map[begin.x][begin.y].id != SPACE
        && map[begin.x][begin.y].type != map[end.x][end.y].type) {
        printf("CAN");
        switch (map[begin.x][begin.y].id)
        {
        case 車:
        case 俥:
            if (begin.x == end.x) {
                j = 1;
                if (end.y > begin.y) {
                    printf("canmove");
                    for (int i = begin.y + 1; i < end.y; i++) {
                        printf("i=%d\nmap[][]=%d\n", i, map[begin.x][i].id);
                        if (map[begin.x][i].id != SPACE) { j = 0; printf("stop\n"); }
                    }
                }
                else if (end.y < begin.y) {
                    for (int i = end.y + 1; i < begin.y; i++) {
                        if (map[begin.x][i].id != SPACE) j = 0;
                    }
                }
            }
            else if (end.y == begin.y) {
                j = 1;
                if (begin.x < end.x) {
                    for (int i = begin.x + 1; i < end.x; i++) {
                        if (map[i][end.y].id != SPACE) j = 0;
                    }
                }
                else if (begin.x > end.x) {
                    for (int i = end.x + 1; i < begin.x; i++) {
                        if (map[i][end.y].id != SPACE) j = 0;
                    }
                }
            }
            canmove += j;
            break;
        case 馬:
        case 马:
            if (end.y - begin.y == 2 && map[begin.x][begin.y + 1].type == WHITE && abs(end.x - begin.x) == 1)canmove = 1;
            else if (end.y - begin.y == -2 && map[begin.x][begin.y - 1].id == SPACE && abs(end.x - begin.x) == 1)canmove = 1;
            else if (end.x - begin.x == -2 && map[begin.x - 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)canmove = 1;
            else if (end.x - begin.x == 2 && map[begin.x + 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)canmove = 1;
            break;
        case 象:
        case 相:
            if ((begin.x < 5 && end.x < 5) || (begin.x > 4 && end.x > 4)) {
                if (end.y - begin.y == 2 && map[begin.x + 1][begin.y + 1].type == WHITE && end.x - begin.x == 2)canmove = 1;
                else if (end.y - begin.y == -2 && map[begin.x + 1][begin.y - 1].type == WHITE && end.x - begin.x == 2)canmove = 1;
                else if (end.y - begin.y == 2 && map[begin.x - 1][begin.y + 1].type == WHITE && end.x - begin.x == -2)canmove = 1;
                else if (end.y - begin.y == -2 && map[begin.x - 1][begin.y - 1].type == WHITE && end.x - begin.x == -2)canmove = 1;
            }
            break;
        case 士:
        case 仕:
            if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
                if (end.y > 2 && end.y < 6) {
                    if (abs(end.y - begin.y) == 1 && abs(end.x - begin.x) == 1)canmove = 1;
                }
            break;
        case 将:
        case 帥:
            if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
                if (end.y > 2 && end.y < 6) {
                    if (abs(end.y - begin.y) + abs(end.x - begin.x) == 1)canmove = 1;
                }
            break;
        case 砲:
        case 炮:
            if (begin.x == end.x) {
                j = 1;
                if (end.y > begin.y) {
                    printf("canmove");
                    for (int i = begin.y + 1; i < end.y; i++) {
                        if (map[begin.x][i].id != SPACE) { j--; }
                    }
                    if (map[begin.x][end.y].type == WHITE) j--;
                }
                else if (end.y < begin.y) {
                    for (int i = end.y + 1; i < begin.y; i++) {
                        if (map[begin.x][i].id != SPACE) j--;
                    }
                    if (map[end.x][end.y].type == WHITE) j--;
                }
            }
            else if (end.y == begin.y) {
                j = 1;
                if (begin.x < end.x) {
                    for (int i = begin.x + 1; i < end.x; i++) {
                        if (map[i][end.y].id != SPACE) j--;
                    }
                    if (map[end.x][end.y].type == WHITE) j--;
                }
                else if (begin.x > end.x) {
                    for (int i = end.x + 1; i < begin.x; i++) {
                        if (map[i][end.y].id != SPACE) j--;
                    }
                    if (map[end.x][end.y].type == WHITE) j--;
                }
            }
            if (j == 0) canmove = 1;
            break;
        case 卒:
        case 兵:
            if (map[begin.x][begin.y].type == BLACK && end.x - begin.x >= 0) {
                if (end.x - begin.x == 1 && end.y == begin.y) canmove = 1;
                else if (begin.x > 4 && begin.x == end.x && abs(begin.y - end.y) == 1) canmove = 1;
            }
            else if (map[begin.x][begin.y].type == RED && end.x - begin.x <= 0) {
                if (end.x - begin.x == -1 && end.y == begin.y) canmove = 1;
                else if (begin.x < 5 && begin.x == end.x && abs(begin.y - end.y) == 1) canmove = 1;
            }
            break;
        }
        if (canmove) {
            if (map[end.x][end.y].id == 将 || map[end.x][end.y].id == 帥) O = 0;
            map[end.x][end.y].id = map[begin.x][begin.y].id;
            map[end.x][end.y].type = map[begin.x][begin.y].type;
            map[begin.x][begin.y].id = SPACE;
            map[begin.x][begin.y].type = WHITE;

        }
        step += canmove;
        printf("step=%d", step);
    }
}
void MouseControl()
{
    ExMessage m;
    while(peekmessage(&m,EX_MOUSE))
    {
        if (m.message == WM_LBUTTONDOWN)
        {
            //获取鼠标点击的数组的下标
            map[-1][-1].type = WHITE;
            int row = (m.y - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
            int col = (m.x - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
            if ((step % 4 == 0 && map[row][col].type == RED || step % 4 == 2 && map[row][col].type == BLACK))
            {

                state = END;
                begin.x = row;
                begin.y = col;
                step++;
            }
            else if ((step % 4 == 1 || step % 4 == 3))
            {
                state = BEGIN;
                end.x = row;
                end.y = col;
                chessMove();
            }
            printf("%d,%d  %d\n", row, col, state);
        }
    }
}

int main()
{
    //创建一个图形窗口
    initgraph(800, 800, SHOWCONSOLE);
    GameInit();
    BeginBatchDraw();
    while (O)
    {
        MouseControl();
        GameDraw();
        FlushBatchDraw();

    }printf("GAME OVER");
    return 0;

}

ava
🦁辛巴

2023-12-18

0

#include<graphics.h>
#include<stdio.h>
#include<thread>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<conio.h>
#include<tchar.h>


#define INTERVAL 50  //前面的间隔
#define CHESS_GRID_SIZE 70  //格子宽度
#define ROW  10
#define COL  9
int flag_play = 1, step = 0;
enum PIECES//枚举棋子
{
 車, 馬, 象, 士, 将, 砲, 卒,
 俥, 马, 相, 仕, 帥, 炮, 兵,
 SPACE, BEGIN, END, NONE
};
//红黑方棋子、
int blackChess[] = { 車, 馬, 象, 士, 将, 砲, 卒 };
int redChess[] = { 俥, 马, 相, 仕, 帥, 炮, 兵 };
const char* chessName[] = { "車", "馬", "象", "士", "将", "砲", "卒", "俥", "马", "相", "仕", "帥", "炮", "兵" };
clock_t start; //创建时间变量


struct Chess//棋子属性
{
 int x;
 int y;
 int id;//哪个棋子
 int type;//是哪一方的棋子 红,黑?
 bool river;//判断小兵是否过了河
};
struct Chess  map[ROW][COL];//结构体数组,用来保存每一个点的信息
POINT begin = { -1, -1 }, end = { -1, -1 };//保存前后两次点击的数组下标
int state = BEGIN;
IMAGE Pic1;
IMAGE Pic2;


void visualize() {
 for (int i = 0; i < 10; i++)
 {
 printf("\n");
 for (int j = 0; j < 9; j++) {
 printf("  %d", map[i][j].type);
 }
 }
}
//初始化游戏数据
void GameInit()
{
 for (int i = 0; i < ROW; i++)
 {
 for (int k = 0; k < COL; k++)
 {
 int chessname = SPACE;
 int mcolor = WHITE;
 //黑棋
 if (i <= 4)
 {
 //初始化第一行的棋子,
 if (i == 0)
 {
 mcolor = BLACK;
 if (k <= 4)
 {
 chessname = blackChess[k];
 }
 else
 {
 chessname = blackChess[8 - k];
 }
 }
 //设置炮
 if (i == 2 && (k == 1 || k == 7))
 {
 chessname = blackChess[5];
 mcolor = BLACK;
 }
 //设置小兵
 if (i == 3 && k % 2 == 0)
 {
 chessname = blackChess[6];
 mcolor = BLACK;
 }


 }
 //紅棋
 else
 {
 //初始化第一行的棋子,
 if (i == 9)
 {
 mcolor = RED;
 if (k <= 4)
 {
 chessname = redChess[k];
 }
 else
 {
 chessname = redChess[8 - k];
 }
 }
 //设置炮
 if (i == 7 && (k == 1 || k == 7))
 {
 chessname = redChess[5];
 mcolor = RED;
 }
 //设置小兵
 if (i == 6 && k % 2 == 0)
 {
 chessname = redChess[6];
 mcolor = RED;
 }
 }
 map[i][k].id = chessname;
 map[i][k].river = false;
 map[i][k].type = mcolor;
 map[i][k].x = k * CHESS_GRID_SIZE + INTERVAL;
 map[i][k].y = i * CHESS_GRID_SIZE + INTERVAL;
 }
 }
 loadimage(&Pic1, "wj1.bmp");//背景图片设置
 loadimage(&Pic2, "wj2.bmp");
 putimage(680, 80, &Pic1);
 putimage(680, 600, &Pic2);
 //cleardevice();
 settextcolor(BLACK);
 settextstyle(35, 0, "微软雅黑", 0, 0, 500, false, false, false);
 outtextxy(670, 160, "玩家一");
 settextcolor(BLACK);
 settextstyle(35, 0, "微软雅黑", 0, 0, 500, false, false, false);
 outtextxy(670, 555, "玩家二");
 solidrectangle(650, 340, 800, 410); // 用背景颜色清除时间文本区域,达到使得时间文本区域透明的效果


 visualize();
}


void Outlook() {
 
 settextcolor(BLACK);
 clock_t t = 600, t0 = 1000; //初始化时间变量
 char time_str[50];//计时器部分
 //int minutes = 9, seconds = 59;//初始化,参数即为开始倒计时的时间


 while (t/*minutes != 0 || seconds != 0*/) {//倒计时逻辑
 /*if (seconds == 0) {
 minutes--; seconds = 59;
 }
 else seconds--;*/
 
 t = 600 - (clock() - start) / 1000;  //倒计时剩余时间


 settextcolor(BLACK);
 //settextstyle(35, 0, "微软雅黑", 0, 0, 500, false, false, false);
 //setbkcolor(RGB(252, 215, 162)); // 设置刷新小矩形背景颜色
 //setfillstyle(SOLID_FILL, RGB(252, 215, 162)); // 设置填充样式
 //bar(650, 340, 800, 410); // 用背景颜色清除时间文本区域,达到使得时间文本区域透明的效果
 //sprintf_s(t,"%02d:%02d",minutes,seconds); //打印时间文字的函数
 if (t != t0) {
 settextstyle(50, 0, "Consolas");//时间显示文字的样式
 sprintf(time_str, "%02d:%02d", t / 60, t % 60); //打印时间文字的函数
 outtextxy(680, 340, time_str);//打印时间文字的函数
 FlushBatchDraw();
 t0 = t;


 }
 while (t == 300)  //五分钟后弹出弹窗
 {
 HWND hand = GetHWnd();    // 获取图形窗口的句柄
 int ResultMB = MessageBox(hand, "随机天罚降临!!!", "温馨提示", MB_OKCANCEL);
 // 点击不同按钮之后的不同操作,省略了取消按钮
 if (IDOK == ResultMB) {
 //printf("弹窗点击了确认");可以不加
 }
 break;
 }
 Sleep(100);
 }
}


//游戏的绘制函数
void GameDraw()
{
 //设置背景颜色 red black 
 //setbkcolor(RGB(252, 215, 162));
 solidrectangle(0, 0, 670, 800);
 //绘制棋盘
 setlinecolor(BLACK);
 setlinestyle(PS_SOLID, 2);
 for (int i = 0; i < 10; i++)
 {
 //画横线
 line(INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, i * CHESS_GRID_SIZE + INTERVAL);
 //画竖线
 if (i < 9)
 {
 line(i * CHESS_GRID_SIZE + INTERVAL, INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
 }
 }
 rectangle(INTERVAL - 5, INTERVAL - 5, 8 * CHESS_GRID_SIZE + INTERVAL + 5, 5 + 9 * CHESS_GRID_SIZE + INTERVAL);
 //楚河汉界显示
 fillrectangle(INTERVAL, 4 * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL);
 //显示文字


 char river[21] = "楚河        汉界";
 settextstyle(50, 0, _T("楷体"));
 settextcolor(BLACK);
 //setbkmode(TRANSPARENT);
 outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
 outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
 //画九宫格
 //画上面
 line(3 * CHESS_GRID_SIZE + INTERVAL, INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL);
 line(3 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, INTERVAL);
 //画下面
 line(3 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
 line(3 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL);
 //画棋子
 settextstyle(30, 0, _T("楷体"));
 for (int i = 0; i < ROW; i++)
 {
 for (int k = 0; k < COL; k++)
 {
 if (map[i][k].id != SPACE)
 {
 setlinecolor(map[i][k].type);
 fillcircle(map[i][k].x, map[i][k].y, 30);
 fillcircle(map[i][k].x, map[i][k].y, 25);
 settextcolor(map[i][k].type);
 outtextxy(map[i][k].x - 15, map[i][k].y - 15, chessName[map[i][k].id]);
 }


 }
 }
 //Outlook();
}
void chessMove() {
 bool canmove = 0;
 int j = 0;
 printf("begin.x=%d\nbegin.y=%d\nend.x=%d\nend.y=%d\n", begin.x, begin.y, end.x, end.y);
 if (!(begin.x == end.x && begin.y == end.y)
 && end.x != -1 && end.y != -1
 && map[begin.x][begin.y].id != SPACE
 && map[begin.x][begin.y].type != map[end.x][end.y].type) {
 printf("CAN");
 switch (map[begin.x][begin.y].id)
 {
 case 車:
 case 俥:
 if (begin.x == end.x) {
 j = 1;
 if (end.y > begin.y) {
 printf("canmove");
 for (int i = begin.y + 1; i < end.y; i++) {
 printf("i=%d\nmap[][]=%d\n", i, map[begin.x][i].id);
 if (map[begin.x][i].id != SPACE) { j = 0; printf("stop\n"); }
 }
 }
 else if (end.y < begin.y) {
 for (int i = end.y + 1; i < begin.y; i++) {
 if (map[begin.x][i].id != SPACE) j = 0;
 }
 }
 }
 else if (end.y == begin.y) {
 j = 1;
 if (begin.x < end.x) {
 for (int i = begin.x + 1; i < end.x; i++) {
 if (map[i][end.y].id != SPACE) j = 0;
 }
 }
 else if (begin.x > end.x) {
 for (int i = end.x + 1; i < begin.x; i++) {
 if (map[i][end.y].id != SPACE) j = 0;
 }
 }
 }
 canmove += j;
 break;
 case 馬:
 case 马:
 if (end.y - begin.y == 2 && map[begin.x][begin.y + 1].type == WHITE && abs(end.x - begin.x) == 1)canmove = 1;
 else if (end.y - begin.y == -2 && map[begin.x][begin.y - 1].id == SPACE && abs(end.x - begin.x) == 1)canmove = 1;
 else if (end.x - begin.x == -2 && map[begin.x - 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)canmove = 1;
 else if (end.x - begin.x == 2 && map[begin.x + 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)canmove = 1;
 break;
 case 象:
 case 相:
 if ((begin.x < 5 && end.x < 5) || (begin.x > 4 && end.x > 4)) {
 if (end.y - begin.y == 2 && map[begin.x + 1][begin.y + 1].type == WHITE && end.x - begin.x == 2)canmove = 1;
 else if (end.y - begin.y == -2 && map[begin.x + 1][begin.y - 1].type == WHITE && end.x - begin.x == 2)canmove = 1;
 else if (end.y - begin.y == 2 && map[begin.x - 1][begin.y + 1].type == WHITE && end.x - begin.x == -2)canmove = 1;
 else if (end.y - begin.y == -2 && map[begin.x - 1][begin.y - 1].type == WHITE && end.x - begin.x == -2)canmove = 1;
 }
 break;
 case 士:
 case 仕:
 if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
 if (end.y > 2 && end.y < 6) {
 if (abs(end.y - begin.y) == 1 && abs(end.x - begin.x) == 1)canmove = 1;
 }
 break;
 case 将:
 case 帥:
 if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
 if (end.y > 2 && end.y < 6) {
 if (abs(end.y - begin.y) + abs(end.x - begin.x) == 1)canmove = 1;
 }
 break;
 case 砲:
 case 炮:
 if (begin.x == end.x) {
 j = 1;
 if (end.y > begin.y) {
 printf("canmove");
 for (int i = begin.y + 1; i < end.y; i++) {
 if (map[begin.x][i].id != SPACE) { j--; }
 }
 if (map[begin.x][end.y].type == WHITE) j--;
 }
 else if (end.y < begin.y) {
 for (int i = end.y + 1; i < begin.y; i++) {
 if (map[begin.x][i].id != SPACE) j--;
 }
 if (map[end.x][end.y].type == WHITE) j--;
 }
 }
 else if (end.y == begin.y) {
 j = 1;
 if (begin.x < end.x) {
 for (int i = begin.x + 1; i < end.x; i++) {
 if (map[i][end.y].id != SPACE) j--;
 }
 if (map[end.x][end.y].type == WHITE) j--;
 }
 else if (begin.x > end.x) {
 for (int i = end.x + 1; i < begin.x; i++) {
 if (map[i][end.y].id != SPACE) j--;
 }
 if (map[end.x][end.y].type == WHITE) j--;
 }
 }
 if (j == 0) canmove = 1;
 break;
 case 卒:
 case 兵:
 if (map[begin.x][begin.y].type == BLACK && end.x - begin.x >= 0) {
 if (end.x - begin.x == 1 && end.y == begin.y) canmove = 1;
 else if (begin.x > 4 && begin.x == end.x && abs(begin.y - end.y) == 1) canmove = 1;
 }
 else if (map[begin.x][begin.y].type == RED && end.x - begin.x <= 0) {
 if (end.x - begin.x == -1 && end.y == begin.y) canmove = 1;
 else if (begin.x < 5 && begin.x == end.x && abs(begin.y - end.y) == 1) canmove = 1;
 }
 break;
 }
 if (canmove) {
 if (map[end.x][end.y].id == 将 || map[end.x][end.y].id == 帥) flag_play = 0;
 map[end.x][end.y].id = map[begin.x][begin.y].id;
 map[end.x][end.y].type = map[begin.x][begin.y].type;
 map[begin.x][begin.y].id = SPACE;
 map[begin.x][begin.y].type = WHITE;


 }
 step += canmove;
 printf("step=%d", step);
 }
}
void MouseControl()
{
 ExMessage msg;
 while (true)
 {
 msg = getmessage(); //接收键鼠消息
 if (msg.message == WM_KEYUP&&msg.vkcode==VK_ESCAPE) { //如果按下ESC键,则表示退出游戏
 flag_play = 0;
 break;
 }
 if (msg.message == WM_LBUTTONUP)
 {
 //获取鼠标点击的数组的下标
 map[-1][-1].type = WHITE;
 int row = (msg.y - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
 int col = (msg.x - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
 if ((step % 4 == 0 && map[row][col].type == RED || step % 4 == 2 && map[row][col].type == BLACK))
 {


 state = END;
 begin.x = row;
 begin.y = col;
 step++;
 
 }
 else if ((step % 4 == 1 || step % 4 == 3))
 {
 state = BEGIN;
 end.x = row;
 end.y = col;
 chessMove();
 break;
 }
 printf("%d,%d  %d\n", row, col, state);
 }
 }
}


int main()
{
 //创建一个图形窗口
 initgraph(800, 800, SHOWCONSOLE);
 setbkcolor(RGB(252, 215, 162));
 setfillcolor(RGB(252, 215, 162));
 //setbkmode(0);
 cleardevice();
 GameInit();
 BeginBatchDraw();
 start = clock(); //重置倒计时时间
 std::thread(Outlook).detach();
 while (flag_play)
 {
 GameDraw();
 MouseControl();
 start = clock(); //重置倒计时时间
 FlushBatchDraw();


 }
 printf("GAME OVER");
 EndBatchDraw();
 return 0;


}

ava
开心一课

2023-12-18

0

sleep 1秒的延时占用了大部分循环时间, 计时部分应该在子线程中运行,把outlook函数拆分一下即可

添加线程头文件

#include <thread>

outlook中的这三个变量提升为全局变量, 给子线程处理,主线程显示

char t[100];                   // 计时器部分
int minutes = 9, seconds = 59; // 初始化,参数即为开始倒计时的时间

outlook函数拆分成以下两个函数, 

void TimerThread(){
    while (minutes != 0 || seconds != 0)
    { // 倒计时逻辑
        if (seconds == 0)
        {
            minutes--;
            seconds = 59;
        }
        else
            seconds--;
        Sleep(1000);                                  // 1秒
    }
}


void Outlook()
{
    settextstyle(50, 0, "Consolas"); // 时间显示文字的样式
    settextcolor(BLACK);


    if (minutes != 0 || seconds != 0)
    { // 倒计时逻辑
                               // 1秒
        setbkcolor(RGB(252, 215, 162));               // 设置刷新小矩形背景颜色
        setfillstyle(SOLID_FILL, RGB(252, 215, 162)); // 设置填充样式
        bar(650, 340, 800, 410);                      // 用背景颜色清除时间文本区域,达到使得时间文本区域透明的效果
        sprintf_s(t, "%02d:%02d", minutes, seconds);  // 打印时间文字的函数
        outtextxy(650, 340, t);                       // 打印时间文字的函数
        FlushBatchDraw();
        while (minutes == 4 && seconds >= 59) // 五分钟后弹出弹窗
        {
            HWND hand = GetHWnd(); // 获取图形窗口的句柄
            int ResultMB = MessageBox(hand, "随机天罚降临!!!", "温馨提示", MB_OKCANCEL);
            // 点击不同按钮之后的不同操作,省略了取消按钮
            if (IDOK == ResultMB)
            {
                // printf("弹窗点击了确认");可以不加
            }
            break;
        }
    }
}

main函数如下修改

int main()
{
    // 创建一个图形窗口
    initgraph(800, 800, SHOWCONSOLE);
    GameInit();
    BeginBatchDraw();

    // 在子线程中启动监听
    auto inputThread = std::thread([]()
                                   {
                                    while(1){
                                       TimerThread();

                                    } });
    while (O)
    {
        MouseControl();
        GameDraw();
        FlushBatchDraw();
    }
    printf("GAME OVER");
    return 0;
}

以下是完整代码

#include <stdio.h>
#include <tchar.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#include <easyx.h>
#include <thread>
#define INTERVAL 50        // 前面的间隔
#define CHESS_GRID_SIZE 70 // 格子宽度
#define ROW 10
#define COL 9
int O = 1, step = 0;
enum PIECES // 枚举棋子
{
    JU,
    MA,
    XIANG,
    SHI,
    JIANG,
    PAO,
    ZU,
    CHE,
    MAA,
    XIANGG,
    SHII,
    SHUAI,
    PAOO,
    BING,
    SPACE,
    BEGIN,
    END,
    NONE
};
// 红黑方棋子、
int blackChess[] = {JU, MA, XIANG, SHI, JIANG, PAO, ZU};
int redChess[] = {CHE, MAA, XIANGG, SHII, SHUAI, PAOO, BING};
const char* chessName[] = { "車", "馬", "象", "士", "将", "砲", "卒", "俥", "马", "相", "仕", "帥", "炮", "兵" };
char t[100];                   // 计时器部分
int minutes = 9, seconds = 59; // 初始化,参数即为开始倒计时的时间

struct Chess // 棋子属性
{
    int x;
    int y;
    int id;     // 哪个棋子
    int type;   // 是哪一方的棋子 红,黑?
    bool river; // 判断小兵是否过了河
};
struct Chess map[ROW][COL];             // 结构体数组,用来保存每一个点的信息
POINT begin = {-1, -1}, end = {-1, -1}; // 保存前后两次点击的数组下标
int state = BEGIN;
void visualize()
{
    for (int i = 0; i < 10; i++)
    {
        printf("\n");
        for (int j = 0; j < 9; j++)
        {
            printf("  %d", map[i][j].type);
        }
    }
}
// 初始化游戏数据
void GameInit()
{
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            int chessname = SPACE;
            int mcolor = WHITE;
            // 黑棋
            if (i <= 4)
            {
                // 初始化第一行的棋子,
                if (i == 0)
                {
                    mcolor = BLACK;
                    if (k <= 4)
                    {
                        chessname = blackChess[k];
                    }
                    else
                    {
                        chessname = blackChess[8 - k];
                    }
                }
                // 设置炮
                if (i == 2 && (k == 1 || k == 7))
                {
                    chessname = blackChess[5];
                    mcolor = BLACK;
                }
                // 设置小兵
                if (i == 3 && k % 2 == 0)
                {
                    chessname = blackChess[6];
                    mcolor = BLACK;
                }
            }
            // 紅棋
            else
            {
                // 初始化第一行的棋子,
                if (i == 9)
                {
                    mcolor = RED;
                    if (k <= 4)
                    {
                        chessname = redChess[k];
                    }
                    else
                    {
                        chessname = redChess[8 - k];
                    }
                }
                // 设置炮
                if (i == 7 && (k == 1 || k == 7))
                {
                    chessname = redChess[5];
                    mcolor = RED;
                }
                // 设置小兵
                if (i == 6 && k % 2 == 0)
                {
                    chessname = redChess[6];
                    mcolor = RED;
                }
            }
            map[i][k].id = chessname;
            map[i][k].river = false;
            map[i][k].type = mcolor;
            map[i][k].x = k * CHESS_GRID_SIZE + INTERVAL;
            map[i][k].y = i * CHESS_GRID_SIZE + INTERVAL;
        }
    }
    visualize();
}

void TimerThread()
{
    while (minutes != 0 || seconds != 0)
    { // 倒计时逻辑
        if (seconds == 0)
        {
            minutes--;
            seconds = 59;
        }
        else
            seconds--;
        Sleep(1000); // 1秒
    }
}

void Outlook()
{
    settextstyle(50, 0, "Consolas"); // 时间显示文字的样式
    settextcolor(BLACK);

    if (minutes != 0 || seconds != 0)
    {                                                 // 倒计时逻辑
                                                      // 1秒
        setbkcolor(RGB(252, 215, 162));               // 设置刷新小矩形背景颜色
        setfillstyle(SOLID_FILL, RGB(252, 215, 162)); // 设置填充样式
        bar(650, 340, 800, 410);                      // 用背景颜色清除时间文本区域,达到使得时间文本区域透明的效果
        sprintf_s(t, "%02d:%02d", minutes, seconds);  // 打印时间文字的函数
        outtextxy(650, 340, t);                       // 打印时间文字的函数
        FlushBatchDraw();
        while (minutes == 4 && seconds >= 59) // 五分钟后弹出弹窗
        {
            HWND hand = GetHWnd(); // 获取图形窗口的句柄
            int ResultMB = MessageBox(hand, "随机天罚降临!!!", "温馨提示", MB_OKCANCEL);
            // 点击不同按钮之后的不同操作,省略了取消按钮
            if (IDOK == ResultMB)
            {
                // printf("弹窗点击了确认");可以不加
            }
            break;
        }
    }
}

// 游戏的绘制函数
void GameDraw()
{
    // 设置背景颜色 red black
    setbkcolor(RGB(252, 215, 162));
    IMAGE Pic1;
    IMAGE Pic2;

    loadimage(&Pic1, "wj1.png"); // 背景图片设置
    loadimage(&Pic2, "wj2.png");

    cleardevice();
    settextcolor(BLACK);
    settextstyle(35, 0, "微软雅黑", 0, 0, 500, false, false, false);
    outtextxy(670, 160, "玩家一");
    settextcolor(BLACK);
    settextstyle(35, 0, "微软雅黑", 0, 0, 500, false, false, false);
    outtextxy(670, 555, "玩家二");

    putimage(680, 80, &Pic1);
    putimage(680, 600, &Pic2);

    // 绘制棋盘
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 2);
    for (int i = 0; i < 10; i++)
    {
        // 画横线
        line(INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, i * CHESS_GRID_SIZE + INTERVAL);
        // 画竖线
        if (i < 9)
        {
            line(i * CHESS_GRID_SIZE + INTERVAL, INTERVAL, i * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
        }
    }
    rectangle(INTERVAL - 5, INTERVAL - 5, 8 * CHESS_GRID_SIZE + INTERVAL + 5, 5 + 9 * CHESS_GRID_SIZE + INTERVAL);
    // 楚河汉界显示
    setfillcolor(RGB(252, 215, 162));
    fillrectangle(INTERVAL, 4 * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL);
    // 显示文字

    char river[21] = "楚河        汉界";
    settextstyle(50, 0, _T("楷体"));
    settextcolor(BLACK);
    setbkmode(TRANSPARENT);
    outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
    outtextxy(INTERVAL + 100, 4 * CHESS_GRID_SIZE + INTERVAL + 10, river);
    // 画九宫格
    // 画上面
    line(3 * CHESS_GRID_SIZE + INTERVAL, INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, INTERVAL);
    // 画下面
    line(3 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL);
    // 画棋子
    settextstyle(30, 0, _T("楷体"));
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            if (map[i][k].id != SPACE)
            {
                setlinecolor(map[i][k].type);
                fillcircle(map[i][k].x, map[i][k].y, 30);
                fillcircle(map[i][k].x, map[i][k].y, 25);
                settextcolor(map[i][k].type);
                outtextxy(map[i][k].x - 10, map[i][k].y - 10, chessName[map[i][k].id]);
            }
        }
    }
    Outlook();
}
void chessMove()
{
    int canmove = 0;
    int j = 0;
    printf("begin.x=%d\nbegin.y=%d\nend.x=%d\nend.y=%d\n", begin.x, begin.y, end.x, end.y);
    if (!(begin.x == end.x && begin.y == end.y) && end.x != -1 && end.y != -1 && map[begin.x][begin.y].id != SPACE && map[begin.x][begin.y].type != map[end.x][end.y].type)
    {
        printf("CAN");
        switch (map[begin.x][begin.y].id)
        {
        case JU:
        case CHE:
            if (begin.x == end.x)
            {
                j = 1;
                if (end.y > begin.y)
                {
                    printf("canmove");
                    for (int i = begin.y + 1; i < end.y; i++)
                    {
                        printf("i=%d\nmap[][]=%d\n", i, map[begin.x][i].id);
                        if (map[begin.x][i].id != SPACE)
                        {
                            j = 0;
                            printf("stop\n");
                        }
                    }
                }
                else if (end.y < begin.y)
                {
                    for (int i = end.y + 1; i < begin.y; i++)
                    {
                        if (map[begin.x][i].id != SPACE)
                            j = 0;
                    }
                }
            }
            else if (end.y == begin.y)
            {
                j = 1;
                if (begin.x < end.x)
                {
                    for (int i = begin.x + 1; i < end.x; i++)
                    {
                        if (map[i][end.y].id != SPACE)
                            j = 0;
                    }
                }
                else if (begin.x > end.x)
                {
                    for (int i = end.x + 1; i < begin.x; i++)
                    {
                        if (map[i][end.y].id != SPACE)
                            j = 0;
                    }
                }
            }
            canmove += j;
            break;
        case MA:
        case MAA:
            if (end.y - begin.y == 2 && map[begin.x][begin.y + 1].type == WHITE && abs(end.x - begin.x) == 1)
                canmove = 1;
            else if (end.y - begin.y == -2 && map[begin.x][begin.y - 1].id == SPACE && abs(end.x - begin.x) == 1)
                canmove = 1;
            else if (end.x - begin.x == -2 && map[begin.x - 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)
                canmove = 1;
            else if (end.x - begin.x == 2 && map[begin.x + 1][begin.y].id == SPACE && abs(end.y - begin.y) == 1)
                canmove = 1;
            break;
        case XIANG:
        case XIANGG:
            if ((begin.x < 5 && end.x < 5) || (begin.x > 4 && end.x > 4))
            {
                if (end.y - begin.y == 2 && map[begin.x + 1][begin.y + 1].type == WHITE && end.x - begin.x == 2)
                    canmove = 1;
                else if (end.y - begin.y == -2 && map[begin.x + 1][begin.y - 1].type == WHITE && end.x - begin.x == 2)
                    canmove = 1;
                else if (end.y - begin.y == 2 && map[begin.x - 1][begin.y + 1].type == WHITE && end.x - begin.x == -2)
                    canmove = 1;
                else if (end.y - begin.y == -2 && map[begin.x - 1][begin.y - 1].type == WHITE && end.x - begin.x == -2)
                    canmove = 1;
            }
            break;
        case SHI:
        case SHII:
            if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
                if (end.y > 2 && end.y < 6)
                {
                    if (abs(end.y - begin.y) == 1 && abs(end.x - begin.x) == 1)
                        canmove = 1;
                }
            break;
        case JIANG:
        case SHUAI:
            if ((begin.x < 3 && end.x < 3) || (begin.x > 6 && end.x > 6))
                if (end.y > 2 && end.y < 6)
                {
                    if (abs(end.y - begin.y) + abs(end.x - begin.x) == 1)
                        canmove = 1;
                }
            break;
        case PAO:
        case PAOO:
            if (begin.x == end.x)
            {
                j = 1;
                if (end.y > begin.y)
                {
                    printf("canmove");
                    for (int i = begin.y + 1; i < end.y; i++)
                    {
                        if (map[begin.x][i].id != SPACE)
                        {
                            j--;
                        }
                    }
                    if (map[begin.x][end.y].type == WHITE)
                        j--;
                }
                else if (end.y < begin.y)
                {
                    for (int i = end.y + 1; i < begin.y; i++)
                    {
                        if (map[begin.x][i].id != SPACE)
                            j--;
                    }
                    if (map[end.x][end.y].type == WHITE)
                        j--;
                }
            }
            else if (end.y == begin.y)
            {
                j = 1;
                if (begin.x < end.x)
                {
                    for (int i = begin.x + 1; i < end.x; i++)
                    {
                        if (map[i][end.y].id != SPACE)
                            j--;
                    }
                    if (map[end.x][end.y].type == WHITE)
                        j--;
                }
                else if (begin.x > end.x)
                {
                    for (int i = end.x + 1; i < begin.x; i++)
                    {
                        if (map[i][end.y].id != SPACE)
                            j--;
                    }
                    if (map[end.x][end.y].type == WHITE)
                        j--;
                }
            }
            if (j == 0)
                canmove = 1;
            break;
        case ZU:
        case BING:
            if (map[begin.x][begin.y].type == BLACK && end.x - begin.x >= 0)
            {
                if (end.x - begin.x == 1 && end.y == begin.y)
                    canmove = 1;
                else if (begin.x > 4 && begin.x == end.x && abs(begin.y - end.y) == 1)
                    canmove = 1;
            }
            else if (map[begin.x][begin.y].type == RED && end.x - begin.x <= 0)
            {
                if (end.x - begin.x == -1 && end.y == begin.y)
                    canmove = 1;
                else if (begin.x < 5 && begin.x == end.x && abs(begin.y - end.y) == 1)
                    canmove = 1;
            }
            break;
        }
        if (canmove)
        {
            if (map[end.x][end.y].id == JIANG || map[end.x][end.y].id == SHUAI)
                O = 0;
            map[end.x][end.y].id = map[begin.x][begin.y].id;
            map[end.x][end.y].type = map[begin.x][begin.y].type;
            map[begin.x][begin.y].id = SPACE;
            map[begin.x][begin.y].type = WHITE;
        }
        step += canmove;
        printf("step=%d", step);
    }
}
void MouseControl()
{
    if (MouseHit())
    {
        MOUSEMSG msg = GetMouseMsg();
        if (msg.uMsg == WM_LBUTTONDOWN)
        {
            // 获取鼠标点击的数组的下标
            map[-1][-1].type = WHITE;
            int row = (msg.y - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
            int col = (msg.x - INTERVAL + CHESS_GRID_SIZE / 2) / CHESS_GRID_SIZE;
            if ((step % 4 == 0 && map[row][col].type == RED || step % 4 == 2 && map[row][col].type == BLACK))
            {

                state = END;
                begin.x = row;
                begin.y = col;
                step++;
            }
            else if ((step % 4 == 1 || step % 4 == 3))
            {
                state = BEGIN;
                end.x = row;
                end.y = col;
                chessMove();
            }
            printf("%d,%d  %d\n", row, col, state);
        }
    }
}

int main()
{
    // 创建一个图形窗口
    initgraph(800, 800, SHOWCONSOLE);
    GameInit();
    BeginBatchDraw();

    // 在子线程中启动监听
    auto inputThread = std::thread([]()
                                   {
                                    while(1){
                                       TimerThread();

                                    } });
    while (O)
    {
        MouseControl();
        GameDraw();
        FlushBatchDraw();
    }
    printf("GAME OVER");
    return 0;
}
ava
このすば

2023-12-18

技术讨论社区