本程式出處: John Hansen的 Robotics in C
一個有趣的彈跳線程式,跟彈跳球不一樣的地方是這邊是用線在彈跳且用了矩陣,歡迎大家指教~~~
也歡迎大家直接下載到NXT來看看唷! 宗翰老師的彈跳球1.1 , 彈跳球2.0
===================================
#define SCREEN_WIDTH 100
#define SCREEN_HEIGHT 64
#define TAIL_LENGTH 20
int xDir = 1;
int yDir = 1;
int X[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};
int Y[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};
// move the elements in the tail
void moveTail()
{
for (int i = TAIL_LENGTH – 1; i > 0; i–)
{
X[i] = X[i-1];
Y[i] = Y[i-1];
}
}
//move the X[0] and Y[0] to a new position, keeping them on the screen
void move()
{
X[0] += xDir;
Y[0] += yDir;
if( (X[0] < 2 ) || ( X[0] >= SCREEN_WIDTH-1) )
xDir *= -1;
if( (Y[0] < 2 ) || ( Y[0] >= SCREEN_HEIGHT-1) )
yDir *= -1;
}
task main()
{
while (true)
{
//move each element in the array out one slot so that the value in slot 0 goes to slot 1,etc.
moveTail();
//move the 0 slot in the array to its new position
move();
ClearScreen();
//draw the array in reverse order
for ( int i = TAIL_LENGTH – 1; i > 0; i–)
PointOut( X[i], Y[i]);
}
}