IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » ankur's snipts » pong The latest pong snipts from ankur.

showing 1-1 of 1 snipts for pong
  • a simple pong game in JavaFX
    /*
     * A Simple Pong game in JavaFX
     */
    package javafxpong;
    
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.paint.Color;
    import javafx.scene.paint.RadialGradient;
    import javafx.scene.paint.Stop;
    import javafx.scene.Scene;
    import javafx.scene.shape.Circle;
    import javafx.scene.text.Font;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    import javafx.scene.shape.Rectangle;
    
     /**
     * author Ankur Shrivastava
     * web http://ankurs.com
     * email ankur [at] ankurs [dot] com
     */
    
    var position = 0;
    var rev = 0;
    
    var t = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            KeyFrame {
                time: 40ms
                action: function() {
                    if (rev == 0) {
                        position1 = position1 + 10;
                    }
                    if (rev == 1) {
                        position1 = position1 - 10;
                    }
                    if (position1 > 860) {
                        rev =1;
                    }
                    if (position1 < 0) {
                        rev =0;
                    }
                }
            }
        ]
    };
    
    var points = 0;
    var ppp = bind points;
    var pt: Timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            KeyFrame {
                time: 100ms
                action: function() {
                    points +=1;
                    ptext.content = "Points {ppp}"
                }
            }
        ]
    }
    
    var t1: Timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            KeyFrame {
                time: 40ms
                action: function() {
                    if (position1 > recY and position > 1410)
                       {
                        if (position1 < recY + 100 and position > 1410)
                              {
                            rev1 = 1;
                            points+=20;
                        }
                    }
                    if (position1 > recY and position < 30)
                       {
                        if (position1 < recY + 100 and position < 30)
                              {
                            rev1 = 0;
                            points+=20;
                        }
                    }
                    if (rev1 == 0) {
                        position = position + 10;
                    }
                    if (rev1 == 1) {
                        position = position - 10;
                    }
                    if (position > 1440) {
                        rev1 =1;
                        lost();
                        reset();
                    }
                    if (position < 0) {
                        rev1 =0;
                        lost();
                        reset();
                    }
                }
            }
        ]
    };
    
    function lost() {
        t.stop();
        t1.stop();
        pt.stop();
        tex.content = "PRESS ANY KEY TO CONTINUE\nYour Points were {points}";
        points =0;
    }
    
    function reset() {
        position1= 450;
        position = 720
    }
    
    var tex = Text {
        x: 550
        y: 390
        font: Font { size: 20
        }
        content: "WELCOME TO PONG"
    }
    var ptext = Text {
        x: 550
        y: 50
        font: Font { size: 20
        }
        content: ""
    }
    var rec1 = Rectangle {
        x: 0,
        y: bind recY;
        width: 20,
        height: 100
        fill: Color.BLACK
    }
    var rec2 = Rectangle {
        x: 1420,
        y: bind recY
        width: 20,
        height: 100
        fill: Color.BLACK
    }
    
    var recY = 300;
    var position1 = 0;
    var rev1 = 0;
    Stage {
        title: "JavaFX Pong"
        width: 1440
        height: 250
        fullScreen: true;
        scene: Scene {
            fill: Color.TRANSPARENT;
            content: [
                Circle {
                    onMouseDragged: function( e: MouseEvent ):Void {
                        position = e.sceneX as Integer;
                        println(e.x);
                        position1 = e.sceneY as Integer;
                        println(e.y);
                    }
                    onMousePressed: function( e: MouseEvent ):Void {
                        t.stop();
                        t1.stop();
                    }
                    onMouseReleased: function( e: MouseEvent ):Void {
                        t1.play();
                        t.play();
                    }
                    onKeyPressed: function( e: KeyEvent ):Void {
                        println(e.text);
                        if (e.text == "Up") {
                            if (recY > 0) {
                                recY-=30;
                            }
                        }
    else if (e.text == "Down") {
                            if (recY < 800) {
                                recY+=30;
                            }
                        }
    else
    {
                            if (not t.running){
                                t.play();
                                t1.play();
                                pt.play();
                                tex.content = "";
                            }
                        }
                    }
                    centerX: 0,
                    centerY: 0,
                    translateX: bind position,
                    translateY: bind position1,
                    radius: 20
                    fill: RadialGradient {
                        centerX: 0
                        centerY: 0
                        focusX: 0.1
                        focusY: 0.1
                        radius: 21
                        proportional: false
                        stops: [
                            Stop {
                                color: Color.RED
                                offset: 0.0
                            },
                            Stop {
                                color: Color.BLACK
                                offset: 1.0
                            },
                        ]
                    }
                }
            ,tex,rec1,rec2,ptext]
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by ankur on Mar 01, 2009 at 11:57 a.m. EST
Sign up to create your own snipts, or login.