Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.34 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Race</title>
  6.     <style type="text/css">
  7.         * {
  8.             margin: 0;
  9.             padding: 0;
  10.         }
  11.         #canvas {
  12.             width: 854px;
  13.             height: 480px;
  14.             margin: 0 auto;
  15.         }
  16.         #images {
  17.             display: none;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.     <div id="images">
  23.         <img id="image_lisa" width="32" height="32" src="lisa.png">
  24.         <img id="image_meggie" width="32" height="32" src="meggie.png">
  25.     </div>
  26.     <canvas id="canvas" width="854" height="480"></canvas>
  27.  
  28.     <script type="text/javascript">
  29.         var Game = function() {
  30.             var canvas = document.getElementById('canvas');
  31.             this.ctx = canvas.getContext('2d');
  32.             this.height = parseInt(canvas.attributes.height.value);
  33.             this.width = parseInt(canvas.attributes.width.value);
  34.  
  35.             this.mouse = {};
  36.             canvas.addEventListener('mousemove', this.mouseMove.bind(this));
  37.  
  38.             window.graphics = new Graphics(this.ctx, this.width, this.height);
  39.  
  40.             this.objects = [];
  41.             this.objects.push(new Player(0, 'image_lisa'));
  42.             this.objects.push(new Player(1, 'image_meggie'));
  43.  
  44.             this.update();
  45.         };
  46.  
  47.         Game.prototype.update = function() {
  48.             for(var i = 0; i < this.objects.length; i++) {
  49.                 this.objects[i].update();
  50.             }
  51.  
  52.             this.render();
  53.  
  54.             setTimeout(this.update.bind(this), 16);
  55.         };
  56.  
  57.         Game.prototype.render = function() {
  58.             graphics.clearScreen();
  59.  
  60.             for(var i = 0; i < this.objects.length; i++) {
  61.                 this.objects[i].render();
  62.             }
  63.         };
  64.  
  65.         Game.prototype.mouseMove = function(e) {
  66.         };
  67.  
  68.         var Graphics = function(ctx, width, height) {
  69.             this.ctx = ctx;
  70.             this.width = width;
  71.             this.height = height;
  72.             this.images = {};
  73.         };
  74.  
  75.         Graphics.prototype.clearScreen = function() {
  76.             this.ctx.clearRect(0, 0, this.width, this.height);
  77.             this.drawRect(0, 0, this.width, this.height);
  78.         };
  79.  
  80.         Graphics.prototype.drawRect = function(x, y, w, h, c, a) {
  81.             if(typeof c === 'undefined') c = '#000';
  82.             if(typeof a === 'undefined') a = 1;
  83.  
  84.             this.ctx.fillStyle = c;
  85.             this.ctx.globalAlpha = a;
  86.             this.ctx.fillRect(x, y, w, h);
  87.         };
  88.  
  89.         Graphics.prototype.drawOutline = function(x, y, w, h, c, a) {
  90.             if(typeof c === 'undefined') c = '#000';
  91.             if(typeof a === 'undefined') a = 1;
  92.  
  93.             this.ctx.strokeStyle = c;
  94.             this.ctx.globalAlpha = a;
  95.             this.ctx.strokeRect(x, y, w, h);
  96.         };
  97.  
  98.         Graphics.prototype.drawImage = function(imageId, x, y) {
  99.             this.ctx.drawImage(this.getImage(imageId), x, y);
  100.         };
  101.  
  102.         Graphics.prototype.getImage = function(imageId) {
  103.             if(typeof this.images[imageId] === 'undefined') {
  104.                 var image = document.getElementById(imageId);
  105.                 if(image === null) {
  106.                     console.error('Image with id ' + imageId + ' was not found');
  107.                     return undefined;
  108.                 }
  109.                 this.images[imageId] = image;
  110.             }
  111.             return this.images[imageId];
  112.         };
  113.  
  114.         var Player = function(row, imageId) {
  115.             var image = graphics.getImage(imageId);
  116.             this.imageId = imageId;
  117.             this.x = 0;
  118.             this.y = row * image.height;
  119.         };
  120.  
  121.         Player.prototype.update = function() {
  122.         };
  123.  
  124.         Player.prototype.render = function() {
  125.             graphics.drawImage(this.imageId, this.x, this.y);
  126.         };
  127.  
  128.         var Dice = function() {
  129.             this.x = 64;
  130.             this.y = 256;
  131.         }
  132.  
  133.         Dice.prototype.update = function() {
  134.         };
  135.  
  136.         Dice.prototype.render = function() {
  137.         };
  138.  
  139.         window.onload = function() {
  140.             window.game = new Game();
  141.         }
  142.     </script>
  143. </body>
  144. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement