Skip to content

Commit 9268893

Browse files
committed
11-12, switchscreen works
1 parent ec23435 commit 9268893

File tree

4 files changed

+55
-25
lines changed

4 files changed

+55
-25
lines changed

index.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111
</head>
1212

1313
<body id="body">
14-
15-
14+
<canvas id="canvas"></canvas>
15+
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
16+
<script>
17+
WebFont.load({
18+
custom:{
19+
families: ['Minecraft']
20+
}
21+
});
22+
</script>
1623

1724

1825

src/GameScreen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class GameScreen{
2828
alignment: CanvasTextAlign = "center",
2929
color: string = "blue",
3030
) {
31-
this.ctx.font = `${fontSize}px Minecraft`;
32-
this.ctx.fillStyle = color;
33-
this.ctx.textAlign = alignment;
34-
this.ctx.fillText(text, xCoordinate, yCoordinate);
31+
ctx.font = `${fontSize}px Minecraft`;
32+
ctx.fillStyle = color;
33+
ctx.textAlign = alignment;
34+
ctx.fillText(text, xCoordinate, yCoordinate);
3535
}
3636
}

src/game.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,50 @@ class Game {
2222
this.ctx = this.canvas.getContext('2d');
2323

2424
this.keyboardListener = new KeyboardListener();
25-
this.currentScreen = new SchoolParty(this.game);
25+
this.currentScreen = new StartScreen(this.game);
26+
27+
this.loop();
2628
}
2729

28-
public switchScreen(newScreen: GameScreen) {
29-
// If the current screen is an instance of the StartScreen class
30-
// Basically: if the current screen is the start screen
31-
// And the user pressed "s", render the level screen
32-
if (
33-
this.currentScreen instanceof StartScreen
34-
&& this.keyboardListener.isKeyDown(KeyboardListener.KEY_S)
35-
) {
36-
this.currentScreen = new SchoolParty(this.game);
37-
}
30+
private loop = () => {
31+
// this.switchScreen()
32+
33+
// Clear the canvas
34+
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
35+
// Let the current screen draw itself on the rendering context
36+
this.currentScreen.draw(this.ctx);
37+
38+
// Request the next animation frame
39+
requestAnimationFrame(this.loop);
40+
41+
3842
}
3943

44+
// private switchScreen() {
45+
// // If the current screen is an instance of the StartScreen class
46+
// // Basically: if the current screen is the start screen
47+
// // And the user pressed "s", render the level screen
48+
// if (
49+
// this.currentScreen instanceof StartScreen
50+
// && this.keyboardListener.isKeyDown(KeyboardListener.KEY_S)
51+
// ) {
52+
// this.currentScreen = new LevelScreen(this.canvas, this.ctx, this.keyboardListener);
53+
// }
54+
55+
// if (
56+
// this.currentScreen instanceof LevelScreen
57+
// && this.keyboardListener.isKeyDown(KeyboardListener.KEY_ESC)
58+
// ) {
59+
// this.currentScreen = new TitleScreen(this.canvas, this.ctx);
60+
// }
61+
// }
62+
4063

4164
}
4265

4366
// This will get an HTML element. I cast this element in de appropriate type using <>
4467
let init = () => {
45-
const Asteroids = new Game(document.getElementById("canvas") as HTMLCanvasElement);
68+
const You = new Game(document.getElementById("canvas") as HTMLCanvasElement);
4669
};
4770

4871
// Add EventListener to load the game whenever the browser is ready

src/startScreen.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class StartScreen extends GameScreen {
1919
}
2020
}
2121

22-
public adjust(game: Game) {
23-
if (this.shouldStartLevel) {
24-
game.switchScreen(new SchoolParty(game));
25-
}
26-
}
22+
// public adjust(game: Game) {
23+
// if (this.shouldStartLevel) {
24+
// game.switchScreen();
25+
// }
26+
// }
2727

2828
public draw(ctx: CanvasRenderingContext2D) {
29-
this.writeTextToCanvas(ctx, "You", 140, this.canvas.width/2, 200,);
30-
this.writeTextToCanvas(ctx, "PRESS ENTER TO PLAY", 30, this.canvas.width/2, 400, )
29+
this.writeTextToCanvas(ctx, "You", 140, 500, 200,);
30+
this.writeTextToCanvas(ctx, "PRESS ENTER TO PLAY", 30, 500, 400, )
3131
}
3232
}
3333

0 commit comments

Comments
 (0)