I am running into some troubles with my simple Brick Breaker game. I made a simple animation for my ball object but I have a hard time with it detecting when it comes into contact with other elements on the page. Not to mention, I can't seem to keep my player tile from moving outside the container div I made for it. I simply want the ball to bounce off the player paddle, wall and bricks.
$(function(){
$(document).keydown(function(e){
var Playerposition = $("#player").position();
var Ballposition = $("#ball").position();
switch (e.keyCode){
case 37: //Left Movement
$("#player").css('left', Playerposition.left - 20 + 'px');
break;
case 39://Right movement
$("#player").css('left', Playerposition.left + 20 + 'px');
break;
}
});
var top = 275;
var left = 325;
var interval = 5; // time interval in milliseconds
var increment = 1;
var move = function() {
if(left > 750) {
increment = -increment;
}
if(left < 0) {
increment = -increment
}
if(top > 325){
increment = -increment;
}
if(top < 0) {
increment = -increment;
}
top = top + increment;
left = left + increment;
$('#ball').css('left', left + 'px');
$('#ball').css('top', top + 'px');
};
setInterval(move, interval)});
I do have gameQuery installed, as well as Jquery. This is my game so far with my current html/css/javascript: jfiddle Sorry it does match up well, it looks fine with a full webpage.