generated from nighthawkcoders/student
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'refs/remotes/origin/main'
- Loading branch information
Showing
17 changed files
with
951 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
--- | ||
toc: false | ||
comments: false | ||
layout: post | ||
title: Tic-Tac-Toe | ||
description: Simple Tic-Tac-Toe minigame made from youtube tutorial | ||
type: hacks | ||
courses: { compsci: {week: 7} } | ||
--- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Tic-tac-toe JavaScript</title> | ||
<link rel="stylesheet" href="styvles.css"> | ||
</head> | ||
<body> | ||
<div id="gameboard"></div> | ||
<p id="info"></p> | ||
</body> | ||
</html> | ||
|
||
<script> | ||
const gameBoard = document.querySelector('#gameboard') | ||
const infoDisplay = document.querySelector('#info') | ||
const startCells = [ | ||
"", "", "", "", "", "", "", "", "" | ||
] | ||
let go = "circle" | ||
infoDisplay.textContent = "Circle goes first" | ||
|
||
function createBoard(){ | ||
startCells.forEach((_cell, index) => { | ||
const cellElement = document.createElement('div') | ||
cellElement.classList.add('square') | ||
cellElement.id = index //giving id to each box element | ||
cellElement.addEventListener('click', addGo) | ||
gameBoard.append(cellElement) | ||
}) | ||
} | ||
createBoard() | ||
|
||
|
||
function addGo(e) { //add circle or cross if nothing is there yet | ||
console.log(e.target) //returns id of square each time you click it | ||
const goDisplay = document.createElement('div') | ||
goDisplay.classList.add(go) //add class of circle every time we click, append to whatever we click on | ||
e.target.append(goDisplay) //append element that we just created | ||
go = go === "circle" ? "cross" : "circle" //if this is true, return cross, otherwise return circle | ||
infoDisplay.textContent = "it is now " + go + "'s go." //will read, it is cross's go if it was circle's go before | ||
e.target.removeEventListener("click", addGo) | ||
checkScore() | ||
} | ||
|
||
function checkScore() { | ||
const allSquares = document.querySelectorAll(".square") | ||
const winningCombos = [ | ||
[0, 1, 2], [3, 4, 5], [6, 7, 8], //horizontal combos | ||
[0, 3, 6], [1, 4, 7], [2, 5, 8] //vertical combos | ||
[0, 4, 8], [2, 4, 6] | ||
] | ||
|
||
console.log(allSquares[4]) | ||
|
||
winningCombos.forEach(array => { | ||
const circleWins = array.every(cell => | ||
allSquares[cell].firstChild?.classList.contains('circle')) | ||
|
||
if (circleWins) { | ||
infoDisplay.textContent = "Circle Wins!" | ||
allSquares.forEach(square => square.replaceWith(square.cloneNode(true))) | ||
return | ||
} | ||
|
||
}) | ||
|
||
winningCombos.forEach(array => { | ||
const crossWins = array.every(cell => | ||
allSquares[cell].firstChild?.classList.contains('cross')) | ||
|
||
if (crossWins) { | ||
infoDisplay.textContent = "Cross Wins!" | ||
allSquares.forEach(square => square.replaceWith(square.cloneNode(true))) | ||
return | ||
} | ||
|
||
}) | ||
|
||
|
||
} | ||
</script> | ||
|
||
<style> | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-direction: column; | ||
height: 100vh; | ||
} | ||
|
||
#gameboard { /*hashtag for ID*/ | ||
width: 300px; | ||
height: 300px; | ||
background-color: black; | ||
display: flex; | ||
flex-wrap: wrap; | ||
border: solid 1px black; | ||
} | ||
|
||
.square { /*class of square*/ | ||
width: 100px; | ||
height: 100px; | ||
background-color: white; | ||
border: solid 2px black; | ||
box-sizing: border-box; | ||
display: flex; | ||
justify-content: center; /*centering O*/ | ||
align-items: center; | ||
} | ||
|
||
.circle { | ||
height: 90px; | ||
width: 90px; | ||
border-radius: 50%; | ||
border: 15px solid blue; | ||
box-sizing: border-box; | ||
} | ||
|
||
.cross { | ||
height: 90px; | ||
width: 90px; | ||
position: relative; | ||
transform: rotate(45deg); | ||
} | ||
|
||
.cross:before, .cross:after { | ||
content: ""; | ||
position: absolute; | ||
background-color: red; | ||
} | ||
|
||
.cross:before { | ||
left: 50%; | ||
width: 30%; | ||
margin-left: -15%; | ||
height: 100%; | ||
} | ||
|
||
.cross:after { | ||
top: 50%; | ||
height: 30%; | ||
margin-top: -15%; | ||
width: 100%; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
--- | ||
layout: base | ||
title: Alien World | ||
image: /images/alien_planet.jpg | ||
images: | ||
background: | ||
src: /images/Stone_Background.jpg | ||
dog: | ||
src: /images/dogSprite.png | ||
monkey: | ||
src: /images/monkeySprite.png | ||
--- | ||
|
||
<!-- Liquid code, run by Jekyll, used to define location of asset(s) --> | ||
{% assign backgroundFile = site.baseurl | append: page.images.background.src %} | ||
{% assign dogSpriteImage = site.baseurl | append: page.images.dog.src %} | ||
{% assign monkeySpriteImage = site.baseurl | append: page.images.monkey.src %} | ||
|
||
<style> | ||
#controls { | ||
position: relative; | ||
z-index: 2; /* Ensure the controls are on top */ | ||
} | ||
</style> | ||
|
||
<!-- Prepare DOM elements --> | ||
<!-- Wrap both the dog canvas and controls in a container div --> | ||
<div id="canvasContainer"> | ||
<div id="controls"> <!-- Controls --> | ||
<!-- Background controls --> | ||
<button id="toggleCanvasEffect">Invert</button> | ||
<!-- Dog controls --> | ||
<input type="radio" name="animation" id="idle"> | ||
<label for="idle">Idle</label> | ||
<input type="radio" name="animation" id="barking"> | ||
<label for="barking">Barking</label> | ||
<input type="radio" name="animation" id="walking" checked> | ||
<label for="walking">Walking</label> | ||
</div> | ||
</div> | ||
|
||
<script type="module"> | ||
import GameEnv from '{{site.baseurl}}/assets/js/alienWorld/GameEnv.js'; | ||
import GameObject from '{{site.baseurl}}/assets/js/alienWorld/GameObject.js'; | ||
import Background from '{{site.baseurl}}/assets/js/alienWorld/Background.js'; | ||
import Character from '{{site.baseurl}}/assets/js/alienWorld/Character.js'; | ||
import Platform from '{{site.baseurl}}/assets/js/alienWorld/Platform.js'; | ||
import { initDog } from '{{site.baseurl}}/assets/js/alienWorld/CharacterDog.js'; | ||
import { initMonkey } from '{{site.baseurl}}/assets/js/alienWorld/CharacterMonkey.js'; | ||
|
||
// Create a function to load an image and return a Promise | ||
function loadImage(src) { | ||
return new Promise((resolve, reject) => { | ||
const image = new Image(); | ||
image.src = src; | ||
image.onload = () => resolve(image); | ||
image.onerror = reject; | ||
}); | ||
} | ||
|
||
// Game loop | ||
function gameLoop() { | ||
for (var gameObj of GameObject.gameObjectArray){ | ||
gameObj.update(); | ||
gameObj.draw(); | ||
} | ||
requestAnimationFrame(gameLoop); // cycle game, aka recursion | ||
} | ||
|
||
// Window resize | ||
function setSize() { | ||
GameEnv.setGameEnv(); // Update GameEnv dimensions | ||
|
||
// Call the sizing method on all game objects | ||
for (var gameObj of GameObject.gameObjectArray){ | ||
gameObj.size(); | ||
} | ||
} | ||
|
||
|
||
async function startGame() { | ||
// Resolve file dependencies before starting gam | ||
try { | ||
|
||
// Define data for Game Objects | ||
|
||
const [backgroundImg, dogImg, monkeyImg] = await Promise.all([ | ||
loadImage('{{backgroundFile}}'), | ||
loadImage('{{dogSpriteImage}}'), | ||
loadImage('{{monkeySpriteImage}}'), | ||
]); | ||
|
||
// Prepare HTML with Background Canvas | ||
const backgroundCanvas = document.createElement("canvas"); | ||
backgroundCanvas.id = "background"; | ||
document.querySelector("#canvasContainer").appendChild(backgroundCanvas); | ||
|
||
// Prepare HTML with Dog Canvas | ||
const dogCanvas = document.createElement("canvas"); | ||
dogCanvas.id = "characters"; | ||
document.querySelector("#canvasContainer").appendChild(dogCanvas); | ||
|
||
// Prepare HTML with Monkey Canvas | ||
const monkeyCanvas = document.createElement("canvas"); | ||
monkeyCanvas.id = "characters"; | ||
document.querySelector("#canvasContainer").appendChild(monkeyCanvas); | ||
|
||
// Setup Globals | ||
GameEnv.gameSpeed = 2; | ||
GameEnv.controls = document.getElementById("controls"); | ||
GameEnv.gravity = 3; | ||
|
||
// Create Game Objects | ||
|
||
// Background object(s) | ||
const backgroundSpeedRatio = 0.5 | ||
var backgroundObj = new Background(backgroundCanvas, backgroundImg, backgroundSpeedRatio); | ||
|
||
// Character object(s) | ||
const dogSpeedRatio = 0.2 | ||
var dogObj = initDog(dogCanvas, dogImg, dogSpeedRatio); | ||
const monkeySpeedRatio = 0.7 | ||
var monkeyObj = initMonkey(monkeyCanvas, monkeyImg, monkeySpeedRatio); | ||
|
||
//var platform = new Platform(0, GameEnv.bottom - 50, GameEnv.innerWidth, 10); | ||
|
||
// Define Event Listeners | ||
|
||
// Listen for window resize events and trigger the handleResize function | ||
window.addEventListener('resize', setSize); | ||
|
||
/* Toggle "canvas filter property" between alien and normal */ | ||
var isFilterEnabled = true; | ||
const defaultFilter = getComputedStyle(document.documentElement).getPropertyValue('--default-canvas-filter'); | ||
|
||
toggleCanvasEffect.addEventListener("click", function () { | ||
if (isFilterEnabled) { // toggle off | ||
backgroundCanvas.style.filter = "none"; // remove filter | ||
dogCanvas.style.filter = "none"; | ||
monkeyCanvas.style.filter = "none"; | ||
} else { // toggle on | ||
backgroundCanvas.style.filter = defaultFilter; // Apply the default filter value | ||
dogCanvas.style.filter = defaultFilter; | ||
monkeyCanvas.style.filter = defaultFilter; | ||
} | ||
isFilterEnabled = !isFilterEnabled; // switch boolean value | ||
}); | ||
|
||
|
||
// Start the game | ||
|
||
setSize(); | ||
gameLoop(); | ||
|
||
|
||
// Trap errors on bad images | ||
} catch (error) { | ||
console.error('Failed to load one or more images:', error); | ||
} | ||
} | ||
|
||
// Call the initializeGame function to start the game | ||
startGame(); | ||
|
||
</script> | ||
|
Oops, something went wrong.