Created
July 25, 2013 14:13
-
-
Save bulkan/6080085 to your computer and use it in GitHub Desktop.
color wars - jenniferdewalt.com/color_war.html
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta property="og:image" content="http://jenniferdewalt.com/images/fb_icon.png"/> | |
<title>Color War | Jennifer Dewalt</title> | |
<link href="css/reset.css" rel="stylesheet"> | |
<link href="css/color_war.css" rel="stylesheet"> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script src="js/underscore.js" type="text/javascript"></script> | |
<script src="js/analytics.js" type="text/javascript"></script> | |
<script src="js/color_war.js" type="text/javascript" ></script> | |
<!--[if IE]> | |
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
</head> | |
<body id="color_war"> | |
<header> | |
<a href="color_war.html" id="title">Color War</a> | |
<div id="links_container"> | |
<a href="index.html" id="home">Home</a> | |
<a href="http://blog.jenniferdewalt.com" id="blog">Blog</a> | |
</div> | |
<br class="clear"> | |
</header> | |
<div class="modal" id="start"> | |
<div class="inner_modal"> | |
<h1>Color War</h1> | |
<h2>Witness the epic battle for survival.</h2> | |
<button>Start</button> | |
</div> | |
</div> | |
<div id="container"> | |
<canvas id="canvas"></canvas> | |
</div> | |
</body> | |
</html> |
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
$(document).ready(function () { | |
var canvas = document.getElementById('canvas'), | |
ctx = canvas.getContext('2d'), | |
h = 550, | |
w = 900, | |
tiles= []; | |
canvas.height = h; | |
canvas.width = w; | |
function Tile(x, y, color) { | |
this.x = x; | |
this.y = y; | |
this.color = color; | |
}; | |
function makeTile() { | |
ctx.clearRect(0,0,w,h); | |
_.each(tiles, function (tile) { | |
ctx.fillStyle = tile.color; | |
ctx.fillRect(tile.x, tile.y, 10, 10); | |
}); | |
}; | |
function init() { | |
var posX = 0, | |
posY =0; | |
for (var i = 0; i < h*w/100; i++) { | |
tiles.push(new Tile(posX, posY, randomColor())) | |
posX += 10; | |
if (posX % w/10 == 0) { | |
posX = 0; | |
posY += 10; | |
} | |
} | |
makeTile(); | |
}; | |
function fight() { | |
var choices = ['rock', 'paper', 'scissors']; | |
_.each(tiles, function (tile, i){ | |
var tileChoice = choices[Math.floor(Math.random()*3)], | |
opponents = [tiles[i+1], tiles[i-1], tiles[i+w/10], tiles[i-w/10]], | |
tile = tile; | |
_.each(opponents, function (opp, i){ | |
if (opp == undefined) { | |
opponents.splice(i,1); | |
} | |
}); | |
_.each(opponents, function (opp) { | |
var oppChoice = choices[Math.floor(Math.random()*3)]; | |
if ( tileChoice == oppChoice ) { | |
; | |
} else if (tileChoice == 'rock' && oppChoice == 'scissors' || tileChoice == 'paper' && oppChoice == 'rock' || tileChoice == 'scissors' && oppChoice == 'paper' ) { | |
opp.color = tile.color; | |
} else { | |
tile.color = opp.color; | |
} | |
}); | |
}); | |
makeTile(); | |
}; | |
init(); | |
$('button').on('click', function () { | |
$('.modal').fadeOut('100'); | |
setTimeout(function () { | |
setInterval(fight, 100); | |
}, 500); | |
}); | |
}); | |
function randomColor() { | |
return '#' + Math.random().toString(16).slice(2, 8); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment