[Scilab-users] cancelling a loop by key pressing

Stéphane Mottelet stephane.mottelet at utc.fr
Fri Jun 7 09:25:19 CEST 2019


To stop animation when window is closed, don't use event_handler with 
ibut=-1000. Here is a simpler solution:

h  =  gcf();
while  %t
     if  is_handle_valid(h)
         xtitle(msprintf("k = %d",k))
     else
         break
     end
     k=k+1;
end

S.

Le 06/06/2019 à 21:29, P M a écrit :
> Dear all,
>
> out of curiosity I made a small animation ... see code below.
>
> Note:
> The part of the sound section is from a example where I generate 
> different tones within one sound file.
> Hence the intervalls....
>
> To the topic:
>
> The animation runs in a while loop.
> It is possible to stop the loop, by just closing the figure that 
> displays the animation.
> However this is kind of crashing the program.
>
> Question:
> Is it possible to have a function within the loop to check if a 
> specific key is pressed?
> And if this key is pressed, the control variable of the loop changes 
> and the loop stops?
>
> Thank you,
> Philipp
>
> Here the code (Scilab 6.0.2)
>
> clc;
> clear();
> xdel();
>
> // define the sound
> //set bit rate
> bitRate  =  22050;
> // At first we create 0.05 seconds of sound parameters.
> t=soundsec(0.05,  bitRate);
> [nr,nc]=size(t);
> // decide how many tones we have
> nTones  =  1;
> // get the intervall size..must be an integer
> intervall  =  nc  /  nTones;
> // Then we generate the sound.
> for(i  =  1:nTones)
>      si  =  sin(2*%pi*(i*700)*t((i-1)*intervall+1  :  i*intervall));   
>      s1((i-1)*intervall+1  :  i*intervall)  =  si;
> end
> for(i  =  1:nTones)
>      si  =  0.5+sin(2*%pi*(i*800)*t((i-1)*intervall+1  :  i*intervall));   
>      s2((i-1)*intervall+1  :  i*intervall)  =  si;
> end
> for(i  =  1:nTones)
>      si  =  0.5+sin(2*%pi*(i*400)*t((i-1)*intervall+1  :  i*intervall));   
>      s3((i-1)*intervall+1  :  i*intervall)  =  si;
> end
> for(i  =  1:nTones)
>      si  =  0.5+sin(2*%pi*(i*600)*t((i-1)*intervall+1  :  i*intervall));   
>      s4((i-1)*intervall+1  :  i*intervall)  =  si;
> end
>
> // create the scene
> xRange  =  linspace(0,99,100);
> yRange  =  linspace(0,49,100);
> xMin  =  min(xRange);
> xMax  =  max(xRange);
> yMin  =  min(yRange);
> yMax  =  max(yRange);
> rectW  =  xMax  -  xMin;
> rectH  =  yMax  -  yMin;  
>
> //define the scene rectangle
> rectScene  =  [xMin,yMax,  rectW,  rectH];
>
> // define a point center = start position
> xc  =  rectW  /  2;
> yc  =  rectH  /  2;
>
> // plot the scene
> f  =  figure();
> f.background  =  8;
>
> // plot the rectangle (scene field)
> xrect(rectScene);
>
> // plot the center
> plot(xc,yc,'o');
> a  =  gca();
> e  =  gce();
>
> a.axes_visible  =  ["off","off","off"];
> a.data_bounds  =  [xMin,yMin;xMax,yMax];
> a.margins  =  [0.01,  0.01,  0.01,  0.01];
> a.tight_limits  =  ["on","on","off"];
>
> // animate the point as long as playGame = 1
> playGame  =  1;
>
> xDir  =  0.5;    // xDir <0 = to the left; xDir >0 = to the right
> yDir  =  -1;    // yDir <0 = falling; yDir >0 = raising
> speed  =  1;
>
> while  playGame  ==  1
>      
>      if(xDir  <  0)
>          xc  =  xc-speed;
>      else
>          xc  =  xc+speed;
>      end
>      
>      if(yDir  <  0)
>          yc  =  yc  -  speed;
>      else
>          yc  =  yc  +  speed;
>      end
>      
>      e.children.data  =  [xc,  yc];
>      
>      // this is what happens when we reach left border
>      if(xc<=xMin+1)
>          playsnd(s1);
>          xDir  =  1;
>      end
>      // this is what happens when we reach right border
>      if(xc>=xMax-1)
>          playsnd(s2);
>          xDir  =  -1;
>      end
>          // this is what happens when we reach lower border
>      if(yc<=yMin+1)
>          playsnd(s3);
>          yDir  =  1;
>      end
>      // this is what happens when we reach upper border
>      if(yc>=yMax-1)
>          playsnd(s4);
>          yDir  =  -1;
>      end
>
>      sleep(1);
>      
>      // implement a event handle function ?
>      // to check if (and if yes which) key has been pressed
>      // eg.: if return value of event handle function = 0
>      // -->set playGame = 0;
>      // -->thus stops the while - loop
>      
> end
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users

-- 
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20190607/473f65ed/attachment.htm>


More information about the users mailing list