-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #635 from Kalivarapubindusree/play
Piano_Playing_Game Added
- Loading branch information
Showing
36 changed files
with
36,489 additions
and
0 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 |
---|---|---|
@@ -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* |
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,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> |
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.
36,028 changes: 36,028 additions & 0 deletions
36,028
src/Games/Piano_Playing_Game/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
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,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 not shown.
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,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> |
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.
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,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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,3 @@ | ||
# https://www.robotstxt.org/robotstxt.html | ||
User-agent: * | ||
Disallow: |
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,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); | ||
} | ||
} |
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,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; |
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,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; | ||
} |
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,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 }; |
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,4 @@ | ||
.piano { | ||
display: flex; | ||
flex-direction: row; | ||
} |
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,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 }; |
Oops, something went wrong.