HOW TO MAKE GAME WITH HTML

Advantages and Use of Canvas


Canvas is used to make game in HTML using CSS or JavaScript, we can make our game more attractive

Adding Canvas

To insert a Canvas we use the syntax:

<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
Your browser does not support the HTML canvas tag.
</canvas>

Adding Game Object

An Game Object is the role playing character which is simply known as Character. In this Example we will add only one Game Object


Syntax:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>



<script>

var myGamePiece;

function startGame() {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 120);
}

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]);
    }
}

function component(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;    
    ctx = myGameArea.context;
    ctx.fillStyle = color;
    ctx.fillRect(this.x, this.y, this.width, this.height);
}

</script>

We have added a component to our game, a red square!

</body>

Comments

Popular posts from this blog

What is a Blogs? How to create account on Blog?

What is JAVASCRIPT?How to make a program using HTML and Java | "Hello World" | Prakhar Doneria

HOW TO ADD PIE CHART IN HTML | PRAKHAR DONERIA