JavaScript game ending alert? - javascript

I have created a functioning maze with arrow movement and coins to collect, however I'm breaking the game when trying to display a message that comes up once the user has collected all 6 rewards.
I have tried to copy someone's alert from github and apply it as best as I think I can to my work but it just breaks the canvas and nothing actually appears in the browser.
My code is below:
character = {
x: 6,
y: 4
}
var el = document.getElementById('game');
function drawWorld() {
el.innerHTML = '';
for (var y = 0; y < map.length; y = y + 1) {
for (var x = 0; x < map[y].length; x = x + 1) {
if (map[y][x] === 1) {
el.innerHTML += "<div class='borders'></div>";
} else if (map[y][x] === 2) {
el.innerHTML += "<div class='reward'></div>";
} else if (map[y][x] === 3) {
el.innerHTML += "<div class='ground'></div>";
} else if (map[y][x] === 5) {
el.innerHTML += "<div class='character'></div>";
}
}
el.innerHTML += "<br>";
}
winGame();
}
function winGame() {
if (!map[5].includes(2))
alert("Well done!");
}
drawWorld();
document.onkeydown = function(event) {
if (event.keyCode === 37) {
if (map[character.y][character.x - 1] !== 1) {
map[character.y][character.x] = 3;
character.x = character.x - 1;
map[character.y][character.x] = 5;
drawWorld();
}
} else if (event.keyCode === 38) {
if (map[character.y - 1][character.x] !== 1) {
map[character.y][character.x] = 3;
character.y = character.y - 1;
map[character.y][character.x] = 5;
drawWorld();
}
} else if (event.keyCode === 39) {
if (map[character.y][character.x + 1] !== 1) {
map[character.y][character.x] = 3;
character.x = character.x + 1;
map[character.y][character.x] = 5;
drawWorld();
}
} else if (event.keyCode === 40) {
if (map[character.y + 1][character.x] !== 1) {
map[character.y][character.x] = 3;
character.y = character.y + 1;
map[character.y][character.x] = 5;
drawWorld();
}
}
console.log(map)
}

Related

im trying to increment the score by 10 only when the end of myCar passes the end of enemyCar

Im trying to increment the score by 10 only when the end of myCar passes the end of enemyCar. The score should be incremented by 10 at each passing and each time i reload it I can't seem to start with 0 score or the whole score just disappears:
Here's my code below:
<script>
const score = document.querySelector(".score");
const startScreen = document.querySelector(".startScreen");
const gameArea = document.querySelector(".gameArea");
startScreen.addEventListener("click", initializeGame);
let player = { speed: 5, score: 0 };
let keys = {
ArrowUp: false,
ArrowDown: false,
ArrowLeft: false,
ArrowRight: false,
};
document.addEventListener("keydown", keyDown);
document.addEventListener("keyup", keyUp);
function keyDown(e) {
e.preventDefault();
keys[e.key] = true;
}
function keyUp(e) {
e.preventDefault();
keys[e.key] = false;
}
function isCollide(a, b) {
aRect = a.getBoundingClientRect();
bRect = b.getBoundingClientRect();
return !(
aRect.bottom < bRect.top ||
aRect.top > bRect.bottom ||
aRect.right < bRect.left ||
aRect.left > bRect.right
);
}
function moveLines() {
let lines = document.querySelectorAll(".lines");
lines.forEach(function (item) {
if (item.y >= 700) {
item.y -= 750;
}
item.y += player.speed;
item.style.top = item.y + "px";
});
}
function endGame() {
clearInterval(timer);
player.start = false;
startScreen.classList.remove("hide");
startScreen.innerHTML =
"Game over <br> Your final score is " +
player.score +
" <br> press here to restart the game.";
}
function moveEnemy(myCar) {
let enemyCarList = document.querySelectorAll(".enemyCar");
enemyCarList.forEach(function (enemyCar) {
if (isCollide(myCar, enemyCar)) {
endGame();
}
if (enemyCar.y >= 750) {
enemyCar.y = -300;
enemyCar.style.left = Math.floor(Math.random() * 350) + "px";
}
// Here's the problem when i try to execute it doesn't show anything
if (myCar.x + myCar.y < enemyCar.x + enemyCar.y) {
player.score += 10;
}
enemyCar.y += player.speed;
enemyCar.style.top = enemyCar.y + "px";
});
}
function runGame() {
let car = document.querySelector(".myCar");
let road = gameArea.getBoundingClientRect();
if (player.start) {
moveLines();
moveEnemy(car);
if (keys.ArrowUp && player.y > road.top + 150) {
player.y -= player.speed;
}
if (keys.ArrowDown && player.y < road.bottom - 85) {
player.y += player.speed;
}
if (keys.ArrowLeft && player.x > 0) {
player.x -= player.speed;
}
if (keys.ArrowRight && player.x < road.width - 50) {
player.x += player.speed;
}
car.style.top = player.y + "px";
car.style.left = player.x + "px";
window.requestAnimationFrame(runGame);
player.score++;
score.innerText =
"Score: " + player.score + "\nSpeed: " + player.speed;
}
}
function initializeGame() {
startScreen.classList.add("hide");
gameArea.innerHTML = "";
player.start = true;
player.score = 0;
window.requestAnimationFrame(runGame);
for (x = 0; x < 5; x++) {
let roadLine = document.createElement("div");
roadLine.setAttribute("class", "lines");
roadLine.y = x * 150;
roadLine.style.top = roadLine.y + "px";
gameArea.appendChild(roadLine);
}
let car = document.createElement("div");
car.setAttribute("class", "myCar");
gameArea.appendChild(car);
player.x = car.offsetLeft;
player.y = car.offsetTop;
for (x = 0; x < 3; x++) {
let enemyCar = document.createElement("div");
enemyCar.setAttribute("class", "enemyCar");
enemyCar.y = (x + 1) * 350 * -1;
enemyCar.style.top = enemyCar.y + "px";
enemyCar.style.left = Math.floor(Math.random() * 350) + "px";
gameArea.appendChild(enemyCar);
}
}
</script>
I tried to put this in the function moveEnemy(myCar) but it doesn't work
if (myCar.x + myCar.y < enemyCar.x + enemyCar.y) {
player.score += 10;
}

Convert bits to binary value

function calculateBinary(bits) {
var bitValue = 0;
for(var i=0; i<bits.length; i++) {
if(bits[0] === '1') {
bitValue += 128;
} else if (bits[1] === '1') {
bitValue += 64;
} else if (bits[2] === '1') {
bitValue += 32;
} else if (bits[3] === '1') {
bitValue += 16;
} else if (bits[4] === '1') {
bitValue += 8;
} else if (bits[5] === '1') {
bitValue += 4;
} else if (bits[6] === '1') {
bitValue += 2;
} else if (bits[7] === '1') {
bitValue += 1;
}
}
return bitValue;
}
calculateBinary('11111111');
// Should return 255 (128 + 64 + 32 + 16 + 8 + 4 + 2 + 1)
Why is my for loop treating every iteration of the bits string as bits[0]? The returned value is '1028' or 12 * 8. What am I doing wrong to cause this in my For loop?
Consider
for(var i=0; i<bits.length; i++) {
if(bits[0] === '1') {
bitValue += 128;
} else if
You aren't checking the index inside the loop - you're always checking bits[0], and then if that condition doesn't succeed, you're going onto bits[1], etc, without regard to the index.
Remove the loop.
function calculateBinary(bits) {
var bitValue = 0;
if (bits[0] === '1') {
bitValue += 128;
}
if (bits[1] === '1') {
bitValue += 64;
}
if (bits[2] === '1') {
bitValue += 32;
}
if (bits[3] === '1') {
bitValue += 16;
}
if (bits[4] === '1') {
bitValue += 8;
}
if (bits[5] === '1') {
bitValue += 4;
}
if (bits[6] === '1') {
bitValue += 2;
}
if (bits[7] === '1') {
bitValue += 1;
}
return bitValue;
}
console.log(calculateBinary('11111111'));
// Should return 255 (128 + 64 + 32 + 16 + 8 + 4 + 2 + 1)
Or use the index inside the loop to calculate the amount to add.
function calculateBinary(bits) {
return [...bits].reverse().reduce(
(a, bit, i) => bit === '0' ? a : a + 2 ** i,
0
);
}
console.log(calculateBinary('11111111'));
console.log(calculateBinary('1000'));
(or, even better, don't reinvent the wheel)

How to add the operators * and / in this calculation algorithm

const calculate = (s) => {
let code;
let index;
let startIndex;
let stackSign = [];
let result = [0];
let numberSign = 1;
for (var i = 0; i < s.length; i++) {
if (s[i] === ' ') {
continue;
} else if (s[i] === '(') {
stackSign.push(numberSign);
result.push(0);
numberSign = 1;
} else if (s[i] === ')') {
result[result.length - 2] += result.pop() * stackSign.pop();
} else if (s[i] === '+') {
numberSign = 1;
} else if (s[i] === '-') {
numberSign = -1;
} else if (s[i] === '/') {
numberSign = result / 1;
} else if (s[i] === '*') {
numberSign = result * 1;
} else {
startIndex = i;
while (
(index = i + 1) < s.length &&
(code = s[index].charCodeAt()) > 47 &&
code < 58
) {
i++;
}
result[result.length - 1] +=
+s.substring(startIndex, i + 1) * numberSign;
}
}
return result.pop();
};
Hello guys i made this algorithm to create a calculator without javascript "eval" but im struggling to add the * and / operators, can you guys help me with that? thanks
How i'm using it:
calculate('50 + 50');

Won't let me set class name of a Javascript created div

I'm using a function to create a div element on the page. I want to assign a className but I get the error: cannot set property className of undefined.
But I did this in another function and it worked. Why is this one not working?
If I failed to provide the code relevant to the problem I apologize. I'm in a rush and included code that I thought might be relevant in order of relevancy.
** WORKING FUNCTION **
function makeBomb() {
if (player.ready && (player.hasBomb < player.maxBombs)) {
player.score -= 300;
player.hasBomb++;
player.bomb = document.createElement('div');
player.bomb.className = 'bomb'; //DOESN'T THROW ERROR ----
gameArea.appendChild(player.bomb);
player.bomb.x = player.x;
player.bomb.y = player.y;
player.bomb.style.left = player.bomb.x + 'px';
player.bomb.style.top = player.bomb.y + 'px';
player.ready = false;
setTimeout(function () {
player.ready = true;
}, 1000);
}
}
** NOT WORKING FUNCTION **
function makeBullet() {
if (player.enemy.bulletCount < player.enemy.maxBulletCount &&
player.enemy.bulletInterval == true)
player.enemy.bullet = document.createElement('div');
player.enemy.bullet.className = 'bullet'; //THROWS ERROR -----
gameArea.appendChild(player.enemy.bullet);
player.enemy.bullet.x = player.enemy.x;
player.enemy.bullet.y = player.enemy.y;
player.enemy.bullet.style.left = player.enemy.bullet.x +
(player.enemy.offsetWidth / 3) + 'px';
player.enemy.bullet.style.top = player.enemy.bullet.y +
(player.enemy.offsetHeight / 4) + 'px';
player.enemy.bulletCount++;
player.enemy.bulletInterval = false;
setInterval(function(){
player.enemy.bulletInterval = true;
}, 4000);
}
** STARTING CODE THAT MIGHT HAVE CONTEXT IDK**
function start() {
if (player.games === 1) {
gameArea.removeChild(finalScore);
}
gameMessage.style.display = 'none';
score.style.display = "inline-block";
scoreArea.style.display = "inline-block";
text.style.display = "inline-block";
player.inplay = true;
makeEnemy();
player.plane = document.createElement("div");
player.plane.setAttribute("class", "plane");
gameArea.appendChild(player.plane);
player.enemy.x = player.enemy.offsetLeft;
player.enemy.y = player.enemy.offsetTop;
player.x = player.plane.offsetLeft;
player.y = player.plane.offsetTop;
window.requestAnimationFrame(playGame);
}
** OTHER CODE THAT MIGHT HAVE CONTEXT **
function makeEnemy() {
player.enemy = document.createElement('div');
player.enemy.className = 'enemy';
player.enemy.style.left = Math.floor(Math.random() * .
gameArea.offsetWidth - 300) + 100 + 'px';
gameArea.appendChild(player.enemy);
player.enemy.x = player.enemy.offsetLeft;
}
** JUST IN CASE YOU NEED -- idk its hard for me to keep track of everything that might be related to the problem... sorry.. **
title.addEventListener("click", changeColor);
const gameArea = document.querySelector(".gameArea");
const game = document.querySelector(".game");
const scoreArea = document.querySelector(".scoreArea");
const score = document.querySelector(".score");
const text = document.querySelector(".text");
document.addEventListener('keydown', pressOn);
document.addEventListener('keyup', pressOff);
gameMessage.addEventListener('click', start);
let player = {
score: 2000,
speed: 5,
inplay: false,
ready: true,
maxBombs: 4,
hasBomb: 0,
hit: 0,
hitMax: 9,
games: 0,
enemy: {
x: 0
},
getHit: 0,
getHitMax: 20,
swing: false
}
let keys = {
space: false
}
** FULL JS FILE: BE WARNED IM VERY NEW AND THIS IS NOT GOOD CODE **
const title = document.querySelector(".title");
const gameMessage = document.querySelector(".gameMessage");
function changeColor() {
let newArray = ["darksalmon", "lightsalmon", "crimson", "red", "deeppink", "yellowgreen", "ghostwhite"];
let random = Math.floor(Math.random() * Math.floor(newArray.length - 1));
if (title.style.color != newArray[random]) {
title.style.color = newArray[random];
console.log(title.style.color);
} else {
changeColor();
}
}
title.addEventListener("click", changeColor);
const gameArea = document.querySelector(".gameArea");
const game = document.querySelector(".game");
const scoreArea = document.querySelector(".scoreArea");
const score = document.querySelector(".score");
const text = document.querySelector(".text");
document.addEventListener('keydown', pressOn);
document.addEventListener('keyup', pressOff);
gameMessage.addEventListener('click', start);
let player = {
score: 2000,
speed: 5,
inplay: false,
ready: true,
maxBombs: 4,
hasBomb: 0,
hit: 0,
hitMax: 9,
games: 0,
enemy: {
x: 0
},
getHit: 0,
getHitMax: 20,
swing: false
}
let keys = {
space: false
}
function start() {
if (player.games === 1) {
gameArea.removeChild(finalScore);
}
gameMessage.style.display = 'none';
score.style.display = "inline-block";
scoreArea.style.display = "inline-block";
text.style.display = "inline-block";
player.inplay = true;
makeEnemy();
player.plane = document.createElement("div");
player.plane.setAttribute("class", "plane");
gameArea.appendChild(player.plane);
player.enemy.x = player.enemy.offsetLeft;
player.enemy.y = player.enemy.offsetTop;
player.x = player.plane.offsetLeft;
player.y = player.plane.offsetTop;
window.requestAnimationFrame(playGame);
}
function playGame() {
if (player.inplay) {
moveBomb();
if(player.x < (gameArea.offsetWidth / 2)) {
console.log('WORKED');
makeBullet();
}
if (player.swing){
player.plane.style.backgroundImage ='url(guts1.png)';
player.swing = false;
//player.plane.style.width = 210 + 'px';
}
if (keys['x'] && player.enemy && isCollide(player.plane, player.enemy)) {
removeEnemy();
}
if (player.enemy) {
if (isCollide(player.plane, player.enemy)) {
player.getHit++;
if (player.getHit > player.getHitMax){
endGame();
}
} else {
player.getHit = 0;
}
if (player.x > player.enemy.x) {
player.enemy.x += 1;
}
if (player.x < player.enemy.x) {
player.enemy.x -= 1;
}
player.enemy.style.left = player.enemy.x + 'px';
}
if (player.hasBomb >= player.maxBombs && player.bomb.y > gameArea.offsetHeight - 20) {
endGame();
}
if (keys.space) {
makeBomb()
}
if (keys.ArrowUp && player.y > 0) {
player.y -= (player.speed + (player.speed * .5));
}
if (keys.ArrowDown && player.y < (gameArea.offsetHeight - player.plane.offsetHeight - 30)) {
player.y += (player.speed + (player.speed * .5));
}
if (keys.ArrowLeft && player.x > 0) {
player.x -= (player.speed + (player.speed * .5));
}
if (keys.ArrowRight && player.x < (gameArea.offsetWidth)) {
player.x += (player.speed - (player.speed * .5));
}
if (player.x == (gameArea.offsetWidth)) {
player.x = 0;
player.score -= 100;
if (!player.enemy) {
makeEnemy();
}
}
player.score -= .4;
if (player.score < 0) {
player.score = 0;
}
player.x += (player.speed * .5);
score.innerHTML = Math.floor(player.score) + ' Bombs left: ' + (player.maxBombs - player.hasBomb);
player.plane.style.left = player.x + 'px';
player.plane.style.top = player.y + 'px';
window.requestAnimationFrame(playGame);
}
}
function pressOn(e) {
e.preventDefault();
let tempKey = (e.key == " ") ? "space" : e.key;
keys[tempKey] = true;
if (keys['x'] && player.swing == false){
playerPlane = player.plane;
player.plane.style.backgroundImage ='url(guts2.png)';
setTimeout(function () {
player.swing = true;
}, 300);
//player.plane.style.width = 400 + 'px';
}
console.log(tempKey)
console.log(keys);
}
function pressOff(e) {
e.preventDefault();
let tempKey = (e.key== " ") ? "space" : e.key;
console.log(tempKey);
// if (keys['x'] && player.swing){
// playerPlane = player.plane;
// player.plane.style.backgroundImage ='url(guts1.png)';
// player.swing = false;
// //player.plane.style.width = 210 + 'px';
//
// }
if (keys['space'] || keys['x']) {
keys['space'] = 0;
keys['x'] = 0;
}
keys[tempKey] = false;
console.log(keys);
}
function moveBomb() {
let bombs = document.querySelectorAll('.bomb');
bombs.forEach(function (item) {
item.y += 10;
item.style.top = item.y + 'px';
if (item.y > gameArea.offsetHeight) {
item.parentNode.removeChild(item);
player.bomb = null;
}
if (player.enemy && player.bomb) {
if (isCollide(item, player.enemy)) {
player.hit++;
}
}
if (player.hit > player.hitMax) {
item.parentNode.removeChild(item);
player.bomb = null;
player.score += 2000;
player.hit = 0;
player.hasBomb -= 2
gameArea.removeChild(player.enemy);
player.enemy = null;
}
})
}
function makeEnemy() {
player.enemy = document.createElement('div');
player.enemy.className = 'enemy';
player.enemy.style.left = Math.floor(Math.random() * gameArea.offsetWidth - 300) + 100 + 'px';
gameArea.appendChild(player.enemy);
player.enemy.x = player.enemy.offsetLeft;
}
//function getLocationX(a){
// let aRect = a.getBoundingClientRect();
// let aX = aRect.x;
// return aX;
// //w console.log(aX);
//}
//
//function getLocationY(a){
// let aRect = a.getBoundingClientRect();
// let aY = aRect.y;
// return aY;
// // console.log(aY);
//}
function isCollide(a, b) {
let aRect = a.getBoundingClientRect();
let bRect = b.getBoundingClientRect();
return !(
(aRect.bottom < bRect.top) ||
(aRect.top > bRect.bottom) ||
(aRect.right < bRect.left) ||
(aRect.left > bRect.right)
)
}
function makeBomb() {
if (player.ready && (player.hasBomb < player.maxBombs)) {
player.score -= 300;
player.hasBomb++;
player.bomb = document.createElement('div');
player.bomb.className = 'bomb';
gameArea.appendChild(player.bomb);
player.bomb.x = player.x;
player.bomb.y = player.y;
player.bomb.style.left = player.bomb.x + 'px';
player.bomb.style.top = player.bomb.y + 'px';
player.ready = false;
setTimeout(function () {
player.ready = true;
}, 1000);
}
}
function makeBullet() {
if (player.enemy.bulletCount < player.enemy.maxBulletCount && player.enemy.bulletInterval == true)
player.enemy.bullet = document.createElement('div');
player.enemy.bullet.className = 'bullet';
gameArea.appendChild(player.enemy.bullet);
player.enemy.bullet.x = player.enemy.x;
player.enemy.bullet.y = player.enemy.y;
player.enemy.bullet.style.left = player.enemy.bullet.x + (player.enemy.offsetWidth / 3) + 'px';
player.enemy.bullet.style.top = player.enemy.bullet.y + (player.enemy.offsetHeight / 4) + 'px';
player.enemy.bulletCount++;
player.enemy.bulletInterval = false;
setInterval(function(){
player.enemy.bulletInterval = true;
}, 4000);
}
function endGame() {
if (player.enemy) {
gameArea.removeChild(player.enemy);
}
gameArea.removeChild(player.plane);
if (player.bomb){
gameArea.removeChild(player.bomb);
player.bomb = null;
}
score.style.display = 'none';
scoreArea.style.display = 'none';
text.style.display = 'none';
gameMessage.style.display = 'inline-block';
player.inplay = false;
player.hasBomb = 0;
finalScore = document.createElement('div');
finalScore.classList.add('finalScore');
finalScore.innerHTML = 'YOU SCORED: ' + Math.floor(player.score);
gameArea.appendChild(finalScore);
player.games = 1;
player.getHit = 0;
}
function removeEnemy() {
gameArea.removeChild(player.enemy);
player.enemy = null;
player.score += 2000;
}
function makeBullet() {
if (player.enemy.bulletCount < player.enemy.maxBulletCount &&
player.enemy.bulletInterval == true)
youre missing curly braces at the end of the if statement and i think its interpretting the next line as a part single line if statement
change to
function makeBullet() {
if (player.enemy.bulletCount < player.enemy.maxBulletCount &&
player.enemy.bulletInterval == true) {
...
...
...
}
}

Need help printing 2 Fractal trees

Yesterday, I had asked for help in printing out a Fractal tree. That question was answered, but now I am trying to implement one more rule into my code. And while implementing it in my code, it is printing the tree, but not at the right coordinates. Here is a link to my previous question. I would like to print both the trees side-by-side instead of on top of each other. So my question is how do I achieve that? Thanks for all the help.
Here is the new code:
var sentence = "F";
var sentence2 = "F";
var sentence3 = "X";
var rules = [];
rules[0] = {
a: "F",
b: "F[+F]F[-F]F"
}
var rulesB = [];
rulesB[0] = {
a: "F",
b: "FF-[-F+F+F]+[+F-F-F]"
}
/*
var rulesC = [];
rulesC[0] = {
a: "F",
b: "FF"
}
rulesC[1] = {
a: "X",
b: "F-[[X]+X]+F[+FX]-X"
}
*/
var x = 50; // starting x
var y = 400; // starting y
var y_stack = []; // save & restore don't handle coordinates
var y_stack2 = []
draw();
function turtle(sentence, context){
for (var i = 0; i < sentence.length; i++) {
var current = sentence.charAt(i);
if (current == "F") {
y -= 20;
context.lineTo(x, y);
context.stroke();
} else if (current == "+") {
context.translate(x, y);
context.rotate((20 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "-") {
context.translate(x, y);
context.rotate((-20 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "[") {
context.save();
y_stack.push(y);
} else if (current == "]") {
context.restore();
y = y_stack.pop();
context.moveTo(x, y)
}
}
}
function generate(sentence){
var nextSentence = "";
for (var i = 0; i < sentence.length; i++) {
var current = sentence.charAt(i);
var found = false;
for (var j = 0; j < rules.length; j++ ) {
if (current == rules[j].a) {
found = true;
nextSentence += rules[j].b;
break;
}
}
if (!found) {
nextSentence += current;
}
}
return nextSentence;
}
function turtle2(sentence2, context){
for (var i = 0; i < sentence2.length; i++) {
var current = sentence2.charAt(i);
if (current == "F") {
y -= 20;
context.lineTo(x, y);
context.stroke();
} else if (current == "+") {
context.translate(x, y);
context.rotate((22.5 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "-") {
context.translate(x, y);
context.rotate((-22.5 * Math.PI) / 180);
context.translate(-x, -y);
} else if (current == "[") {
context.save();
y_stack2.push(y);
} else if (current == "]") {
context.restore();
y = y_stack2.pop();
context.moveTo(x, y)
}
}
}
function generate2(sentence2){
var nextsentence2 = "";
for (var i = 0; i < sentence2.length; i++) {
var current = sentence2.charAt(i);
var found = false;
for (var j = 0; j < rulesB.length; j++ ) {
if (current == rulesB[j].a) {
found = true;
nextsentence2 += rulesB[j].b;
break;
}
}
if (!found) {
nextsentence2 += current;
}
}
return nextsentence2;
}
function draw() {
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext('2d');
// context.moveTo(x, y);
for (i = 0; i < 2; i++) {
sentence = generate(sentence);
}
console.log(sentence);
turtle(sentence, context);
//context.moveTo(100,400);
for(i = 0; i < 2; i++) {
sentence2 = generate2(sentence2);
}
console.log(sentence2);
turtle2(sentence2, context);
}
/*
function turtle3(){
//resetMatrix();
context.translate(width/6, height);
for (var n = 0; n < sentence3.length; n++){
var current = sentence3.charAt(n);
if (current == "F"){
context.stroke("#28b232");
context.lineTo(0, -len3);
context.translate(0, -len3);
}else if (current == "X"){
context.stroke("#024d08");
context.lineTo(-len3, 0);
context.translate(0, -len3);
}else if (current == "+"){
context.rotate(angle2);
}else if (current == "-"){
context.rotate(-angle2);
}else if (current == "["){
save();
}else if (current == "]"){
restore();
}
}
}
*/
/*
function generate3(){
len2 *= .5;
var nextSentence3 = "";
for (var i = 0; i < sentence3.length; i++){
var current3 = sentence3.charAt(i);
var found3 = false;
for (var j = 0; j < rulesC.length; j++ ){
if (current3 == rulesC[j].a){
found3 = true;
nextSentence3 += rulesC[j].b;
break;
}
}
if (!found3){
nextSentence3 += current3;
}
}
sentence3 = nextSentence3;
turtle3();
}*/

Categories