iT邦幫忙

2021 iThome 鐵人賽

DAY 13
0
Modern Web

JavaScript 嗨起來用 JS 做動畫 ε= ᕕ( ᐛ )ᕗ系列 第 13

Day13 - 明天複習貪食蛇,今天先鋪舞台

class Game {
    constructor(){
        // 每格寬為 26px
        this.blockWidth = 26;
        // 格與格之間間距 2px
        this.blockSpace = 2;
        // 總共鋪設 20 格
        this.blocks = 20;
        
        // init
        this.init();
    }
    init(){
        // set canvas
        this.canvas = document.getElementById('canvasSnake');
        this.canvas.width = this.blockWidth * this.blocks + this.blockSpace * (this.blocks - 1);
        this.canvas.height = this.canvas.width;
        this.ctx = this.canvas.getContext('2d');

        // render canvas view
        this.render();

    };

    render(){
        // bg
        this.ctx.fillStyle = '#dbe3c5';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.width);
        // 鋪設 blocks
        for (var bX = 0; bX < this.blocks; bX++) {
            for (var bY = 0; bY < this.blocks; bY++) {
                this.drawBlock(new Vector(bX, bY), '#ced6b8')
            };
        };
    }
};


今天筆記到這,如果內容有出入,都在麻煩糾正了,謝謝您 ε= ᕕ( ᐛ )ᕗ


上一篇
Day12 - audio tag 幫我設定背景音樂
下一篇
Day14 - 結果今天只做小蛇,小蛇還不貪
系列文
JavaScript 嗨起來用 JS 做動畫 ε= ᕕ( ᐛ )ᕗ17

尚未有邦友留言

立即登入留言