简体   繁体   English

在allegro5(C ++)中的另一个图像上的图像

[英]Image over another image in allegro5 (C++)

I have a problem with adding a background in my game. 我在游戏中添加背景时遇到问题。 My code looks like that: 我的代码如下所示:

while (true)
{
    al_draw_bitmap(background, 0, 0, NULL);
    al_flip_display();
    some code(...);
    al_draw_bitmap(snake,0,0,NULL);
    /*drawing my snake's tail*/
    al_clear_to_color(al_map_rgb(0,0,0));
    al_draw_bitmap(apple,0,0,NULL);
    al_flip_display();
}

And I have visible an apple and a black screen most of the time. 而且大部分时间我都看到一个苹果和一个黑屏。 I've been changing order of some lines in code and none of these combination worked (best case was when I had visible snake and background but there was no apple). 我一直在更改代码中某些行的顺序,但这些组合都不起作用(最好的情况是当我看到蛇和背景但没有苹果时)。 Before adding a background, my snake's tail sometimes disappered, but it wasn't very visible, and except that, everything seemed to be okay. 在添加背景之前,我的蛇的尾巴有时会消失,但是它不是很明显,而且,一切似乎都还不错。 Anyone knows how to add a background properly? 有人知道如何正确添加背景吗? Or maybe it's fault of my computer? 还是这是我的计算机的故障?

Once per draw loop, you should: 每个绘制循环一次,您应该:

  1. clear the display 清除显示
  2. draw your background 画你的背景
  3. draw bitmaps on top of the background 在背景上方绘制位图
  4. flip the display 翻转显示

For example: 例如:

al_clear_to_color(al_map_rgb(0,0,0));
al_draw_bitmap(background, 0, 0, NULL);
al_draw_bitmap(apple,0,0,NULL);
al_draw_bitmap(snake,0,0,NULL);
al_flip_display();

Note that you should only call al_flip_display once per draw loop , after you have drawn everything for that loop. 请注意,在绘制 al_flip_display该循环的所有内容后,您只应在每个绘制循环中调用一次 al_flip_display al_clear_to_color should be called first as it will wipe out everything you have drawn. 应该先调用al_clear_to_color ,因为它将清除您绘制的所有内容。 In the example you gave, you are drawing your apple and snake to the same location, so I wouldn't be suprised if one is blocking part of the other. 在您给出的示例中,您正在将苹果和蛇绘制到相同的位置,因此如果一个对象阻塞了另一个对象,我不会感到惊讶。

Also, you probably don't want to stick your draw loop in a while(true) as the framerate will be unconstrained. 另外,您可能不想在while(true)停留绘制循环,因为帧率不受限制。 That is, it will run as fast as it is allowed, and the framerate won't be consistent. 也就是说,它将以允许的速度运行,并且帧速率将不一致。 The typical approach is to use an event-driven draw loop . 典型的方法是使用事件驱动的绘制循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM