Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created August 21, 2012 07:17
Show Gist options
  • Save ynonp/3413047 to your computer and use it in GitHub Desktop.
Save ynonp/3413047 to your computer and use it in GitHub Desktop.
/**
ืžืฉื—ืงื•ืŸ ืžืฆื ืืช ื”ื™ื•ืฆื ื“ื•ืคืŸ ืœืกืžืืจื˜ืคื•ืŸ
ื‘ืžืฉื—ืง ืžื•ืฆื’ื™ื ืขืœ ื”ืžืกืš 4 ืขืฆืžื™ื, ื•ื”ืžืฉืชืžืฉ ืฆืจื™ืš ืœื‘ื—ื•ืจ ืืช ื”ื™ื•ืฆื ื“ื•ืคืŸ.
ื‘ื ื•ืกืฃ, ืขืœ ื”ืžืกืš ืžื•ืฆื’ ื’ื ืœื•ื— ืชื•ืฆืื•ืช.
ื”ืงื•ื“ ืžืชืืจ ื’ื™ืฉื” ืžื•ื ื—ื™ืช ืขืฆืžื™ื ืœื‘ืขื™ื™ื”. ืื ื• ืžื’ื“ื™ืจื™ื ืื•ื‘ื™ื™ืงื˜ ืœื›ืœ ืื—ื“ ืžื”ืืœืžื ื˜ื™ื ื”ืคืขื™ืœื™ื ืฉืฆืจื™ื›ื™ื ืœื”ื•ืคื™ืข ืขืœ ื”ืžืกืš:
ื“ืžื•ืช ืฉืฆืจื™ืš ืœืœื—ื•ืฅ ืขืœื™ื” ื›ื“ื™ ืœื ืฆื—
3 ื“ืžื•ื™ื•ืช ืฉืœื—ื™ืฆื” ืขืœื™ื”ืŸ ื’ื•ืจืžืช ืœื”ืคืกื“
ืคืื ืœ ืฉืœ ืชื•ืฆืื•ืช
ื›ืœ ืื—ื“ ืžื”ืื•ื‘ื™ื™ืงื˜ื™ื ืžื ื”ืœ ืืช ืขืฆืžื•, ื•ื‘ืžื™ื“ืช ื”ืฆื•ืจืš ืžืขื“ื›ืŸ ืืช ื”ืื•ื‘ื™ื™ืงื˜ื™ื ื”ืื—ืจื™ื ื‘ืื™ืจื•ืขื™ื.
ื‘ื ื•ืกืฃ, ืœื›ืœ ืื—ื“ ืžื”ืื•ื‘ื™ื™ืงื˜ื™ื ื™ืฉ ื™ื›ื•ืœืช ืœืฆื™ื™ืจ ืืช ืขืฆืžื• ืขืœ ื”ืžืกืš - ืคื•ื ืงืฆื™ื™ืช render
ื•ื’ื ืœื”ื—ืœื™ืฃ ืืช ื”ืชืžื•ื ื” ืœืฆื•ืจืš ืื ื™ืžืฆื™ื” - ืคื•ื ืงืฆื™ื™ืช update
*/
(function() {
/**
ื”ืื•ื‘ื™ื™ืงื˜ ื”ืจืืฉื•ืŸ ื‘ืชื•ื›ื ื™ืช ื”ื•ื ืœื•ื— ื”ื ืงื•ื“ื•ืช. ื”ืœื•ื— ืžื—ื–ื™ืง ืืช ื”ื ื™ืงื•ื“ ื”ื ื•ื›ื—ื™,
ื•ืžืกืคืง ืคื•ื ืงืฆื™ื•ืช ืœื”ื•ืกื™ืฃ ืื• ืœืืคืก ื ื™ืงื•ื“, ืื• ืœืฆื™ื™ืจ ืืช ืขืฆืžื• ืขืœ ื”ืžืกืš
ื›ืืŸ ืื ื• ืขื“ื™ื™ืŸ ืœื ื™ื•ืฆืจื™ื ืืช ืœื•ื— ื”ื ืงื•ื“ื•ืช, ืืœื ืจืง ืžื’ื“ื™ืจื™ื ืชื‘ื ื™ืช (ืคื•ื ืงืฆื™ื™ื”) ืฉืชื•ื›ืœ ืœื™ืฆื•ืจ
ืœื•ื—ื•ืช ื ืงื•ื“ื•ืช ื‘ื”ืžืฉืš ื”ืชื•ื›ื ื™ืช. ื”ื™ืฆื™ืจื” ืขืฆืžื” ืชืชื‘ืฆืข ื‘ืืžืฆืขื•ืช ืงืจื™ืื” ืœ
new Score()
ื‘ื”ืžืฉืš
*/
var Score = function() {
this.value = 0;
this.increase = function() {
this.value += 1;
};
this.zero = function() {
this.value = 0;
};
/**
ื›ืฉืœื•ื— ื ืงื•ื“ื•ืช ืžืฆื™ื™ืจ ืืช ืขืฆืžื• ืขืœ ื”ืžืกืš ื”ื•ื ืžื—ืคืฉ ืืœืžื ื˜ ืขื ืงืœืืก
score
ื•ืชื—ืชื™ื• ืจื•ืฉื ืฉื•ืจืช ื˜ืงืกื˜ ื”ื›ื•ืœืœืช ืืช ื”ื ื™ืงื•ื“ ื”ื ื•ื›ื—ื™
*/
this.render = function() {
$('.score').html('Player score: <span>' + this.value + '</span>')
};
};
//////////////////////////////////////////////////////
/**
ื”ื’ื“ืจืช ื”ืžืฉืชื ื™ื ื”ื’ืœื•ื‘ืืœื™ื™ื:
ืจืฉื™ืžืช ื”ืฉื—ืงื ื™ื ืžื•ื’ื“ืจืช ืœื”ื™ื•ืช ืžืขืจืš ืจื™ืง,
ืœื•ื— ื”ื ืงื•ื“ื•ืช ืžื•ื’ื“ืจ ืœื”ื™ื•ืช ืื•ื‘ื™ื™ืงื˜ ื—ื“ืฉ ืžืกื•ื’ Score
*/
var players = [];
var score = new Score();
//////////////////////////////////////////////////////
/**
ื”ืขืฆืžื™ื ื”ื‘ืื™ื ืฉื™ื•ืคื™ืขื• ื”ื ื”ืฉื—ืงื ื™ื: ืžื ืฆื— ื•ืžืคืกื™ื“
ื”ืงื•ื“ ืฉืœ ืฉื ื™ื”ื ื“ื™ ื“ื•ืžื”.
ื›ืืŸ ื™ืฉ ืœื ื• ืฉื ื™ ื“ื‘ืจื™ื ืžืขื ื™ื™ื ื™ื - ื’ื ืื ื™ืžืฆื™ื”, ื•ื’ื ื™ื›ื•ืœืช ืœื”ืฆื™ื’ ืืช ืขืฆืžื• ืขืœ ื”ืžืกืš
ื‘ื ื•ืกืฃ ืืœื™ื”ื, ืฉื—ืงืŸ ืžื ืฆื— ื’ื ืชื•ืžืš ื‘ืœื—ื™ืฆื•ืช ื•ืœื›ืŸ ืฆืจื™ืš ืœื–ื”ื•ืช ื›ืฉืœื•ื—ืฆื™ื ืขืœื™ื• ื›ื“ื™ ืœื”ืชื—ื™ืœ ืกื™ื‘ื•ื‘
ืžืฉื—ืง ื—ื“ืฉ.
ื›ืœ ื”ื—ืœืง ื”ืจืืฉื•ืŸ ืžื•ืงื“ืฉ ืœืื ื™ืžืฆื™ื”. ืื ื• ืžื’ื“ื™ืจื™ื ืžืขืจืš ืฉืœ ืชืžื•ื ื•ืช ืจืงืข
ื•ื‘ื›ืœ ืคืขื™ืžืช ืฉืขื•ืช ืžื—ืœื™ืคื™ื ืืช ืชืžื•ื ืช ื”ืจืงืข - ื–ื” ืžื” ืฉืžื™ื™ืฆืจ ืืช ื”ืื ื™ืžืฆื™ื”.
ื”ืคื•ื ืงืฆื™ื™ื” update ื ืงืจืืช ืžื”ืงื•ื“ ื”ื—ื™ืฆื•ื ื™. ื›ืš ื›ืœ ื”ืื ื™ืžืฆื™ื•ืช ื‘ืžืฉื—ืง ืžืกื•ื ื›ืจื ื•ืช
ื‘ืžืงื•ื ืœื ื”ืœ ืžืกืคืจ ืฉืขื•ื ื™ื ื‘ืžืงื‘ื™ืœ.
ืคื•ื ืงืฆื™ื™ืช render ืฉืžืฆื™ื’ื” ืืช ื”ืฉื—ืงืŸ ืขืœ ื”ืžืกืš ืขื•ืฉื” ืขื•ื“ ื“ื‘ืจ ื‘ื ื•ืกืฃ ืœื”ืฆื’ื” -
ื”ื™ื ืงื•ืฉืจืช ืคื•ื ืงืฆื™ื™ืช ื˜ื™ืคื•ืœ ื‘ืœื—ื™ืฆื”.
ื›ืš ื›ืฉืžื™ืฉื”ื• ื™ืœื—ืฅ ืขืœ ื”ื“ืžื•ืช ื™ืงืจื ื”ืงื•ื“ ื‘ืคื•ื ืงืฆื™ื”
clicked_on
*/
var Winner = function() {
var backgrounds = [
'-8px -5px;',
'-52px -6px;',
'-94px -6px;',
'-139px -5px;'
];
var background_idx = 0;
this.update = function() {
// Choose the next image from the array
// ( % means modulus, so the numbers go: 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, ... )
var next_image =
backgrounds[background_idx % backgrounds.length];
// update the css
$('div.winner').css('background-position', next_image);
// and increase the index
background_idx += 1;
};
// When the winner is clicked on, we increase the score and start a new round
this.clicked_on = function() {
score.increase();
start_new_round();
};
this.render = function() {
$('div.container').append('<div class="winner"></div>');
$('div.container div').last().click( this.clicked_on );
};
};
var Looser = function() {
backgrounds = [
'-4px -55px;',
'-53px -56px',
'-98px -55px',
'-143px -55px'
];
var background_idx = 0;
// When the looser is clicked on, we zero the score and start a new round
this.clicked_on = function() {
score.zero();
start_new_round();
};
this.update = function() {
var next_image =
backgrounds[background_idx % backgrounds.length];
$('div.looser').css('background-position', next_image);
background_idx += 1;
};
this.render = function() {
$('div.container').append('<div class="looser"></div>');
$('div.container div').last().click( this.clicked_on );
};
};
/////////////////////////////////////////////////////
/**
ื”ื—ืœืง ื”ืื—ืจื•ืŸ ื‘ืชื•ื›ื ื™ืช ื”ื•ื ื”ืงื•ื“ ื”ืจืืฉื™ ืฉืžืคืขื™ืœ ืืช ื”ืชื•ื›ื ื™ืช
ื”ืคื•ื ืงืฆื™ื™ืช start_new_round
ืคื•ื ืงืฆื™ื™ื” ื–ื• ื ืงืจืืช ื‘ืชื—ื™ืœืช ื”ืžืฉื—ืง, ื•ื’ื ื‘ื™ืŸ ื›ืœ ืกื™ื‘ื•ื‘. ื”ื™ื ืžืืคืกืช ืืช ืœื•ื— ื”ืžืฉื—ืง,
ืžื’ืจื™ืœื” 4 ืฉื—ืงื ื™ื ื—ื“ืฉื™ื (3 ืžืคืกื™ื“ื™ื ื•ืžื ืฆื—)
ื•ืžืฆื™ื’ื” ืื•ืชื ืขืœ ืœื•ื— ื”ืžืฉื—ืง ื”ื—ื“ืฉ ื‘ืืžืฆืขื•ืช ืงืจื™ืื” ืœ render
ื‘ื ื•ืกืฃ ื”ื™ื ื’ื ืžื•ืกื™ืคื” ืืช ื›ืœ ื”ืฉื—ืงื ื™ื ืœืžืขืจืš ื›ื“ื™ ืฉื ื•ื›ืœ ืœื”ืคืขื™ืœ ืขืœื™ื”ื ืืช ื”ืื ื™ืžืฆื™ื”
*/
var start_new_round = function() {
players = [];
$('div.container').html('');
var winning_idx = Math.floor( Math.random() * 4 );
for ( var i=0; i < 4; i++ ) {
var next;
if ( i === winning_idx ) {
next = new Winner();
} else {
next = new Looser();
}
players.push( next );
next.render();
}
score.render();
};
// On page load
$(function() {
/*
ื›ืฉื”ืขืžื•ื“ ื ื˜ืขืŸ, ืžืชื—ื™ืœื™ื ืกื™ื‘ื•ื‘ ื—ื“ืฉ ื•ื’ื ืžืชื—ื™ืœื™ื ืฉืขื•ืŸ ืœืื ื™ืžืฆื™ื”
ื‘ื›ืœ 200 ืžื™ืœื™ ืฉื ื™ื•ืช ื”ืงื•ื“ ืขื•ื‘ืจ ืขืœ ื›ืœ ื”ืฉื—ืงื ื™ื ื‘ืžืขืจืš ื•ืžืคืขื™ืœ ืืช ืคื•ื ืงืฆื™ื™ืช
update
ืฉืœื”ื ื›ื“ื™ ืฉื”ืชืžื•ื ื” ืขืœ ื›ืœ ืฉื—ืงืŸ ืชืชื—ืœืฃ
*/
start_new_round();
setInterval(function() {
for ( var i=0; i < players.length; i++ ) {
players[i].update();
}
}, 200);
});
}());
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Ultimate RedSpotter (Free)</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=0" />
<style>
div.container {
width:150px;
margin:0 auto;
}
div.container div {
background: url('billyblaze.png') no-repeat -139px -5px;
width: 16px;
height: 32px;
float: left;
margin:20px;
}
div.container div.winner {
background: url('billyblaze.png') no-repeat -139px -5px;
}
div.score {
clear:both;
}
</style>
</head>
<body>
<div class="container">
</div>
<div class="score">
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/zepto/1.0rc1/zepto.min.js"></script>
<script src="game.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment