代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html>
<head>
<title>游戏角色操控</title>
<meta charset="utf-8">
<style>
#player {
position: absolute;
left: 0;
top: 0;
}
body {
background-color: black;
}
</style>
</head>
<body>
<img id="player" src="player.gif">
<script src="jQuery.js"></script>
<script>
$(function() {
var $player = $('#player');
var isBig = false; // 是否变身
var timer = null; // 按键长按计时器
$(document).on('keydown', function(event) {
var left = parseInt($player.css('left')),
top = parseInt($player.css('top'));
switch (event.keyCode) {
case 37: // 左键
clearInterval(timer);
$player.attr('src', isBig ? 'playerbig_left.gif' : 'player_left.gif');
$player.css('left', left - 10 + 'px');
break;
case 38: // 上键
clearInterval(timer);
$player.attr('src', isBig ? 'playerbig.gif' : 'player.gif');
$player.css('top', top - 10 + 'px');
break;
case 39: // 右键
clearInterval(timer);
$player.attr('src', isBig ? 'playerbig_right.gif' : 'player_right.gif');
$player.css('left', left + 10 + 'px');
break;
case 40: // 下键
clearInterval(timer);
$player.attr('src', isBig ? 'playerbig.gif' : 'player.gif');
$player.css('top', top + 10 + 'px');
break;
}
// 监听按键长按事件
$(document).on('keyup', function(event) {
clearInterval(timer);
}).on('keydown', function(event) {
var direction = event.keyCode;
clearInterval(timer);
timer = setInterval(function() {
switch (direction) {
case 37: // 左键
$player.attr('src', isBig ? 'playerbig_left.gif' : 'player_left.gif');
$player.css('left', left - 5 + 'px');
break;
case 38: // 上键
$player.attr('src', isBig ? 'playerbig.gif' : 'player.gif');
$player.css('top', top - 5 + 'px');
break;
case 39: // 右键
$player.attr('src', isBig ? 'playerbig_right.gif' : 'player_right.gif');
$player.css('left', left + 5 + 'px');
break;
case 40: // 下键
$player.attr('src', isBig ? 'playerbig.gif' : 'player.gif');
$player.css('top', top + 5 + 'px');
break;
}
}, 50);
});
});
$(document).on('dblclick', '#player', function(event) {
isBig = !isBig;
$player.attr('src', isBig ? 'playerbig.gif' : 'player.gif');
});
$(document).on('click', function(event) {
var x = event.pageX,
y = event.pageY,
left = parseInt($player.css('left')),
top = parseInt($player.css('top'));
if (x < left) {
$player.attr('src', isBig ? 'playerbig_left.gif' : 'player_left.gif');
$player.animate({
left: x + 'px'
});
} else {
$player.attr('src', isBig ? 'playerbig_right.gif' : 'player_right.gif');
$player.animate({
left: x + 'px'
});
}
$player.animate({
top: y + 'px'
});
});
});
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。