Skip to content

Commit

Permalink
added all the project files
Browse files Browse the repository at this point in the history
  • Loading branch information
keys-i committed Jul 1, 2023
1 parent b15fa77 commit 114a9b7
Show file tree
Hide file tree
Showing 64 changed files with 2,030 additions and 0 deletions.
Binary file added assets/sounds/BallsCollide-old1.wav
Binary file not shown.
Binary file added assets/sounds/BallsCollide.wav
Binary file not shown.
Binary file added assets/sounds/Bossa Antigua.mp3
Binary file not shown.
Binary file added assets/sounds/Hole.wav
Binary file not shown.
Binary file added assets/sounds/Side.mp3
Binary file not shown.
Binary file added assets/sounds/Side.wav
Binary file not shown.
Binary file added assets/sounds/Strike.wav
Binary file not shown.
Binary file added assets/sprites/1_player_button.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 assets/sprites/1_player_button_hover.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 assets/sprites/2_players_button.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 assets/sprites/2_players_button_hover.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 assets/sprites/about_button.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 assets/sprites/about_button_hover.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 assets/sprites/back_button.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 assets/sprites/back_button_hover.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 assets/sprites/continue_button.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 assets/sprites/continue_button_hover.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 assets/sprites/controls.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 assets/sprites/easy_button.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 assets/sprites/easy_button_hover.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 assets/sprites/favicon.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 assets/sprites/hard_button.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 assets/sprites/hard_button_hover.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 assets/sprites/insane_button.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 assets/sprites/insane_button_hover.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 assets/sprites/main_menu_background.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 assets/sprites/medium_button.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 assets/sprites/medium_button_hover.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 assets/sprites/mute_button.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 assets/sprites/mute_button_hover.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 assets/sprites/mute_button_pressed.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 assets/sprites/mute_button_pressed_hover.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 assets/sprites/spr_background4.png
Binary file added assets/sprites/spr_background5.png
Binary file added assets/sprites/spr_ball2.png
Binary file added assets/sprites/spr_blackBall2.png
Binary file added assets/sprites/spr_redBall2.png
Binary file added assets/sprites/spr_stick.png
Binary file added assets/sprites/spr_yellowBall2.png
11 changes: 11 additions & 0 deletions css/game-layout.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
html, body {
margin: 0;
}

/*#screen{
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
}*/
45 changes: 45 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<title>Classic Pool Game</title>
<link rel="stylesheet" type="text/css" href="css/game-layout.css"/>
<link rel="shortcut icon" type="image/png" href="assets/sprites/favicon.png"/>
<script src="script/lib/LAB.min.js"></script>
<script>
$LAB
.script('script/system/Keys.js').wait()
.script('script/system/Color.js').wait()
.script('script/geom/Vector2.js').wait()
.script('script/input/ButtonState.js').wait()
.script('script/input/Keyboard.js').wait()
.script('script/input/Mouse.js').wait()
.script('script/Global.js').wait()
.script('script/Canvas2D.js').wait()
.script('script/game_objects/Score.js').wait()
.script('script/game_objects/Ball.js').wait()
.script('script/game_objects/Stick.js').wait()
.script('script/menu/Label.js').wait()
.script('script/menu/Button.js').wait()
.script('script/menu/Menu.js').wait()
.script('script/menu/MainMenu.js').wait()
.script('script/AI/Opponent.js').wait()
.script('script/AI/AIPolicy.js').wait()
.script('script/AI/AITrainer.js').wait()
.script('script/game_objects/Player.js').wait()
.script('script/GamePolicy.js').wait()
.script('script/GameWorld.js').wait()
.script('script/Game.js').wait()
.script('script/Assets.js').wait(function () {
Game.start('gameArea','screen', 1500, 825);
});

</script>
</head>

<body style = "background-color:black">
<div id="gameArea">
<canvas id="screen" width="2000" height="1000"></canvas>
</div>
</body>
</html>
49 changes: 49 additions & 0 deletions script/AI/AIPolicy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function AIPolicy(){

}

AIPolicy.prototype.evaluate = function(state, gamePolicy){

let evaluation = 1;

for (var i = 0 ; i < state.balls.length; i++){
for(var j = i + 1 ; j < state.balls.length ; j++){

let firstBall = state.balls[i];
let secondBall = state.balls[j];

if(firstBall === state.whiteBall || secondBall === state.whiteBall
||
firstBall.inHole || secondBall.inHole){
continue;
}
evaluation += firstBall.position.distanceFrom(secondBall.position);
}
}

evaluation = evaluation/5800;

if(!gamePolicy.firstCollision){
evaluation+= 100;
}

evaluation += 2000 * gamePolicy.validBallsInsertedOnTurn;

gamePolicy.updateTurnOutcome();


if(gamePolicy.won){
if(!gamePolicy.foul){
evaluation += 10000;
}
else{
evaluation -= 10000;
}
}

if(gamePolicy.foul){
evaluation = evaluation - 3000;
}

return evaluation;
}
177 changes: 177 additions & 0 deletions script/AI/AITrainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
function AITrainer(){

this.AIPolicy = new AIPolicy();

}

AITrainer.prototype.init = function(state, gamePolicy){

AI.opponents = [];
AI.currentOpponent = new Opponent();
AI.finishedSession = true;
AI.iteration = 0;

AI.bestOpponentIndex = 0;
AI.bestOpponentEval = 0;

if(gamePolicy.foul){
//TO DO: Pick best position for the white ball.
state.whiteBall.position.x = 413;
state.whiteBall.position.y = 413;
state.whiteBall.inHole = false;
gamePolicy.foul = false;
}
AI.initialState = JSON.parse(JSON.stringify(state));
AI.initialGamePolicyState = JSON.parse(JSON.stringify(gamePolicy));

AI.state = state;
AI.gamePolicy = gamePolicy;

}

AITrainer.prototype.train = function(){

if(AI.iteration === TRAIN_ITER){
AI.finishedSession = true;
AI.playTurn();
return;
}

let ballsMoving = AI.state.ballsMoving();

if(!ballsMoving){

if(AI.iteration !== 0){
AI.currentOpponent.evaluation = AI.AIPolicy.evaluate(this.state, this.gamePolicy);

AI.opponents.push(JSON.parse(JSON.stringify(AI.currentOpponent)));

if(AI.currentOpponent.evaluation > AI.bestOpponentEval){
AI.bestOpponentEval = AI.currentOpponent.evaluation;
AI.bestOpponentIndex = AI.opponents.length - 1;
}

if(LOG){
console.log('-------------'+new Number(AI.iteration+1)+'--------------------');
console.log('Current evaluation: ' + AI.currentOpponent.evaluation);
console.log('Current power: ' + AI.currentOpponent.power);
console.log('Current rotation: ' + AI.currentOpponent.rotation);
console.log('---------------------------------');
}
}

AI.state.initiateState(AI.initialState.balls);
AI.gamePolicy.initiateState(AI.initialGamePolicyState);
AI.buildNewOpponent();
AI.simulate();
}

}

AITrainer.prototype.buildNewOpponent = function(){

if(AI.iteration % 10 === 0){
AI.currentOpponent = new Opponent();
AI.iteration++;
return;
}

let bestOpponent = AI.opponents[AI.bestOpponentIndex];

let newPower = bestOpponent.power;
newPower += + ((Math.random() * 30) - 15);
newPower = newPower < 20 ? 20 : newPower;
newPower = newPower > 75 ? 75 : newPower;

let newRotation = bestOpponent.rotation;

if(bestOpponent.evaluation > 0){
newRotation += (1/bestOpponent.evaluation)*(Math.random() * 2 * Math.PI - Math.PI)
}
else{
newRotation = (Math.random() * 2 * Math.PI - Math.PI);
}

AI.currentOpponent = new Opponent(newPower,newRotation);

AI.iteration++;

}

AITrainer.prototype.simulate = function(){
AI.state.stick.shoot(AI.currentOpponent.power, AI.currentOpponent.rotation);
}

AITrainer.prototype.playTurn = function(){

bestOpponent = AI.opponents[AI.bestOpponentIndex];
Game.gameWorld.stick.rotation = bestOpponent.rotation;
Game.gameWorld.stick.trackMouse = false;

setTimeout(() => {

Game.gameWorld.stick.visible = true;
Canvas2D.clear();
Game.gameWorld.draw();

Game.sound = true;
Game.gameWorld.initiateState(AI.initialState.balls);
Game.policy.initiateState(AI.initialGamePolicyState);

DISPLAY = true;

requestAnimationFrame(Game.mainLoop);

Game.gameWorld.stick
.shoot(
bestOpponent.power,
bestOpponent.rotation
);
Game.gameWorld.stick.trackMouse = true;

}, 1000);
}

AITrainer.prototype.opponentTrainingLoop = function(){

Game.sound = false;
DISPLAY = false;

if(DISPLAY_TRAINING){
if(!AI.finishedSession){
AI.train();
Game.gameWorld.handleInput(DELTA);
Game.gameWorld.update(DELTA);
Canvas2D.clear();
Game.gameWorld.draw();
Mouse.reset();
setTimeout(AI.opponentTrainingLoop,0.00000000001);
}
}
else{
while(!AI.finishedSession){
AI.train();
Game.gameWorld.handleInput(DELTA);
Game.gameWorld.update(DELTA);
Mouse.reset();
}
}

}

AITrainer.prototype.startSession = function(){
setTimeout(
()=>{
Game.gameWorld.stick.visible = false;
Canvas2D.clear();
Game.gameWorld.draw();

AI.init(Game.gameWorld, Game.policy);
AI.finishedSession = false;
AI.opponentTrainingLoop();
},
1000
);
}

const AI = new AITrainer();
5 changes: 5 additions & 0 deletions script/AI/Opponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Opponent(power, rotation){
this.power = power || (Math.random() * 75 + 1);
this.rotation = rotation || (Math.random()*6.283)-3.141;
this.evaluation = 0;
}
73 changes: 73 additions & 0 deletions script/Assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"use strict";

var sprites = {};
var sounds = {};

Game.loadAssets = function () {
var loadSprite = function (sprite) {
return Game.loadSprite("assets/sprites/" + sprite);
};

var loadSound = function (sound) {
return new Audio("assets/sounds/" + sound);
};

sprites.mainMenuBackground = loadSprite("main_menu_background.png");
sprites.background = loadSprite("spr_background4.png");
sprites.ball = loadSprite("spr_ball2.png");
sprites.redBall = loadSprite("spr_redBall2.png");
sprites.yellowBall = loadSprite("spr_yellowBall2.png");
sprites.blackBall = loadSprite("spr_blackBall2.png");
sprites.stick = loadSprite("spr_stick.png");
sprites.twoPlayersButton = loadSprite("2_players_button.png");
sprites.twoPlayersButtonHover = loadSprite("2_players_button_hover.png");
sprites.onePlayersButton = loadSprite("1_player_button.png");
sprites.onePlayersButtonHover = loadSprite("1_player_button_hover.png");
sprites.muteButton = loadSprite("mute_button.png");
sprites.muteButtonHover = loadSprite("mute_button_hover.png");
sprites.muteButtonPressed = loadSprite("mute_button_pressed.png");
sprites.muteButtonPressedHover = loadSprite("mute_button_pressed_hover.png");
sprites.easyButton = loadSprite("easy_button.png");
sprites.easyButtonHover = loadSprite("easy_button_hover.png");
sprites.mediumButton = loadSprite("medium_button.png");
sprites.mediumButtonHover = loadSprite("medium_button_hover.png");
sprites.hardButton = loadSprite("hard_button.png");
sprites.hardButtonHover = loadSprite("hard_button_hover.png");
sprites.backButton = loadSprite("back_button.png");
sprites.backButtonHover = loadSprite("back_button_hover.png");
sprites.continueButton = loadSprite("continue_button.png");
sprites.continueButtonHover = loadSprite("continue_button_hover.png");
sprites.insaneButton = loadSprite("insane_button.png");
sprites.insaneButtonHover = loadSprite("insane_button_hover.png");
sprites.aboutButton = loadSprite("about_button.png");
sprites.aboutButtonHover = loadSprite("about_button_hover.png");
sprites.controls = loadSprite("controls.png");

sounds.side = loadSound("Side.wav");
sounds.ballsCollide = loadSound("BallsCollide.wav");
sounds.strike = loadSound("Strike.wav");
sounds.hole = loadSound("Hole.wav");

// Bossa Antigua Kevin MacLeod (incompetech.com)
// Licensed under Creative Commons: By Attribution 3.0 License
// http://creativecommons.org/licenses/by/3.0/
sounds.jazzTune = loadSound("Bossa Antigua.mp3");
}

sounds.fadeOut = function(sound) {

var fadeAudio = setInterval(function () {

if(GAME_STOPPED)
return;

// Only fade if past the fade out point or not at zero already
if ((sound.volume >= 0.05)) {
sound.volume -= 0.05;
}
else{
sound.pause();
clearInterval(fadeAudio);
}
}, 400);
}
Loading

0 comments on commit 114a9b7

Please sign in to comment.