CSS页面代码:
*{
margin: 0;
padding: 0;
}
.games{
width: 1500px;
height: 800px;
margin: 50px auto;
background-color: #ccc;
position: relative;
overflow: hidden;
}
.role{
width: 100px;
height: 200px;
position: absolute;
bottom: 0;
}
.role img{
width: 100%;
height: 100%;
}
.barrier{
width: 100px;
height: 100px;
position: absolute;
left: 1300px;
bottom: 0;
}
.barrier img{
width: 100%;
height: 100%;
}
@keyframes move {
0%{
left: 1500px;
}
100%{
left: -100px;
}
}
.move{
animation: move 2s linear infinite;
}
.animate-pause{
animation-play-state: paused;
}
.btn-mask{
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .8);
position: absolute;
left: 0;
top: 0;
z-index: 9;
}
.btn-mask .start,.games .pause{
width: 100px;
height: 100px;
font-size: 18px;
color: #fff;
text-align: center;
line-height: 100px;
background-color:rgb(0, 102, 255);
position: absolute;
border-radius: 50%;
cursor: pointer;
}
.btn-mask .start{
transform: translate(-50%,-50%);
position: absolute;
left: 50%;
top: 50%;
}
.games .pause{
right: 40px;
top: 40px;
}
HTML页面代码:
<!-- 游戏界面 -->
<div class="games">
<!-- 游戏角色 -->
<div class="role">
<img src="https://img1.baidu.com/it/u=4071404386,3974115926&fm=253&fmt=auto&app=138&f=PNG?w=268&h=331" alt="">
</div>
<!-- 障碍物 -->
<div class="barrier">
<img src="https://img0.baidu.com/it/u=3949025014,4235362944&fm=253&fmt=auto&app=138&f=PNG?w=256&h=256" alt="">
</div>
</div>
JS页面详细代码:
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(function(){
let _role=$('.role'),
_barrier=$('.barrier'),
_games=$('.games'),
_games_h=_games.height(),
_role_h=_role.height(),
_diff_h=_games_h-_role_h;
_timer=null;
_games.append($('<div class="btn-mask"><div class="start jq-start">开始游戏</div></div>'));
$('.jq-start').click(function(){
startGames();
});
// 判断游戏是否结束
function isOver(){
// 判断角色是否与障碍物碰撞
let _role_bottom=_diff_h-_role.position().top;
let _barrier_left=_barrier.position().left;
if(_role_bottom<40 &&_barrier_left>-40 &&_barrier_left<40){
restartGames();
}
}
// 重新开始
function restartGames(){
clearInterval(_timer);
$('.pause').remove();
$(window).off('keydown');
_barrier.removeClass('move');
$('.jq-start').html('重新开始');
$('.btn-mask').show();
}
// 继续游戏
function continueGames(){
_timer=setInterval(isOver,10);
$(window).on('keydown',jump);
_barrier.removeClass('animate-pause');
$('.jq-pause').html('暂停游戏');
$('.jq-pause').click(function(){
pasuseGames();
});
}
// 暂停游戏
function pasuseGames(){
clearInterval(_timer);
$(window).off('keydown');
_barrier.addClass('animate-pause');
$('.jq-pause').html('继续游戏');
$('.jq-pause').click(function(){
continueGames();
})
}
// 角色跳跃
function jump(event){
if(event.keyCode==32){
_role.animate({
bottom:'300px'
},'fast','swing').delay(150).animate({
bottom:0
},'fast','swing');
}
}
// 开始游戏
function startGames(){
$('.btn-mask').hide();
_games.append($('<div class="pause jq-pause">暂停游戏</div>'));
$('.jq-pause').click(function(){
pasuseGames();
});
_barrier.addClass('move');
_timer=setInterval(isOver,10);
$(window).on('keydown',jump);
}
})
</script>
实现效果如图: