From e1276ec55e495c51115d35c8512107c6eb2824a9 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Fri, 22 Apr 2016 07:49:39 -0700 Subject: [PATCH] Initial Commit --- custom.js | 40 +++++++++++++ gameScripts.js | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 21 +++++++ lib.js | 84 ++++++++++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100755 custom.js create mode 100755 gameScripts.js create mode 100755 index.html create mode 100755 lib.js diff --git a/custom.js b/custom.js new file mode 100755 index 0000000..cc513f0 --- /dev/null +++ b/custom.js @@ -0,0 +1,40 @@ +addShortcut(function(){ + toggleFullScreen(); +},[key.F]); + +addShortcut(function(){ + userDir=-1; +},[key.UP]); +addRemovalShortcut(function(){ + userDir=0; +},[key.UP]); +addRemovalShortcut(function(){ + userDir=0; +},[key.DOWN]); +addShortcut(function(){ + userDir=1; +},[key.DOWN]); +addRemovalShortcut(function(){ + console.log(1); +},[key.DOWN]); +addShortcut(function(){ + console.log(2); +},[key.DOWN]); + + +//keep in mind that alert() blocks the keyup event, so if you have problems, include the following in your code: +//to release all keys: + +// addShortcut(function(){ + // alert("You pressed the keys Control, Y, and Enter."); + // releaseAllKeys(); +// },[key.CTRL,key.Y,key.ENTER]); +// //This may not be desireable, as the user might still have keys pressed on the keyboard, and this would require the user to let up all keys. +// //It might be better to only release one key, but check out how only releasing select keys might be problematic: +// addShortcut(function(){ + // alert("You pressed the keys 1, 2, and 3."); + + // releaseKeys([key.ONE,key.TWO]); +// // keyStrokes[key.THREE] = false; +// },[key.ONE,key.TWO,key.THREE]); +// //alternatively, usee releaseKey() to release only one key diff --git a/gameScripts.js b/gameScripts.js new file mode 100755 index 0000000..c3a3331 --- /dev/null +++ b/gameScripts.js @@ -0,0 +1,159 @@ +var score=0; +var isMute = false; +//var kingAudio = new Audio('King.mp3'), knockAudio = new Audio('Knock.mp3'), matchAudio = new Audio('match.mp3'); + +var canvas; +var ctx; +var width; +var height; + +var ballDir=1; +var userDir=0; +var compDir=0; +var gameSpeed=2; + +var userLoc, compLoc, ballXLoc, ballYLoc; + +var gameLoop + +function newGame() +{ + canvas = document.getElementById("myCanvas"); + canvas.width=screen.availWidth*.98; + canvas.height=screen.availHeight*.899; + ctx = canvas.getContext("2d"); + width = canvas.width; + height = canvas.height; + userLoc = height*.45-10; + compLoc = height*.5-10; + ballXLoc = width*.06-10; + ballYLoc = height*.5-10; + gameOver = false; + drawBoard(); + // fillBoard(); + // makePiecesDraggable(); + // makeMouseUpDetectable(); + // lastPlayer="null"; + // locationCount=1; + // isMute = false; + // multPossible = false; +// gameLoop= setInterval(move,5); + +} +function toggleFullScreen() +{ + if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) + { + if (canvas.requestFullscreen) + { + canvas.requestFullscreen(); + } + else if (canvas.mozRequestFullScreen) + { + canvas.mozRequestFullScreen(); + } + else if (canvas.webkitRequestFullscreen) + { + canvas.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); + } + } + +} +function drawBoard() +{ + ctx.fillStyle = "#D3D3D3"; + ctx.fillRect(0,0,width,height); + ctx.fillStyle = "#000000"; + ctx.fillRect(0,height*.95,width,15); + ctx.fillRect(0,height*.05,width,15); + // ctx.strokeStyle = "#98999A"; + // for(var i=100;i<=800;i+=100) + // { + // ctx.moveTo(0,i); + // ctx.lineTo(800,i); + // // ctx.stroke(); + // } + // ctx.stroke(); + drawUser(); + drawBall(); + drawComp(); +} + +function drawUser() +{ + ctx.fillStyle = "#000000"; + userLoc=userLoc+gameSpeed*userDir; + if(userLoc>height*.95-175) + { + userLoc=height*.95-175; + } + else if(userLocwidth||ballXLoc<-10) + { + newGame(); + } +} +function move() +{ + ctx.clearRect(0, 0, canvas.width, canvas.height); + drawBoard(); + // userDir=0; + // compDir=0; + // gameSpeed=5; + //requestAnimationFrame(move); + +} +function mute() +{ + var holder = document.getElementById("soundHolder"); + if(isMute) + { + holder.style.backgroundPosition = "0px 0px"; + } + else + { + holder.style.backgroundPosition = "60px 0px"; + } + isMute = !isMute; + playSound("match"); +} + +function playSound(soundType) +{ + if(!isMute) + { + if(soundType == "match") + { + matchAudio.play(); + } + else if(soundType == 'Knock') + { + knockAudio.play(); + } + else if(soundType == "King") + { + kingAudio.play(); + } + } +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100755 index 0000000..f8cc814 --- /dev/null +++ b/index.html @@ -0,0 +1,21 @@ + + + + + + + + +
+ + Your browser does not support the HTML5 canvas tag.
+ Try using a more modern browser, like Google Chrome,to play the game. +
+
+ + \ No newline at end of file diff --git a/lib.js b/lib.js new file mode 100755 index 0000000..3379dee --- /dev/null +++ b/lib.js @@ -0,0 +1,84 @@ +/* +JSKeyCapture Version 1.0 +Written by: Tai Groot, FreLancelot +Last revision date: 18 April, 2016 +Contact: groot.tai@gmail.com +*/ + +var keyStrokes = []; +window.callDefinedFunction = function(callback,keyCodeValues){ + if(typeof callback !== "function") + throw new TypeError("Expected callback as first argument"); + if(typeof keyCodeValues !== "object" && (!Array.isArray || Array.isArray(keyCodeValues))) + throw new TypeError("Expected array as second argument"); + var pressedKeysValid = true; + for(var i = 0; i < keyCodeValues.length; ++i) + pressedKeysValid = pressedKeysValid && keyStrokes[keyCodeValues[i]]; + if(pressedKeysValid) + callback(); +}; +window.addShortcut = function(callback,keyCodeValues){ + if(typeof keyCodeValues === "number") + keyCodeValues = [keyCodeValues]; + var funct = function(cb,val){ + return function(e){ + keyStrokes[e.keyCode] = true; + callDefinedFunction(cb,val); + }; + }(callback,keyCodeValues); + window.addEventListener('keydown',funct); + return funct; +}; +window.addRemovalShortcut = function(callback,keyCodeValues){ + if(typeof keyCodeValues === "number") + keyCodeValues = [keyCodeValues]; + var funct = function(cb,val){ + return function(e){ + keyStrokes[e.keyCode] = false; + callDefinedRemovalFunction(cb,val); + }; + }(callback,keyCodeValues); + window.addEventListener('keyup',funct); + return funct; +}; +window.callDefinedRemovalFunction = function(callback,keyCodeValues){ + if(typeof callback !== "function") + throw new TypeError("Expected callback as first argument"); + if(typeof keyCodeValues !== "object" && (!Array.isArray || Array.isArray(keyCodeValues))) + throw new TypeError("Expected array as second argument"); + var pressedKeysValid = true; + for(var i = 0; i < keyCodeValues.length; ++i) + pressedKeysValid = pressedKeysValid && !keyStrokes[keyCodeValues[i]]; + if(pressedKeysValid) + callback(); +}; +window.addEventListener('keyup',function(e){ + keyStrokes[e.keyCode] = false; +}); +function releaseAllKeys() +{ + for(var i=0;i< keyStrokes.length;i++) + { + keyStrokes[i] = false; + } +} +function releaseKeys(toRelease) +{ + if(typeof toRelease !== "object" && (!Array.isArray || Array.isArray(toRelease))) + throw new TypeError("Expected array"); + for(var i=0;i< toRelease.length;i++) + { + keyStrokes[toRelease[i]] = false; + } +} +function releaseKey(toRelease) +{ + keyStrokes[toRelease] = false; +} +var key = { + A:65,B:66,C:67,D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78,O:79,P:80,Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90, + ZERO:48,ONE:49,TWO:50,THREE:51,FOUR:52,FIVE:53,SIX:54,SEVEN:55,EIGHT:56,NINE:57, + SHIFT:16,CTRL:17,ALT:18, + ENTER:13, + LEFT:37,UP:38,RIGHT:39,DOWN:40 +}; \ No newline at end of file