percent = input("Enter percent:")
ofNumber = input("Enter number:")
percent2 = percent++0.
answer = percent2 * ofNumber
print(answer)
'use strict';
var Game = {
canvas: undefined,
canvasContext: undefined
};
Game.start = function () {
Game.canvas = document.getElementById('myCanvas');
Game.canvasContext = Game.canvas.getContext('2d');
Game.mainLoop();
};
document.addEventListener('DOMContentLoaded', Game.start);
Game.update = function () {
};
Game.draw = function () {
Game.canvasContext.fillStyle = 'blue';
Game.canvasContext.fillReact(0, 0, Game.canvas.width, Game.canvas.height);
};
Game.mainLoop = function () {
Game.update();
Game.draw();
window.setTimeout(mainLoop, 1000 / 60);
};
以上代码应将'canvas'的颜色设置为蓝色,但该代码无法运行。函数“ Game.draw()”很可能是问题所在,如果有人愿意花时间帮助我,将不胜感激。谢谢
答案 0 :(得分:0)
问题是您有Game.canvasContext.fillReact
而不是Game.canvasContext.fillRect
。
您还希望将Game.mainLoop
传递给window.setTimeout
而不是像mainLoop
那样传递给window.setTimeout(Game.mainLoop, 1000 / 60);
。话虽如此,您实际上可能是在寻找window.setInterval()
而不是window.setTimeout()
-区别在于 .setInterval()
将重复循环,而 {{ 3}} 仅会触发一次。
这是一个更新的示例:
'use strict';
var Game = {
canvas: undefined,
canvasContext: undefined
};
Game.start = function () {
Game.canvas = document.getElementById('myCanvas');
Game.canvasContext = Game.canvas.getContext('2d');
Game.mainLoop();
};
document.addEventListener('DOMContentLoaded', Game.start);
Game.update = function () {
};
Game.draw = function () {
Game.canvasContext.fillStyle = 'blue';
Game.canvasContext.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
};
Game.mainLoop = function () {
Game.update();
Game.draw();
window.setInterval(Game.mainLoop, 1000 / 60);
};
<!DOCTYPE html>
<html>
<head>
<title>FlyingSprite</title>
<script src="FlyingSprite.js"></script>
</head>
<body>
<div id="gameArea">
<canvas id="myCanvas" width="800" height="480"></canvas>
</div>
</body>
</html>