/* Made entirely in one line! Pain and suffering.*/ package; import openfl.display.Sprite; import flixel.text.FlxText; import flixel.group.FlxGroup; import flixel.FlxSprite; import flixel.FlxG; import flixel.FlxState; class OneLinePong extends Sprite {public function new(){super();addChild(new flixel.FlxGame(0, 0, OneLinePong.PlayState));}} class Paddle extends FlxSprite {public var vel:Int = 200; var cpu:Bool = false; public var cpuDumbness:Int = 0; public function new(X:Float = 0, Y:Float = 0, isCPU:Bool = false) {super(X, Y); cpu = isCPU; makeGraphic(15, 100); immovable = true; } override function update(elapsed:Float) {super.update(elapsed); if (!cpu) {velocity.y = 0; if (FlxG.keys.anyPressed([UP, W])) velocity.y = -vel; if (FlxG.keys.anyPressed([DOWN, S])) velocity.y = vel; }else if (cpu && x >= (FlxG.width/2) + cpuDumbness) {if (y < PlayState.instance.ball.y) velocity.y = vel; if (y > PlayState.instance.ball.y) velocity.y = -vel;} if (y <= 0) y = 0; if (y >= FlxG.height - height) y = FlxG.height - height;}} class Ball extends FlxSprite { public var DEFAULT_VELOCITY:Int = 200; public function new(X:Float = 0, Y:Float = 0) {super(X, Y); makeGraphic(18, 18); elasticity = 1; DEFAULT_VELOCITY = PlayState.youWon ? 200 : -200; velocity.set(DEFAULT_VELOCITY, DEFAULT_VELOCITY);} override function update(elapsed:Float) {super.update(elapsed); FlxG.collide(this, PlayState.instance.collidables, (swag, balls) -> {velocity.x += 10; velocity.y += 10; PlayState.instance.cpuPaddle.cpuDumbness += FlxG.random.int(0, 3);}); }} class PlayState extends FlxState {public static var instance:PlayState; public var collidables:FlxGroup; static var score:Array = [0, 0]; public static var youWon:Bool = false; var paddle:Paddle; public var cpuPaddle:Paddle; public var ball:Ball; var crying:FlxText; override function create() {instance = this; collidables = new FlxGroup(); add(collidables); var top:FlxSprite = new FlxSprite().makeGraphic(FlxG.width, 1); top.immovable = true; var bottom:FlxSprite = new FlxSprite(0, FlxG.height).makeGraphic(FlxG.width, 1); bottom.immovable = true; paddle = new Paddle(20); paddle.screenCenter(Y); cpuPaddle = new Paddle(FlxG.width - 40, 0, true); cpuPaddle.screenCenter(Y); ball = new Ball(); ball.screenCenter(); add(ball); collidables.add(top); collidables.add(bottom); collidables.add(paddle); collidables.add(cpuPaddle); crying = new FlxText(0, 20, 0, '${score[0]}:${score[1]}', 48); crying.screenCenter(X); add(crying); super.create(); } override function update(elapsed:Float) {super.update(elapsed);if (!ball.isOnScreen(FlxG.camera) && !(ball.y <= 0 || ball.y >= FlxG.height)){ if (ball.x <= 0) { youWon = false; score[1]++; }if (ball.x >= FlxG.width){ youWon = true; score[0]++; }FlxG.resetState();}}}