Skip to content

Commit

Permalink
Blokazh Topki
Browse files Browse the repository at this point in the history
  • Loading branch information
GreeningSiren committed Dec 15, 2024
1 parent 8ed5ff4 commit 146dea0
Showing 1 changed file with 178 additions and 91 deletions.
269 changes: 178 additions & 91 deletions gaemjam/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
let myX, myY;
let topki = [];
const kufte = tryToLoad("kufte", "brown");
let startLenTopki = 20
let shot = false
let firstBall = true
let brTopki = 50;
let shot = false;
let firstBall = true;
let blok = [];
let jiwot = [];
let timer = 0;
let random = 5;
let win = false;
function init() {
// Kodut tuk se izpulnqva vednuj v nachaloto
myX = 400
myY = 580
myX = 400;
myY = 580;
for (let i = 0; i < 16; i++) {
blok[i] = [];
jiwot[i] = [];
for (let j = 0; j < 6; j++) {
if (randomInteger(3) !== 2) {
blok[i][j] = true;
jiwot[i][j] = randomInteger(random) + 1;
} else {
blok[i][j] = false;
}
Expand All @@ -23,114 +29,187 @@ function init() {
}
function update() {
// Kodut tuk se izpulnqva (okolo) 100 puti v sekunda
if (shot) {
// firstBall = true
for (let i = 0; i < topki.length; i++) {
topki[i].moveTopka();
if (areColliding(-10, 1, 10, 800, topki[i].x, topki[i].y, 20, 20) || areColliding(800, 1, 20, 800, topki[i].x, topki[i].y, 20, 20)) { // proverka lqvo i dqsno
topki[i].dx = -topki[i].dx
}
if (areColliding(0, 0, 800, 10, topki[i].x, topki[i].y, 20, 20)) { // proverka gore
topki[i].dy = -topki[i].dy
}

if (topki[0].y >= 580 && firstBall) {
myX = topki[0].x
firstBall = false
}
if (topki[i].y >= 580 && topki[i].x != myX) {
topki[i].dy = 0
topki[i].dx = myX - topki[i].x
}
if (topki[i].y >= 580 && Math.floor(topki[i].x) == Math.floor(myX)) {
topki.splice(i, 1)
i--;
continue;
}
if (!win) {
if (shot) {
// firstBall = true
for (let i = 0; i < topki.length; i++) {
topki[i].moveTopka();
if (areColliding(-10, 1, 10, 800, topki[i].x, topki[i].y, 20, 20) || areColliding(800, 1, 20, 800, topki[i].x, topki[i].y, 20, 20)) { // proverka lqvo i dqsno
topki[i].dx = -topki[i].dx;
}
if (areColliding(0, 0, 800, 10, topki[i].x, topki[i].y, 20, 20)) { // proverka gore
topki[i].dy = -topki[i].dy;
}
if (topki[i].y >= 580 && topki[i].x != myX) {
if (firstBall) {
myX = topki[i].x;
firstBall = false;
}
topki[i].y = 580;
topki[i].dy = 0;
topki[i].dx = (myX - topki[i].x) / 13;
}
if (topki[i].y >= 580 && Math.floor(topki[i].x) == Math.floor(myX)) {
topki.splice(i, 1);
i--;
continue;
}

for (let j = 0; j < blok.length; j++) {
for (let k = 0; k < blok[j].length; k++) {
if (blok[j][k]) {
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50), k * 50, 50, 5)) { // gore proverka
topki[i].dy = -topki[i].dy;
blok[j][k] = false;
break;
}
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50), (k * 50) + 1, 5, 50)) { // lqvo proverka
topki[i].dx = -topki[i].dx;
blok[j][k] = false;
break;
}
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50) + 45, k * 50, 5, 50)) { // dqsno proverka
topki[i].dx = -topki[i].dx;
blok[j][k] = false;
break;
}
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50), (k * 50) + 45, 50, 5)) { // dole proverka
topki[i].dy = -topki[i].dy;
blok[j][k] = false;
break;
for (let j = 0; j < blok.length; j++) {
for (let k = 0; k < blok[j].length; k++) {
if (blok[j][k]) {
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50), k * 50, 50, 5)) { // gore proverka
topki[i].dy = -topki[i].dy;
// blok[j][k] = false;
jiwot[j][k]--;
break;
}
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50), (k * 50) + 1, 5, 50)) { // lqvo proverka
topki[i].dx = -topki[i].dx;
// blok[j][k] = false;
jiwot[j][k]--;
break;
}
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50) + 45, k * 50, 5, 50)) { // dqsno proverka
topki[i].dx = -topki[i].dx;
// blok[j][k] = false;
jiwot[j][k]--;
break;
}
if (areColliding(topki[i].x, topki[i].y, 20, 20, (j * 50), (k * 50) + 45, 50, 5)) { // dole proverka
topki[i].dy = -topki[i].dy;
// blok[j][k] = false;
jiwot[j][k]--;
break;
}
if (jiwot[j][k] <= 0) {
blok[j][k] = false;
}
}
}
}

}
if (topki.length == 0) {
shot = false;
firstBall = true;
let giveBalls = true
for (let i = 0; i < blok.length; i++) {
for (let j = 0; j < blok[i].length; j++) {
if (blok[i][j]) {
giveBalls = false
}

}
let sigma;
if (randomInteger(3) !== 2) {
sigma = true;
} else {
sigma = false;
}
blok[i].unshift(sigma);
jiwot[i].unshift(randomInteger(random) + 1);

}
if (topki.length == 0) {
shot = false
firstBall = true
for(let i = 0; i < blok.length; i++) {
let sigma;
if (randomInteger(3) !== 2) {
sigma = true;
} else {
sigma = false;
}
blok[i].unshift(sigma);
if (giveBalls) {
brTopki += 50;

if (timer >= 16) {
win = true
}
}
timer++;
}
}
switch (Math.floor(timer)) {
case 0:
case 1:
case 2:
case 3:
random = 5;
break;

case 4:
case 5:
case 6:
case 7:
random = 10;
break;

case 8:
case 9:
case 10:
case 11:
random = 15;
break;

case 12:
case 13:
case 14:
case 15:
random = 25;
break;

case 16:
random = 35;
break;

default:
random = 35;
break;
}
}
}
function draw() {
// Tuk naprogramirai kakvo da se risuva
context.fillStyle = "black";
context.fillRect(0, 0, 800, 600);
// drawImage(backField, 0, 0, 800, 600);
drawImage(kufte, myX, myY, 20, 20);
for (let i = 0; i < topki.length; i++) {
drawImage(kufte, topki[i].x, topki[i].y, 20, 20);
}
for (let i = 0; i < blok.length; i++) {
for (let j = 0; j < blok[i].length; j++) {

if (blok[i][j]) {
drawImage(box, i * 50, j * 50, 50, 50);
// context.fillStyle = "red";
// context.fillRect((i * 50), (j * 50) + 1, 5, 50);

if (!win) {
// Tuk naprogramirai kakvo da se risuva
context.fillStyle = "black";
context.fillRect(0, 0, 800, 600);
// drawImage(backField, 0, 0, 800, 600);
drawImage(kufte, myX, myY, 20, 20);
for (let i = 0; i < topki.length; i++) {
drawImage(kufte, topki[i].x, topki[i].y, 20, 20);
}
writeText("13px Tahoma", "white", brTopki - topki.length, myX + 3, myY + 3);
for (let i = 0; i < blok.length; i++) {
for (let j = 0; j < blok[i].length; j++) {

if (blok[i][j]) {
drawImage(box, i * 50, j * 50, 50, 50);
writeText("30px Tahoma", "white", jiwot[i][j], i * 50 + 15, j * 50 + 15);
// context.fillStyle = "red";
// context.fillRect((i * 50), (j * 50) + 1, 5, 50);

}
}
}
} else {
context.fillStyle = "black";
context.fillRect(0, 0, 800, 600);
writeText("50px Tahoma", "white", "You won!", 400, 300);
}
}
function mouseup() {
// Pri klik s lqv buton - pokaji koordinatite na mishkata
console.log("Mouse clicked at", mouseX, mouseY);
console.log(shot);
if (!shot) {
let d = dist(myX, myY, mouseX, mouseY);
let raztoqnieX = mouseX - myX;
let raztoqnieY = mouseY - myY;
for (let i = 0; i < startLenTopki; i++) {
setTimeout(() => suzdajTopka(myX, myY, raztoqnieX / d * 2, raztoqnieY / d * 2, mouseX, mouseY), i * 100);
if (!win) {
// Pri klik s lqv buton - pokaji koordinatite na mishkata
console.log("Mouse clicked at", mouseX, mouseY);
console.log(shot);
if (!shot) {
let d = dist(myX, myY, mouseX, mouseY);
let raztoqnieX = mouseX - myX;
let raztoqnieY = mouseY - myY;
for (let i = 0; i < brTopki; i++) {
setTimeout(() => suzdajTopka(myX, myY, raztoqnieX / d * 2, raztoqnieY / d * 2, mouseX, mouseY), i * 100);
}
shot = true;
}
shot = true;
}
}
function mousedown() {
// set_d();
}
function keyup(key) {

// Pechatai koda na natisnatiq klavish
console.log("Pressed", key);
}
Expand All @@ -144,8 +223,8 @@ function suzdajTopka(x, y, dx, dy, mX, mY) {
dx: dx,
dy: dy,
moveTopka: function () {
this.x = this.x + this.dx ;
this.y = this.y + this.dy ;
this.x = this.x + this.dx;
this.y = this.y + this.dy;
}
});
}
Expand Down Expand Up @@ -173,3 +252,11 @@ function resetBoxes() {
}
}
}

function writeText(font, style, text, x, y) {
context.save();
context.font = font;
context.fillStyle = style;
context.fillText(text, x, y);
context.restore();
}

0 comments on commit 146dea0

Please sign in to comment.