Multiplayer Java Script game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
3.2 KiB

<html>
<head>
<title>Block Battle Beta</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style type="text/css">
canvas{
border: 1px solid black;
}
.row {
margin: 0; padding: 0;
}
</style>
</head>
<body>
<div class="row">
<div class="col-md-6">
<canvas width="700" height="700" ID="mycanvas" tabindex="0" >Canvas tag not supported</canvas>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
$(document).ready(function()
{
var socket = io('http://localhost:3000');
io.connect();
socket.emit('register', "Jrtechs");
var canvas = document.getElementById("mycanvas");
canvas.addEventListener('keydown', doKeyDown, true);
canvas.addEventListener('keyup', doKeyUp, true);
var context = canvas.getContext("2d");
var width = 700;
var height = 700;
var players = {};
var blocks = new Array();
var name = "Jrtechs";
socket.on("connected", function(data)
{
console.log("got message connected");
console.log(data);
players = data.players;
blocks = data.blocks;
});
socket.on("updateClient",function(data)
{
console.log(data);
players[data.name] = data.player;
});
function doKeyUp(e)
{
socket.emit("keyDown",{"direction":e.keyCode});
}
function doKeyDown(e)
{
console.log("key down");
socket.emit("keyUp",{"direction":e.keyCode});
}
function draw()
{
context.fillStyle = "#000000";
context.fillRect(0, 0, width, height);
console.log(blocks);
for(var i = 0; i< blocks.length; i++)
{
if(blocks[i] != -1)
{
context.fillStyle = "rgba(255, 0, 199, 1)"; //pink
context.fillRect(blocks[i].x, blocks[i].y, 10,10);
}
}
Object.keys(players).forEach(function(key)
{
context.fillStyle = "rgba(0, 45, 160, 1)"; //blue
context.fillRect(players[key].x, players[key].y, 25,25);
});
}
var speed = 5;
setInterval(function ()
{
Object.keys(players).forEach(function(key)
{
console.log(key);
if(players[key].upB)
{
players[key].y -= speed;
}
else if(players[key].downB)
{
players[key].y += speed;
}
else if(players[key].leftB)
{
players[key].x -= speed;
}
else if(players[key].rightB)
{
players[key].x += speed;
}
});
draw();
}, 1000/60);
});
</script>
</body>
</html>