[Scilab-users] cancelling a loop by key pressing

P M p.muehlmann at gmail.com
Thu Jun 6 21:29:38 CEST 2019


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 ratebitRate = 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 havenTones = 1;//
get the intervall size..must be an integerintervall = 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;endfor(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;endfor(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;endfor(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 scenexRange = 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 rectanglerectScene = [xMin,yMax, rectW, rectH];
// define a point center = start positionxc = rectW / 2;yc = rectH / 2;
// plot the scenef = figure();f.background = 8;
// plot the rectangle (scene field)xrect(rectScene);
// plot the centerplot(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 = 1playGame = 1;
xDir = 0.5;   // xDir < 0 = to the left; xDir > 0 = to the rightyDir =
-1;   // yDir < 0 = falling; yDir >0 = raisingspeed = 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20190606/9e67586a/attachment.htm>


More information about the users mailing list