-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
262 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const canvas = document.getElementById("canvas"); | ||
const ctx = canvas.getContext("2d"); | ||
const CANVAS_WIDTH = canvas.width; | ||
const CANVAS_HEIGHT = canvas.height; | ||
const ASPECT_RATIO = CANVAS_WIDTH / CANVAS_HEIGHT | ||
const GRAVITY = 1; | ||
const GROUND_HEIGHT = CANVAS_HEIGHT * 0.7; | ||
|
||
const jumpSprites = document.getElementsByClassName("jumpSprite"); | ||
let player = new Player({ x: 100, y: 100 }, 30, jumpSprites, null); | ||
|
||
const groundTexture = document.getElementById("ground"); | ||
const backgrounds = document.getElementsByClassName("background"); | ||
let world = new World(groundTexture, backgrounds, 200); | ||
|
||
let lastTime = performance.now(); | ||
let deltaTime = 1; | ||
let secondTimer = performance.now(); | ||
let avgFPSTime = 0; | ||
let frameCount = 0; | ||
let FPSText = "FPS: "; | ||
|
||
function mainLoop() { | ||
requestAnimationFrame(mainLoop); | ||
|
||
// Update | ||
player.update(); | ||
world.update(); | ||
|
||
// Draw | ||
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | ||
|
||
world.draw(); | ||
player.draw(); | ||
|
||
updateFPS(); | ||
} | ||
|
||
function updateFPS() { | ||
let currentTime = performance.now(); | ||
deltaTime = (currentTime - lastTime) / 1000.0; | ||
lastTime = currentTime; | ||
avgFPSTime += deltaTime; | ||
frameCount++; | ||
|
||
if (performance.now() - secondTimer > 1000) { | ||
FPSText = "FPS: " + (1.0 / (avgFPSTime / frameCount)).toFixed(2); | ||
secondTimer = performance.now(); | ||
frameCount = 0; | ||
avgFPSTime = 0; | ||
} | ||
ctx.fillText(FPSText, 10, 10); | ||
} | ||
|
||
function changeBackground() { | ||
world.alternateBackground() | ||
} | ||
|
||
mainLoop(); | ||
setInterval(changeBackground, Math.random() * 5000 + 5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
let pressed_space = false; | ||
|
||
class Player { | ||
constructor(pos, speed, jumpingSprites, walkingSprites) { | ||
this.pos = pos; | ||
this.speed = speed; | ||
this.velocity_y = 0; | ||
this.is_in_air = true; | ||
this.jump_height = 20; | ||
this.scale = 0.7; | ||
this.current_sprite = jumpingSprites[0]; | ||
this.jumpingSprites = jumpingSprites; | ||
this.walkingSprites = walkingSprites; | ||
} | ||
|
||
draw() { | ||
ctx.drawImage( | ||
this.current_sprite, | ||
this.pos.x - (this.current_sprite.width / 2) * this.scale, | ||
this.pos.y - this.current_sprite.height * this.scale, | ||
this.current_sprite.width * this.scale, | ||
this.current_sprite.height * this.scale | ||
); | ||
} | ||
|
||
update() { | ||
this.process_input(); | ||
this.pos.y -= this.velocity_y * deltaTime * this.speed; | ||
this.velocity_y -= GRAVITY * deltaTime * this.speed; | ||
if (this.pos.y >= GROUND_HEIGHT) { | ||
this.pos.y = GROUND_HEIGHT; | ||
this.is_in_air = false; | ||
this.velocity_y = 0; | ||
} | ||
|
||
if (this.velocity_y == 0) { | ||
this.current_sprite = this.jumpingSprites[0]; | ||
} else if (Math.abs(this.velocity_y) < 9) { | ||
this.current_sprite = this.jumpingSprites[3]; | ||
} else if (Math.abs(this.velocity_y) < 16) { | ||
this.current_sprite = this.jumpingSprites[2]; | ||
} else { | ||
this.current_sprite = this.jumpingSprites[1]; | ||
} | ||
} | ||
|
||
process_input() { | ||
if (pressed_space) { | ||
if (!this.is_in_air) { | ||
player.is_in_air = true; | ||
player.velocity_y += this.jump_height; | ||
} | ||
} | ||
} | ||
} | ||
|
||
document.body.addEventListener("keydown", keyDown); | ||
document.body.addEventListener("keyup", keyUp); | ||
|
||
function keyDown(event) { | ||
let key = event.key.toLowerCase(); | ||
if (key == " ") { | ||
pressed_space = true; | ||
} | ||
} | ||
|
||
function keyUp(event) { | ||
let key = event.key.toLowerCase(); | ||
if (key == " ") { | ||
pressed_space = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
class Tile { | ||
constructor(x1, x2) { | ||
this.x1 = x1; | ||
this.x2 = x2; | ||
} | ||
} | ||
|
||
class World { | ||
constructor(groundTexture, backgrounds, groundScrollSpeed) { | ||
this.tiles = []; | ||
this.minWidthFactor = 0.7; | ||
this.randWidthFactor = 1.0 - this.minWidthFactor; | ||
this.holeWidth = 100; | ||
this.groundOffset = 30; | ||
this.groundScrollSpeed = groundScrollSpeed; | ||
this.groundTex = groundTexture; | ||
this.currentBgIndex = 0 | ||
this.lastBgIndex = null | ||
this.bgAlpha = 1.0 | ||
this.transitionSpeed = 0.3 | ||
this.backgrounds = backgrounds; | ||
//this.groundTex.onload = function () { | ||
// th | ||
//} | ||
} | ||
|
||
update() { | ||
// Generate or delete ground tiles | ||
this.randWidthFactor = 1.0 - this.minWidthFactor; | ||
if (this.tiles.length != 0) { | ||
if (this.tiles[0].x2 < 0) this.tiles.shift(); | ||
} | ||
while (this.tiles.length < 7) { | ||
let last_tile_x = null; | ||
if (this.tiles.length == 0) { | ||
last_tile_x = -this.holeWidth; | ||
} else { | ||
last_tile_x = this.tiles[this.tiles.length - 1].x2; | ||
} | ||
let new_tile_x1 = last_tile_x + this.holeWidth; | ||
let new_tile_x2 = | ||
new_tile_x1 + | ||
Math.min( | ||
this.minWidthFactor * this.groundTex.width + | ||
Math.random() * this.groundTex.width * this.randWidthFactor, | ||
this.groundTex.width | ||
); | ||
let new_tile = new Tile(new_tile_x1, new_tile_x2); | ||
this.tiles.push(new_tile); | ||
} | ||
|
||
// Move the ground tiles | ||
for (let i = 0; i < this.tiles.length; i++) { | ||
this.tiles[i].x1 -= deltaTime * this.groundScrollSpeed; | ||
this.tiles[i].x2 -= deltaTime * this.groundScrollSpeed; | ||
} | ||
|
||
if (this.lastBgIndex != null) { | ||
this.bgAlpha = Math.min(1.0, this.bgAlpha + deltaTime * this.transitionSpeed) | ||
if (this.bgAlpha == 1.0) { | ||
this.lastBgIndex = null | ||
} | ||
} | ||
} | ||
|
||
draw() { | ||
if (this.lastBgIndex != null) { | ||
let currentBg = this.backgrounds[this.currentBgIndex]; | ||
let lastBg = this.backgrounds[this.lastBgIndex]; | ||
ctx.globalAlpha = 1.0 - this.bgAlpha | ||
ctx.drawImage(lastBg, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT) | ||
ctx.globalAlpha = this.bgAlpha | ||
ctx.drawImage(currentBg, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT) | ||
ctx.globalAlpha = 1.0 | ||
} else { | ||
let bg = this.backgrounds[this.currentBgIndex]; | ||
ctx.drawImage(bg, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT) | ||
} | ||
|
||
for (let i = 0; i < this.tiles.length; i++) { | ||
let tile = this.tiles[i]; | ||
let texWidth = tile.x2 - tile.x1; | ||
let texHeight = this.groundTex.height; | ||
ctx.drawImage( | ||
this.groundTex, | ||
0, | ||
0, | ||
texWidth, | ||
texHeight, | ||
tile.x1, | ||
GROUND_HEIGHT - this.groundOffset, | ||
texWidth, | ||
texHeight | ||
); | ||
} | ||
} | ||
|
||
alternateBackground() { | ||
if (this.lastBgIndex == null) { | ||
this.bgAlpha = 0.0 | ||
this.lastBgIndex = this.currentBgIndex | ||
this.currentBgIndex = Math.floor(Math.random() * this.backgrounds.length) | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.