Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <canvas width="300" height="300"></canvas>
- <script>
- var canvas = document.querySelector('canvas'),
- ctx = canvas.getContext('2d');
- var Bullet = function (x, y, vx, vy) {
- this.x = x;
- this.y = y;
- this.vx = vx;
- this.vy = vy;
- };
- Bullet.prototype.tick = function (dt) {
- this.x += dt * this.vx;
- this.y += dt * this.vy;
- }
- Bullet.prototype.draw = function (ctx) {
- ctx.beginPath();
- ctx.arc(this.x, this.y, 10, 0, 2*Math.PI);
- ctx.stroke();
- }
- var bullets = [];
- bullets.push(new Bullet(0, 150, 200, 40));
- bullets.push(new Bullet(0, 150, 200, 0));
- bullets.push(new Bullet(0, 150, 200, -40));
- window.setInterval(function () {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- bullets.forEach(function (b) {
- b.tick(1 / 60);
- b.draw(ctx);
- });
- }, 1000 / 60);
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement