Background

This commit is contained in:
Bluwww 2025-11-28 20:42:00 +07:00
parent a1333f6d3f
commit 38795f7ac9
5 changed files with 85 additions and 2 deletions

View File

@ -21,9 +21,27 @@ var keys = {
fire: false
}
var playerShipImg = new Image();
playerShipImg.src = 'img/pixelShip.png'
var bg0 = new Image();
bg0.src = 'img/bg_0.png';
var bg1 = new Image();
bg1.src = "img/bg_1.png";
var bg2 = new Image();
bg2.src = "img/bg_2.png";
window.onload = function () {
init()
}
@ -84,12 +102,15 @@ function clearGame() {
}
function updateGame() {
addStarField();
player1.update();
game.frames++;
}
function drawGame(){
function drawGame() {
player1.draw();
drawNewText('Score: ' +player1.score, 30, 610, 'white');
@ -155,3 +176,65 @@ function drawNewText(txt, x, y, color){
ctx.fillStyle = color;
ctx.fillText(txt, x, y);
}
class backgroundObj{
constructor(img, x, y, speed) {
this.x = x;
this.y = y;
this.width = 2000;
this.height = 1200;
this.img = img;
this.speed = speed;
}
draw() {
ctx.save();
ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
ctx.restore();
}
update() {
this.x -= this.speed;
if (this.x < -2000) {
this.x = 2000;
}
}
}
let background1 = new backgroundObj(bg0, 0, 0, game.speed*3);
let background1a = new backgroundObj(bg0, 2000, 0, game.speed*3);
let background2 = new backgroundObj(bg1, 0, 0, game.speed*2);
let background2a = new backgroundObj(bg1, 2000, 0, game.speed*2);
let background3 = new backgroundObj(bg2, 0, 0, game.speed*1);
let background3a = new backgroundObj(bg2, 2000, 0, game.speed*1);
function addStarField() {
background3.draw();
background3.update();
background3a.draw();
background3a.update();
background2.draw();
background2.update();
background2a.draw();
background2a.update();
background1.draw();
background1.update();
background1a.draw();
background1a.update();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
img/bg_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 MiB

BIN
img/bg_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

BIN
img/bg_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB