彈跳小球

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;

}


此程序來自學習知乎童星老師的作品,本人由於在一個不太好的學校,所以只能靠自學來度日。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章