From db9fdf59324d05c267e3612e35d4e9fe5e2c87ea Mon Sep 17 00:00:00 2001 From: s-prechtl Date: Tue, 13 Dec 2022 10:40:47 +0100 Subject: [PATCH] game loop --- frontend/game.ts | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/frontend/game.ts b/frontend/game.ts index 4eb9194..68ef7d0 100644 --- a/frontend/game.ts +++ b/frontend/game.ts @@ -14,10 +14,34 @@ function setup() { createCanvas(2000, 1000); obstacleOffset = width / 3; + setupGame(); +} + +function draw() { + background(backgroundImage) + raspberry.draw(); + raspberry.update(); + + obstacles.forEach((obstacle) => { + + obstacle.draw(); + obstacle.update(); + + checkObstacleReset(obstacle); + }); + + if (obstacles[0].collides(raspberry)) { + setupGame(); + } + obstacles[0].draw(); +} + +function setupGame() { raspberry = new Raspberry(); raspberry.image = raspberryImagePath; raspberry.showHitbox = true; + obstacles = []; obstacles.push(new Obstacle( new Position(width, 0), obstacleWidth, @@ -40,27 +64,8 @@ function setup() { obstacles.forEach((obstacle) => obstacle.resetPosition(false)); } -function draw() { - background(backgroundImage) - raspberry.draw(); - raspberry.update(); - - obstacles.forEach((obstacle) => { - - obstacle.draw(); - obstacle.update(); - - checkObstacleReset(obstacle); - }); - - if (obstacles[0].collides(raspberry)) { - obstacles[0].draw(); - console.log("SAMC") - } -} - -function checkObstacleReset(obstacle: Obstacle){ - if(obstacle.position.x < -obstacleWidth) { +function checkObstacleReset(obstacle: Obstacle) { + if (obstacle.position.x < -obstacleWidth) { obstacle.resetPosition(true); obstacles.shift(); obstacles.push(obstacle);