Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. //Hey Reddit. Tried playing around with this a bit. Resolution and framerate increased
  2. //as well as bar size. For some reason fullscreen makes 90% of the particles bounce
  3. //through the bar. *shrug* ... Fixed. Thickening the bar helped the collision detection
  4.  
  5. package
  6.  
  7. {
  8.  
  9. import flash.display.Bitmap;
  10.  
  11. import flash.display.BitmapData;
  12.  
  13. import flash.display.Sprite;
  14.  
  15. import flash.events.Event;
  16.  
  17. import flash.events.MouseEvent;
  18.  
  19. import flash.geom.ColorTransform;
  20.  
  21. import flash.geom.Matrix;
  22.  
  23. import flash.geom.Rectangle;
  24.  
  25. import flash.text.TextField;
  26.  
  27. import flash.text.TextFieldAutoSize;
  28.  
  29. import net.hires.debug.Stats;
  30.  
  31.  
  32.  
  33. [SWF(width = "1000", height = "1000", frameRate = "144")]
  34.  
  35. public class BlockBreaker extends Sprite
  36.  
  37. {
  38.  
  39. private static const HEIGHT:Number = 1000;
  40.  
  41. private static const WIDTH:Number = 1000;
  42.  
  43. private var _canvas:BitmapData;
  44.  
  45. private var _blocks:Blocks;
  46.  
  47. private var _fallBlocks:Vector.<Particle>;
  48.  
  49. private var _balls:Vector.<Particle>;
  50.  
  51. private var _bar:Bitmap;
  52.  
  53.  
  54.  
  55. public function BlockBreaker()
  56.  
  57. {
  58.  
  59. _canvas = new BitmapData(WIDTH, HEIGHT,false,0x000000);
  60.  
  61. addChild(new Bitmap(_canvas));
  62.  
  63.  
  64.  
  65. _blocks = new Blocks(WIDTH, 200);
  66.  
  67.  
  68.  
  69. _fallBlocks = new Vector.<Particle>();
  70.  
  71.  
  72.  
  73. var b:BitmapData = new BitmapData(500, 20, false, 0x00FF00);
  74.  
  75. addChild(_bar = new Bitmap(b));
  76.  
  77. _bar.y = WIDTH -90;
  78.  
  79. var _ball:Particle = new Particle(WIDTH / 2, HEIGHT / 2);
  80.  
  81. _ball.vx = Math.random() *10;
  82.  
  83. _ball.vy = -Math.random() *9 -1;
  84.  
  85. _ball.color = 0xFFFFFF;
  86.  
  87.  
  88.  
  89. _balls = new Vector.<Particle>();
  90.  
  91. _balls.push(_ball);
  92.  
  93.  
  94.  
  95. //var stats:Stats = new Stats();
  96.  
  97. //stats.y = 200;
  98.  
  99. //addChild(stats);
  100.  
  101.  
  102.  
  103. addEventListener(Event.ENTER_FRAME, update);
  104.  
  105. }
  106.  
  107. private function update(e:Event):void
  108.  
  109. {
  110.  
  111. _canvas.lock();
  112.  
  113. _canvas.colorTransform(_canvas.rect, new ColorTransform (0.9, 0.5, 0.9));
  114.  
  115.  
  116.  
  117. for each(var block:Particle in _blocks.values)
  118.  
  119. {
  120.  
  121. if (block)
  122.  
  123. {
  124.  
  125. _canvas.setPixel(block.x, block.y, block.color);
  126.  
  127. }
  128.  
  129. }
  130.  
  131. var removeBalls:Vector.<Particle> = new Vector.<Particle>();
  132.  
  133. for each(var ball:Particle in _balls)
  134.  
  135. {
  136.  
  137. var bvx:Number = ball.vx;
  138.  
  139. var bvy:Number = ball.vy;
  140.  
  141. var bspeed:Number = Math.sqrt(bvx * bvx + bvy * bvy);
  142.  
  143. var bradius:Number = Math.atan2(bvy, bvx);
  144.  
  145. for (var i:int = 0; i < bspeed;i++)
  146.  
  147. {
  148.  
  149. ball.x += ball.vx/bspeed;
  150.  
  151. ball.y += ball.vy/bspeed;
  152.  
  153. var hitParticle:Particle = _blocks.getParticle(ball.x, ball.y);
  154.  
  155. if(hitParticle)
  156.  
  157. {
  158.  
  159. var removedP:Particle = _blocks.removeParticle(ball.x, ball.y);
  160.  
  161. removedP.vx = Math.cos(bradius+Math.PI*2/(30*Math.random())-15)*3;
  162.  
  163. removedP.vy = 1;
  164.  
  165. removedP.color = hitParticle.color;
  166.  
  167. _fallBlocks.push(removedP);
  168.  
  169. ball.vy = -ball.vy;
  170.  
  171. }
  172.  
  173.  
  174.  
  175. if ((ball.x < 0 && ball.vx < 0) || (ball.x > WIDTH && ball.vx > 0))
  176.  
  177. {
  178.  
  179. ball.vx = -ball.vx;
  180.  
  181. }
  182.  
  183. if (ball.y < 0 && ball.vy < 0)
  184.  
  185. {
  186.  
  187. ball.vy = -ball.vy;
  188.  
  189. }
  190.  
  191. if (ball.y > HEIGHT)
  192.  
  193. {
  194.  
  195. removeBalls.push(ball);
  196.  
  197. }
  198.  
  199. if (_bar.hitTestPoint(ball.x, ball.y))
  200.  
  201. {
  202.  
  203. ball.vy = -Math.abs(ball.vy);
  204.  
  205. }
  206.  
  207. _canvas.setPixel(ball.x, ball.y, ball.color);
  208.  
  209. }
  210.  
  211. }
  212.  
  213. removeBalls.forEach(function(b:Particle, ...args):void {
  214.  
  215. var index:int = _balls.indexOf(b);
  216.  
  217. if (index != -1)
  218.  
  219. {
  220.  
  221. _balls.splice(index, 1);
  222.  
  223. }
  224.  
  225. });
  226.  
  227.  
  228.  
  229. var removeFallBs:Vector.<Particle> = new Vector.<Particle>();
  230.  
  231. _fallBlocks.forEach(function(fallP:Particle, ...args):void {
  232.  
  233. fallP.vy += 0.1;
  234.  
  235. fallP.x += fallP.vx;
  236.  
  237. fallP.y += fallP.vy;
  238.  
  239. _canvas.setPixel(fallP.x, fallP.y, fallP.color);
  240.  
  241. if (_bar.hitTestPoint(fallP.x,fallP.y))
  242.  
  243. {
  244.  
  245. var newball:Particle = new Particle(fallP.x,fallP.y);
  246.  
  247. newball.vx = Math.random() * 10;
  248.  
  249. newball.vy = Math.random() * 9 + 1;
  250.  
  251. newball.color = fallP.color;
  252.  
  253. _balls.push(newball);
  254.  
  255. removeFallBs.push(fallP);
  256.  
  257. }else if (fallP.y > HEIGHT)
  258.  
  259. {
  260.  
  261. removeFallBs.push(fallP);
  262.  
  263. }
  264.  
  265. });
  266.  
  267.  
  268.  
  269. removeFallBs.forEach(function(b:Particle,...args):void{
  270.  
  271. var index:int = _fallBlocks.indexOf(b);
  272.  
  273. if (index != -1)
  274.  
  275. {
  276.  
  277. _fallBlocks.splice(index, 1);
  278.  
  279. }
  280.  
  281. });
  282.  
  283. _bar.x = stage.mouseX;
  284.  
  285. _canvas.unlock();
  286.  
  287.  
  288.  
  289. if (_blocks.count == 0)
  290.  
  291. {
  292.  
  293. removeEventListener(Event.ENTER_FRAME, update);
  294.  
  295. var clearTF:TextField = new TextField();
  296.  
  297. clearTF.text = "CLEAR!\nใŠใ‚ใงใจ";
  298.  
  299. clearTF.textColor = 0xFFFFFF;
  300.  
  301. clearTF.autoSize = TextFieldAutoSize.LEFT;
  302.  
  303. _canvas.draw(clearTF,new Matrix(5,0,0,5,WIDTH/2-clearTF.width*5/2,HEIGHT/2-clearTF.height*5/2));
  304.  
  305. }
  306.  
  307.  
  308.  
  309. }
  310.  
  311. }
  312.  
  313. }
  314.  
  315. import frocessing.color.ColorHSV;
  316.  
  317. class Blocks
  318.  
  319. {
  320.  
  321. public function get count():int { return _count;}
  322.  
  323. private var _count:int;
  324.  
  325. public function get width():Number { return _width; }
  326.  
  327. private var _width:Number;
  328.  
  329. public function get height():Number { return _height; }
  330.  
  331. private var _height:Number;
  332.  
  333. public var values:Vector.<Particle>;
  334.  
  335. public function Blocks(width:Number,height:Number)
  336. {
  337.  
  338. _width = width;
  339.  
  340. _height = height;
  341.  
  342. _count = width * height;
  343.  
  344. values = new Vector.<Particle>(width * height, false);
  345.  
  346. var c:ColorHSV = new ColorHSV();
  347.  
  348. for (var i:int = 0; i < _width; i++)
  349.  
  350. {
  351.  
  352. c.h = 360 * i / _width;
  353.  
  354. for (var j:int = 0 ; j < _height ; j++ )
  355.  
  356. {
  357.  
  358. var p:Particle = new Particle(i, j);
  359.  
  360. p.color = c.value;
  361.  
  362. values[i + j * _width] = p;
  363.  
  364. }
  365.  
  366. }
  367.  
  368. }
  369.  
  370. public function getParticle(x:int, y:int):Particle
  371.  
  372. {
  373.  
  374. var index:int = x + y * _width;
  375.  
  376. if (index >= values.length || index < 0)
  377.  
  378. {
  379.  
  380. return null;
  381.  
  382. }
  383.  
  384. return values[x + y * _width];
  385.  
  386. }
  387.  
  388. public function removeParticle(x:int, y:int):Particle
  389.  
  390. {
  391.  
  392. var p:Particle = values[x + y * _width];
  393.  
  394. if (p)
  395.  
  396. {
  397.  
  398. _count--;
  399.  
  400. values[x + y * _width] = undefined;
  401.  
  402. }
  403.  
  404. return p;
  405.  
  406. }
  407.  
  408. }
  409.  
  410. class Particle
  411.  
  412. {
  413.  
  414. public var x:Number;
  415.  
  416. public var y:Number;
  417.  
  418. public var vx:Number = 0;
  419.  
  420. public var vy:Number = 0;
  421.  
  422. public var color:uint;
  423.  
  424. public function Particle(x:Number=0,y:Number=0 )
  425.  
  426. {
  427.  
  428. this.x = x;
  429.  
  430. this.y = y;
  431.  
  432. }
  433.  
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement