<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<title></title>
</head>
<body>
<div id="field">
<div id="ball"></div>
<div id="platform"></div>
</div>
<p>For start click ball</p>
</body>
</html>
#field {
background: lightskyblue;
height: 400px;
position: relative;
margin: 0 auto;
width: 840px;
}
#ball {
background: black;
border-radius: 50%;
height: 20px;
left: 0px;
position: absolute;
width: 20px;
}
#platform {
background: black;
border-radius: 10px;
bottom: 10px;
position: absolute;
height: 10px;
width: 100px;
}
.bricks {
background: linear-gradient(lightslategray, lavender);
border-radius: 3px;
display: inline-block;
height: 20px;
margin: 1px;
width: 40px;
}
/**
*
* User: KuzyoYaroslav
* Date: 09.07.13
* Time: 13:47
* To change this template use File | Settings | File Templates.
*/
$(document).ready(function() {
// 1) Нужно создать все обекты с их свойствами которые есть в игре
//Переменные с DOM елементами
var $field = $("#field"),
$platform = $("#platform"),
$ball = $("#ball"),
$bricks = [];
// Обект поле
var field = {
width: $field.width(),// ширина поля
t: $field.offset().top,//верхняя границы поля
l: $field.offset().left,//левая границы поля
r: $field.offset().left + $field.width(),//правая границы поля
b: $field.offset().top + $field.height()//нижняя границы поля
};
//Обект платформа (которая будет отбивать шарик)
var platform = {
width: $platform.width(),
t: 360, // высота до поверхности платформы
l: 0 // X позиция платформы
}
//Обект мяч
var ball = {
width: 20,
height: 20,
t: $ball.offset().top - field.t,
l: $ball.offset().left - field.l,
ballCondact: function() {
function moveDr() {
var timer = setInterval(function() {
$ball.css({top: (ball.t ++) + "px",
left: (ball.l ++) + "px"})
if (ball.t == platform.t && ((ball.l >= platform.l)&&(ball.l <= platform.l + platform.width))) {
clearInterval(timer);
moveUr();
} else if ( ball.t == field.b - ball.height ) {
clearInterval(timer);
moveUr();
} else if (ball.l > field.width - ball.width) {
clearInterval(timer);
moveDl();
}
},1);
}
moveDr();
function moveUr() {
var timer = setInterval(function() {
$ball.css({top: (ball.t --) + "px",
left: (ball.l ++) + "px"})
if (ball.t == field.t) {
clearInterval(timer);
moveDr();
} else if (ball.l > field.width - ball.width) {
clearInterval(timer);
moveUl();
}
},1);
}
function moveDl() {
var timer = setInterval(function() {
$ball.css({top: (ball.t ++) + "px",
left: (ball.l --) + "px"})
if (ball.t == platform.t && ((ball.l >= platform.l)&&(ball.l <= platform.l + platform.width))) {
clearInterval(timer);
moveUl();
} else if (ball.t == field.b - ball.height) {
clearInterval(timer);
moveUl();
} else if (ball.l <= 0) {
clearInterval(timer);
moveDr();
}
},1);
}
function moveUl() {
var timer = setInterval(function() {
$ball.css({top: (ball.t --) + "px",
left: (ball.l --) + "px"})
if (ball.t == field.t) {
clearInterval(timer);
moveDl();
}else if (ball.l <= 0) {
clearInterval(timer);
moveUr();
}
},1);
}
}
}
//$bricks.offset().top;
//console.log($bricks.offset());
// Обект методы которого отвечают за движения в игре
var game = {
// При наведении на документ иницыализируем функцию которая будет рассчитывать
// координаты миши и двигать платформу
initPlatform: function() {
$(document).mousemove( function(e) {
//координаты платформы относительно поля
var mousePos = e.pageX - field.l;
//Условие которое расчитывает чтобы платформа не уезжала за пределы поля и присва��вает
// position: left; относительно движению миши
if ( mousePos <= field.width - platform.width && mousePos > 0 ) {
$platform.css({left:mousePos + "px"});
platform.l = mousePos; // позиция платформы относительно миши + ширина плытформы
}
})
},
initBall: function() {
$ball.click(ball.ballCondact);
},
initBricks: function() {
function addBricks(numLine){
var top=0, left=0;
var bricksWidth = 42, bricksHeight = 22;
var maxWidth= $field.width()-bricksWidth;
var numBricks = Math.floor($field.width()/bricksWidth);
for(var i = 1; i<= numLine*numBricks; i++){
var b = document.createElement('div');
b.className = "bricks";
if(left > maxWidth){
top +=bricksHeight;
left = 0;
}
b.style.left = left +"px";
b.style.top = top +"px";
left += bricksWidth;
$field.append(b);
$bricks.push(b);
}
}
addBricks(5);
},
bricksCollision: function() {
for (var i = 0; i < $bricks.length; i++) {
if ($ball.t == $bricks[i].offset().top) {
$bricks[i].splice(i, 1);
}
}
}
};
game.initPlatform();
game.initBall();
game.initBricks();
game.bricksCollision();
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |