mraps98

JS_OOPGame

May 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function(){
  2.     var Game = {
  3.         canvas : undefined,
  4.         c : undefined,
  5.         spriteHeight: 20,
  6.         spriteWidth: 20,
  7.         backgroundColor: 'black',
  8.         foregroundColor: 'white',
  9.         xPos : 0,
  10.         yPos : 0
  11.     };
  12.  
  13.     Game.start = function(){
  14.         Game.canvas = document.getElementById('myCanvas');
  15.         Game.c = Game.canvas.getContext("2d");
  16.         Game.canvas.width = window.innerWidth;
  17.         Game.canvas.height = window.innerHeight;
  18.         Game.mainLoop();
  19.     };
  20.  
  21.     Game.update = function(){
  22.         if (Game.xPos == Game.canvas.width){
  23.             Game.xPos = 0;
  24.         }else{
  25.             Game.xPos++;
  26.         }
  27.         if (Game.yPos == Game.canvas.height){
  28.             Game.yPos = 0;
  29.         }else{
  30.             Game.yPos++;
  31.         }
  32.     };
  33.  
  34.     Game.draw = function(){
  35.         Game.c.fillStyle = Game.backgroundColor;
  36.         Game.c.fillRect(0,0,Game.canvas.width, Game.canvas.height);
  37.         Game.c.fillStyle = Game.foregroundColor;
  38.         Game.c.fillRect(Game.xPos,Game.yPos,Game.spriteWidth, Game.spriteHeight);
  39.     };
  40.  
  41.     Game.mainLoop = function(){
  42.         Game.update();
  43.         Game.draw();
  44.         setTimeout(Game.mainLoop, 1000/60);
  45.     }
  46.  
  47.     Game.start();
  48. });
Add Comment
Please, Sign In to add comment