package game; import javafx.animation.Timeline; import javafx.animation.KeyFrame; import javafx.scene.Node; import javafx.scene.CustomNode; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Circle; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.transform.Translate; import javafx.scene.paint.RadialGradient; import javafx.scene.paint.Stop; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; import java.lang.Math; import javafx.scene.input.KeyEvent; import javafx.scene.transform.Scale; import javafx.scene.transform.Translate; /** * @author Michal Skvor * @authir cb0 */ var STICK: Integer = 1; var BOUNCING: Integer = 2; var WON: Integer = 3; var state: Integer = STICK; var paddle: Paddle = Paddle { }; var ball: Ball = Ball { x: paddle.x, y: paddle.y - 5 }; var color = Color.YELLOW; var intWidth = 200; var intHeigth = 200; var winner: Timeline=Timeline { repeatCount: Timeline.INDEFINITE keyFrames: KeyFrame { time: 5s canSkip: true action: function() { intext.content=""; winner.stop(); } } } var bouncer: Timeline = Timeline { repeatCount: Timeline.INDEFINITE keyFrames: KeyFrame { time: 10ms action: function() { // Bounce from walls if( ball.x + ball.vx < ball.radius ) { ball.vx = -ball.vx; } if( ball.x + ball.vx > intHeigth - ball.radius ) { ball.vx = -ball.vx; } if( ball.y + ball.vy < ball.radius ) { ball.vy = -ball.vy; } if( ball.y + ball.vy > intWidth + ball.radius ) { state = STICK; bouncer.stop(); } // Bouncle from paddle if( ball.x + ball.vx >= paddle.x - paddle.width / 2 and ball.x + ball.vx <= paddle.x + paddle.width / 2 ) { if( ball.y + ball.vy + ball.radius >= paddle.y and ball.y + ball.vy - ball.radius <= paddle.y + paddle.height ) { ball.vy = -ball.vy; } } else if( ball.y + ball.vy >= paddle.y and ball.y + ball.vy <= paddle.y + paddle.height ) { if( ball.x + ball.vx + ball.radius >= paddle.x - paddle.width / 2 and ball.x + ball.vx - ball.radius <= paddle.x + paddle.width / 2 ) { ball.vx = -ball.vx; } } // Bounce from brick var i: Integer = 0; var count = 0; for( brick in bricks ) { if(bricks[i] == null){ count++; } if( ball.x + ball.vx >= brick.x and ball.x + ball.vx <= brick.x + brick.width ) { if( ball.y + ball.vy + ball.radius >= brick.y and ball.y + ball.vy - ball.radius <= brick.y + brick.height ) { ball.vy = -ball.vy; delete bricks[i]; } } else if( ball.y + ball.vy >= brick.y and ball.y + ball.vy <= brick.y + brick.height ) { if( ball.x + ball.vx + ball.radius >= brick.x and ball.x + ball.vx - ball.radius <= brick.x + brick.width ) { ball.vx = -ball.vx; delete bricks[i]; } } i++; } if(count == i){ i=0; count=0; for( j in [0..3] ) { for( ini in [0..6] ) { if((Math.random() * 100) <= 70){ insert Brick { x: ini * 20, y: j * 10 + 30 } into bricks; } } } stage.title="Level {level++} | by cb0"; state = WON; winner.play(); intext.content="YOU\nWIN!"; bouncer.stop(); } ball.x += ball.vx; ball.y += ball.vy; } } }; // Sequence containing Bricks var bricks: Brick[]; // Create bricks for( j in [0..3] ) { for( i in [0..8] ) { var val = Math.random() * 100; if(val mod 2 <= 50){ insert Brick { x: i * 20, y: j * 10 + 30 } into bricks; } } } var level = 0; // Visual Stage var stage = Stage { title: "Level {level} | by cb0" scene: Scene { content: bind [ Rectangle { width: intWidth, height: intHeigth fill: Color.LIGHTGREY // Handler for mouse movement onMouseMoved: function( e : MouseEvent ): Void { if( e.sceneX < paddle.width / 2 ) { paddle.x = paddle.width / 2; } else if( e.sceneX > intHeigth - paddle.width / 2 ) { paddle.x = 200 - paddle.width / 2; } else { paddle.x = e.sceneX; } if( state == STICK ) { ball.x = paddle.x; ball.y = paddle.y - ball.radius; } } // Handler for mouse press onMousePressed: function( e : MouseEvent ): Void { if( state == STICK or state == WON ) { state = BOUNCING; bouncer.play(); } } onKeyTyped: function( e: KeyEvent ):Void { if(e.char.equals("r")){ var count = 0; for( brick in bricks ) { if(brick != null){ delete bricks[count]; break; } count++; } } if(e.char.equals("s")){ var count = 0; for( brick in bricks ) { if(brick != null){ delete bricks[count]; } count++; } } } }, paddle, ball, bricks,intext ] } width: 207 height: 232 } var intext = Text { font: Font { size: 36 } textAlignment: TextAlignment.CENTER content: "" fill: Color.rgb(Math.random(), Math.random(), Math.random()) transforms: [ Translate{ x: 40 y: 100 } Scale{ x: 1 y: 1 pivotX: 0 pivotY: 5 } ] } //Text // Visual representation of Paddle class Paddle extends CustomNode { // Position of paddle public var x: Number = 85; public var y: Number = 180; // Paddle dimensions public var width: Number = 45; public var height: Number = 15; public override function create(): Node { return Rectangle { transforms: Translate { x: bind x - width / 2, y: bind y } width: bind width, height: bind height, fill: RadialGradient{ centerX: 75 centerY: 75 radius: 90 proportional: false stops: [ Stop{ offset: 0.0 color: Color.RED } Stop{ offset: 1.0 color: Color.DARKRED } ]//Stops } stroke: Color.BLUE, strokeWidth: 1 }; } } // Visua representation of Brick class Brick extends CustomNode { // Position of Brick public var x: Number; public var y: Number; // Brick dimensions public var width: Number = 20; public var height: Number = 10; public override function create(): Node { return Rectangle { transforms: Translate { x: bind x, y: bind y } width: bind width, height: bind height fill: Color.color(Math.random(), Math.random(), Math.random()) stroke: Color.WHITE, strokeWidth: 1 }; } } // Visual representation of Ball class Ball extends CustomNode { // Ball position public var x: Number = 100; public var y: Number = 100; // Ball radius public var radius: Number = 6; // Ball velocity vectors public var vx: Number = 1.0; public var vy: Number = -1.0; public override function create(): Node { return Circle { transforms: Translate { x: bind x, y: bind y } radius: bind radius fill: RadialGradient{ centerX: 0 centerY: 0 radius: 90 proportional: true stops: [ Stop{ offset: 0.0 color: Color.BISQUE } Stop{ offset: 1.0 color: Color.GREENYELLOW } ]//Stops } } } }