Skip to content

Commit

Permalink
Commiting a lot of things for some reason
Browse files Browse the repository at this point in the history
  • Loading branch information
leila010 committed Oct 11, 2024
1 parent d3e2aab commit 49220cb
Show file tree
Hide file tree
Showing 27 changed files with 1,889 additions and 11 deletions.
14 changes: 8 additions & 6 deletions _notebooks/fundamentals/POPCORN HACK 1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 10,
"metadata": {
"vscode": {
"languageId": "html"
Expand All @@ -234,7 +234,7 @@
"\n",
"<div id=\"block\" style=\"width: 36px; height: 36px; background-color: #1100ff; margin-top: 10px;\"></div>\n",
"\n",
"%%javascript\n",
"<script>\n",
"\n",
"function submitScale() {\n",
" const BLOCK_SCALE_DIVISOR = 20;\n",
Expand All @@ -257,7 +257,8 @@
" block.style.width = \"0px\";\n",
" block.style.height = \"0px\"; // Reset block size\n",
" }\n",
"}\n"
"}\n",
"</script>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
Expand All @@ -281,7 +282,7 @@
"\n",
"<div id=\"block\" style=\"width: 36px; height: 36px; background-color: #1100ff; margin-top: 10px;\"></div>\n",
"\n",
"%%javascript\n",
"<script>\n",
"\n",
"function submitScale() {\n",
" const BLOCK_SCALE_DIVISOR = 20;\n",
Expand All @@ -293,7 +294,7 @@
" \n",
" blockSize += 5; // Compound assignment example\n",
"\n",
" if (width >= 1 && width >= 100000) {\n",
" if (width >= 1 && width <= 100000) {\n",
" document.getElementById('output').innerHTML = \"Scale set to: \" + width + \" x \" + height + \" (Block size: \" + blockSize + \"px)\";\n",
" block.style.width = blockSize + \"px\";\n",
" block.style.height = blockSize + \"px\";\n",
Expand All @@ -304,7 +305,8 @@
" block.style.width = \"0px\";\n",
" block.style.height = \"0px\"; // Reset block size\n",
" }\n",
"}\n"
"}\n",
"</script>"
]
}
],
Expand Down
4 changes: 1 addition & 3 deletions _notebooks/fundamentals/POPCORN HACK 2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"metadata": {
"vscode": {
"languageId": "html"
Expand All @@ -223,7 +223,6 @@
"data": {
"text/html": [
"\n",
"<p>This example uses data types, operators, and functions to scale a block based on a user-defined width.</p>\n",
"\n",
"<!-- Input definitions -->\n",
"<div>\n",
Expand Down Expand Up @@ -290,7 +289,6 @@
"source": [
"%%html \n",
"\n",
"<p>This example uses data types, operators, and functions to scale a block based on a user-defined width.</p>\n",
"\n",
"<!-- Input definitions -->\n",
"<div>\n",
Expand Down
4 changes: 2 additions & 2 deletions _notebooks/fundamentals/POPCORN HACK 3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
" c.drawImage(imageBackground, 0, 0, canvas.width, canvas.height);\n",
" \n",
" // Draw squares in each corner of the canvas\n",
" c.fillStyle = 'red';\n",
" c.fillStyle = 'blue';\n",
" c.fillRect(0, 0, blockSize, blockSize); // Top-left\n",
" c.fillRect(canvas.width - blockSize, 0, blockSize, blockSize); // Top-right\n",
" c.fillRect(0, canvas.height - blockSize, blockSize, blockSize); // Bottom-left\n",
Expand Down Expand Up @@ -316,7 +316,7 @@
" c.drawImage(imageBackground, 0, 0, canvas.width, canvas.height);\n",
" \n",
" // Draw squares in each corner of the canvas\n",
" c.fillStyle = 'red';\n",
" c.fillStyle = 'blue';\n",
" c.fillRect(0, 0, blockSize, blockSize); // Top-left\n",
" c.fillRect(canvas.width - blockSize, 0, blockSize, blockSize); // Top-right\n",
" c.fillRect(0, canvas.height - blockSize, blockSize, blockSize); // Bottom-left\n",
Expand Down
45 changes: 45 additions & 0 deletions assets/js/rpg 0x/Background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import GameEnv from './GameEnv.js';

/* Background class for primary background
*/
export class Background {
constructor(data = null) {
if (data.src) {
this.image = new Image();
this.image.src = data.src;
} else {
this.image = null;
}
}

/* This method draws to GameEnv context, primary background
*/
draw() {
const ctx = GameEnv.ctx;
const width = GameEnv.innerWidth;
const height = GameEnv.innerHeight;

if (this.image) {
// Draw the background image scaled to the canvas size
ctx.drawImage(this.image, 0, 0, width, height);
} else {
// Fill the canvas with fillstyle color if no image is provided
ctx.fillStyle = '#87CEEB';
ctx.fillRect(0, 0, width, height);
}
}

/* For primary background, update is the same as draw
*/
update() {
this.draw();
}

/* For primary background, resize is the same as draw
*/
resize() {
this.draw();
}
}

export default Background;
62 changes: 62 additions & 0 deletions assets/js/rpg 0x/GameControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import GameEnv from './GameEnv.js';
import GameLevelSquares from './GameLevelSquares.js';
import GameLevelWater from './GameLevelWater.js';

/**
* The GameControl object manages the game.
*
* This code uses the JavaScript "object literal pattern" which is nice for centralizing control logic.
*
* The object literal pattern is a simple way to create singleton objects in JavaScript.
* It allows for easy grouping of related functions and properties, making the code more organized and readable.
* In the context of GameControl, this pattern helps centralize the game's control logic,
* making it easier to manage game states, handle events, and maintain the overall flow of the game.
*
* @type {Object}
* @property {Player} turtle - The player object.
* @property {Player} fish
* @property {function} start - Initialize game assets and start the game loop.
* @property {function} gameLoop - The game loop.
* @property {function} resize - Resize the canvas and player object when the window is resized.
*/
const GameControl = {

start: function(path) {
// Create the game environment
GameEnv.create();
// Load the game level
const gameLevel = new GameLevelWater(path);
// Prepare game objects for the level
for (let object of gameLevel.objects) {
if (!object.data) object.data = {};
GameEnv.gameObjects.push(new object.class(object.data));
}
// Start the game loop
this.gameLoop();
},

gameLoop: function() {
// Clear the canvas
GameEnv.clear();
// Update the game objects
for (let object of GameEnv.gameObjects) {
object.update(); // Update the game objects
}
// Recursively call the game loop
requestAnimationFrame(this.gameLoop.bind(this));
},

resize: function() {
// Resize the game environment
GameEnv.resize();
// Resize the game objects
for (let object of GameEnv.gameObjects) {
object.resize(); // Resize the game objects
}
}
};

// Detect window resize events and call the resize function.
window.addEventListener('resize', GameControl.resize.bind(GameControl));

export default GameControl;
124 changes: 124 additions & 0 deletions assets/js/rpg 0x/GameEnv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* GameEnv is a static class that manages the game environment.
*
* The focus of the file is the canvas management and the calculation of the game area dimensions.
* All calculations are based on the window size, header, and footer.
*
* This code uses a classic Java static class pattern, which is nice for managing centralized data.
*
* The static class pattern ensures that there is only one instance of the game environment,
* providing a single point of reference for all game objects. This approach helps maintain
* consistency and simplifies the management of shared resources like the canvas and its dimensions.
*
* @class GameEnv
* @property {Object} canvas - The canvas element.
* @property {Object} ctx - The 2D rendering context of the canvas.
* @property {number} innerWidth - The inner width of the game area.
* @property {number} innerHeight - The inner height of the game area.
* @property {number} top - The top offset of the game area.
* @property {number} bottom - The bottom offset of the game area.
*/
class GameEnv {
static gameObjects = [];
static canvas;
static ctx;
static innerWidth;
static innerHeight;
static top;
static bottom;

/**
* Private constructor to prevent instantiation.
*
* @constructor
* @throws {Error} Throws an error if an attempt is made to instantiate the class.
*/
constructor() {
throw new Error('GameEnv is a static class and cannot be instantiated.');
}

/**
* Create the game environment by setting up the canvas and calculating dimensions.
*
* This method sets the canvas element, calculates the top and bottom offsets,
* and determines the inner width and height of the game area. It then sizes the canvas
* to fit within the calculated dimensions.
*
* @static
*/
static create() {
this.setCanvas();
this.setTop();
this.setBottom();
this.innerWidth = window.innerWidth;
this.innerHeight = window.innerHeight - this.top - this.bottom;
this.size();
}

/**
* Sets the canvas element and its 2D rendering context.
*
* @static
*/
static setCanvas() {
this.canvas = document.getElementById('gameCanvas');
this.ctx = this.canvas.getContext('2d');
}

/**
* Sets the top offset based on the height of the header element.
*
* @static
*/
static setTop() {
const header = document.querySelector('header');
this.top = header ? header.offsetHeight : 0;
}

/**
* Sets the bottom offset based on the height of the footer element.
*
* @static
*/
static setBottom() {
const footer = document.querySelector('footer');
this.bottom = footer ? footer.offsetHeight : 0;
}

/**
* Sizes the canvas to fit within the calculated dimensions.
*
* @static
*/
static size() {
this.canvas.width = this.innerWidth;
this.canvas.height = this.innerHeight;
this.canvas.style.width = `${this.innerWidth}px`;
this.canvas.style.height = `${this.innerHeight}px`;
this.canvas.style.position = 'absolute';
this.canvas.style.left = '0px';
this.canvas.style.top = `${this.top}px`;
}

/**
* Resizes the game environment by re-creating it.
*
* @static
*/
static resize() {
this.create();
}

/**
* Clears the canvas.
*
* This method clears the entire canvas, making it ready for the next frame.
*
* @static
*/
static clear() {
this.ctx.clearRect(0, 0, this.innerWidth, this.innerHeight);
}
}

export default GameEnv;
17 changes: 17 additions & 0 deletions assets/js/rpg 0x/GameLevelSquares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// To build GameLevels, each contains GameObjects from below imports
import Background from './Background.js';
import PlayerOne from './PlayerOne.js';
import PlayerTwo from './PlayerTwo.js';

// Minimal Definition
class GameLevelSquares {
constructor(path) {
this.objects = [
{ class: Background, data: {} },
{ class: PlayerOne },
{ class: PlayerTwo },
];
}
}

export default GameLevelSquares;
Loading

0 comments on commit 49220cb

Please sign in to comment.