Compare commits
25 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 832afc1128 | |||
| 4f3897bfaf | |||
| d4228c60f3 | |||
| 7b9529bddb | |||
| ba91f1ec50 | |||
| debc9495ae | |||
| f4763e711c | |||
| 788fc2dfe1 | |||
| 07ebca0834 | |||
| 52e223f2dd | |||
| 60aee72af3 | |||
| 3e524968f3 | |||
| af22f0908a | |||
| 1ae0e8b169 | |||
| 64ac0bbebb | |||
| 2328a7251a | |||
| b56530bf92 | |||
| 011f285bf3 | |||
| 2bf6cbe968 | |||
| 1075422a1d | |||
| 5a682640c6 | |||
| 1a6f07e84f | |||
| 6a3ce486df | |||
| 32bb31ecf1 | |||
| 6b01a0272e |
9 changed files with 229 additions and 25 deletions
|
|
@ -11,9 +11,31 @@
|
|||
<link rel="shortcut icon" type="image/png" href="/favicon.ico" />
|
||||
<link rel="stylesheet" href="./main.css" />
|
||||
<link rel="stylesheet" href="../utils/loading/loading.css" />
|
||||
<style>
|
||||
:host, :root{
|
||||
--mnswpr-transition: 10s ease-in-out;
|
||||
}
|
||||
|
||||
nav {
|
||||
a {
|
||||
color: white;
|
||||
text-decoration-color: orange;
|
||||
transition: 500ms ease-in-out;
|
||||
|
||||
&:hover {
|
||||
text-decoration-thickness: 2px;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="body-wrapper">
|
||||
<nav>
|
||||
<a target="_blank" href="https://npmx.dev/package/@ayo-run/mnswpr">npm</a>
|
||||
·
|
||||
<a target="_blank" href="https://github.com/ayo-run/mnswpr">github</a>
|
||||
</nav>
|
||||
<div id="app">
|
||||
Please use Chrome or Firefox.
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { LoadingService } from '../utils/'
|
|||
import { LeaderBoardService } from './modules/leader-board/leader-board.js'
|
||||
|
||||
const leaderBoardService = new LeaderBoardService()
|
||||
const loadingService = new LoadingService()
|
||||
|
||||
const version = import.meta.env.MODE === 'development'
|
||||
? 'dev'
|
||||
|
|
@ -12,7 +13,6 @@ const version = import.meta.env.MODE === 'development'
|
|||
|
||||
const initializeGameBoard = async (level) => {
|
||||
const prevousLeaderBoard = document.getElementById('leaderboard')
|
||||
const loadingService = new LoadingService()
|
||||
const loadingWrapper = document.createElement('div')
|
||||
loadingWrapper.id = 'loading-wrapper'
|
||||
loadingService.addLoading(loadingWrapper)
|
||||
|
|
@ -38,5 +38,5 @@ const game = new mnswpr('app', version, {
|
|||
levelChanged: (level) => initializeGameBoard(level),
|
||||
gameDone: (game) => sendGameResult(game)
|
||||
})
|
||||
game.initialize()
|
||||
|
||||
game.initialize()
|
||||
|
|
|
|||
|
|
@ -4,6 +4,11 @@
|
|||
"description": "the mnswpr.com web app",
|
||||
"private": true,
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"build:preview": "npm run build && npm run preview"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ayo-run/mnswpr": "workspace:*",
|
||||
"firebase": "^12.11.0"
|
||||
|
|
|
|||
24
lib/LICENSE
Normal file
24
lib/LICENSE
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Ayo Ayco
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
157
lib/README.md
Normal file
157
lib/README.md
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
# Build your own web browser game with `mnswpr`
|
||||
|
||||
Have you ever wondered how games on a web browser are built? Believe it or not, anything you see on a web browser can be built by anyone. That's why the web is so great: it is free and open for everyone to enjoy!
|
||||
|
||||
In this guide, we will use **mnswpr** as a simple building block for you to build your own browser game. I will walk you through the steps to create your own Minesweeper browser game from scratch.
|
||||
|
||||
If you want to skip to the ending, all the code are in this repository: [minimal-mnswpr](https://github.com/ayo-run/minimal-mnswpr)
|
||||
|
||||
First, let's go through the requirements.
|
||||
|
||||
## Requirements
|
||||
|
||||
It is assumed that you have some knowledge in HTML and JavaScript. You can easily read about this and play around examples online. Some knowledge on using a terminal and a text editor is also required.
|
||||
|
||||
You will need a computer with [node.js](https://nodejs.org/en/download).
|
||||
|
||||
If you are familiar with HTML and JavaScript, and has a computer with `node.js` installed... let's now start with the project setup!
|
||||
|
||||
## Project Setup
|
||||
|
||||
Open the terminal and confirm that you have `node.js`.
|
||||
|
||||
```bash
|
||||
# verify the node.js version
|
||||
node --version # Should print the version
|
||||
```
|
||||
Next, create a directory where we'll write some code for your game.
|
||||
|
||||
```bash
|
||||
# on mac or linux
|
||||
mkdir my-game
|
||||
|
||||
cd my-game
|
||||
```
|
||||
Once your terminal is in the new directory `my-game`, we will initialize the JavaScript project using the Node Package Manager or `npm`. Type the following on your terminal:
|
||||
|
||||
```bash
|
||||
npm init
|
||||
```
|
||||
|
||||
This will start the `npm` initialization interface, which will ask you some questions. You can think of what you want to answer, but if you want to go with the defaults, you can just press the `Enter` key repeatedly for each until the questions are done.
|
||||
|
||||
The last question will ask you if everything is OK:
|
||||
|
||||
```bash
|
||||
Is this OK? (yes)
|
||||
|
||||
# Don't be shy, you can just press ENTER again
|
||||
```
|
||||
|
||||
Next, we will add `vite` as a development tool for bundling and as a development server.
|
||||
|
||||
<details>
|
||||
<summary>Additional info on Vite...</summary>
|
||||
Making web pages work in different browsers often brings challenges brought about by differences in technological implementations and limitations. Vite helps us so that our code will work in different environments without us worrying about issues in compatibility and performance. </details><br />
|
||||
|
||||
```bash
|
||||
npm i -D vite
|
||||
```
|
||||
|
||||
Now that the JS project is initialized and we have a development environment with `vite`, we will install **mnswpr** as a dependency:
|
||||
|
||||
```bash
|
||||
npm i @ayo-run/mnswpr
|
||||
```
|
||||
|
||||
Finally, you can run the installed `vite` dev server by running the following:
|
||||
|
||||
```bash
|
||||
# `npx` here is the execute command for npm
|
||||
npx vite # will run the vite dev server
|
||||
```
|
||||
|
||||
Vite will now show the address you can type to your browser to see your project. It will show something like this:
|
||||
|
||||
```bash
|
||||
VITE v8.0.3 ready in 128 ms
|
||||
|
||||
➜ Local: http://localhost:5173/
|
||||
➜ Network: use --host to expose
|
||||
➜ press h + enter to show help
|
||||
```
|
||||
|
||||
You can then open the "Local" address (e.g., http://localhost:5173) on your browser.
|
||||
|
||||
Congratulations. You now have your project setup! It's time to write some code.
|
||||
|
||||
## Write Some Code
|
||||
|
||||
Believe it or not, you have done the hard part. Now we start the fun part: putting the parts of your game together!
|
||||
|
||||
There are mainly 3 kinds of code that work together in a web page: HTML, JavaScript or JS, and Cascading Style Sheets or CSS.
|
||||
|
||||
In this guide, we work mostly with HTML & JS to focus on the basics.
|
||||
|
||||
### The HTML
|
||||
|
||||
Using your favorite text editor, create a file named `index.html` with the following content:
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>My Minesweeper Game</title>
|
||||
<style>
|
||||
html, body {
|
||||
background-color: black;
|
||||
color: white;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>My Minesweeper Game</h1>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Additional info on `index.html`</summary>
|
||||
The file name `index.html` is important. It is the default file that Web browsers look for in any given path/directory as the web page it will show.
|
||||
</details>
|
||||
<br />
|
||||
|
||||
If you have your browser opened to the Local address `vite` just showed earlier, you should see your very first web page with a title **My Minesweeper Game**
|
||||
|
||||
Exciting right? You can try editing the text inside `<h1>...</h1>` to see the web page change as well. :)
|
||||
|
||||
Take a second to read through the content of your `index.html`. The `<div>` element there with `id="app"` attribute will be where the game board will be rendered.
|
||||
|
||||
Now we just need JavaScript to do this. You will find the `<script>` tag that has the `src="main.js"` attribute, which means the web page is ready to load that JavaScript... but this file doesn't exist yet. So let's write the code for that.
|
||||
|
||||
### The JavaScript
|
||||
|
||||
Create a new file named `main.js` with the following content:
|
||||
|
||||
```js
|
||||
/**
|
||||
* main.js
|
||||
*/
|
||||
import '@ayo-run/mnswpr/mnswpr.css'
|
||||
import mnswpr from '@ayo-run/mnswpr'
|
||||
|
||||
const game = new mnswpr('app')
|
||||
game.initialize()
|
||||
```
|
||||
|
||||
When you create this `main.js` file, the dev server will instantly update the web page for you and you should now see your minesweeper browser game!
|
||||
|
||||
|
||||
---
|
||||
|
||||
_Just keep building._<br>
|
||||
_A project by [Ayo](https://ayo.ayco.io)_
|
||||
|
|
@ -95,19 +95,8 @@ const Minesweeper = function(appId, version, hooks = undefined) {
|
|||
if(appElement) {
|
||||
appElement.innerHTML = ''
|
||||
appElement.append(headingElement, gameBoard)
|
||||
appElement.append(initializeSourceLink())
|
||||
}
|
||||
generateGrid(true)
|
||||
}
|
||||
|
||||
function initializeSourceLink() {
|
||||
const sourceLink = document.createElement('a')
|
||||
sourceLink.href = 'https://github.com/ayoayco/mnswpr'
|
||||
sourceLink.innerText = 'Source code'
|
||||
sourceLink.target = '_blank'
|
||||
sourceLink.style.color = 'white'
|
||||
|
||||
return sourceLink
|
||||
generateGrid({ initial: true })
|
||||
}
|
||||
|
||||
function initializeFootbar() {
|
||||
|
|
@ -182,11 +171,17 @@ const Minesweeper = function(appId, version, hooks = undefined) {
|
|||
function updateSetting(key) {
|
||||
setting = levels[key]
|
||||
storageService.saveToLocal('setting', setting)
|
||||
generateGrid(true)
|
||||
generateGrid({ initial: true })
|
||||
}
|
||||
|
||||
|
||||
function generateGrid(initial = false) {
|
||||
/**
|
||||
* Generate the Game Board
|
||||
* @param {{
|
||||
* initial: boolean
|
||||
* }} options - Game Board Options
|
||||
*/
|
||||
function generateGrid(options = { initial: false }) {
|
||||
firstClick = true
|
||||
grid.innerHTML = ''
|
||||
grid.oncontextmenu = () => false
|
||||
|
|
@ -223,7 +218,7 @@ const Minesweeper = function(appId, version, hooks = undefined) {
|
|||
* TODO: add hook afterGridGenerated
|
||||
* - for initializing the leaderboard
|
||||
*/
|
||||
if (initial)
|
||||
if (options.initial)
|
||||
hooks.levelChanged(setting)
|
||||
|
||||
timerService.initialize(timerDisplay)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@ayo-run/mnswpr",
|
||||
"version": "0.4.27",
|
||||
"version": "0.4.31",
|
||||
"description": "Classic Minesweeper browser game",
|
||||
"author": "Ayo",
|
||||
"type": "module",
|
||||
|
|
@ -8,9 +8,9 @@
|
|||
"type": "git",
|
||||
"url": "https://github.com/ayo-run/mnswpr"
|
||||
},
|
||||
"homepage": "https://mnswpr.com",
|
||||
"scripts": {
|
||||
"release": "bumpp && node ../scripts/release.js",
|
||||
"build:release": "npm run build:lib && npm run release"
|
||||
"release": "bumpp && node ../scripts/release.js"
|
||||
},
|
||||
"main": "mnswpr.js",
|
||||
"exports": {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { defineConfig } from 'vite'
|
|||
export default defineConfig({
|
||||
build: {
|
||||
lib: {
|
||||
entry: resolve(import.meta.dirname, './lib/mnswpr.js'),
|
||||
entry: resolve(import.meta.dirname, './mnswpr.js'),
|
||||
name: 'mnswpr',
|
||||
fileName: 'mnswpr'
|
||||
}
|
||||
|
|
@ -9,15 +9,16 @@
|
|||
"type": "git",
|
||||
"url": "https://github.com/ayo-run/mnswpr"
|
||||
},
|
||||
"homepage": "https://mnswpr.com",
|
||||
"scripts": {
|
||||
"test": "echo \"Warn: no test specified\"",
|
||||
"dev": "vite app",
|
||||
"start": "vite app",
|
||||
"build": "vite build app",
|
||||
"preview": "vite preview app",
|
||||
"build:preview": "npm run build && npm run preview",
|
||||
"build:lib": "vite build lib --config vite-lib.config.js",
|
||||
"release": "pnpm run build:lib && pnpm -F @ayo-run/mnswpr release",
|
||||
"build:lib": "vite build lib",
|
||||
"release:lib": "pnpm -F @ayo-run/mnswpr run release",
|
||||
"publish:lib": "cd lib && npm publish",
|
||||
"build:preview": "pnpm -F app run build:preview",
|
||||
"prepare": "husky",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix"
|
||||
|
|
|
|||
Loading…
Reference in a new issue