Skip to content

Commit

Permalink
Fixing Background and Hills Position Issue with One Simple Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wild-railgun committed Dec 21, 2023
1 parent 57abfd8 commit 1d687dd
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,8 @@ let spriteStandRight = createImage('./img/spriteStandRight.png');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// Scrolling Background
let backgroundX = 0; // Initial background X position
const backgroundSpeed = 3; // Speed of background scrolling

// Scrolling Hills
let backgroundX2 = 0; // Initial hills X position
const backgroundSpeed2 = 2; // Speed of hills scrolling

// Function init() Variables
let player = {};
let platforms = [];
let objects = [];

// Winning Situation
let offSet = 0;

function init(){

Expand Down Expand Up @@ -122,18 +109,23 @@ function init(){
// { x: 6000, y: canvas.height - 50, width: 15000, height: 10 },
// Add more platforms as needed
];

// Platforms
objects = [
{image: background, y: 0, width: background.width, height: canvas.height },
{image: hills, y: 0, width: hills.width, height: hills.height },
// { x: platformImage.width - 2, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
];

offSet = 0;
backgroundX = 0;
backgroundX2 = 0;
}

// Scrolling Background
let backgroundX = 0; // Initial background X position
const backgroundSpeed = 3; // Speed of background scrolling

// Scrolling Hills
let backgroundX2 = 0; // Initial background X position
const backgroundSpeed2 = 2; // Speed of background scrolling

// Winning Situation
let offSet = 0;

// A function to draw the player character
function drawPlayer() {

Expand All @@ -153,28 +145,12 @@ function drawPlayer() {

// A function to draw platforms
function drawPlatforms() {
ctx.fillStyle = "green";
platforms.forEach((platform) => {
ctx.drawImage(platformImage, platform.x, platform.y, platformImage.width, platformImage.height);
});
}

// A function to draw hills and background
function drawHills() {
objects.forEach((object) => {
ctx.drawImage(object.image, backgroundX, object.y, object.width, object.height);
});
}

// A function to draw hills and background
// function drawHills() {
// ctx.drawImage(hills, hills.x, hills.y, platformImage.width, platformImage.height);
// }

// A function to draw hills and background
// function drawBackground() {
// ctx.drawImage(platformImage, platform.x, platform.y, platformImage.width, platformImage.height);
// }

// Define a function to update the game's logic.
function updateGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
Expand All @@ -194,13 +170,12 @@ function updateGame() {
player.frame = 0;
}

/*ctx.drawImage(background, backgroundX, 0, background.width, canvas.height);*/
ctx.drawImage(background, backgroundX, 0, background.width, canvas.height);
// ctx.drawImage(image, backgroundX, 0, image.width, canvas.height);
// ctx.drawImage(image, backgroundX + canvas.width, 0, canvas.width, canvas.height);
/*ctx.drawImage(hills, backgroundX2, 20, hills.width, hills.height);
ctx.drawImage(hills, backgroundX2+800, 20, hills.width, hills.height);*/
ctx.drawImage(hills, backgroundX2, 20, hills.width, hills.height);
ctx.drawImage(hills, backgroundX2+800, 20, hills.width, hills.height);
// ctx.drawImage(platformImage, 0, canvas.height-platformImage.height, platformImage.width, platformImage.height);
drawHills();
drawPlayer();
drawPlatforms();

Expand Down

0 comments on commit 1d687dd

Please sign in to comment.