Model Score Lives

This commit is contained in:
Bluwww 2025-11-28 19:58:10 +07:00
parent 45769a6906
commit a1333f6d3f
2 changed files with 38 additions and 11 deletions

View File

@ -21,6 +21,9 @@ var keys = {
fire: false fire: false
} }
var playerShipImg = new Image();
playerShipImg.src = 'img/pixelShip.png'
window.onload = function () { window.onload = function () {
init() init()
} }
@ -86,8 +89,11 @@ function updateGame() {
game.frames++; game.frames++;
} }
function drawGame() { function drawGame(){
player1.draw(); player1.draw();
drawNewText('Score: ' +player1.score, 30, 610, 'white');
drawNewText('Player Lives: ' +player1.lives, 1100, 610, 'white');
} }
function gameLoop(timestamp){ function gameLoop(timestamp){
@ -101,30 +107,51 @@ class PlayerObject{
constructor(x, y){ constructor(x, y){
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = 150; this.width = 170;
this.height = 150; this.height = 105;
this.color = 'blue'; this.image = playerShipImg;
this.speed = 5; this.speed = 8;
this.lives = 3; this.lives = 3;
this.score = 0; this.score = 0;
this.health = 100; this.health = 100;
} }
draw(){ draw(){
ctx.save(); ctx.save();
ctx.fillStyle = this.color; ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.fill();
ctx.restore(); ctx.restore();
} }
update(){ update(){
if(keys.up){ if (keys.up) {
if(this.y > 0){
this.y -= this.speed; this.y -= this.speed;
}
} }
else if (keys.down){ else if (keys.down) {
if (this.x < canvasWidth - this.width) {
this.y += this.speed; this.y += this.speed;
}
}
if (keys.right) {
if (this.x < canvasWidth - this.width) {
this.x += this.speed;
}
}
if (keys.left) {
if (this.x > 10) {
this.x -= this.speed;
}
} }
} }
} }
let player1 = new PlayerObject(100,100); let player1 = new PlayerObject(100, 100);
function drawNewText(txt, x, y, color){
ctx.font = "20px Arial";
ctx.fillStyle = color;
ctx.fillText(txt, x, y);
}

BIN
img/pixelShip.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB