Skip to content

Commit

Permalink
game machine 6 improvements (#109)
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
r4pt0s authored Oct 7, 2024
2 parents 87bf32c + 201463c commit fe6e7f0
Showing 1 changed file with 62 additions and 24 deletions.
86 changes: 62 additions & 24 deletions src/interactions/map_arcade/game_machine_6.interactions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { scaleFactor } from '../../constants';
import { displayDialogue } from '../../utils';

export const interactionWithGameMachine6 = (player, k, map) => {
Expand Down Expand Up @@ -92,12 +91,12 @@ function startChromeDinoGame(k) {
const JUMP_FORCE = 800;
const SPEED = 480;
const GRAVITY = 1600;

let trees = []; // Store references to trees
let score = 0;
// Set up the game scene
k.scene('dinoGame', () => {
//gravity for dino
k.setGravity(GRAVITY);

//load a sprite name dino with run animation
k.loadSprite('dino', './assets/sprites/doux.png', {
sliceX: 24,
Expand All @@ -110,10 +109,10 @@ function startChromeDinoGame(k) {
//add the dino to screen
const dino = k.add([
k.sprite('dino', { anim: 'run' }),
k.pos(80, 200),
k.pos(10, k.height() - 300),
k.area(),
k.body(),
k.scale(scaleFactor),
k.scale(1),
]);

//pressing the spacebar lets the dino jump
Expand All @@ -123,31 +122,44 @@ function startChromeDinoGame(k) {
}
});

//pressing the esc lets player leave game
k.onKeyPress('escape', () =>
import('../../scenes/arcade').then((_) => {
k.setGravity(0);
k.go('arcade');
})
);

//add platform
k.add([
const floor = k.add([
k.rect(k.width(), FLOOR_HEIGHT),
k.area(),
k.outline(4),
k.pos(0, k.height() - 250),
k.anchor('botleft'),
k.outline(4),
k.area(),
k.body({ isStatic: true }),
k.color(127, 200, 255),
'floor',
]);

//spawn trees with different heights at different intervals
function spawnTree() {
k.add([
k.rect(48, k.rand(48, 72)),
const scaleFactor = k.width() / k.height();
const width = k.width() / k.height() < 1 ? 0.5 : 1;
const height = k.width() / k.height() < 1 ? 0.5 : 1;
const tree = k.add([
k.rect(48 * width, k.rand(48, 72) * height),
k.area(),
k.outline(4),
k.pos(k.width() - 10, k.height() - 500),
k.pos(k.width() - 10, k.height() - 300),
k.anchor('botleft'),
k.body(),
k.color(255, 180, 255),
k.move(k.LEFT, SPEED),
k.move(k.LEFT, SPEED * scaleFactor + score / 10),
k.offscreen({ destroy: true }),
'tree',
]);

trees.push(tree);
k.wait(k.rand(1.5, 3), () => {
spawnTree();
});
Expand All @@ -162,13 +174,23 @@ function startChromeDinoGame(k) {
});

// keep track of score
let score = 0;

const scoreLabel = k.add([k.text(score), k.pos(68, 100)]);
// increment score every frame
k.onUpdate(() => {
score++;
scoreLabel.text = score;
});
// Handle screen resize
k.onResize(() => {
const scaleFactor = k.width() / k.height();
if (scaleFactor < 1) {
dino.scaleTo(1);
} else {
dino.scaleTo(scaleFactor);
}
floor.width = k.width();
});
});

//set up lose screen
Expand All @@ -184,18 +206,34 @@ function startChromeDinoGame(k) {
k.anchor('center'),
]);

// go back to game with space is pressed
k.onKeyPress('space', () =>
import('../../scenes/arcade').then((_) => {
k.go('arcade');
})
);
k.onClick(() =>
// Add "Play Again" button
const playAgainButton = k.add([
k.text('Play Again'),
k.pos(k.width() / 2, k.height() / 2 + 140),
k.scale(1),
k.area(),
k.anchor('center'),
]);

// Add "Play Again" button
const exitButton = k.add([
k.text('Exit'),
k.pos(k.width() / 2, k.height() / 2 + 200),
k.scale(1),
k.area(),
k.anchor('center'),
]);

// When the button is clicked or space is pressed, restart the game
playAgainButton.onClick(() => {
startChromeDinoGame(k); // Restart the game
});

exitButton.onClick(() => {
import('../../scenes/arcade').then((_) => {
k.go('arcade');
})
);
k.setGravity(0);
});
});
});

// Start the game scene
Expand Down

0 comments on commit fe6e7f0

Please sign in to comment.