Oyun Puanı
- Önceki Sayfa Oyun Engelleri
- Sonraki Sayfa Oyun Görselleri
按下按钮可移动红色方块:
计分
有很多在游戏中记录分数的方法,我们将向您展示如何将分数写到画布上。
首先做一个分数组件:
var myGamePiece; var myObstacles = []; var myScore; function startGame() { myGamePiece = new component(30, 30, "red", 10, 160); myScore = new component("30px", "Consolas", "black", 280, 40, "text"); myGameArea.start(); }
在画布元素上写入文本的语法与绘制矩形不同。因此,我们必须使用一个额外的参数调用组件构造函数,告诉构造函数该组件的类型为 "text"。
在组件构造函数中,我们测试组件是否为 "text"
类型,并使用 fillText
方法而不是 fillRect
方法:
function component(width, height, color, x, y, type) { this.type = type; this.width = width; this.height = height; this.speedX = 0; this.speedY = 0; this.x = x; this.y = y; this.update = function() { ctx = myGameArea.context; if (this.type == "text") { ctx.font = this.width + " " + this.height; ctx.fillStyle = renk; ctx.fillText(this.text, this.x, this.y); } else {}} ctx.fillStyle = renk; ctx.fillRect(this.x, this.y, this.genişlik, this.yükseklik); } } ... }
Sonunda, updateGameArea fonksiyonuna bazı kod ekledik, puanı kanva'ya yazdık. Kullandık: frameNo
puan hesaplamak için özellikler:
function updateGameArea() { var x, yükseklik, boşluk, enDüşükYükseklik, enYüksekYükseklik, enDüşükBoşluk, enYüksekBoşluk; for (i = 0; i < myObstacles.length; i += 1) { if (myGamePiece.crashWith(myObstacles[i])) { myGameArea.durdur(); return; } } myGameAreaTemizle(); myGameArea.frameNo += 1; if (myGameArea.frameNo == 1 || heraralik(150)) { x = myGameArea.canvas.genişlik; minYükseklik = 20; maksYükseklik = 200; yükseklik = Math.floor(Math.random()*(maksYükseklik-minYükseklik+1)+minYükseklik); minBoşluk = 50; maksBoşluk = 200; boşluk = Math.floor(Math.random()*(maksBoşluk-minBoşluk+1)+minBoşluk); myObstacles.push(new component(10, yükseklik, "yeşil", x, 0)); myObstacles.push(new component(10, x - yükseklik - boşluk, "yeşil", x, yükseklik + boşluk)); } for (i = 0; i < myObstacles.length; i += 1) { myObstacles[i].hizX = -1; myObstacles[i].yeniPozisyon(); myObstacles[i].güncelle(); } myScore.text = "PÜAN: " + myGameArea.frameNo; myScore.update(); myGamePiece.newPos(); myGamePiece.update(); }
- Önceki Sayfa Oyun Engelleri
- Sonraki Sayfa Oyun Görselleri