文章目录
C版本
1.1 显示静止的小球
使用printf函数在屏幕坐标(x, y)处显示一个静止的小球字符’O’,注意屏幕坐标系的原点在左上角,向右为X轴,向下为Y轴。
做法是先输出许多换行符,再输出一些空格,然后是小球字符。
#include <stdio.h>
int main() {
int x, y;
x = 5, y = 10;
for (int i = 0; i < y; i++) {
printf("\n");
}
for (int j = 0; j < x; j++) {
printf(" ");
}
printf("o\n");
return 0;
}
效果如图:
1.2 动态的小球(下落)
如果要实现小球的变动,就要每次输出一个小球后就清一下屏幕,然后改变小球的坐标。先从改变纵坐标y开始,定义一个速度变量velocity。
#include <stdio.h>
#include <stdlib.h>
int main() {
int x, y, velocity = 1;
x = 5, y = 0;
while (1) {
for (int i = 0; i < y; i++) {
printf("\n");
}
for (int j = 0; j < x; j++) {
printf(" ");
}
printf("O\n");
system("cls");
y += velocity;
}
return 0;
}
难以截图,不过发现了问题。小球会一直往下落。可以添加范围检查,当y大于或小于某范围值的时候,改变其方向。
#include <stdio.h>
#include <stdlib.h>
int main() {
int x, y, velocity = 1;
x = 10, y = 0;
while (1) {
for (int i = 0; i < y; i++) {
printf("\n");
}
for (int j = 0; j < x; j++)