[graphics] add pong demo 85/21085/5
Stéphane MOTTELET [Fri, 6 Sep 2019 08:45:49 +0000 (10:45 +0200)]
Change-Id: I81680c56c1c7d8ab696b855f24456ed8a345beb7

scilab/modules/graphics/demos/anim/anim.dem.gateway.sce
scilab/modules/graphics/demos/anim/pong/pong.sce [new file with mode: 0644]
scilab/modules/graphics/demos/anim/pong/pong_gateway.sce [new file with mode: 0644]

index 3b5f203..14bffe1 100644 (file)
@@ -15,7 +15,8 @@ subdemolist=[ ..
 "N link pendulum movement"           ,"anim6/anim6_gateway.sce"; ..
 "Shell"                              ,"anim7/anim7_gateway.sce"; ..
 "Riemann surface"                    ,"anim8/anim8_gateway.sce"; ..
-"Bubble Sort"                        ,"bubbleSort/bubbleSort_gateway.sce"];
+"Bubble Sort"                        ,"bubbleSort/bubbleSort_gateway.sce"; ..
+"Pong"                               ,"pong/pong_gateway.sce"];
 
 subdemolist(:,2) = demopath + subdemolist(:,2);
 clear demopath;
\ No newline at end of file
diff --git a/scilab/modules/graphics/demos/anim/pong/pong.sce b/scilab/modules/graphics/demos/anim/pong/pong.sce
new file mode 100644 (file)
index 0000000..94a72ec
--- /dev/null
@@ -0,0 +1,88 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2019 - Stéphane Mottelet
+//
+// This file is hereby licensed under the terms of the GNU GPL v2.0,
+// For more information, see the COPYING file which you should have received
+// along with this program.
+//
+// A brickout game without bricks
+// Idea from Philipp Muehlmann
+// Code by Stéphane Mottelet
+//
+
+function pong()
+
+    jimport java.awt.MouseInfo;
+
+    clf
+    f = gcf();
+    f.resize = "off";
+    f.toolbar_visible=%f;
+
+    plot(0,0,1,1)
+    isoview
+    //paddle
+    wpx = 0.2;
+    wph = 0.05;
+    xrect(0, wph, wpx, wph)
+    hp = gce()
+
+    //ball
+    xrect(0.5, 0.5, wph, wph)
+    hb = gce()
+    VMAX = 1e-3;
+    vb = [VMAX VMAX];
+
+    k=2;
+    score=0;
+
+    messagebox(msprintf("Click OK to release ball %d",3-k),"","","OK","modal")
+
+    while %t
+        M = MouseInfo.getPointerInfo();
+        X=double(M.getLocation().x)-gcf().figure_position(1);
+
+        MOUSE_X = xchange(X,0,"i2f")
+        if is_handle_valid(f)
+            paddle_x = hp.data(1) + 1e-1*(MOUSE_X-wpx/2-hp.data(1));
+            paddle_x = min(max(0,paddle_x),1-wpx);
+            ball = hb.data(1:2) + vb;
+        else
+            break;
+        end
+        if ball(1) > 1-wph || ball(1) < 0
+            vb(1) = -vb(1)
+        end
+        if ball(2) > 1
+            vb(2) = -vb(2)
+            score = score+1;
+            VMAX = VMAX*1.1;
+            xtitle(msprintf("Score = %d, balls left = %d", score, k))
+        end
+        if ball(2) < 2*wph
+            if ball(2) < 0
+                k = k-1;
+                if k < 0
+                    break
+                end
+                hb.data(1:2) = [.5,.5];
+                VMAX=1e-3;
+                vb=[VMAX VMAX];
+                messagebox(msprintf("Click OK to release ball %d",3-k),"","","OK","modal")
+            elseif ball(1) + wph >  paddle_x && ball(1) < paddle_x+wpx
+                d = (ball(1)+wph/2) - (paddle_x+wpx/2);
+                vb= VMAX*[max(-1,min(1,d/(wpx/2-wph/2))),1];
+            end
+        end
+        if is_handle_valid(f)
+            hp.data(1) = paddle_x;
+            hb.data(1:2) = hb.data(1:2) + vb;
+        end
+        sleep(1)
+    end
+    messagebox("GAME OVER !","","","OK","modal")
+end
+
+pong();
+clear pong;
+
diff --git a/scilab/modules/graphics/demos/anim/pong/pong_gateway.sce b/scilab/modules/graphics/demos/anim/pong/pong_gateway.sce
new file mode 100644 (file)
index 0000000..22c36ef
--- /dev/null
@@ -0,0 +1,8 @@
+//
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) INRIA
+//
+// This file is distributed under the same license as the Scilab package.
+//
+
+demo_run("SCI/modules/graphics/demos/anim/pong/pong.sce");
\ No newline at end of file