gpt4 book ai didi

javascript - 我如何制作自动交通灯?

转载 作者:行者123 更新时间:2023-11-30 08:33:03 25 4
gpt4 key购买 nike

<分区>

var RED = "#FF0000";
var YELLOW = "#FFFF00";
var GREEN = "#00FF00";
var DARK_RED = "#380000";
var DARK_YELLOW = "#383800";
var DARK_GREEN = "#003800";
var X_ALL = 150;
var Y_RED = 100;
var Y_YELLOW = Y_RED + 150;
var Y_GREEN = Y_YELLOW + 150;
var trafficLightsStateMachine;
function TrafficLightsStateMachine() {
this.state = 0;
this.stateMachine = new Array();
this.stateMachine[0] = function () { drawCircles(DARK_RED, YELLOW, DARK_GREEN); };
this.stateMachine[1] = function () { drawCircles(RED, DARK_YELLOW, DARK_GREEN); };
this.stateMachine[2] = function () { drawCircles(RED, YELLOW, DARK_GREEN); };
this.stateMachine[3] = function () { drawCircles(DARK_RED, DARK_YELLOW, GREEN); };
this.process = function() {
this.stateMachine[this.state]();
this.state = (this.state + 1) % this.stateMachine.length;
};
this.drawCircle = function(canvas, color, x, y) {
var context = canvas.getContext('2d');
context.strokeStyle = "#000000";
context.fillStyle = color;
context.beginPath();
context.arc(x, y, 50, 0, Math.PI * 2, true);
context.closePath();
context.stroke();
context.fill();
};
}

function drawCircles(first, second, third) {
var id = 'canvas';
var canvas = document.getElementById(id);
if (canvas.getContext) {
trafficLightsStateMachine.drawCircle(canvas, first, X_ALL, Y_RED);
trafficLightsStateMachine.drawCircle(canvas, second, X_ALL, Y_YELLOW);
trafficLightsStateMachine.drawCircle(canvas, third, X_ALL, Y_GREEN);
}
}

function init() {
trafficLightsStateMachine = new TrafficLightsStateMachine();
drawCircles(DARK_RED, DARK_YELLOW, GREEN);
}
#page
{
width: 300px;
height: 500px;
margin: auto;
}
#canvas:hover
{
cursor: crosshair;
background-color: #191919;
}
#canvas
{
background-color: #252525;
}
body
{
background: #222222;
color: white;
}
<body onload="init()">
<div id="page" onclick="trafficLightsStateMachine.process()" title="Please, press button.">
<canvas id="canvas" height="500px" width="300px">
</canvas>
</div>
</body>

我希望能够更改此代码,以便在运行时红绿灯自动更改。我知道我需要添加 setinterval(trafficLightsStateMachine.process,1000) 但是我不知道在哪里。请指教。

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