有没有一种方法可以使用循环做出“永远”的声明?

时间:2020-04-13 16:03:30

标签: javascript

我正在尝试再次制作交互式汽车游戏,但是这次我正在限制自己的速度。我尝试在汽车的速度超过速度限制加10的情况下执行操作,然后执行代码。但这根本不起作用。甚至没有给我控制台错误。这是我的代码:

/
os.join
var carOn = 0;
var lBlink = 0;
var rBlink = 0;
var time = 'day';
var change = setInterval(changeDay, 120000);
var blink = '';
var lblink = '';
var headlights = 0;
var mph = 0;
var accelerate = setInterval(accelerate, 500);
var gear = 'park';
var overSpeedLimit = false;
var speedLimit = 25;

console.log('Time to start the day!');

function toggleCar() {
	var min = 1;
	var max = 6;
	let starts = Math.random() * (+max - +min) + +min;
	if (carOn == 0) {
		if (condition == 'bad') {
			if (starts >= 5) {
				console.log('Car on');
				document.getElementById('carIgnition').innerHTML = 'Your car is on';
			} else {
				console.log('Your car must be broken...');
				document.getElementById('carIgnition').innerHTML = 'Your car did not start';
				setTimeout(function() {
					document.getElementById('carIgnition').innerHTML = 'Your car is off';
				}, 3000);
			}
		} else {
			console.log('Car on');
			document.getElementById('carIgnition').innerHTML = 'Your car is on';
		}
	} else {
		console.log('Car off');
		document.getElementById('carIgnition').innerHTML = 'Your car is off';
	}
}

var condition = '';

function setupCar() {
	var make = window.prompt('What is the make of your car?', 'Dodge');
	var model = window.prompt('What is the model of your car?', 'Charger');
	var year = window.prompt('What year is your car?', '2018');
	condition = window.prompt('What condition is your car in?');
	document.getElementById('typeCar').innerHTML = make + ' ' + model + ' ' + year;
	document.getElementById('condition').innerHTML = 'Condition: ' + condition;
	document.getElementById('hideMe').style.display = 'none';
	console.log('You bought a nice ' + make + ' ' + model + ' ' + year + ' in ' + condition + ' condition.');
}

function rightBlinker() {
	if (rBlink == 0) {
		rBlink = 1;
		document.getElementById('rBlinker').innerHTML = 'Your right blinker is on →';
		console.log('Right blinker on');
	} else if (rBlink == 1) {
		rBlink = 0;
		console.log('Right blinker off');
		document.getElementById('rBlinker').innerHTML = 'Your right blinker is off';
	}
}

function leftBlinker() {
	if (lBlink == 0) {
		lBlink = 1;
		document.getElementById('lBlinker').innerHTML = 'Your left blinker is on ←';
		console.log('Left blinker on');
	} else if (lBlink == 1) {
		lBlink = 0;
		document.getElementById('lBlinker').innerHTML = 'Your left blinker is off';
		console.log('Left blinker off');
	}
}

function toggleHeadlights() {
	if (headlights == 0) {
		headlights = 1;
		document.getElementById('headlights').innerHTML = 'Your headlights are on';
		document.getElementById('headlights').style.backgroundColor = 'yellow';
		console.log('Headlights on');
	} else if (headlights == 1) {
		headlights = 0;
		document.getElementById('headlights').innerHTML = 'Your headlights are off';
		document.getElementById('headlights').style.backgroundColor = 'transparent';
		console.log('Headlights off');
	}
}

function changeDay() {
	if (time == 'day') {
		time = 'night';
		document.getElementById('time').innerHTML = 'It is night time right now';
		console.log('It is night time, time to go home.');
		document.body.style.backgroundColor = 'black';
	} else if (time == 'night') {
		time = 'day';
		document.getElementById('time').innerHTML = 'It is day time right now';
		console.log('It is now day time, go to work!');
		document.body.style.backgroundColor = 'powderblue';
	}
}

let wipers;

function toggleWipers() {
	console.log('Windshield wipers on');
	wipers = setInterval(function() {
		document.getElementById('wiper1').innerHTML = '↑ ↑';
		document.getElementById('wiper2').style.visibility = 'collapse';
		setTimeout(function() {
			document.getElementById('wiper1').innerHTML = '←';
			document.getElementById('wiper2').style.visibility = 'visible';
			document.getElementById('wiper2').innerHTML = '←';
		}, 1000);
	}, 2000);
}

function wipersOff() {
	clearInterval(wipers);
	console.log('Windshield wipers off');
}

function shiftGears() {
	if (gear == 'park') {
		gear = 'neutral';
		console.log('Gear shifted to Neutral');
		document.getElementById('gear').innerHTML = 'Neutral';
	} else if (gear == 'neutral') {
		gear = '1';
		console.log('Gear shifted to 1');
		document.getElementById('gear').innerHTML = '1st gear';
	} else if (gear == '1') {
		gear = '2';
		console.log('Gear shifted to 2');
		document.getElementById('gear').innerHTML = '2nd gear';
	} else if (gear == '2') {
		gear = '3';
		console.log('Gear shifted to 3');
		document.getElementById('gear').innerHTML = '3rd gear';
	} else if (gear == '3') {
		gear = '4';
		console.log('Gear shifted to 4');
		document.getElementById('gear').innerHTML = '4th gear';
	} else if (gear == '4') {
		gear = 'reverse';
		console.log('Gear shifted to Reverse');
		document.getElementById('gear').innerHTML = 'Reverse';
	} else if (gear == 'reverse') {
		gear = 'park';
		console.log('Gear shifted to park');
		document.getElementById('gear').innerHTML = 'Park';
	}
}

function shiftBackwards() {
	if (gear == 'park') {
		gear = '4';
		console.log('Gear shifted to 4');
		document.getElementById('gear').innerHTML = '4th gear';
	} else if (gear == '4') {
		gear = '3';
		console.log('Gear shifted to 3');
		document.getElementById('gear').innerHTML = '3rd gear';
	} else if (gear == '3') {
		gear = '2';
		console.log('Gear shifted to 2');
		document.getElementById('gear').innerHTML = '2nd gear';
	} else if (gear == '2') {
		gear = '1';
		console.log('Gear shifted to 1');
		document.getElementById('gear').innerHTML = '1st gear';
	} else if (gear == '1') {
		gear = 'neutral';
		console.log('Gear shifted to Neutral');
		document.getElementById('gear').innerHTML = 'Neutral';
	} else if (gear == 'neutral') {
		gear = 'park';
		console.log('Gear shifted to Park');
		document.getElementById('gear').innerHTML = 'Park';
	} else if (gear == 'park') {
		gear = 'reverse';
		console.log('Gear shifted to reverse');
		document.getElementById('gear').innerHTML = 'Reverse';
	}
}

var mph = 0;
var accelerate;

function gas() {
	if (gear == 'park' || gear == 'neutral') {
		mph = 0;
		console.log('This is the maximum speed for your current gear!');
	} else if (gear == '1') {
		accelerate = setInterval(function() {
			if (mph == 15) {
				console.log('This is the maximum speed for 1st gear!');
			} else {
				mph += 1;
				document.getElementById('mph').innerHTML = mph + ' mph';
			}
		}, 100);
	} else if (gear == '2') {
		accelerate = setInterval(function() {
			if (mph == 30) {
				console.log('This is the maximum speed for 2nd gear!');
			} else {
				mph += 1;
				document.getElementById('mph').innerHTML = mph + ' mph';
			}
		}, 100);
	} else if (gear == '3') {
		accelerate = setInterval(function() {
			if (mph == 40) {
				console.log('This is the maximum speed for 3rd gear!');
			} else {
				mph += 1;
				document.getElementById('mph').innerHTML = mph + ' mph';
			}
		}, 100);
	} else if (gear == '4') {
		accelerate = setInterval(function() {
			if (mph == 55) {
				console.log('This is the maximum speed for 4th gear!');
			} else {
				mph += 1;
				document.getElementById('mph').innerHTML = mph + ' mph';
			}
		}, 100);
	}
}

var brakes;

function brake() {
	brakes = setInterval(function() {
		if (mph == 0) {
			console.log('You are at 0 mph already!');
		} else {
			mph -= 1;
			document.getElementById('mph').innerHTML = mph + ' mph';
		}
	}, 100);
}

function stopGas() {
	clearInterval(accelerate);
}

function stopBrake() {
	clearInterval(brakes);
}

var stopLight = 'none';
var stopSign = 'none';
var stop;

function spawnStopLight() {
	if (stopLight == 'green' || stopLight == 'red') {
		console.log('There is already a stop light!');
	} else {
		var min = 1;
		var max = 11;
		var random = Math.random() * (+max - +min) + +min;
		random = random.toFixed();
		if (random >= 5) {
			stopLight = 'green';
			document.getElementById('stop').innerHTML = 'Green';
			stopLight = 'none';
		} else {
			stopLight = 'red';
			document.getElementById('stop').innerHTML = 'Red';
			stop = setTimeout(function() {
				if (mph > 0) {
					mph = 0;
					document.getElementById('stop').innerHTML = 'Drive';
					document.getElementById('mph').innerHTML = mph + ' mph';
					window.alert('You got pulled over for running a stop sign!');
					console.log('Officer: Oh my gosh! Why did you run that stop light?');
					var run = window.prompt('Why did you run it?');
					console.log('You: ' + run);
					console.log('It does not matter. You should never run a stop light. You could have crashed and killed you or someone else.');
					console.log('You: Ok.');
					stopLight = 'none';
				} else {
					document.getElementById('stop').innerHTML = 'Drive';
					stopLight = 'none';
					console.log('You can drive now, the stop light turned green.');
				}
			}, 5000)
		}
	}
}

function spawnStopSign() {
	if (stopSign == 'yes') {
		console.log('There is already a stop sign!')
	} else {
		console.log('Stop sign spawned.');
		stopSign = 'yes';
		document.getElementById('stop').innerHTML = 'Stop';
		setTimeout(function() {
			if (mph > 0) {
				window.alert('You got pulled over for running a stop sign!');
				console.log('Officer: What were you in such a rush for? You do not have to stop for long. Its not a red light...');
				var rush = window.prompt('Why were you in a rush?');
				console.log('You: ' + rush);
				console.log('Officer: Ok. I will let you off this time since it was just a stop sign. But next time you have to be more careful.');
			} else {
				console.log('You can drive now, you went through the stop sign.');
			}
		}, 5000);
		stopSign = 'none';
	}
}

function newRoad() {
	var min = 15;
	var max = 66;
	speedLimit = Math.random() * (+max - +min) + +min;
	speedLimit.toFixed();
	var roads = ['Oakcliff Road', 'Bartholomew Road', 'Main Street', 'Rocky Road', 'Matched Drive', 'Bandana Street', 'Cup Drive', 'Luis Street', 'Ridge Road', 'Carlton Street', 'Newfield Street', 'Washington Street', 'Wadsworth Street', 'South Street', 'North Street', 'East Street', 'West Street', 'Main Street Extention', 'Bradley Street', 'Dixwell Avenue', 'Henry Street', 'Saybrook Road', 'Google Street', 'Hawley Lane', 'Broad Street', 'Pardy Place', 'Green Street', 'Young Street', 'Old Street', 'New Street', 'Front Street', 'Back Street', 'Luther Street', 'Jackson Lane', 'Timbro Lane', 'West Main Street', 'South Main Street', 'Dontfraid Boulevard', 'Atkinson Place', 'Unity Place'];
	var roadNum = 0;
	var roadNum2 = 41;
	var road = Math.random() * (+roadNum2 - +roadNum) + +roadNum;
	document.getElementById('road').innerHTML = roads[road.toFixed()];
	document.getElementById('speedLimit').innerHTML = speedLimit.toFixed();
}
那是我完整的游戏代码,以便您可以运行它并查看我的作品。

2 个答案:

答案 0 :(得分:0)

如果mph小于或等于speedlimit + 10,则第一次overSpeedLimit永远不会设置为true。

这取决于何时执行该代码段。从逻辑上讲,您可能只想设置overSpeedLimit = mph > speedLimit + 10;,但这仍然取决于您在哪里进行测试。

答案 1 :(得分:0)

使用一会儿true / false循环即可完成工作。您可以这样做:

location == null

那应该解决不循环的问题。