Viết game flappy bird bằng HTML và JavaScript (P.1)

Hôm nay chúng ta sẽ viết một game bằng HTML và JavaScript. Các bạn xem demo sản phẩm khi chúng ta hoàn thành trên fiddle:   Demo  đây chỉ ...

Viết game flappy bird bằng HTML và JavaScript (P.1)

Hôm nay chúng ta sẽ viết một game bằng HTML và JavaScript. Các bạn xem demo sản phẩm khi chúng ta hoàn thành trên fiddle:  Demo đây chỉ là demo cơ bản thôi, khi xem xong bài viết này các bạn có thể làm hay hơn thế nữa :)).

Nào ta bắt đầu thôi.

HTML Canvas

Đầu tiên để tạo game chúng ta cần tạo background cho nó, chúng ta sẽ dùng HTML Canvas, Canvas là một element tuyệt vời cho việc thiết kế game trong HTML. Chúng ta sẽ dùng Js để vẽ trên Canvas.

.getContext("2d")

Hàm này dùng để xây dựng lên object trong Canvas.
Bắt đầu thôi, chúng ta hãy tạo background cho game, các bạn tạo 1 file html và nhập đoạn code sau:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
    border: 1px solid #d3d3d3;
    background-color: #f1f1f1;
}
</style>
</head>
<body onload="startGame()">
<script>

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

</script>

<p>We have created a game area! (or at least an empty canvas)</p>

</body>
</html>

Kết quả ta được 1 background:
Method startGame() sẽ được chạy khi trang web được load, object myGameArea sẽ chạy hàm start() của mình và vẽ ra một canvas như kết quả.
document.body.insertBefore(this.canvas, document.body.childNodes[0]) lệnh này sẽ insert thằng canvas ta vừa vẽ vào trước thằng đầu tiên trong thẻ body hiểu nôm na là canvas sẽ là phần tử đầu tiên trong thẻ body.

Game Components

Tiếp theo chúng ta sẽ thêm vào các component cần có trong Game, đầu tiên là con chim đỏ này :))
Kể từ đây code sẽ khá phức tạp, các bạn xem kĩ.
Chúng ta sẽ tạo một constructor để add các component vào Game, chúng ta sẽ tạo component đầu tiên tên là myGamePiece.
Thêm cái function này dưới myGameArea.
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);
Function startGame() đổi thành:
function startGame() {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 120);
}
Refresh lại page ta sẽ có con chim như hình:
ctx = myGameArea.context nghĩa là chúng ta sẽ vẽ component này trong ngữ cảnh myGameArea.
ctx.fillStyle = color phủ màu component
ctx.fillRect(this.x, this.y, this.width, this.height) vẽ 1 hình chữ nhật tương ứng thông số.
Để hiểu rõ các hàm trên các bạn xem thêm về Canvas trên w3schools
Nguồn bài viết: Viết game bằng HTML và JS.

Viết game flappy bird bằng HTML và JavaScript (P.2)

COMMENTS

Tên

game,1,html,1,js,1,mao sơn tróc quỷ nhân,1,
ltr
item
masterseo: Viết game flappy bird bằng HTML và JavaScript (P.1)
Viết game flappy bird bằng HTML và JavaScript (P.1)
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWeo2CB7dKQsoBaNR3Sylmpl6HwJGUVQG9Ua4o4b5k_n2ubGWNAPnzU2NaIGHWZH4RoKGc3cT0KQiqCbJtQgTiKBjW_PMbZFqSCOWj_9t6EZJ0ts_Wx3X9EqdGPhSa-0vF1TIeDsDQth8/s400/01-flappy-smasher-game-html5-preview.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWeo2CB7dKQsoBaNR3Sylmpl6HwJGUVQG9Ua4o4b5k_n2ubGWNAPnzU2NaIGHWZH4RoKGc3cT0KQiqCbJtQgTiKBjW_PMbZFqSCOWj_9t6EZJ0ts_Wx3X9EqdGPhSa-0vF1TIeDsDQth8/s72-c/01-flappy-smasher-game-html5-preview.jpg
masterseo
http://masterseo2018.blogspot.com/2018/03/viet-game-flappy-bird-bang-html-va-js.html
http://masterseo2018.blogspot.com/
http://masterseo2018.blogspot.com/
http://masterseo2018.blogspot.com/2018/03/viet-game-flappy-bird-bang-html-va-js.html
true
6921768850503723684
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy