A simple game to practice Vanilla JavaScript and CSS
You can win the game by pulling all carrots in 10 seconds
http://kill-carrots-game.herokuapp.com/
- Node.js, HTML, CSS, Vanilla JavaScript(ES6)
- play, stop, restart game
STOP GAME: when stop button is clicked
WIN: If all carrots are clicked before the timer expires
GAME OVER: When the timer expires
- make play, stop, restart game button
- display carrots and bugs on the game field
- remove carrots / bugs on click
- play background music
- set a timer
- set a bug counter
- display Win / Lose
-
create a folder
-
npm init
-
node express install --save
-
git remote add origin [git repo HTTPS url]
git add . > discard all changes
-
git commit --allow-empty -m "initial commit"
-
git push origin master --force
-
clear web cash
=> Ctrl + F5 -
Cannot GET /favicon.ico error
no matter if you declared it or not, your browser will try to fetch favicon.ico at the root of your site to display it in your tab. You can add the code below to prevent auto-fetch.
=> -
let iconStart = document.querySelector('.fas fa-play'); // null
=> let iconStart = document.querySelector('.fa-play'); // work -
why does it work only at times?????
-
function die() has an issue...
-
field.onItemClick doesn't work!
=> click event binding issue -
game.js doesn't work!
=> when running it, the console on developer tools doesn't show any error and I can't even find the js file on source tap. -
how to share a variable 'started' that changes consistently between multiple .js files
8-1. declare the variable 'started' (in class)
8-2. declare a function (in class) that changes the variable 'started'
and call other .js file's (class) functions that set the changed variable 'started'
8-3. in other .js files, declare an empty variable 'started' and make a function that sets its value
8-4. now you can use the 'started' variable in multiple files and set its value whenever it changes by calling each file's (class's) setStarted function
import Field from './field.js';
export default class Game {
constructor() {
this.started = false;
this.field = new Field();
this.field.setClickListener(onItemClick);
}
onItemClick(item) {
if (item === 'carrot') {
updateCount();
} else if (item === 'bug') {
stopGame();
}
}
StartGame() {
changeStarted();
}
changeStarted() {
this.started = !this.started;
Field.this.setStarted(this.started);
}
}
export default class Field {
constructor() {
this.started;
this.field = document.querySelector('.field');
this.field.addEventListener('click', this.onClick);
this.carrot = document.querySelector('.carrot');
this.bug = document.querySelector('.bug');
}
setStared(started) {
this.started = started;
}
setClickListener(onItemClick) {
this.onItemClick = onItemClick;
}
onClick(event) => {
if (!started) {
return;
}
if (event.target.matches('.carrot')) {
event.target.remove();
this.onItemClick && this.onItemClick('carrot');
} else if (event.target.matches('.bug')) {
this.onItemClick && this.onItemClick('bug');
}
}
}
-
background: url(img/background.png) center/cover;
-
transition: all 300ms ease-in;
-
pop-up position: transform: translateY(-150%);
-
background-color: #00000090; => black with 90% opacity
make only the background-color has the opacity (not its children) -
make a random number between 2 numbers
function randomNumber(min, max) { return Math.random() \* (max - min) + min; } const x1 = 0; const y1 = 0; const x2 = fieldRect.width - CARROT_SIZE; const y2 = fieldRect.height - CARROT_SIZE; const x = randomNumber(x1, x2); const y = randomNumber(y1, y2); item.style.left = `${x}px`; item.style.top = `${y}px`;
-
setInterval for a specific time
let timer; -
event.target.className('carrot');
=> event.target.matches('.carrot');