I would like to move the doll and it stays just inside the div, when it hits the line that delimits the square, it stops and can not go beyond.
$(document).ready(function(){
$(document).keydown(function(x){
if(x.which == 39 || x.keyCode == 39){
$('.pacman')
.animate({left: '1180px'}, 'slow')
.css({ transform : 'rotate(360deg)'});
}
if(x.which == 40 || x.keyCode == 40){
$('.pacman')
.animate({top: '1180px'}, 'slow')
.css({ transform : 'rotate(90deg)'});
}
if(x.which == 37 || x.keyCode == 37){
$('.pacman')
.animate({left: '-1180px'}, 'slow')
.css({ transform : 'rotate(180deg)'});
}
if(x.which == 38 || x.keyCode == 38){
$('.pacman')
.animate({top: '-1180px'}, 'slow')
.css({ transform : 'rotate(270deg)'});
}
}).keyup(function(){
$('.pacman').stop();
});
});
.pacman{
position:absolute;
}
.cenario{
border:5px solid black;
width:500px;
height:500px;
margin-left:400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="cenario">
<img src="pacman.png" class="pacman" />
</div>