Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My HTML file :

HTML
<html>
<head>
<title> Game </title>
<link rel= "stylesheet" type = "text/css" href = "style.css" />
 </head>
<body>
<canvas id ="maincanvas" width = "500" height = "400"> </canvas>
<canvas id ="maincanvas2" width = "500" height = "400"> </canvas>
<script src ="script.js"> </script>
</body>

</html>

My Script (javascript) :
JavaScript
var canvas = document.getElementById("maincanvas");
var context = canvas.getContext("2d");

var keys = [];

var width = 500, speed = 3, height = 400;

var player = {
          x: 10,
          y: 10, 
    width: 20,
   height: 20
};
window.addEventListener("Keydown",  function(e){
         keys[e.keyCode] = true;
}, false);

window.addEventListener("Keyup",  function(e){
         delete keys[e.keyCode];
}, false);

/*
up - 38
down - 40
left - 37
right - 39
*/

function game(){
   update();
   render();
}

function update(){
       if(keys[38]) player.y--;
       if(keys[40]) player.y++;
       if(keys[37]) player.x--;
       if(keys[39]) player.x++;
} 
function render(){
         context.clearRect(0, 0, 100, 100);
}

setInterval(function(){
           game();
}, 1000/30)

My CCS file :
CSS
#maincanvas {
     background-color: red;
}

The Problem is that, when I run this it doesn't shows why I am trying to do. I mean their is a minor, a bit problem in the code of the script, I think. I'm tired to look for it.
So please, will anyone correct it and then send me back the solution ?

Thanks.
Posted
Updated 5-Feb-15 3:11am
v4
Comments
Richard MacCutchan 5-Feb-15 10:21am    
As far as I can see your 'game' does not actually do anything.
Mudit Jhawar 6-Feb-15 7:55am    
Can I run it?
Have I done any mistake?
Do you have any suggestions? :)
Richard MacCutchan 6-Feb-15 8:49am    
I don't really know what you are trying to achieve with this, as your question is not very clear. However, given the fact that the code does not really do anything, I would suggest working through a few more Javascript tutorials.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900