弹跳小球

来源:互联网 发布:网络主播怎么招聘 编辑:程序博客网 时间:2024/05/17 02:15

jump_ball_1

# include <stdio.h>

# include <stdlib.h>
# include <conio.h>
# include <windows.h>
/*
弹跳小球:
画布 20*10
小球位置 10*5
画布 left top right bottom
速度 velocity_x velocity_y
*/
int main()
{
int ball_x = 0, ball_y = 5;
int left = 0, top = 0, right = 20, bottom = 10; //画布
int position_x = bottom / 2, position_y = right / 2; //小球的位置
int velocity_x = 1, velocity_y = 1;//速度

int bottom_black, right_black; //小球上面的空行和左面的空格

while (1)
{
ball_x = ball_x + velocity_x;
ball_y = ball_y + velocity_y;
system("cls");
for (bottom_black = 0; bottom_black <ball_x; bottom_black++)
printf("\n");
for (right_black = 0; right_black < ball_y; right_black++)
printf(" ");
printf("o");
/* 不能这样写的原因是因为小球的位置是不断变化的 
//画出小球
for (bottom_black = 0; bottom_black <ball_x ; bottom_black++)
{

for (right_black = 0; right_black < ball_y; right_black++)
{
if ((bottom_black == position_x) && (right_black == position_y))
printf("o");
else printf(" ");
}
printf("\n");
}*/

Sleep(50);


if ((ball_x == top) || (ball_x == bottom))
velocity_x = -velocity_x;
if ((ball_y == left) || (ball_y == right))
velocity_y = -velocity_y;
}
_getch();
return 0;

}


此程序来自学习知乎童星老师的作品,本人由于在一个不太好的学校,所以只能靠自学来度日。

0 0
原创粉丝点击