#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int width = 800;
int height = 600;
int x = 400;
int y = 300;
int radius = 50;
initgraph(width, height);
for循环适用于循环次数,边界固定的情况,反之可用while循环
for (y = 300; y < height; y = y + 10) {
cleardevice();
fillcircle(x, y, radius);
Sleep(100);
}
return 0;
}
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int width = 800;
//画面宽度
int height = 600;
//画面高度
int x = 400;
//小球x坐标
int y = 300;
int vx = 2;
//小球x方向速度
int vy = 3;
int radius = 20;
//小球半径
initgraph(width, height);
//新开一个画面
while (1) {
//根据速度,更新小球的位置
x += vx;
y += vy;
//小球碰到边界,对应速度反向
if (x <= radius || x >= width - radius)
vx = -vx;
if (y < radius|| y>height-radius)
vy = -vy;
//清屏
cleardevice();
fillcircle(x, y, radius);//画小球
Sleep(5);//暂停毫秒
}
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int width = 800;
int height = 600;
int x = 400;
int y = 300;
int vx = 3;
int vy = 3;
int radius = 20;
initgraph(width, height);
while (1) {
//根据速度,更新小球的位置
x = x + vx;
y = y + vy;
//小球碰到左右边界,x速度反向
if (x <= radius)
vx = -vx;
if (x >= width - radius)
vx = -vx;
//小球碰到上下边界,y速度反向
if (y <= radius)
vy = -vy;
if (y >= height - radius)
vy = -vy;
//左右弹跳
cleardevice();//清屏
fillcircle(x, y, radius);
//画小球
Sleep(10);
}
return 0;
}