Skip to content

Instantly share code, notes, and snippets.

@Kanol
Last active November 28, 2015 12:56
Show Gist options
  • Save Kanol/a815acc65398ecde2dde to your computer and use it in GitHub Desktop.
Save Kanol/a815acc65398ecde2dde to your computer and use it in GitHub Desktop.
<head>
<meta charset=utf-8>
<style>
.score {
position: absolute;
bottom: 0;
right: 0;
background: blue;
color: white;
}
.win {
position:absolute;
background: yellow;
color: grey;
font-size: 50;
}
.button{
position: absolute;
background: coral;
color: snow;
font-size:50;
}
</style>
</head>
<body>
<div class="score"></div>
<div class="win"></div>
<button class="button"></button>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function showScore(score) {
var $score = $('.score');
$score.text("Ваши очки: " + score);
}
function direction(side){
var direction=getRandomInt(1,4);
while(direction==side){
direction=getRandomInt(1,4)
}
return direction;
}
function move(i,direct){
var x;
var y;
side=direction(direct);
if(side==1){
y=0;
x=getRandomInt(0, w);
}else if(side==2){
x=w;
y=getRandomInt(0, h);
}else if(side==3){
x=getRandomInt(0, w);
y=h;
}else if(side ==4){
x=0;
y =getRandomInt(0, h);
}
$('.q'+i).animate({'top': y,'left': x},2500, function(){setTimeout(move(i,side),0)});
}
function generate(a) {
for (var i = 0; i < a; i+=1) {
var element = $('<div style=\"width: 100px; height: 100px; border: solid;\" class="q'+classnumber+'"> </div>');
element.css('position', 'absolute');
element.css('top', getRandomInt(1, h));
element.css('left', getRandomInt(1, w));
element.css('background', 'rgb'+'('+getRandomInt(0,255)+','+getRandomInt(0,255)+','+getRandomInt(0,255)+')');
element.click(on_click);
$(document.body).append(element);
move(classnumber, 0);
localcount+=1;
classnumber+=1;
}
}
function generate1(){
generate(1);
count+=1;
}
function time(){
gametime= gametime+1;
}
function showTime(){
var stringtime;
if(gametime>60){
stringtime=(gametime-(gametime%60))/60+':'+gametime%60;
}
else{
stringtime='0:'+gametime;
}
gametime = 0;
return(stringtime);
}
function buttonclick(){
button(false);
generate(10);
count=10;
score=0;
timer= setInterval(time, 1000);
interval = setInterval(generate1, 800);
}
function button(isOn){
if(isOn){
$button.css('visibility','visible');
$button.css('bottom', h1-70);
$button.css('left', w1-192);
$button.text("НАЧАТЬ КАТКУ");
}
else{
$button.css('visibility','hidden');
$winner.css('visibility','hidden');
}
}
function winner(){
$winner.css('visibility','visible');
$winner.css('bottom', h1);
$winner.css('left', w1-250);
clearInterval(interval);
interval=null;
clearInterval(timer);
timer=null;
$winner.text('ТЫ ЗАТАЩИЛ ЗА '+showTime());
hide();
button(true);
}
function remove(){
for(var i=0; i<=localcount;i+=1){
var randh=getRandomInt(1, h);
var randw=getRandomInt(1, w);
$('.q'+i).animate({'top': randh,'left': randw});
}
}
function hide(){
for(var i=0; i<=localcount;i+=1){
$('.q'+i).css('display','none');
}
}
var w = $(window).width() - 101;
var h = $(window).height() - 101;
var w1 = $(window).width()/2;
var h1 = $(window).height()/2;
var $button=$('.button');
$button.click(buttonclick);
var score = 0;
var localcount = 0;
var count = 10;
var interval;
var timer;
var gametime=0;
var $winner = $('.win');
var classnumber = 0;
function on_click(e) {
var target = $(e.target);
target.hide();
score += 1;
showScore(score);
count-=1;
if((count==0)||(score==50)){
winner();
}
}
button(true);
showScore(score);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment