当下是我的选择 2022-12-20 02:50 采纳率: 0%
浏览 82
已结题

easy-x实现五子棋的键盘操作

要求使用easy-x实现五子棋的人人对战,键盘操作,显示光标所在位置,落子情况。

我光标的移动是使用清屏来实现的,但是清屏就会清除以前的落子情况,用来输出光标图片的坐标也在变化,不能通过它的参数输出以前的落子,又不知道会下几步棋,于是我放弃了用其他变量输出落子的想法。翻了easy-x的函数库好像又没有可以清除某个指定图片的函数,于是不知道该怎么做下去了。

想知道如何才能实现键盘控制光标标移动,又不会影响落子情况和已绘制的棋盘。

initgraph(480, 540);
    loadimage(&bkg, _T("board.jpg"));
    loadimage(&cursor, _T("6.png"));
    //初始化
    int num = 0;
    int current_player;                                    //0:黑子  1:白子
    int cursor_x, cursor_y;   //光标位置
    int x, y;
    //重置光标
    cursor_x = 213;
    cursor_y = 213;
    putimage(0, 0, 240, 240, &bkg, 60, 60);
    putimage(0, 240, 240, 240, &bkg, 60, 60);
    putimage(240, 0, 240, 240, &bkg, 60, 60);
    putimage(240, 240, 240, 240, &bkg, 60, 60);
    setlinecolor(BLACK);
    for (x = 15; x < 480; x += 30)
    {
        line(x, 15, x, 465);
        for (y = 15; y < 480; y += 30)
            line(15, y, 465, y);
    }
    setfillcolor(BLACK);
    for (int i = 4 * 30 - 15; i <= 12 * 30 - 15; i += 4 * 30)
        for (int j = 4 * 30 - 15; j <= 12 * 30 - 15; j += 4 * 30)
            fillcircle(i, j, 2.5);
    putimage(cursor_x, cursor_y, 26, 26, &cursor, 0, 0);                          //print cursor
    if (1)
        current_player = 1;
    else
        current_player = 2;
    //start game
    while (1)
    {
        int key = _getch();
        switch (key)
        {
        case 0XE0:
            switch (key = _getch())
            {
            case 'w':
            case 'W':
            case 72:if (cursor_y >= 15)
                cursor_y -= 30; break;
            case 'a':
            case 'A':
            case 75:if (cursor_x >= 15)
                cursor_x -= 30; break;
            case 's':
            case 'S':
            case 80:if (cursor_y <= 452)
                cursor_y += 30; break;
            case 'd':
            case 'D':
            case 77:if (cursor_x <= 452)
                cursor_x += 30; break;
            }
        }
        cleardevice();
        putimage(0, 0, 240, 240, &bkg, 60, 60);
        putimage(0, 240, 240, 240, &bkg, 60, 60);
        putimage(240, 0, 240, 240, &bkg, 60, 60);
        putimage(240, 240, 240, 240, &bkg, 60, 60);
        for (x = 15; x < 480; x += 30)
        {
            line(x, 15, x, 465);
            for (y = 15; y < 480; y += 30)
                line(15, y, 465, y);
        }
        for (int i = 4 * 30 - 15; i <= 12 * 30 - 15; i += 4 * 30)
            for (int j = 4 * 30 - 15; j <= 12 * 30 - 15; j += 4 * 30)
                fillcircle(i, j, 2.5);
        putimage(cursor_x, cursor_y, 26, 26, &cursor, 0, 0);
        draw_piece(num, cursor_x + 13, cursor_y + 13);
        //m = GetMouseMsg();
        //if (m.uMsg == WM_LBUTTONDOWN)
            //x++;
    }
    return 0;
    }
void draw_piece(int num, int xx ,int yy)
{
    if (num == 0)
        setfillcolor(BLACK);
    else if (num == 1)
        setfillcolor(WHITE);
    int ch = _getch();
        if (ch == 32)
            fillcircle(xx, yy, 10);
    num = (num + 1) % 2;
}



img

  • 写回答

3条回答 默认 最新

  • Minuw 2022-12-20 08:22
    关注

    https://blog.csdn.net/lie_u/article/details/107950505

    
    #include <graphics.h>
    #include <conio.h>
    #include<stdio.h>
    #include <time.h>
    
    #define NUM_B 225 //棋子数量
    #define HL 15 //上下
    #define X(x) 162+x50
    #define Y(y) 30+y50
    #define BXY 15 //横轴/纵轴数量
    void board(void);
    int get_x(short xx);
    int get_y(short yy);
    int left_right(int(*arr)[BXY], short x, short y); //左右
    int up_down(int(*arr)[BXY], short x, short y); //上下
    int lu_rd(int(*arr)[BXY], short x, short y); //左上右下
    int ld_ru(int(*arr)[BXY], short x, short y); //左下右上
    void win_b(void);
    void win_w(void);
    void fall_w(void); //落白子
    void fall_b(void); //落黑子
    
    int main()
    {
    int undo_x = 0, undo_y = 0;
    int again = 0; //用于胜利后开启下一局的开关
    int cut = 0; //用于胜利后棋盘不可下棋的开关
    int FLAG[BXY][BXY]; //棋盘,储存棋子,0代表空,1代表白子,2代表黑子
    int x, y; //坐标
    int i, j, wb; //i,j储存鼠标上一个状态,wb控制下一局开局的棋子颜色(这个感觉有没有都一样,,,写了就不删了)
    i = j = wb = 0;
    int x1[2] = { -2,-2 }, y1[2] = { -2,-2 }; //储存鼠标前后状态,用于消除十字准星
    board(); //创建窗口
    fall_b();
    /* 储存所有棋子坐标,并且把棋子状态设为0 */
    for (y = 0; y < 15; y++)
    for (x = 0; x < 15; x++)
    FLAG[x][y] = 0;
    
    MOUSEMSG m;        // 定义鼠标消息
    
    while (true)           //游戏无限循环
    {
        m = GetMouseMsg();        //获取鼠标信息
        switch (m.uMsg)
        {
        case WM_MOUSEMOVE:        //鼠标移动
            if (cut == 0 && (get_x(m.x) > -1 && get_x(m.x) < 15) && (get_y(m.y) > -1 && get_y(m.y) < 15) && FLAG[get_x(m.x)][get_y(m.y)] < 1)    //十字准星
            {
                // 创建一个矩形区域
                HRGN rgn = CreateRectRgn(162, 30, 862, 730);
                // 将该矩形区域设置为裁剪区
                setcliprgn(rgn);
                // 不再使用 rgn,清理 rgn 占用的系统资源
                DeleteObject(rgn);
    
                if (i == 0)
                {
                    x1[i] = get_x(m.x);
                    y1[i] = get_y(m.y);
                    i = 1;
                    j = 0;
                }
                else if (i == 1)
                {
                    x1[i] = get_x(m.x);
                    y1[i] = get_y(m.y);
                    i = 0;
                    j = 1;
                }
                setfillcolor(LIGHTBLUE);
                solidrectangle(X(get_x(m.x)) - 15, Y(get_y(m.y)) - 3, X(get_x(m.x)) + 15, Y(get_y(m.y)) + 3);
                solidrectangle(X(get_x(m.x)) - 3, Y(get_y(m.y)) - 15, X(get_x(m.x)) + 3, Y(get_y(m.y)) + 15);
            }
            break;
        case WM_LBUTTONDOWN:   //鼠标左键点击
            if (cut == 0 && (get_x(m.x) > -1 && get_x(m.x) < 15) && (get_y(m.y) > -1 && get_y(m.y) < 15) && FLAG[get_x(m.x)][get_y(m.y)] < 1)
            {
                // 取消之前设置的裁剪区
                setcliprgn(NULL);
    
                if (wb == 0)
                {
                    setfillcolor(BLACK);
                    solidcircle(X(get_x(m.x)), Y(get_y(m.y)), 20);
                    undo_x = get_x(m.x);
                    undo_y = get_y(m.y);
                    fall_w();
                    wb = 1;
                    FLAG[get_x(m.x)][get_y(m.y)] = 2;
    
                    if (left_right(FLAG, m.x, m.y) > 0 || up_down(FLAG, m.x, m.y) > 0 || lu_rd(FLAG, m.x, m.y) > 0 || ld_ru(FLAG, m.x, m.y) > 0)
                    {
    
                        win_b();
                        again = 1;
                        cut = 1;
                    }
                }
                else if (wb == 1)
                {
                    setfillcolor(WHITE);
                    solidcircle(X(get_x(m.x)), Y(get_y(m.y)), 20);
                    undo_x = get_x(m.x);
                    undo_y = get_y(m.y);
                    fall_b();
                    wb = 0;
                    FLAG[get_x(m.x)][get_y(m.y)] = 1;
                    if (left_right(FLAG, m.x, m.y) > 0 || up_down(FLAG, m.x, m.y) > 0 || lu_rd(FLAG, m.x, m.y) > 0 || ld_ru(FLAG, m.x, m.y) > 0)
                    {
                        win_w();
                        again = 1;
                        cut = 1;
                    }
                }
            }
            break;
        case WM_RBUTTONUP:               //鼠标右键点击
            if (again == 1)
            {
                for (y = 0; y < 15; y++)
                    for (x = 0; x < 15; x++)
                        FLAG[x][y] = 0;
                // 取消之前设置的裁剪区
                setcliprgn(NULL);
                board();
                undo_x = undo_y = 0;
                again = 0;
                cut = 0;
                if (wb == 1)
                {
                    wb = 0;
                    fall_b();
                }
                else
                {
                    wb = 1;
                    fall_w();
                }
            }
            else
            {
                if (FLAG[undo_x][undo_y] == 1)
                {
                    // 取消之前设置的裁剪区
                    setcliprgn(NULL);
                    setfillcolor(RGB(209, 186, 116));                              //填充背景颜色
                    solidrectangle(X(undo_x) - 20, Y(undo_y) - 20, X(undo_x) + 20, Y(undo_y) + 20);
                    line(X(undo_x) - 20, Y(undo_y), X(undo_x) + 20, Y(undo_y));
                    line(X(undo_x), Y(undo_y) - 20, X(undo_x), Y(undo_y) + 20);
                    /*9个带黑点的落子位置*/
                    if (undo_x != 15 && undo_y != 15 && (undo_x + 1) % 4 == 0 && (undo_y + 1) % 4 == 0)
                    {
                        setfillcolor(BLACK);
                        fillcircle(X(undo_x), Y(undo_y), 5);
                    }
                    fall_w();
                    FLAG[undo_x][undo_y] = 0;
                    wb = 1;
                }
                else if (FLAG[undo_x][undo_y] == 2)
                {
                    // 取消之前设置的裁剪区
                    setcliprgn(NULL);
    
                    setfillcolor(RGB(209, 186, 116));                              //填充背景颜色
                    solidrectangle(X(undo_x) - 20, Y(undo_y) - 20, X(undo_x) + 20, Y(undo_y) + 20);
                    line(X(undo_x) - 20, Y(undo_y), X(undo_x) + 20, Y(undo_y));
                    line(X(undo_x), Y(undo_y) - 20, X(undo_x), Y(undo_y) + 20);
                    /*9个带黑点的落子位置*/
                    if (undo_x != 15 && undo_y != 15 && (undo_x + 1) % 4 == 0 && (undo_y + 1) % 4 == 0)
                    {
                        setfillcolor(BLACK);
                        fillcircle(X(undo_x), Y(undo_y), 5);
                    }
                    fall_b();
                    FLAG[undo_x][undo_y] = 0;
                    wb = 0;
                }
            }
            break;
        }
        if (((get_x(m.x)) != x1[j] || (get_y(m.y) != y1[j])) && FLAG[x1[j]][y1[j]] < 1)
        {
            setfillcolor(RGB(209, 186, 116));                              //填充背景颜色
            solidrectangle(X(x1[j]) - 15, Y(y1[j]) - 15, X(x1[j]) + 15, Y(y1[j]) + 15);
            line(X(x1[j]) - 15, Y(y1[j]), X(x1[j]) + 15, Y(y1[j]));
            line(X(x1[j]), Y(y1[j]) - 15, X(x1[j]), Y(y1[j]) + 15);
            /*9个带黑点的落子位置*/
            if (x1[j] != 15 && y1[j] != 15 && (x1[j] + 1) % 4 == 0 && (y1[j] + 1) % 4 == 0)
            {
                setfillcolor(BLACK);
                fillcircle(X(x1[j]), Y(y1[j]), 5);
            }
        }
    }
    // 关闭图形窗口
    closegraph();
    
    _getch();
    
    return 0;
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    }
    
    void board()
    {
    initgraph(1024, 760); //窗口大小
    setbkcolor(RGB(190, 231, 233)); //窗口背景
    cleardevice();
    setlinecolor(BLACK); //线条颜色
    setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 2); //棋盘边框线条样式
    setfillcolor(RGB(209, 186, 116)); //棋盘背景颜色
    fillrectangle(132, 0, 892, 760);
    fillrectangle(162, 30, 862, 730); //棋盘大小,棋盘规格15X15
    setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 2); //棋盘线条样式
    for (int i = 80; i <= 730; i += 50) //棋盘横线
    line(162, i, 862, i);
    for (int i = 212; i <= 862; i += 50) //棋盘竖线
    line(i, 30, i, 730);
    setfillcolor(BLACK); //圆点填充颜色为黑色
    for (int i = 3; i <= 12; i += 4) //绘制9个圆点
    {
    fillcircle(X(i), Y(3), 5);
    fillcircle(X(i), Y(7), 5);
    fillcircle(X(i), Y(11), 5);
    }
    setfillcolor(RGB(190, 237, 199)); //棋盘背景颜色
    fillrectangle(20, 310, 115, 425);
    settextcolor(BLACK);
    settextstyle(25, 0, _T(“Consolas”));
    wchar_t s[] = L"请落子";
    outtextxy(30, 315, s);
    
    fillrectangle(5, 460, 125, 495);
    settextstyle(15, 0, _T("Consolas"));
    wchar_t s2[] = L"点击鼠标右键悔棋";
    outtextxy(10, 470, s2);
    
    settextstyle(25, 0, _T("Consolas"));
    wchar_t s1[] = L"yp_lien";
    settextcolor(RGB(214, 213, 183));
    outtextxy(930, 730, s1);
    1
    2
    3
    4
    5
    6
    7
    8
    9
    }
    
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月28日
  • 创建了问题 12月20日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境