Advertisement
Le_BuG63

Untitled

Aug 18th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. library ball;
  2.  
  3. import 'dart:async';
  4. import 'dart:html';
  5. import 'dart:math';
  6.  
  7. void main() {
  8.   CanvasElement canvas = querySelector("#area");
  9.   scheduleMicrotask(new Game(canvas).start);
  10. }
  11.  
  12. class Ball {
  13.   num   x;
  14.   num    y;
  15.   num    radius;
  16.   String color;
  17.  
  18.   Ball(this.x, this.y, this.radius, this.color) {
  19.     this.X = x;
  20.     this.Y = y;
  21.     this.Color = color;
  22.     this.Radius = radius;
  23.   }
  24.  
  25.   void draw(CanvasRenderingContext2D context) {
  26.     context..lineWidth = 0.5
  27.            ..fillStyle = color
  28.            ..strokeStyle = color;
  29.  
  30.     context
  31.         ..beginPath()
  32.         ..arc(x, y, radius, 0, PI * 2, false)
  33.         ..fill()
  34.         ..closePath();
  35.   }
  36.  
  37.   num get X => this.x;
  38.       set X(x) => this.x = x;
  39.  
  40.   num get Y => this.y;
  41.       set Y(y) => this.y = y;
  42.  
  43.   num get Radius => radius;
  44.       set Radius(r) => radius = r;
  45.  
  46.   String get Color => this.color;
  47.          set Color(c) => this.color = c;
  48. }
  49.  
  50. class Game {
  51.   CanvasElement canvas;
  52.   final List<Ball> balls = <Ball>[];
  53.  
  54.   var rng = new Random();
  55.  
  56.   num width;
  57.   num height;
  58.  
  59.   Game(this.canvas);
  60.  
  61.   start() {
  62.     Rectangle rect = canvas.parent.client;
  63.  
  64.     width = rect.width;
  65.     height = rect.height;
  66.  
  67.     canvas.width = width;
  68.     canvas.height = height;
  69.  
  70.     for (var i = 0; i < 100; ++i) {
  71.       final Ball ball = new Ball(rng.nextInt(500) + 100, rng.nextInt(500) + 100, rng.nextInt(50), '#' + rng.nextInt(1000).toString());
  72.  
  73.       balls.add(ball);
  74.     }
  75.    
  76.     requestDraw();
  77.   }
  78.  
  79.   void drawBackground(CanvasRenderingContext2D context) {
  80.     context.clearRect(0, 0, width, height);
  81.   }
  82.  
  83.   void drawBalls(CanvasRenderingContext2D context) {
  84.     for (var i in balls) {
  85.       i.draw(context);
  86.     }
  87.   }
  88.  
  89.   void draw(num _) {
  90.     var context = canvas.context2D;
  91.  
  92.     drawBackground(context);
  93.     drawBalls(context);
  94.  
  95.     requestDraw();
  96.   }
  97.  
  98.   void requestDraw() {
  99.     window.requestAnimationFrame(draw);
  100.   }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement