This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
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.
Format, ease game's difficulty, update readme, remove fake P key
Allow multiple letters to appear at once, increase vowels probability to drop, decrease letters probability to drop from .05 to .03, fix leaderboard not displayed yes when the word was found
- Loading branch information
Showing
10 changed files
with
294 additions
and
263 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
# HangSnake | ||
This is my little gift for my girlfriend to play some old games her younger self liked very much. | ||
|
||
Enjoy! | ||
Enjoy! | ||
|
||
## Note | ||
You need to run this from a server. I'm using the plugin "Live Server" to run & dispose of a server from Visual Studio Code anytime I need it. | ||
|
||
## To do | ||
If I've got some spare time, I might think to properly implement a pause button using the P key (yes, only setting the `inputDirection` back to `{ x: 0, y: 0 }` is not enough). I'd also want to get rid of those `<div>` generation and use a `<canvas>`. Also want to display a real snake instead of blocks. But this will be for a different story! |
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 |
---|---|---|
@@ -1,67 +1,74 @@ | ||
import { draw as drawSnake, update as updateSnake, getSpeed, getSnakeHead, snakeIntersection } from './snake.js'; | ||
import { | ||
draw as drawSnake, | ||
update as updateSnake, | ||
getSpeed, | ||
getSnakeHead, | ||
snakeIntersection, | ||
} from './snake.js'; | ||
import { draw as drawFood, update as updateFood } from './food.js'; | ||
import { draw as drawPoints, getPoints } from './points.js'; | ||
import { draw as drawHangman, hasFoundTheWord } from './hangman.js'; | ||
import { outsideGrid } from './grid.js'; | ||
|
||
const gameboard = document.getElementById("gameboard"); | ||
const pointsboard = document.getElementById("pointsboard"); | ||
const hangmanboard = document.getElementById("hangmanboard"); | ||
const lettersboard = document.getElementById("lettersboard"); | ||
const leaderboard = document.getElementById("leaderboard"); | ||
const gameboard = document.getElementById('gameboard'); | ||
const pointsboard = document.getElementById('pointsboard'); | ||
const hangmanboard = document.getElementById('hangmanboard'); | ||
const lettersboard = document.getElementById('lettersboard'); | ||
const leaderboard = document.getElementById('leaderboard'); | ||
let lastRenderTime = 0; | ||
let gameOver = false; | ||
|
||
function main(currentTime) { | ||
const hasFoundIt = hasFoundTheWord(); | ||
if (gameOver || hasFoundIt) { | ||
if (hasFoundIt) { | ||
document.getElementById("app").display = "none"; | ||
document.getElementById("victory").display = "flex"; | ||
} | ||
const hasFoundIt = hasFoundTheWord(); | ||
if (gameOver || hasFoundIt) { | ||
if (hasFoundIt) { | ||
document.getElementById('app').display = 'none'; | ||
document.getElementById('victory').display = 'flex'; | ||
} | ||
|
||
const profile = prompt("What is your name?") || "Unknown"; | ||
const profileEntry = { points: getPoints(), wordFound: hasFoundTheWord() }; | ||
window.localStorage.setItem(profile, JSON.stringify(profileEntry)) | ||
const profile = prompt('What is your name?') || 'Unknown'; | ||
const profileEntry = { points: getPoints(), wordFound: hasFoundTheWord() }; | ||
window.localStorage.setItem(profile, JSON.stringify(profileEntry)); | ||
|
||
if (confirm(`${(hasFoundIt ? "You won!" : "You lost.")} Press ok to restart.`)) { | ||
window.location = "/"; | ||
} | ||
return; | ||
if ( | ||
confirm(`${hasFoundIt ? 'You won!' : 'You lost.'} Press ok to restart.`) | ||
) { | ||
window.location = '/'; | ||
} | ||
return; | ||
} | ||
|
||
window.requestAnimationFrame(main); | ||
|
||
window.requestAnimationFrame(main); | ||
|
||
const secondsSinceLastRender = (currentTime - lastRenderTime) / 1000; | ||
if (secondsSinceLastRender < 1/getSpeed()) return; | ||
const secondsSinceLastRender = (currentTime - lastRenderTime) / 1000; | ||
if (secondsSinceLastRender < 1 / getSpeed()) return; | ||
|
||
lastRenderTime = currentTime; | ||
update(); | ||
draw(); | ||
lastRenderTime = currentTime; | ||
update(); | ||
draw(); | ||
} | ||
|
||
window.requestAnimationFrame(main); | ||
|
||
function update() { | ||
updateSnake(); | ||
updateFood(); | ||
checkDeath(); | ||
updateSnake(); | ||
updateFood(); | ||
checkDeath(); | ||
} | ||
|
||
function draw() { | ||
gameboard.innerHTML = ""; | ||
pointsboard.innerHTML = ""; | ||
hangmanboard.innerHTML = ""; | ||
lettersboard.innerHTML = ""; | ||
leaderboard.innerHTML = ""; | ||
gameboard.innerHTML = ''; | ||
pointsboard.innerHTML = ''; | ||
hangmanboard.innerHTML = ''; | ||
lettersboard.innerHTML = ''; | ||
leaderboard.innerHTML = ''; | ||
|
||
drawSnake(gameboard); | ||
drawFood(gameboard); | ||
drawPoints(pointsboard); | ||
drawHangman(hangmanboard, lettersboard, leaderboard); | ||
drawSnake(gameboard); | ||
drawFood(gameboard); | ||
drawPoints(pointsboard); | ||
drawHangman(hangmanboard, lettersboard, leaderboard); | ||
} | ||
|
||
function checkDeath() { | ||
gameOver = outsideGrid(getSnakeHead()) | ||
|| snakeIntersection(); | ||
gameOver = outsideGrid(getSnakeHead()) || snakeIntersection(); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,17 @@ | ||
const GRID_SIZE = 25; | ||
|
||
export function getRandomGridPosition() { | ||
return { | ||
x: Math.floor(Math.random() * GRID_SIZE) + 1, | ||
y: Math.floor(Math.random() * GRID_SIZE) + 1 | ||
} | ||
return { | ||
x: Math.floor(Math.random() * GRID_SIZE) + 1, | ||
y: Math.floor(Math.random() * GRID_SIZE) + 1, | ||
}; | ||
} | ||
|
||
export function outsideGrid(position) { | ||
return position.x < 1 || position.x > GRID_SIZE | ||
|| position.y < 1 || position.y > GRID_SIZE; | ||
return ( | ||
position.x < 1 || | ||
position.x > GRID_SIZE || | ||
position.y < 1 || | ||
position.y > GRID_SIZE | ||
); | ||
} |
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
Oops, something went wrong.