#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的功能啊 一次一次的按太麻烦了