Jak*_*ake 5 html javascript css jquery html5
所以我是HTML5的新手,并决定开始编写乒乓球游戏.我想用键盘顺利移动我的角色,左边的W和S键以及右边的向上和向下箭头.我似乎无法让它发挥作用.这可能很容易,但我是我的菜鸟,我需要一些帮助.提前致谢!
<!DOCTYPE html>
<html>
<head>
<title>Pingedy Pong</title>
</head>
<body style="font-family:Arial;">
<canvas id="ctx" width="699" height="400" style="background-color:black;border:2px solid black;background-image: url('background.jpg');"></canvas>
<script>
// STARTING SCRIPT
var ctx = document.getElementById("ctx").getContext("2d");
ctx.fillStyle="white";
ctx.font = '30px Arial';
// VARIABLES
var keys1 = [];
var keys2 = [];
var width = 699;
var height = 400;
var ball = {
width:20,
height:20,
spdX:2.9,
spdY:2.9,
x:340,
y:190,
};
var char1 = {
w:15,
h:90,
spdX:3,
spdY:3,
x:10,
y:155,
};
var char2 = {
w:15,
h:90,
spdX:3,
spdY:3,
x:674,
y:155,
};
// UPDATE
function updateEntity(a) {
a.x+=a.spdX;
a.y+=a.spdY;
ctx.clearRect(0,0,width,height);
ctx.fillRect(a.x,a.y,a.height,a.width);
if(a.x > width-a.width || a.x < 0) {
a.spdX = -a.spdX;
};
if(a.y > height-a.height || a.y < 0) {
a.spdY = -a.spdY;
};
};
function renderChar(b) {
ctx.fillRect(b.x,b.y,b.w,b.h);
};
function checkInput() {
document.addEventListener('onkeydown', function(e) {
if(e.keyCode == 37) {
char1.y += 1;
}
else if(e.keyCode == 39) {
char1.y -= 1;
}
});
};
function checkWinP1() {
if (ball.x < 0.33) {
console.log("P1 Wins");
};
};
function checkWinP2() {
if (ball.x > 679) {
console.log("P2 Wins");
};
};
function update() {
updateEntity(ball);
renderChar(char1);
renderChar(char2);
checkInput();
checkWinP1();
checkWinP2();
};
//TICKS
setInterval(update,1000/120);
</script>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
更新 1:使用事件keydown代替keypress。
此更新的原因是箭头键不会触发事件keypress(或者它们会触发事件,但事件不会返回有关按下哪个键的信息)。当您想了解更多信息时请阅读本文。
更新2
我现在意识到你问题中的“顺利”关键字。如果短时间间隔调用该update函数对您来说还不够好,那么requestAnimationFrame() API就是您的最佳选择。
当你编写游戏时,我建议你使用 jQuery。
试试这个代码:
var characterSpeed = 15;
$(document).on("keydown", function (e) {
switch (e.which)
{
case 87: //w
char1.y -= characterSpeed;
break;
case 83: //s
char1.y += characterSpeed;
break;
case 38: //up arrow
char2.y -= characterSpeed;
break;
case 40: //down arrow
char2.y += characterSpeed;
break;
default:
alert("You have pressed the key " + e.which);
}
});
Run Code Online (Sandbox Code Playgroud)
这里有片段:
var characterSpeed = 15;
$(document).on("keydown", function (e) {
switch (e.which)
{
case 87: //w
char1.y -= characterSpeed;
break;
case 83: //s
char1.y += characterSpeed;
break;
case 38: //up arrow
char2.y -= characterSpeed;
break;
case 40: //down arrow
char2.y += characterSpeed;
break;
default:
alert("You have pressed the key " + e.which);
}
});
Run Code Online (Sandbox Code Playgroud)
// STARTING SCRIPT
var ctx = document.getElementById("ctx").getContext("2d");
ctx.fillStyle="white";
ctx.font = '30px Arial';
// VARIABLES
var keys1 = [];
var keys2 = [];
var width = 699;
var height = 400;
var ball = {
width:20,
height:20,
spdX:2.9,
spdY:2.9,
x:340,
y:190,
};
var char1 = {
w:15,
h:90,
spdX:3,
spdY:3,
x:10,
y:155,
};
var char2 = {
w:15,
h:90,
spdX:3,
spdY:3,
x:674,
y:155,
};
// UPDATE
function updateEntity(a) {
a.x+=a.spdX;
a.y+=a.spdY;
ctx.clearRect(0,0,width,height);
ctx.fillRect(a.x,a.y,a.height,a.width);
if(a.x > width-a.width || a.x < 0) {
a.spdX = -a.spdX;
};
if(a.y > height-a.height || a.y < 0) {
a.spdY = -a.spdY;
};
};
function renderChar(b) {
ctx.fillRect(b.x,b.y,b.w,b.h);
};
function checkInput() {
document.addEventListener('onkeydown', function(e) {
if(e.keyCode == 37) {
char1.y += 1;
}
else if(e.keyCode == 39) {
char1.y -= 1;
}
});
};
function checkWinP1() {
if (ball.x < 0.33) {
console.log("P1 Wins");
};
};
function checkWinP2() {
if (ball.x > 679) {
console.log("P2 Wins");
};
};
function update() {
updateEntity(ball);
renderChar(char1);
renderChar(char2);
checkInput();
checkWinP1();
checkWinP2();
};
//TICKS
setInterval(update,1000/120);
//NEW CODE TO MANAGE THE CHARACTERS MOTION
var characterSpeed = 15;
$(document).on("keydown", function (e) {
switch (e.which)
{
case 87: //w
char1.y -= characterSpeed;
break;
case 83: //s
char1.y += characterSpeed;
break;
case 38: //up arrow
char2.y -= characterSpeed;
break;
case 40: //down arrow
char2.y += characterSpeed;
break;
default:
alert("You have pressed the key " + e.which);
}
});Run Code Online (Sandbox Code Playgroud)
另外,您可能想阅读这两篇文章:
您将了解如何将多个处理程序绑定到同一事件、IE 和其他浏览器之间的区别、功能String.fromCharCode,以及(如果您有兴趣)如何使用普通 javascript 编写相同的代码。
希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
1140 次 |
| 最近记录: |