两个 o 遇到边界跳反,一开始都挺正常 到两个o 坐标相同时就不对了。。。
代码在下面#include<stdio.h>#include<windows.h>
int width = 70;
int hight = 22;
int main()
{
int x, y, x1, y1;
int xSpeed = 1, ySpeed = 1, x1Speed=1, y1Speed=1;
void gotoxy(int x, int y);
void printfBall(int a, int b);
void drawString(char ch, int n);
void drawInterFace();
void drawBall(int x, int y);
void clearBall(int x, int y);
void Ballgame(int *x, int *y, int xSpeed, int ySpeed);
void Deriction(int x, int y, int *xSpeed, int *ySpeed);
x = 1;
y = 1;
x1 = 68;
y1 = 1;
drawInterFace();
while (1)
{
drawBall(x, y);
Sleep(200);
clearBall(x, y);
Ballgame(&x, &y, xSpeed, ySpeed);
Deriction(x, y, &xSpeed, &ySpeed);
drawBall(x1, y1);
Sleep(200);
clearBall(x1, y1);
Ballgame(&x1, &y1, x1Speed, y1Speed);
Deriction(x1, y1, &x1Speed, &y1Speed);
}
system("pause");
return 0;
}
void gotoxy(int x, int y)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = x; scrn.Y = y;
SetConsoleCursorPosition(hOuput, scrn);
}
void drawString(char ch, int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
printf("%c", ch);
}
}
void drawInterFace()
{
int i;
drawString('*', width);
printf("\n");
for (i = 0; i < hight - 2; i++)
{
drawString('*', 1);
drawString(' ', 68);
drawString('*', 1);
printf("\n");
}
drawString('*', 70);
}
void Ballgame(int* x, int* y, int xSpeed, int ySpeed)
{
*x += xSpeed;
*y += ySpeed;
if (*x > width - 2)
{
*x = width - 2;
}
if (*y > hight - 2)
{
*y = hight - 2;
}
if (*x < 1)
{
*x = 1;
}
if (*y < 1)
{
*y = 1;
}
}
void drawBall(int x, int y)
{
gotoxy(x, y); printf("O\n");
}
void clearBall(int x, int y)
{
gotoxy(x, y); printf(" \n");
}
void Deriction(int x, int y, int *xSpeed, int *ySpeed)
{
if (x >= width - 2)
*xSpeed = -*xSpeed;
if (y >= hight - 2)
*ySpeed = -*ySpeed;
}
然后vs调试有没有直接跳到x=x1的功能啊一次一次的按太麻烦了 题目来自 《hello C语言》{:9_222:} 木有一个人理我{:10_284:} fury可 发表于 2019-10-19 11:48
木有一个人理我
超过40行代码请添加注释 bin554385863 发表于 2019-10-19 11:51
超过40行代码请添加注释
额 马上加上 em。。。怎么准确的加注释我不太明白大概就这样? #include<stdio.h>
#include<windows.h>
int width = 70;
int hight = 22;
int main()
{
int x, y, x1, y1;
int xSpeed = 1, ySpeed = 1, x1Speed=1, y1Speed=1;
void gotoxy(int x, int y); //**按坐标打印
void printfBall(int a, int b);
void drawString(char ch, int n); //**打印字符及个数
void drawInterFace(); //**打印边界
void drawBall(int x, int y); //**通过坐标打印 O
void clearBall(int x, int y);//**消除 0
void Ballgame(int *x, int *y, int xSpeed, int ySpeed); //**使 0 运动
void Deriction(int x, int y, int *xSpeed, int *ySpeed); //**判断0的运动方向
x = 1;
y = 1;
x1 = 68;
y1 = 1;
drawInterFace();
while (1)
{
drawBall(x, y);
Sleep(200);
clearBall(x, y);
Ballgame(&x, &y, xSpeed, ySpeed);
Deriction(x, y, &xSpeed, &ySpeed);
drawBall(x1, y1);
Sleep(200);
clearBall(x1, y1);
Ballgame(&x1, &y1, x1Speed, y1Speed);
Deriction(x1, y1, &x1Speed, &y1Speed);
}
system("pause");
return 0;
}
void gotoxy(int x, int y)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = x; scrn.Y = y;
SetConsoleCursorPosition(hOuput, scrn);
}
void drawString(char ch, int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
printf("%c", ch);
}
}
void drawInterFace()
{
int i;
drawString('*', width);
printf("\n");
for (i = 0; i < hight - 2; i++)
{
drawString('*', 1);
drawString(' ', 68);
drawString('*', 1);
printf("\n");
}
drawString('*', 70);
}
void Ballgame(int* x, int* y, int xSpeed, int ySpeed)
{
*x += xSpeed;
*y += ySpeed;
if (*x > width - 2)
{
*x = width - 2;
}
if (*y > hight - 2)
{
*y = hight - 2;
}
if (*x < 1)
{
*x = 1;
}
if (*y < 1)
{
*y = 1;
}
}
void drawBall(int x, int y) //**通过坐标打印 O
{
gotoxy(x, y); printf("O\n");
}
void clearBall(int x, int y) //**消除 0
{
gotoxy(x, y); printf(" \n");
}
void Deriction(int x, int y, int *xSpeed, int *ySpeed) //**判断0的运动方向
{
if (x >= width - 2)
*xSpeed = -*xSpeed;
if (y >= hight - 2)
*ySpeed = -*ySpeed;
}
最后一个函数有问题
void Deriction(int x, int y, int *xSpeed, int *ySpeed) //**判断0的运动方向
{
if (x <=1 || x >= width - 2)
*xSpeed = -*xSpeed;
if (y <=1 || y >= hight - 2)
*ySpeed = -*ySpeed;
} superbe 发表于 2019-10-20 00:46
最后一个函数有问题
em 都没注意到 谢谢指出
页:
[1]