Skip to content

Commit 1d687dd

Browse files
committed
Fixing Background and Hills Position Issue with One Simple Fix
1 parent 57abfd8 commit 1d687dd

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

game.js

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,8 @@ let spriteStandRight = createImage('./img/spriteStandRight.png');
2828
canvas.width = window.innerWidth;
2929
canvas.height = window.innerHeight;
3030

31-
// Scrolling Background
32-
let backgroundX = 0; // Initial background X position
33-
const backgroundSpeed = 3; // Speed of background scrolling
34-
35-
// Scrolling Hills
36-
let backgroundX2 = 0; // Initial hills X position
37-
const backgroundSpeed2 = 2; // Speed of hills scrolling
38-
39-
// Function init() Variables
4031
let player = {};
4132
let platforms = [];
42-
let objects = [];
43-
44-
// Winning Situation
45-
let offSet = 0;
4633

4734
function init(){
4835

@@ -122,18 +109,23 @@ function init(){
122109
// { x: 6000, y: canvas.height - 50, width: 15000, height: 10 },
123110
// Add more platforms as needed
124111
];
125-
126-
// Platforms
127-
objects = [
128-
{image: background, y: 0, width: background.width, height: canvas.height },
129-
{image: hills, y: 0, width: hills.width, height: hills.height },
130-
// { x: platformImage.width - 2, y: canvas.height - 130, width: platformImage.width, height: platformImage.height },
131-
];
132112

133113
offSet = 0;
134114
backgroundX = 0;
115+
backgroundX2 = 0;
135116
}
136117

118+
// Scrolling Background
119+
let backgroundX = 0; // Initial background X position
120+
const backgroundSpeed = 3; // Speed of background scrolling
121+
122+
// Scrolling Hills
123+
let backgroundX2 = 0; // Initial background X position
124+
const backgroundSpeed2 = 2; // Speed of background scrolling
125+
126+
// Winning Situation
127+
let offSet = 0;
128+
137129
// A function to draw the player character
138130
function drawPlayer() {
139131

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

154146
// A function to draw platforms
155147
function drawPlatforms() {
148+
ctx.fillStyle = "green";
156149
platforms.forEach((platform) => {
157150
ctx.drawImage(platformImage, platform.x, platform.y, platformImage.width, platformImage.height);
158151
});
159152
}
160153

161-
// A function to draw hills and background
162-
function drawHills() {
163-
objects.forEach((object) => {
164-
ctx.drawImage(object.image, backgroundX, object.y, object.width, object.height);
165-
});
166-
}
167-
168-
// A function to draw hills and background
169-
// function drawHills() {
170-
// ctx.drawImage(hills, hills.x, hills.y, platformImage.width, platformImage.height);
171-
// }
172-
173-
// A function to draw hills and background
174-
// function drawBackground() {
175-
// ctx.drawImage(platformImage, platform.x, platform.y, platformImage.width, platformImage.height);
176-
// }
177-
178154
// Define a function to update the game's logic.
179155
function updateGame() {
180156
ctx.clearRect(0, 0, canvas.width, canvas.height);
@@ -194,13 +170,12 @@ function updateGame() {
194170
player.frame = 0;
195171
}
196172

197-
/*ctx.drawImage(background, backgroundX, 0, background.width, canvas.height);*/
173+
ctx.drawImage(background, backgroundX, 0, background.width, canvas.height);
198174
// ctx.drawImage(image, backgroundX, 0, image.width, canvas.height);
199175
// ctx.drawImage(image, backgroundX + canvas.width, 0, canvas.width, canvas.height);
200-
/*ctx.drawImage(hills, backgroundX2, 20, hills.width, hills.height);
201-
ctx.drawImage(hills, backgroundX2+800, 20, hills.width, hills.height);*/
176+
ctx.drawImage(hills, backgroundX2, 20, hills.width, hills.height);
177+
ctx.drawImage(hills, backgroundX2+800, 20, hills.width, hills.height);
202178
// ctx.drawImage(platformImage, 0, canvas.height-platformImage.height, platformImage.width, platformImage.height);
203-
drawHills();
204179
drawPlayer();
205180
drawPlatforms();
206181

0 commit comments

Comments
 (0)