gpt4 book ai didi

javascript - 如何使乒乓球运动

转载 作者:行者123 更新时间:2023-11-28 07:01:46 25 4
gpt4 key购买 nike

这是我的 html:

<html lang="en-US">
<head>
<meta charset ="UTF-8"/>
<title>Pong</title>
<link rel="stylesheet" type="text/css" href="pong.css"/>
</head>
<body>
<canvas id="mainCanvas" width="700" height="710"></canvas>
<script type="text/javascript" src="pong.js"></script>
</body>
</html>

这是我的CSS:

#mainCanvas{
width: 700px;
height: 710px;
background-color: black;
}

这是我的js:

//variables

var canvas = document.getElementById('mainCanvas');
var ctx = canvas.getContext('2d');

var keys = [];

var speed = 12,
playerWidth = 8,
playerHeight = 75,
canvasW = 700,
canvasH = 710,
player1X = canvasW - 670,
player2X = canvasW - 30,
ballS = 15,
running = true,
acc = 0,
ranNum = Math.floor(Math.random() * 10 + 1);

var requestAnimFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;

//objects

function player(x,y,width,height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

function gameObj(x,y,vel,side,speed){
this.x = x;
this.y = y;
this.side = side;
this.speed = speed;
}

var player1 = new player(player1X,canvasH/2-playerHeight/2,playerWidth,playerHeight);
var player2 = new player(player2X,canvasH/2-playerHeight/2,playerWidth,playerHeight);

var ball = new gameObj(canvasW/2-ballS/2,canvasH/2-ballS/2,ballS,15);

//Events

window.addEventListener("keydown", function(e){
keys[e.keyCode] = true;
}, false);
window.addEventListener("keyup", function(e){
delete keys[e.keyCode];
}, false);

/*
keys
up-38
down-40

*/

//functions

function game(){
update();
render();
}

function update(){
if(keys[38])player1.y -=speed;
if(keys[40])player1.y +=speed;
if(keys[87])player2.y -=speed;
if(keys[83])player2.y +=speed;

if(player1.y <0) player1.y=0;
if(player1.y >= canvasH - player1.height) player1.y = canvasH - player1.height;

if(player2.y <0) player2.y=0;
if(player2.y >= canvasH - player2.height) player2.y = canvasH - player2.height;
console.log("player1.y: " + player1.y);
console.log("player2.y: " + player2.y);
}
function render(){
ctx.clearRect(0,0,canvasW,canvasH);
ctx.fillStyle="white";
ctx.fillRect(player1.x, player1.y, player1.width, player1.height);
ctx.fillStyle="white";

ctx.fillRect(player2.x, player2.y, player2.width, player2.height);
ctx.fillStyle="white";

ctx.fillRect(canvasW/2-3, 0, 3, canvasH);
ctx.fillStyle="white";
ctx.fillStyle="red";
ctx.fillRect(ball.x,ball.y,ball.side,ball.side);
}

function animate() {
if (running) {
game();
}
requestAnimFrame(animate);
}
animate();

我在打乒乓球时遇到了一些问题。我没有太多经验。我知道你可以做 ball.x += ball.speed 但我不确定如何让它在墙壁和 Racket 上弹跳。我试图做到这一点,所以当球与 Racket 碰撞时,它等于它的 x 和 y 值,但随后球会跟随 Racket 。当我试图通过使速度与碰撞速度相反来改变方向时,球在碰撞时只是保持静止。请帮忙。

欢迎任何帮助。

如果我的代码有点马虎,我很抱歉,我没有接受过正规培训。

最佳答案

这是一个基本的乒乓 Action 。希望它能给你一些帮助。如果你想让别人更正你的代码,你可以把它放在 Code Review 上?这一运动的一个关键部分是拥有比人们最初想象的更多的变量。例如。这段代码是否需要 box1Left1 box1Left2

var box1Left1 = 0; 
var box1Left2;

setInterval(box1Fly, 10);
function box1Fly() {
// Fly Right
if ( box1Left1 < 300 ) {
box1Left1++;
document.getElementById("box1").style.left = box1Left1 + "px";
box1Left2 = box1Left1;
}
// Fly Left
if ( box1Left1 >= (300) ) {
box1Left2--;
document.getElementById("box1").style.left = box1Left2 + "px";
}
// Fly Right Again
if( box1Left2 == 0 ) { box1Left1 = box1Left2; }

}
<div id="box1" style="position:absolute; top: 10px; left: 0px; width: 50px; height: 50px; background-color: #aa39fc;"></div>

关于javascript - 如何使乒乓球运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com