运行时,右侧长方块向上移动时,实际为向上伸长,不知代码哪里出问题了,请指点一二
#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;
}