标题:运行时,右侧长方块向上移动时,实际为向上伸长,不知代码哪里出问题了,请 ...
只看楼主
huaheshang
Rank: 2
来 自:日照
等 级:论坛游民
帖 子:91
专家分:37
注 册:2010-12-7
结帖率:79.31%
 问题点数:0 回复次数:2 
运行时,右侧长方块向上移动时,实际为向上伸长,不知代码哪里出问题了,请指点一二
#include"graphics.h"
#include"conio.h"
#include"time.h"

int main()
{
    initgraph(640, 480);
    srand((unsigned)time(NULL));
    setbkcolor(WHITE);
    cleardevice();


    setfillcolor(BLACK);
    setlinecolor(BLACK);
    settextstyle(30, 0, L"verdana");
    settextcolor(BLACK);


    int p1x, p1y, p2x, p2y, ball_x, ball_y;
    int dx, dy;
    int  win = 1;
    p1x = 45;
    p1y = 240;
    p2x = 595;
    p2y = 240;
    ball_x = rand() % 240 + 200;
    ball_y = rand() % 180 + 150;
    dx = (rand() % 2*2 - 1) * 2;
    dy = (rand() % 2*2 - 1) * 2;
   
   
    outtextxy(190, 200, L"Put Enter To  Begin ");
    while (_getwch()!= 13);


    cleardevice();

    settextstyle(20, 0, L"Verdana");
    outtextxy(20, 50, L"Player1");
    outtextxy(565, 50, L"Player2");


    settextstyle(15, 0, L"Verdana");
    outtextxy(20, 400, L"W 向上");
    outtextxy(20, 425, L"S 向下");
    outtextxy(595, 400, L"O 向上");
    outtextxy(595, 425, L"L 向下");

   
    line(50, 100, 590, 100);
    line(50, 380, 590, 380);

    solidcircle(ball_x, ball_y,10);


    wchar_t key = 0;
    while (true)
    {
        clearcircle(ball_x, ball_y,10);

        clearrectangle(p1x - 5, p1y - 20, p1x + 5, p1x + 20);

        clearrectangle(p2x -5 , p2y - 20, p2x + 5, p2y + 20);

;
         
        if (_kbhit())
        {
    key = _getwch();

    if (key == L's')
        p1y += 30;
    else if (key == L'w')
        p1y -= 30;
    else if (key == L'l')
        p2y += 30;
    else if (key == L'o')
        p2y -= 30;

         }
    if ((p1y - 20) < 100)
        p1y = 120;
    else if ((p1y + 20) > 380)
        p1y = 360;
    else if ((p2y - 20) < 100)
        p2y = 120;
    else if ((p2y + 20) > 380)
        p2y = 360;


        solidrectangle(p1x - 5, p1y - 20, p1x + 5, p1y + 20);
        solidrectangle(p2x - 5, p2y - 20, p2x + 5, p2y + 20);

        ball_x = ball_x + dx;
        ball_y = ball_y + dy;
        if (ball_x < 62)
        {
            if (ball_y<p1y + 20 && ball_y>p1y - 20)
            {
                dx = -dx;
                dx += dx / 10;
            }
            else
            {
                win = 2;
                solidcircle(ball_x, ball_y,10);
                break;
            }
        }
        
        else if (ball_x > 578)
        {
            if ((ball_y<p2y + 20) &&( ball_y>p2y - 20))
            {
                dx = -dx;
                dx += dx / 10;
            }

            else
            {
                solidcircle(ball_x, ball_y, 10);
                break;
            }
        }
        if (ball_y < 115)
            dy = -dy;
        else if (ball_y > 364)
            dy = -dy;


        solidcircle(ball_x, ball_y,10);

    Sleep(50);
   }

    settextstyle(30, 0, L"verdana");
    if (win == 1)
        outtextxy(250, 200, L"Player1 赢");
    else
        outtextxy(250, 200, L"Player2 赢");

    _getch();
    closegraph();
    return 0;
}
搜索更多相关主题的帖子: rand key int 向上 if 
2021-03-28 01:58
huaheshang
Rank: 2
来 自:日照
等 级:论坛游民
帖 子:91
专家分:37
注 册:2010-12-7
得分:0 
是左侧长方块,右侧正常
2021-03-28 01:59
huaheshang
Rank: 2
来 自:日照
等 级:论坛游民
帖 子:91
专家分:37
注 册:2010-12-7
得分:0 

        clearrectangle(p1x - 5, p1y - 20, p1x + 5, p1x + 20);
最后应为p1y+20
2021-04-01 22:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505270-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013070 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved