gpt4 book ai didi

javascript - 在桨击中更改 HTML Canvas Pong 动画中的圆圈颜色

转载 作者:行者123 更新时间:2023-11-30 12:00:13 24 4
gpt4 key购买 nike

我正在使用一个简单的 Canvas 布局,并试图弄清楚如何修改 pong 脚本,以便它每次击中球时将球的颜色更改为红色,每次未命中时将颜色更改为蓝色。

Canvas 设置与此网页相同:

Informit Canvas for Gaming

我正在使用 Context.strokeStyle 来更改颜色,但它在我放置它的上下文中不起作用。

这是我的代码:

HTML:

<HTML>
<BODY>
<canvas id="myCanvas" width="600" height="600" style="border:1px solid #000000;" />
<script src="JQuery.js"></script>
<script src="scripts.js"></script>
</BODY>

Canvas 上的 pong 元素的 Javascript:

    var Main = {};                                      // scope a (global) main object

Main.Canvas = document.getElementById('myCanvas'); // 600 x 600 canvas (per HTML)
Main.Context = Main.Canvas.getContext('2d');
Main.MX = 0; // keep track of X mouse position
Main.MY = 0;

Main.CX = 395;
Main.CY = 150;
Main.CRAD = 20;

Main.XINC = 1;
Main.YINC = 1;
Main.OFFSET = 1;

Main.HITS = 0;
Main.MISSES = 0;

// keep track of mouse movements
Main.Canvas.onmousemove = function(event)
{
if (event.offsetX)
{
mouseX = event.offsetX;
mouseY = event.offsetY;
}
else if (event.layerX)
{
mouseX = event.layerX;
mouseY = event.layerY;
}

Main.MX = mouseX;
Main.MY = mouseY;
}

Main.Animate = function()
{
Main.Context.clearRect(0, 0, Main.Canvas.width, Main.Canvas.height); // clear entire canvas
// upper left X & Y coordinates, width & height

// Draw Rectangle
Main.Context.fillStyle = "#FF0000"; // color red
Main.Context.fillRect(0, Main.MY, 25, 50); // position and size (follow mouse)

// Draw Circle
Main.Context.beginPath(); // start the circle

// When ball crosses the paddle width,
// check to see if paddle intersects path
if ( (Main.CX-Main.CRAD == 25) && (Main.XINC == -1) ) {

// if ball hits paddle, change increment (both X & Y) and change color of circle
if ( (Main.CY>Main.MY) && (Main.CY<(Main.MY+50)) ){

Main.XINC = Main.XINC * (-1);
Main.YINC = Main.YINC * (-1);

Main.HITS = Main.HITS + 1;
Main.Context.beginPath();
Main.Context.strokeStyle = 'red';

} else Main.MISSES = Main.MISSES + 1;
Main.Context.beginPath();
Main.Context.strokeStyle = 'blue';

}

// If we hit a wall in x coordinate, then change x direction
if ( (Main.CX < 0+Main.CRAD) || (Main.CX > 600-Main.CRAD))
Main.XINC = Main.XINC * (-1);
Main.CX = Main.CX + (Main.XINC);

// If we hit a wall in y coordinate, then change y direction
if ( (Main.CY < 0+Main.CRAD) || (Main.CY > 600-Main.CRAD))
Main.YINC = Main.YINC * (-1);
Main.CY = Main.CY + Main.YINC;

Main.Context.arc(Main.CX, Main.CY, Main.CRAD, 0, 2 * Math.PI); // draw the circle
Main.Context.stroke(); // fill the circle

// Display the location of the mouse and circle
Main.Context.font = "10px Arial";
Main.Context.fillText("Mouse: X: " + Main.MX + " Y: " + Main.MY, 50, 25);
Main.Context.fillText("Ball: X: " + Main.CX + " Y: " + Main.CY, 350, 25);

// Display the score
Main.Context.font = "30px Arial";
Main.Context.fillText("Hits: " + Main.HITS + " Misses: " + Main.MISSES, 50, 100);

requestAnimFrame(function() { Main.Animate(); }); // must call at end of Main.Animate (recursive)
}

window.requestAnimFrame = (function(callback) // part of sample standard framework
{ // for browser compatibilty
return window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(callback) { window.setTimeout(callback, 1000 / 60); }; // control repainting speed
})();

$(document).ready(function() // called when document loads
{
Main.Animate(); // this method is all that executes here
});

最佳答案

您只是在 else 上缺少打开和关闭 {} 括号,请参阅 fiddle :https://jsfiddle.net/5wwg1q5j/61/

else{ 
Main.MISSES = Main.MISSES + 1;
Main.Context.beginPath();
Main.Context.strokeStyle = 'blue';
}

关于javascript - 在桨击中更改 HTML Canvas Pong 动画中的圆圈颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803343/

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