iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 29
0
自我挑戰組

學習 canvas 日記系列 第 29

第 29 天 迷宮 四

  • 分享至 

  • twitterImage
  •  

可以移動後再來就是要作到達終點的動作
當移動遇到黑線以外的顏色就會結束

if(pixels[i+1] === 128 && pixels[i+3] === 239){
    // 當遇到綠色的值 [0, 128, 0, 239] 時
    circleStart.y += parseInt(distance); 
    game_start('到達終點再玩一次?')
    // 再來再放結束的畫面
    
}

移動時不一定是向下也有可能是左右
所以在按按鍵時都要加入執行的話
作成 function 就不怕要改一、兩行時會忘了改其他方向的

function game_start(text){
  var imgData= ctx.getImageData(0, 0, canvas.width, canvas.height);
  ctx.putImageData(imgData,0,0);
  // 把到達時拍照下來貼在原地
  
  ctx.fillStyle = 'rgba(255, 255, 255, 0.5)'
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.fill()
  
  ctx.font = '700 20px 微軟正黑體'; 
  ctx.fillStyle = "red";
  ctx.lineWidth = 10;
  
  if(text.length < 5){
    ctx.fillText(text, canvas.width/4, canvas.height/2 -20); 
  }
  
  // 當字太多塞不下時就可以用 slice 分成兩行
  if(text.length > 4){ 
    ctx.fillText(text.slice(0, 4), canvas.width/4, canvas.height/2 -20); 
    ctx.fillText(text.slice(4, 9), canvas.width/4 -5 , canvas.height/2 + 10); 
  }
}

https://i.imgur.com/rSmomn6.gif


上一篇
第 28 天 迷宮 三
下一篇
第 30 天 迷宮 五
系列文
學習 canvas 日記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言