Running a for and while loop simultaneously

1 view (last 30 days)
Amber
Amber on 23 Apr 2012
Hi all,
I need to make one graphical experiment using Psychtoolbox.
The goal is to create some kind of game in which (a) "apples" (green ovals) will fall down from the top of the computer screen (starting at random positions). As if it would be "raining" ovals. I have the script ready for this. Also, (b) a bar will be present on the screen which a participant can move using the keyboard (key L for moving the bar to the right and key A for moving the bar to the left). I have the script ready for this.
The goal is that participants can "catch" the apples with the bars. This means that both my scripts need to be combined into one. I want participants to be able to move the bar while the apples are falling, but that the speed of the bar and the speed of the apples are independent of each other. Both scripts are working, but I have no idea how to combine them together so that the game will work as I want. For now I have tried different structures, but it will results in events like ' one apple falls > a bar can be moved (after the apple has dissappeared) > when i hit Escape another apple falls, etc.. Obviously, this is wrong. See below the (overview and structure of) two seperate working scripts.
SCRIPT A: FALLING APPLES
for a = 1: nApples
set a randomposition for each apple to start from
for i = 1:100
Use Screen function to draw an oval on the screen, which position will be updated (= a bit lower) on each loop. Every apple has 100 positions before it hits the bottom of the screen and dissapears.
end
end
SCRIPT B: MOVING THE BAR
while KbCheck end
while 1 [KeyIsDown, seconds, keyCode] = kbCheck;
if KeyIsDown
Record the KeyCode and update the horizontal position of the bar
end
while KbCheck; end
Draw the bar on the screen using the Screen function.
end
end
Is there anyone who could help me get on the right track?
Thanks, Amber

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!