Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Piano_Playing_Game Added #635

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Games/Piano_Playing_Game/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

.eslintcache

npm-debug.log*
yarn-debug.log*
yarn-error.log*
21 changes: 21 additions & 0 deletions src/Games/Piano_Playing_Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# README

## Setup

Getting started with this project is very easy. Run:

```
npm install
npm start
```

If you get a `react-scripts start` error, then remove the `node_modules`
folder and re-run the two commands in the code block.

## Demo

#### Piano UI
<div class="flex flex-row">
<img src="./img/piano_0.png" width="400">
<img src="./img/piano_2.png" width="400">
</div>
Binary file added src/Games/Piano_Playing_Game/img/piano_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Games/Piano_Playing_Game/img/piano_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Games/Piano_Playing_Game/img/piano_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36,028 changes: 36,028 additions & 0 deletions src/Games/Piano_Playing_Game/package-lock.json

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions src/Games/Piano_Playing_Game/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "piano",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.8",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added src/Games/Piano_Playing_Game/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions src/Games/Piano_Playing_Game/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added src/Games/Piano_Playing_Game/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Games/Piano_Playing_Game/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/Games/Piano_Playing_Game/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Binary file added src/Games/Piano_Playing_Game/public/notes/a.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/af.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/b.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/bf.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/c.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/d.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/df.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/e.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/ef.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/f.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/g.mp3
Binary file not shown.
Binary file added src/Games/Piano_Playing_Game/public/notes/gf.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Games/Piano_Playing_Game/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
38 changes: 38 additions & 0 deletions src/Games/Piano_Playing_Game/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
14 changes: 14 additions & 0 deletions src/Games/Piano_Playing_Game/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import './App.css';
import { Piano } from './components/Piano.js';

function App() {
return (
<div className="App">
<header className="App-header">
<Piano />
</header>
</div>
);
}

export default App;
29 changes: 29 additions & 0 deletions src/Games/Piano_Playing_Game/src/components/Key.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.key {
background-color: white;
height: 300px;
width: 60px;
border: 2px solid black;
}

.key.flat {
position: relative;
background-color: black;
margin-left: -17px;
margin-right: -17px;
height: 200px;
width: 30px;
z-index: 2;
}

.key.pressed, .key.flat.pressed {
background-color: #00d8ff;
}

.key-text {
position: relative;
color: black;
font-weight: 600;
font-size: 36px;
margin-top: 240px;
pointer-events: none;
}
41 changes: 41 additions & 0 deletions src/Games/Piano_Playing_Game/src/components/Key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import _ from 'lodash';
import React from 'react';

import './Key.css';
import { NOTE_TO_KEY } from '../global/constants';

class Key extends React.Component {
noteIsFlat = (note) => {
return note.length > 1;
}

keyIsPressed = (note, pressedKeys) => {
return _.includes(pressedKeys, NOTE_TO_KEY[note]);
}

render() {
let keyClassName = "key";
const noteIsFlat = this.noteIsFlat(this.props.note);
const keyIsPressed = this.keyIsPressed(this.props.note, this.props.pressedKeys);
if (noteIsFlat) {
keyClassName += " flat";
}
if (keyIsPressed) {
keyClassName += " pressed";
}

let key;
if (noteIsFlat) {
key = <div className={keyClassName}></div>;
} else {
key = (
<div className={keyClassName}>
<div className="key-text">{this.props.note.toUpperCase()}</div>
</div>
);
}
return key;
}
}

export { Key };
4 changes: 4 additions & 0 deletions src/Games/Piano_Playing_Game/src/components/Piano.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.piano {
display: flex;
flex-direction: row;
}
90 changes: 90 additions & 0 deletions src/Games/Piano_Playing_Game/src/components/Piano.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import _ from 'lodash';
import React from 'react';

import './Piano.css';
import { Key } from './Key.js'
import {
NOTES,
VALID_KEYS,
KEY_TO_NOTE,
} from '../global/constants';

class Piano extends React.Component {
constructor(props) {
super(props);
this.state = {
pressedKeys: [],
};
}

playNote = (note) => {
if (!_.isEmpty(note)) {
const noteAudio = new Audio(document.getElementById(note).src);
noteAudio.play();
}
}

handleKeyDown = (event) => {
if (event.repeat) {
return;
}
const key = event.key;
const updatedPressedKeys = [...this.state.pressedKeys];
if (!updatedPressedKeys.includes(key) && VALID_KEYS.includes(key)) {
updatedPressedKeys.push(key);
}
this.setState({
pressedKeys: updatedPressedKeys,
});
this.playNote(KEY_TO_NOTE[key]);
}

handleKeyUp = (event) => {
const index = this.state.pressedKeys.indexOf(event.key);
if (index > -1) {
this.setState(state => ({
pressedKeys: state.pressedKeys.splice(index, 1)
}));
}
}

componentDidMount = () => {
window.addEventListener('keydown', this.handleKeyDown);
window.addEventListener('keyup', this.handleKeyUp);
}

render() {
const keys = _.map(NOTES, (note, index) => {
return (
<Key
key={index}
note={note}
pressedKeys={this.state.pressedKeys}
/>
);
});

const audioFiles = _.map(NOTES, (note, index) => {
return (
<audio
id={note}
key={index}
src={`../../notes/${note}.mp3`}
/>
);
});

return (
<div>
<div className="piano">
{keys}
</div>
<div>
{audioFiles}
</div>
</div>
);
}
}

export { Piano };
Loading
Loading