iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
Modern Web

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

Day14 - 結果今天只做小蛇,小蛇還不貪

class Snake {
    constructor() {
        // 蛇頭位子
        this.head = new Vector(0, 0);
        // 除了蛇頭外蛇身的位子
        this.body = [];
        // 蛇移動的步伐
        this.step = new Vector(1, 0);
        // 蛇頭加上蛇身長度
        this.maxLength = 3;
    }
    update() {
        // push head to body
        this.body.push(this.head);
        // generate new head
        // 整隻蛇往前一步,蛇頭當然也就往前一步
        this.head = this.head.add(this.step);
        // shift the tail
        // 整隻蛇往前一步,蛇尾當然也就往前一步,故刪除舊蛇尾位子
        while(this.body.length > this.maxLength){
            this.body.shift();
        }
    
    };
};

祝大家健康開心~內容有出入都希望能多多提醒~感謝 ε= ᕕ( ᐛ )ᕗ


上一篇
Day13 - 明天複習貪食蛇,今天先鋪舞台
下一篇
Day15 - 請蛇上台
系列文
JavaScript 嗨起來用 JS 做動畫 ε= ᕕ( ᐛ )ᕗ17
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言