Animation XPCE/prolog中的动画不够平滑

Animation XPCE/prolog中的动画不够平滑,animation,prolog,swi-prolog,xpce,Animation,Prolog,Swi Prolog,Xpce,我试图为一个项目用XPCE/Prolog编写一个简单的动画,但不够流畅;它似乎在闪烁或闪烁。 我在一台装有英特尔i7(四核,2.4 Ghz+)的华硕N550jv笔记本电脑上运行了这个程序,所以这应该不会是个问题。 移动的对象总是相同的(只是在不同的位置绘制),双缓冲是显式启用的,我使用40 FPS 如何提高平滑度 以下是我正在使用的代码: sm:- %run this sm(600,4,40). %runs animation over 600 pixels, 4 seconds l

我试图为一个项目用XPCE/Prolog编写一个简单的动画,但不够流畅;它似乎在闪烁或闪烁。 我在一台装有英特尔i7(四核,2.4 Ghz+)的华硕N550jv笔记本电脑上运行了这个程序,所以这应该不会是个问题。 移动的对象总是相同的(只是在不同的位置绘制),双缓冲是显式启用的,我使用40 FPS

如何提高平滑度

以下是我正在使用的代码:

sm:-   %run this
    sm(600,4,40). %runs animation over 600 pixels, 4 seconds long, with 40 FPS

sm(Length,Duration,FPS):-
    (object( @window),!,free(@window);true), %check is window exists; if it does, delete it 
    (object( @floor), !, free( @floor); true), %check is floor exists; if it does, delete it 
    (object( @box), !, free( @box); true), %%check if box exists; if it does, delete it
    new( @window,picture('Window',size(900,300))), %create window
    send( @window,open), %display window
    send(@window,buffered_update,true), %make sure to use double-buffering
    new( @floor,box(800,10)), %create floor
    send( @floor,fill_pattern,colour(green)), %colour floor
    send( @window,display,@floor,point(50,70)),  %show floor
    new( @box,box(50,50)), %creates box
    send( @box,fill_pattern,colour(yellow)), %colours box yellow
    Frames is Duration*FPS, %how many frames are to be drawn
    Step is Length/Frames, %what's the distance between steps
    Delay is 1/FPS, %what is the delay between steps
    send( @window,display,@box,point(50,20)), %shows Object
    send( @window,flush), %flushes window
    ani(Step,Delay,Frames,point(50,20)), %actual animation
    sleep(1), %wait 1 second, and then
    free( @box), %delete box
    free( @floor), %delete floor
    free( @window). %delete window (and exit)


ani(_,_,0,_).
ani(Step,Delay,Frames,point(X,Y)):-
    send( @box,x(X+Step)), %moves box to the right
    send( @window,flush), %flushes
    repeat,
    sleep(Delay),
    FramesLeft is Frames - 1,
    NewX is X + Step,
    ani(Step,Delay,FramesLeft,point(NewX,Y)).

您应该使用计时器,它可以避免睡眠:

sm:-   %run this
    sm(600,4,40). %runs animation over 600 pixels, 4 seconds long, with 40 FPS

sm(Length,Duration,FPS):-
    (object( @window),!,free(@window);true), %check is window exists; if it does, delete it
    (object( @floor), !, free( @floor); true), %check is floor exists; if it does, delete it
    (object( @box), !, free( @box); true), %%check if box exists; if it does, delete it
    (object( @my_timer), !, free( @my_timer); true), %%check if box exists; if it does, del
    new( @window,picture('Window',size(900,300))), %create window
    send( @window,open), %display window
     send(@window,buffered_update,true), %make sure to use double-buffering
    new( @floor,box(800,10)), %create floor
    send( @floor,fill_pattern,colour(green)), %colour floor
    send( @window,display,@floor,point(50,70)),  %show floor
    new( @box,box(50,50)), %creates box
    send( @box,fill_pattern,colour(yellow)), %colours box yellow
    Frames is Duration*FPS, %how many frames are to be drawn
    Step is Length/Frames, %what's the distance between steps
    Delay is 1/FPS, %what is the delay between steps
    send( @window,display,@box,point(50,20)), %shows Object
    send( @window,flush), %flushes window
    ani(Step,Delay,Frames,point(50,20)).

ani(Step,Delay,Frames,point(X,Y)) :-
    send( @box,x(X+Step)), %moves box to the right
    send( @window,flush), %flushes
    new(@my_timer, my_timer(Step, Delay, Frames, X)).

:- pce_begin_class(my_timer, object).
variable(mytimer,   timer,  both, "timer lançant l'animation fréquence 20 fois par seconde").
variable(step, number, both, "delta x").
variable(frames, number, both, "number of moves").
variable(posX, number, both, "xpos of the box").

% initialisation of the tmer
initialise(P, Step, Delay, Frames, PosX) :->
    send(P, slot, step, Step),
    send(P, slot, frames, Frames),
    send(P, slot, posX, PosX),
    send(P, mytimer, new(_, timer(Delay,message(P, my_message)))),
    send(P?mytimer, start).

% must be called before destruction
% avoid any problem of non-freed resources
unlink(F) :->
    send(F?mytimer, stop),
    send(F, send_super, unlink).


my_message(P) :->
    get(P, slot, frames, Frame),
    (   get(Frame, value, 0)
    ->  send(P?mytimer, stop),
        free( @box), %delete box
        free( @floor), %delete floor
        free( @window), %delete window (and exit)
        free(@my_timer)
    ;   get(P, slot, step, Step),
        get(P, slot, posX, PosX),
        send( @box,x(PosX+Step)), %moves box to the right
        send( @window,flush), %flushes
        send(PosX, plus, Step),
        send(Frame, minus, 1)).
:- pce_end_class.

对不起,我对Prolog一无所知,但是当我试图编译它时,swipl找不到
timer
类。你知道我在哪里能找到它吗?谢谢我使用Windows7 64位,我的swi prolog版本是6.5.3。我不能说任何关于Linux版本的事情。当我在Linux上使用swiprolog时,我编译了源代码,它可以工作。哦,对不起,它现在也可以为我工作了。我用的是旧的游泳池。谢谢感谢你的提示,4秒动画的实际动画时间从4.73秒缩短到4.06秒,但我不认为动画在视觉上更流畅,这就是我要问的。阅读我答案的第一行,我只是建议使用定时器而不是“睡眠”来制作动画,仅此而已。也许你可以用定时器的持续时间(延迟)来提高它。