Game 3
Game 3
<head>
<style>
canvas {
background-color: #f1f1f1;
</style>
</head>
<body onload="startGame()">
<script>
var myGamePiece;
function startGame() {
myGameArea.start();
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
},
clear : function() {
1
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
stop : function() {
clearInterval(this.interval);
this.type = type;
if (type == "image") {
this.image.src = color;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
if (type == "image") {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
} else {
ctx.fillStyle = color;
2
this.newPos = function() {
this.x += this.speedX;
this.y += this.speedY;
function updateGameArea() {
myGameArea.clear();
myGamePiece.newPos();
myGamePiece.update();
function moveup() {
myGamePiece.speedY = -1;
function movedown() {
myGamePiece.speedY = 1;
function moveleft() {
myGamePiece.speedX = -1;
function moveright() {
myGamePiece.speedX = 1;
function clearmove() {
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
3
}
</script>
<div style="text-align:center;width:480px;">
</div>
<p>The component constructor uses the built-in image object to draw images onto the canvas.</p>
</body>
</html>