clean up indentation and spacing
[supercollider.git] / examples / other / keepyuppy.scd
blob3041cc43de6fcae9c864f008b6acdf46659ec42f
1 /*
2 * KEEPY-UPPY
4 * An AUDIO-ONLY game by Dan Stowell <danstowell@users.sourceforge.net> (C) 2005
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
8   *  the Free Software Foundation; either version 2 of the License, or
9   *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
20 * This game is inspired by the work of "One Switch"
21 * http://www.oneswitch.org.uk/ - developing games and controllers
22 * to accommodate various types of disability, including...
23 * audio-only games.
25 * RULES:
26 *  You are kicking a football into the air. You can hear it fly 
27 *  up into the air and down again, and when it comes down you 
28 *  must press SPACE to kick it up again.
30 *  The ball may move slightly towards the left or right so you
31 *  may need to move left or right using the cursor keys to 
32 *  keep within range of the ball (wear headphones for best 
33 *  control!)
35 *  The aim is to kick it up as many times as you can.
41 s.boot; // First make sure the server is on
43 // Then send it the SynthDefs we need
45 SynthDef(\kuball, { |amp=0.5, pan=0, freq=100|
46   Out.ar(0, Pan2.ar(amp * 2 * (BPF.ar(WhiteNoise.ar, freq, 0.1)*10).clip2 * SinOsc.ar(4.5), pan))
47 }).send(s);
49 SynthDef(\gameover, { Out.ar(0, SinOsc.ar(
50                                 Line.kr(200, 100, 1.0, doneAction:0),
51                                 0,
52                                 EnvGen.kr(Env([0.3, 0.2, 0.2, 0], [0.1, 0.8, 0.1]), 1, doneAction:2)
53                                 ).dup(2))}).send(s);
55 SynthDef(\whackit, {Out.ar(0, SinOsc.ar(74, 0.5pi, 
56                 EnvGen.kr(Env.perc(0, 0.05), doneAction:2)).dup(2))}).send(s);
60 // Now define the game (won't run until we run the last line in this file)
62 ~game = {
64 var whacks, xpos, ypos, xvel, yvel, whack, dogravity, framedur, doc, whackdisabled, whackspacing;
67 whacks = 0;
68 xpos   = 0;
69 ypos   = 0;
70 xvel   = 0;
71 yvel   = 10;
72 framedur = 0.1; // The amount of time between two "frames" of calculation
73 whackdisabled = false;
74 whackspacing = 0.3; // The amount of time you must wait between whacks 
76 whack = {
77   if(ypos < 20,
78   {
79     if(xpos.abs < 1,
80     {
81       if((yvel < 0) && (whackdisabled==false),
82       {
83         yvel = rrand(6.0, 14.0);
84         ypos = max(0.1, ypos);
85         xvel = 0.1.bilinrand;
86         Synth(\whackit);
87         whackdisabled = true;
88         Task({whackspacing.wait; whackdisabled = false}).play;
89         whacks = whacks + 1;
90       });
91     });
92   });
93 }; // End of whack function
95 dogravity = {
96   yvel = yvel - 1.0;
97   ypos = ypos + yvel;
98   xpos = xpos + xvel;
101 Task({
102   var kuball;
103   
104   kuball = Synth(\kuball);
105   
106   // Set up keyboard handlers
107   {
108         doc = Window("Use LEFT and RIGHT arrows and SPACEBAR");
109         doc.front;
110   }.defer;
111   
112   0.1.wait;
113   doc.view.keyDownAction_({arg ...args;
114     switch(args[3],
115       32   , {whack.value}, // space
116       63234, {xpos = xpos + 0.1}, // left arrow
117       63235, {xpos = xpos - 0.1} // right arrow
118     );
119   });
121   // This is the loop which runs the game
122   while({ypos >= 0},
123   {
124     dogravity.value;
125     kuball.set(\amp, 0.5 * (1.0/max(1.0, ypos)), 
126                \pan, max(-1.0, min(1.0, xpos)), 
127                \freq, 200 + max(0, ypos * 10));
128     framedur.wait;
129   });
130   
131   // Destroy keyboard handlers
132   doc.view.keyDownAction_(nil);
133   kuball.free;
134   
135   // Run the end-of-game feedback etc
136   Synth(\gameover);
137   1.5.wait;
138   AppClock.sched(0.0, {doc.close;});
139   switch(whacks,
140      0, {("You didn't hit the ball - sorry").speak;}, 
141      1, {("You hit the ball once").speak;},
142      2, {("You hit the ball twice").speak;},
143      3, {("You hit the ball 3 times").speak;},
144      4, {("You hit the ball 4 times - well done").speak;},
145      5, {("You hit the ball 5 times - well done").speak;},
146       {("You hit the ball "+whacks+" times - well done indeed!").speak;}
147   );
149 }).play;
154 ~game.value; // Press Enter to play!