我正在编写一个小程序,其中包含 25 个点的数组。每个点都有自己的位置、大小和颜色,因为我使用的是图形库 (Allegro),所以我有一种方法可以将它们打印在屏幕上。
我需要同时打印它们,并使用:
for (int i = 0; i < m_size ; i++)
{
points[i].move();
points[i].draw();
}
一个接一个地打印它们。绝对更好的是:
for (int i = 0; i < m_size ; i++)
{
points[0].move();
points[0].draw();
points[1].move();
points[1].draw();
// ...
points[24].move();
points[24].draw();
}
这当然不是最佳解决方案;但效果很好。问题是,有没有办法将第二个选项减少到更少的行数?
编辑:
void Particula::move(){
// Modifies private position attributes.
m_x += rand() % 50;
m_y += rand() % 50;
}
void Particula::draw(){
// Draws the point given its private attributes.
printf("Drawing circle... \n");
// printf ("[ DEBUG:] X: %f, Y: %f, Radius: %f", m_x, m_y, m_radius);
al_draw_filled_circle(m_x, m_y, m_radius, m_color); // Draws the point.
al_flip_display(); // Updates the display.
}
预期的结果是:
像这样修改代码可以解决问题:
for(int i = 0; i < N; i++) // being N a variable number.
{
for (int h = 0; h < m_size; h++)
{
point[h].move();
point[h].draw();
}
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
al_rest(0.1);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句