Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function(){
- "use strict";
- var Game = {
- canvas : undefined,
- c : undefined,
- spriteHeight: 340,
- spriteWidth: 454,
- backgroundColor: 'black',
- foregroundColor: 'white',
- xPos : 0,
- yPos : 0,
- sprite : undefined,
- xSpeed : 1,
- ySpeed : 1
- };
- Game.start = function(){
- Game.canvas = document.getElementById('myCanvas');
- Game.c = Game.canvas.getContext("2d");
- Game.canvas.width = 1000;
- Game.canvas.height = 600;
- Game.sprite = new Image(Game.spriteWidth, Game.spriteHeight);
- Game.sprite.src = "sprite.png";
- setTimeout(Game.mainLoop,1000);
- };
- Game.update = function(){
- switch(Game.xPos){
- case (Game.canvas.width - Game.spriteWidth):
- Game.xSpeed = -2;
- break;
- case 0:
- Game.xSpeed = 2;
- }
- switch(Game.yPos){
- case (Game.canvas.height - Game.spriteHeight):
- Game.ySpeed = -2;
- break;
- case 0:
- Game.ySpeed = 2;
- }
- Game.xPos = Game.xPos + Game.xSpeed;
- Game.yPos = Game.yPos + Game.ySpeed;
- console.log(Game.yPos,Game.ySpeed);
- };
- Game.draw = function(){
- Game.clearCanvas();
- Game.c.fillStyle = Game.backgroundColor;
- Game.c.fillRect(0,0,Game.canvas.width, Game.canvas.height);
- Game.c.fillStyle = Game.foregroundColor;
- // Game.c.fillRect(Game.xPos,Game.yPos,Game.spriteWidth, Game.spriteHeight);
- Game.c.drawImage(Game.sprite,Game.xPos,Game.yPos,Game.spriteWidth,Game.spriteHeight);
- };
- Game.mainLoop = function(){
- Game.update();
- Game.draw();
- setTimeout(Game.mainLoop, 1000/30);
- }
- Game.clearCanvas = function(){
- Game.c.clearRect(0,0,Game.canvas.width,Game.canvas.height);
- }
- Game.start();
- });
Add Comment
Please, Sign In to add comment