3Dstudy/js/snake.js

367 lines
11 KiB
JavaScript
Raw Normal View History

2018-01-10 02:07:28 +01:00
2018-01-16 15:35:05 +01:00
2018-01-17 00:57:27 +01:00
$(document).ready(function () {
2018-01-24 11:28:31 +01:00
2018-01-10 02:07:28 +01:00
var canvas = $("#myCanvas")[0];
var ctx = canvas.getContext("2d");
var w = $("#myCanvas").width();
var h = $("#myCanvas").height();
var cw = 15;
var food;
var snake;
var d = "RIGHT";
var gameloop;
2018-01-24 02:16:58 +01:00
var id_question;
2018-01-10 13:06:49 +01:00
2018-01-17 00:57:27 +01:00
2018-01-10 02:07:28 +01:00
function start(gameloop){
gameloop = setInterval(update, 100);
return gameloop
2017-12-19 11:27:17 +01:00
}
2018-01-10 02:07:28 +01:00
function reset() {
2018-01-17 00:57:27 +01:00
window.location.reload();
2018-01-24 02:16:58 +01:00
if(localStorage.getItem("life_couter") === null ){
2018-01-17 00:57:27 +01:00
setLifeCouter(3);
}
}
2018-01-24 11:28:31 +01:00
2018-01-17 00:57:27 +01:00
function tryAgain(){
window.location.reload();
if(localStorage.getItem("life_couter") === null ){
setLifeCouter(3);
}else{
var lifeCouter = getLifeCouter();
setLifeCouter(--lifeCouter);
}
2018-01-10 02:07:28 +01:00
}
function stop(gameloop){
clearInterval(gameloop);
}
2018-01-16 15:35:05 +01:00
function getQuestionCouter(){
var questionCouter = localStorage.getItem("question_couter");
return questionCouter;
}
function setQuestionCouter(questionCouter){
localStorage.setItem("question_couter", questionCouter);
}
2018-01-17 00:57:27 +01:00
function getLifeCouter(){
var lifeCouter = localStorage.getItem("life_couter");
return lifeCouter;
}
function setLifeCouter(lifeCouter){
localStorage.setItem("life_couter", lifeCouter);
}
2018-01-10 02:07:28 +01:00
function getSessionScore(){
var sessionScore = localStorage.getItem("session_score");
return sessionScore;
}
function getCurrentScore(){
var currentScore = localStorage.getItem("current");
return currentScore;
2017-12-19 11:43:44 +01:00
}
2018-01-10 02:07:28 +01:00
function setSessionScore(score){
localStorage.setItem("session_score", score);
}
function setCurrentScore(score){
localStorage.setItem("current", score);
}
function checkScore(score){
var session = getSessionScore();
var current = getCurrentScore();
if(current === null){
setCurrentScore(score);
setSessionScore(score);
}
if (session < score){
current = 0;
setCurrentScore(score);
setSessionScore(score);
2017-12-19 17:02:39 +01:00
}
}
2018-01-10 02:07:28 +01:00
$('#popup_question').submit(function(e){
var form = $(this).serializeArray();
e.preventDefault();
form = JSON.stringify(form);
2018-01-24 02:16:58 +01:00
if(localStorage.getItem("new_question") === null){
localStorage.setItem('id_question', 0);
localStorage.setItem("new_question",form);
2018-01-24 11:28:31 +01:00
alert('Dodano pytanie. Wcisnij Ok. Dodaj nastepne pytanie lub wcisnij F5 i graj');
2018-01-24 02:16:58 +01:00
}
else{
id_question = localStorage.getItem('id_question');
id_question++ ;
localStorage.setItem(`new_question-${id_question}`,form);
localStorage.setItem('id_question', id_question);
2018-01-24 11:28:31 +01:00
alert('Dodano pytanie. Wcisnij Ok. Dodaj nastepne pytanie lub wcisnij F5 i graj');
2018-01-24 02:16:58 +01:00
}
2018-01-10 13:06:49 +01:00
$(this).closest('form').find("input[type=text]").val("");
2018-01-10 02:07:28 +01:00
});
2018-01-24 11:28:31 +01:00
2018-01-17 00:57:27 +01:00
// $('#accept').click(function(){
// var goodAnswer = $("input[class='yes']");
// console.log(goodAnswer);
// if(goodAnswer.is(':checked')){
// console.log('dobra opdowiedz');
// }
// else{
// console.log('zla odpowiedz');
// }
2018-01-24 11:28:31 +01:00
2018-01-17 00:57:27 +01:00
// });
2018-01-24 02:16:58 +01:00
$('#clear_local_storage').click(function(){
localStorage.removeItem(`new_question`);
var numberOfQuestions = localStorage.getItem('id_question');
for (var i = 0; i <= numberOfQuestions; i++){
localStorage.removeItem(`new_question-${i}`);
}
localStorage.removeItem('id_question');
})
$('#restart_game').click(function(){
setLifeCouter(3);
setSessionScore(0);
2018-01-24 11:28:31 +01:00
alert('Zresetowales swoje zycia, wcisnij Ok, a nastepnie F5 zeby zaczac od nowa');
2018-01-24 02:16:58 +01:00
})
2018-01-10 02:07:28 +01:00
$('#add_question').click(function(){
var isHidden = localStorage.getItem('isHidden');
if(isHidden === null){
isHidden = 'true';
}
if(isHidden === 'true'){
$('#popup_question').fadeIn();
localStorage.setItem('isHidden', 'false');
}else if(isHidden === 'false'){
$('#popup_question').fadeOut();
localStorage.setItem('isHidden', 'true');
}
})
2018-01-17 00:57:27 +01:00
$('#restart').click(function(){
reset();
})
$('#try_again').click(function(){
tryAgain();
})
2018-01-24 11:28:31 +01:00
2018-01-24 02:16:58 +01:00
$('#session').text(getSessionScore());
$('#lifes').text(localStorage.getItem('life_couter'));
if(localStorage.getItem('life_couter') == 0){
2018-01-24 11:28:31 +01:00
alert(`Koniec gry! Twoj wynik to: ${localStorage.getItem('session_score')}. Jezeli chcesz zagrac jeszcze raz wcisnij Ok, nastepnie Resetuj gre, pozniej F5`);
2018-01-24 02:16:58 +01:00
}
2018-01-10 02:07:28 +01:00
function update() {
if (d == "RIGHT")
snake.x = snake.x + 1;
else if (d == "LEFT")
snake.x = snake.x - 1;
else if (d == "UP")
snake.y = snake.y - 1;
else if (d == "DOWN")
snake.y = snake.y + 1;
check_borders();
check_food();
blank();
paint_cell(food.x, food.y, "grey");
paint_cell(snake.x, snake.y, "green");
}
2018-01-24 02:16:58 +01:00
function randomYes() {
2018-01-24 11:28:31 +01:00
2018-01-24 02:16:58 +01:00
var newClass = "yes";
var randomKey = parseInt(Math.floor(Math.random() * 4));
$("div").each(function () {
var inputsArr = $(this).children("input[name='yes_no']")
var inputYes = $(inputsArr[randomKey]);
inputYes.attr('class', newClass);
inputYes.removeClass("no");
})
2018-01-24 03:12:04 +01:00
}
function addTextToAnserws(randomQuestion) {
var i = 0;
var j = 2;
console.log(randomQuestion[1].value);
var inputsArrNo = $(".no");
var inputNo0 = $(inputsArrNo[0]);
var inputNo1 = $(inputsArrNo[1]);
var inputNo2 = $(inputsArrNo[2]);
inputNo0.next().text(randomQuestion[2].value);
inputNo1.next().text(randomQuestion[3].value);
inputNo2.next().text(randomQuestion[4].value);
$('h3').text(randomQuestion[0].value);
$('.yes').next().text(randomQuestion[1].value);
2018-01-24 02:16:58 +01:00
}
function showQuestionTask() {
2018-01-24 11:28:31 +01:00
2018-01-24 02:16:58 +01:00
function pickRandomQuestion(){
var questionKey = 'new_question';
var id = localStorage.getItem('id_question');
var randomKey = Math.floor(Math.random() * id);
if(randomKey === 0){
var randomQuestion = localStorage.getItem(questionKey);
randomQuestion = JSON.parse(randomQuestion);
}else{
questionKey = `new_question-${randomKey}`;
var randomQuestion = localStorage.getItem(questionKey);
randomQuestion = JSON.parse(randomQuestion);
}
2018-01-17 00:57:27 +01:00
return randomQuestion;
}
2018-01-24 02:16:58 +01:00
var randomQuestion = pickRandomQuestion();
2018-01-17 00:57:27 +01:00
console.log(randomQuestion);
2018-01-10 02:07:28 +01:00
var current = $('#current').text();
$('#final').text(current);
2018-01-24 02:16:58 +01:00
if(randomQuestion === null){
2018-01-24 11:28:31 +01:00
alert('Pytania nie zostaly dodane. Wcisnij Ok, nastepnie F5 i dodaj pytania');
2018-01-24 02:16:58 +01:00
}
randomYes();
2018-01-24 03:12:04 +01:00
addTextToAnserws(randomQuestion);
$('#popup').fadeIn();
2018-01-24 11:28:31 +01:00
2018-01-24 02:16:58 +01:00
// $(".popup-item").each(function(index){
// var newId = "yes";
2018-01-24 11:28:31 +01:00
2018-01-24 02:16:58 +01:00
// });
2018-01-16 15:35:05 +01:00
$('.yes').click(function () {
2018-01-17 00:57:27 +01:00
if ($('.yes').is(':checked')) {
2018-01-10 02:07:28 +01:00
$('#correct').fadeIn('slow');
2018-01-17 00:57:27 +01:00
$('.no').attr('disabled','disabled').delay(1000);;
$('.yes').prop('checked', true);
2018-01-16 15:35:05 +01:00
var sessionScore = getSessionScore();
var questionCouter = getQuestionCouter();
questionCouter++;
sessionScore++;
2018-01-17 00:57:27 +01:00
//fixuj ten projekt
2018-01-16 15:35:05 +01:00
$('#question_couter').text(questionCouter);
$('#session').text(sessionScore);
2018-01-24 02:16:58 +01:00
$('lifes').text(localStorage.getItem('life_couter'))
2018-01-16 15:35:05 +01:00
setSessionScore(sessionScore);
setQuestionCouter(questionCouter);
2018-01-17 00:57:27 +01:00
$('#restart').fadeIn('slow');
2018-01-24 11:28:31 +01:00
2017-12-19 17:02:39 +01:00
}
});
2018-01-16 15:35:05 +01:00
$('.no').click(function(){
2018-01-17 00:57:27 +01:00
if($('.no').is(':checked')){
2018-01-10 02:07:28 +01:00
$('#incorrect').fadeIn('slow');
2018-01-17 00:57:27 +01:00
$('.yes').attr('disabled','disabled').delay(1000);;
2018-01-24 11:28:31 +01:00
$('.no').attr('disabled', 'disabled');
$('#try_again').fadeIn('slow');
2018-01-10 02:07:28 +01:00
}
})
2017-12-19 11:27:17 +01:00
}
2018-01-10 02:07:28 +01:00
function updateHighScore() {
var current = $("#current").text();
var highest = $("#highest").text();
if (parseInt(current) > parseInt(highest)) {
$('#highest').text(current)
}
}
2018-01-24 11:28:31 +01:00
2018-01-10 02:07:28 +01:00
function placeFood() {
food = {
x: Math.round(Math.random() * (w - cw) / cw),
y: Math.round(Math.random() * (h - cw) / cw)
}
}
function placeSnake() {
snake = {
x: Math.round(Math.random() * (w - cw) / cw),
y: Math.round(Math.random() * (h - cw) / cw)
}
}
function blank() {
//Paint The Canvas
ctx.fillStyle = "black";
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = "white";
ctx.strokeRect(0, 0, w, h);
}
function check_borders() {
if (snake.x < 0 || snake.x > (w - cw) / cw || snake.y < 0 || snake.y > (h - cw) / cw) {
2018-01-16 15:35:05 +01:00
console.log("sprawdzimy")
2018-01-10 02:07:28 +01:00
}
}
function paint_cell(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * cw, y * cw, cw, cw);
ctx.strokeStyle = "white";
ctx.strokeRect(x * cw, y * cw, cw, cw);
}
function check_food() {
if (food.x == snake.x && food.y == snake.y) {
2018-01-16 15:35:05 +01:00
// var current = parseInt($('#current').text());
// current += 1;
// $('#current').text(current);
// checkScore(current);
stop(gameloop);
showQuestionTask();
2018-01-10 02:07:28 +01:00
placeFood();
}
}
2018-01-16 15:35:05 +01:00
gameloop = start();
2018-01-24 11:28:31 +01:00
2018-01-10 02:07:28 +01:00
placeFood();
paint_cell(food.x, food.y, "grey");
placeSnake();
paint_cell(snake.x, snake.y, "green");
//Keyboard Controller
$(document).keydown(function (e) {
//39: RIGHT
//37: LEFT
//38: UP
//40: DOWN
var key = e.which;
if (key == "37") {
snake.x -= 1;
d = "LEFT";
} else if (key == "38") {
snake.y -= 1;
d = "UP";
} else if (key == "39") {
snake.x += 1;
d = "RIGHT";
} else if (key == "40") {
snake.y += 1;
d = "DOWN";
}
2017-12-19 11:27:17 +01:00
2018-01-10 02:07:28 +01:00
check_food();
blank();
color = "grey";
paint_cell(food.x, food.y);
color = "green";
paint_cell(snake.x, snake.y);
2017-12-19 11:27:17 +01:00
2018-01-10 02:07:28 +01:00
});
2017-12-19 11:27:17 +01:00
2018-01-10 02:07:28 +01:00
});