game loop
This commit is contained in:
parent
e0e7f5bb60
commit
db9fdf5932
1 changed files with 26 additions and 21 deletions
|
|
@ -14,10 +14,34 @@ function setup() {
|
||||||
createCanvas(2000, 1000);
|
createCanvas(2000, 1000);
|
||||||
obstacleOffset = width / 3;
|
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 = new Raspberry();
|
||||||
raspberry.image = raspberryImagePath;
|
raspberry.image = raspberryImagePath;
|
||||||
raspberry.showHitbox = true;
|
raspberry.showHitbox = true;
|
||||||
|
|
||||||
|
obstacles = [];
|
||||||
obstacles.push(new Obstacle(
|
obstacles.push(new Obstacle(
|
||||||
new Position(width, 0),
|
new Position(width, 0),
|
||||||
obstacleWidth,
|
obstacleWidth,
|
||||||
|
|
@ -40,27 +64,8 @@ function setup() {
|
||||||
obstacles.forEach((obstacle) => obstacle.resetPosition(false));
|
obstacles.forEach((obstacle) => obstacle.resetPosition(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function checkObstacleReset(obstacle: Obstacle) {
|
||||||
background(backgroundImage)
|
if (obstacle.position.x < -obstacleWidth) {
|
||||||
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) {
|
|
||||||
obstacle.resetPosition(true);
|
obstacle.resetPosition(true);
|
||||||
obstacles.shift();
|
obstacles.shift();
|
||||||
obstacles.push(obstacle);
|
obstacles.push(obstacle);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue