Can anybody tell me, what to do if the browser isn’t accepting import and export. I created the classes in an another file that I tried to import it in my main.js. But Google Chrome was showing, ‘There was an issue in importing file’.
I believe all of the major browsers now support ES6 module import/export. Maybe you can give us more specifics? Do you have your code anywhere we can look at it (e.g. github)?
And since I can’t see your code I’m just guessing here, but are you using type="module"
on the <script>
for main.js?
Main.js
import Game from "/src/game";
let canvas = document.getElementById("gameScreen");
let ctx = canvas.getContext("2d");
const GAME_WIDTH = 800;
const GAME_HEIGHT = 600;
let game = new Game(GAME_WIDTH, GAME_HEIGHT);
let lastTime = 0;
function gameLoop(timestamp) {
let deltaTime = timestamp - lastTime;
lastTime = timestamp;
ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
game.update(deltaTime);
game.draw(ctx);
requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);
Paddle.js
export default class Paddle {
constructor(game) {
this.gameWidth = game.gameWidth;
this.width = 150;
this.height = 20;
this.maxSpeed = 7;
this.speed = 0;
this.position = {
x: game.gameWidth / 2 - this.width / 2,
y: game.gameHeight - this.height - 10
};
}
moveLeft() {
this.speed = -this.maxSpeed;
}
moveRight() {
this.speed = this.maxSpeed;
}
stop() {
this.speed = 0;
}
draw(ctx) {
ctx.fillStyle = "#0ff";
ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
}
update(deltaTime) {
this.position.x += this.speed;
if (this.position.x < 0) this.position.x = 0;
if (this.position.x + this.width > this.gameWidth)
this.position.x = this.gameWidth - this.width;
}
}
Game.js
import Paddle from "/src/paddle";
import InputHandler from "/src/input";
import Ball from "/src/ball";
import Brick from "/src/brick";
import { buildLevel, level1, level2 } from "/src/levels";
const GAMESTATE = {
PAUSED: 0,
RUNNING: 1,
MENU: 2,
GAMEOVER: 3,
NEWLEVEL: 4
};
export default class Game {
constructor(gameWidth, gameHeight, bricksPerRow) {
this.gameWidth = gameWidth;
this.gameHeight = gameHeight;
this.gamestate = GAMESTATE.MENU;
this.ball = new Ball(this);
this.paddle = new Paddle(this);
this.gameObjects = [];
this.bricks = [];
this.lives = 3;
this.levels = [level1, level2];
this.currentLevel = 0;
new InputHandler(this.paddle, this);
}
start() {
if (
this.gamestate !== GAMESTATE.MENU &&
this.gamestate !== GAMESTATE.NEWLEVEL
)
return;
this.bricks = buildLevel(this, this.levels[this.currentLevel]);
this.ball.reset();
this.gameObjects = [this.ball, this.paddle];
this.gamestate = GAMESTATE.RUNNING;
}
update(deltaTime) {
if (this.lives === 0) this.gamestate = GAMESTATE.GAMEOVER;
if (
this.gamestate === GAMESTATE.PAUSED ||
this.gamestate === GAMESTATE.MENU ||
this.gamestate === GAMESTATE.GAMEOVER
)
return;
if (this.bricks.length === 0) {
this.currentLevel++;
this.gamestate = GAMESTATE.NEWLEVEL;
this.start();
}
[...this.gameObjects, ...this.bricks].forEach(object =>
object.update(deltaTime)
);
this.bricks = this.bricks.filter(brick => !brick.markedForDeletion);
}
draw(ctx) {
[...this.gameObjects, ...this.bricks].forEach(object => object.draw(ctx));
if (this.gamestate === GAMESTATE.PAUSED) {
ctx.rect(0, 0, this.gameWidth, this.gameHeight);
ctx.fillStyle = "rgba(0,0,0,0.5)";
ctx.fill();
ctx.font = "30px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText("Paused", this.gameWidth / 2, this.gameHeight / 2);
}
if (this.gamestate === GAMESTATE.MENU) {
ctx.rect(0, 0, this.gameWidth, this.gameHeight);
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fill();
ctx.font = "30px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText(
"Press SPACEBAR To Start",
this.gameWidth / 2,
this.gameHeight / 2
);
}
if (this.gamestate === GAMESTATE.GAMEOVER) {
ctx.rect(0, 0, this.gameWidth, this.gameHeight);
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fill();
ctx.font = "30px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText("GAME OVER", this.gameWidth / 2, this.gameHeight / 2);
}
}
togglePause() {
if (this.gamestate == GAMESTATE.PAUSED) {
this.gamestate = GAMESTATE.RUNNING;
} else {
this.gamestate = GAMESTATE.PAUSED;
}
}
}
main.html
<!DOCTYPE html>
<html>
<head>
<title>Brick Breaker</title>
<meta charset="UTF-8" />
<style>
#gameScreen {
border: 1px solid black;
}
img {
display: none;
}
</style>
</head>
<body>
<img id="img_ball" src="assets/images/ball.png">
<img id="img_brick" src="assets/images/brick.png">
<canvas id="gameScreen" width="800" height="600"></canvas>
<script src="src/main.js"></script>
</body>
</html>
@anshsansh Welcome to FCC (Free Code Camp). I thought this link might be useful to you.
A couple of things:
-
The script tag needs to include
type="module"
-
The paths on your import statements need to be relative to the path for main.js you use in the script tag. It appears you are keeping everything in the src directory, so your imports would be from “./”, such as:
import Game from "./game.js";
-
Notice how I added the js extension above. When using native JS modules you must include the entire filename, you can’t leave off the extension.
-
You refer to these files as Main.js, Paddle.js, and Game.js here but yet you are using lowercase versions in the import statement.
You can get a complete example of how to use native modules at: