implemented sounds master
implemented sounds

--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -31,6 +31,13 @@
                 }
             }
         },
+        copy: {
+          main: {
+            files: [
+                {expand: true, cwd: 'public/', src: ['audio/*.ogg'], dest: 'dist/'},
+                ],
+            },
+        },
         jshint: {
             files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
             options: {
@@ -53,8 +60,9 @@
     grunt.loadNpmTasks('grunt-contrib-watch');
     grunt.loadNpmTasks('grunt-contrib-concat');
     grunt.loadNpmTasks('grunt-contrib-cssmin');
+    grunt.loadNpmTasks('grunt-contrib-copy');
 
     //grunt.registerTask('test', ['jshint']);
 
-    grunt.registerTask('default', [/*'jshint', */'concat', 'uglify', 'cssmin']);
+    grunt.registerTask('default', [/*'jshint', */'concat', 'uglify', 'cssmin', 'copy']);
 };

--- a/package.json
+++ b/package.json
@@ -5,6 +5,14 @@
   "dependencies": {
     "express": "4.10.2",
     "socket.io": "1.2.0"
+  },
+  "devDependencies": {
+    "grunt-contrib-concat": "^0.5.1",
+    "grunt-contrib-copy": "^0.8.2",
+    "grunt-contrib-cssmin": "^0.14.0",
+    "grunt-contrib-jshint": "^0.12.0",
+    "grunt-contrib-uglify": "^0.11.0",
+    "grunt-contrib-watch": "^0.6.1"
   }
 }
 

 Binary files /dev/null and b/public/audio/hit.ogg differ
--- a/public/index.html
+++ b/public/index.html
@@ -23,6 +23,10 @@
     </div>
   </div>
 </nav>
+
+<audio preload="true" id="hitSound">
+    <source src="/audio/hit.ogg" />
+</audio>
 
 <div class="container">
     <br /><br /><br /><br />

--- a/public/js/pong.js
+++ b/public/js/pong.js
@@ -22,6 +22,8 @@
 var playersMove = {1:{},2:{},3:{},4:{}};
 var offset = {1:0,2:0,3:0,4:0};
 var rand = 0;
+
+var hitSound;
 
 var graphics = {
     ball: {src: '/img/ball.png', instance: null},
@@ -360,12 +362,14 @@
     if (top_y > 700) {
         if (this.checkCollision(this, paddle1)) {
             //if (currentPlayer == 1) {
+                playHitSound();
                 socket.emit('send:hitball', {x_speed:(paddle1.x_speed / 1.5) + randMove(), y_speed:-SpeedY()});
             //}
         }
     } else if (top_y < 100) {
         if (this.checkCollision(this, paddle2)) {
             //if (currentPlayer == 2) {
+                playHitSound();
                 socket.emit('send:hitball', {x_speed:(paddle2.x_speed / 1.5) + randMove(), y_speed:SpeedY()});
             //}
         }
@@ -374,11 +378,13 @@
     if (top_x < 100) {
         if (this.checkCollision(this, paddle3)) {
             //if (currentPlayer == 3) {
+                playHitSound();
                 socket.emit('send:hitball', {x_speed:SpeedX(), y_speed:(paddle3.y_speed / 1.5) + randMove()});
             //}
         }
     } else if (top_x > 700) {
         if (this.checkCollision(this, paddle4)) {
+            playHitSound();
             socket.emit('send:hitball', {x_speed:-SpeedX(), y_speed:(paddle4.y_speed / 1.5) + randMove()});
         }
     }
@@ -460,12 +466,19 @@
 $(function(){
     $('.game').html(canvas);
     $('<img />', {src:'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Click to play http://' + window.location.host + '/play.html'}).appendTo('.play');
+    hitSound = document.getElementById("hitSound");
 });
 animate(step);
 
 function randMove()
 {
     return rand+=0.2;
+}
+
+function playHitSound() {
+    hitSound.pause();
+    hitSound.currentTime = 0;
+    hitSound.play();
 }
 
 window.addEventListener("keydown", function (event) {

comments