{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "comments: true\n", "layout: post\n", "title: JavaScript For Loops and Sprites\n", "description: An introduction to JavaScript for loops and working with Sprites.\n", "permalink: /csse/javascript/fundamentals/for-loops/\n", "categories: [JavaScript Fundamentals]\n", "courses: { csse: {week: 7} }\n", "type: ccc\n", "---\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MetaData, Class, For Loops\n", "\n", "The objective of this article is to enhance your knowledge and understanding of MetaData, Class Definitions, and For-Loops.\n", "\n", "**MetaData**: In this article, MetaData contains information about the sprite, including its name, source URL, and orientation details such as the number of rows and columns, header size, padding, and jagged rows.\n", "\n", "**Class**: In this context, the canvas and drawing operations are initialized and stored in a class. These are used to output the sprite sheet image and individual frames within the sprite sheet.\n", " - **constructor**: Initializes the canvas, context, and sprite image.\n", " - **draw() method**: Uses nested **for-loops** to iterate through the sprite sheet and draw each frame independently on the canvas. It calculates the source and destination coordinates for each frame, taking into account the header and padding.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Introduction to For Loops\n", "\n", "For Loops are commonly used to iterate over data structures, including JavaScript Arrays and Objects.\n", "\n", "Below is an example of a conventional for loop that iterates over an array of names and displays each name in a paragraph (`

`) tag within a designated HTML div.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "html" } }, "outputs": [], "source": [ "\n", "

\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ForEach Loop\n", "\n", "The ForEach loop is another way to iterate over data structures, such as JavaScript Arrays and Objects. Below is an example of a ForEach loop that iterates over an array of coding adventures and displays each adventure in a paragraph (`

`) tag within a designated HTML div.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "html" } }, "outputs": [], "source": [ "\n", "

\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2D array\n", "\n", "There is a data structure called **arrays in arrays** or **2D arrays**. The data structure helps organize the data efficiently and access it using **nested loops**. Each row in the 2D array will represent a category (e.g., GitHub, Terminal), and each column will contain an array of questions and answers for that category.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "html" } }, "outputs": [], "source": [ "
\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hack #1: Apply Your Own Game Idea\n", "\n", "Create new code cells to implement some of the sprite interactions or features you have ideated for your game. This exercise is crucial if you plan to have interactions with a Non-Player Character (NPC).\n", "\n", "**Challenge**: Use the concepts of 2D arrays and nested loops to create and display interactions or features for your game. Think about how you can organize and manage different elements, such as NPC dialog, questions, and receiving answers.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sprite Files\n", "\n", "### Transition to Sprite Files\n", "\n", "Now that we have a solid understanding of data structures and loops, we will transition to working with Sprite Files. This section will help you understand how to manage and display sprite images, which are essential for creating animations in your game.\n", "\n", "### Sprite Files\n", "\n", "Sprite files are essentially a 2D table of sprite images. They contain 2D columnar sequences of pictures that aid in creating animation.\n", "\n", "### Display Sprite File\n", "\n", "The next code block shows a sprite file. This can be helpful in understanding the properties of your sprite. It contains `console.log` output that shows the sprite properties.\n", "\n", "Here are some terms that you will see in the next code block:\n", "- **MetaData**: Data that describes the file\n", " - **name**: A friendly identifier naming the file\n", " - **src**: The location of the file\n", "- **drawImage**: A function call that, when used with five parameters, outputs the entirety of the file\n", "- **class**: A coding structure that contains a constructor, data, and method (draw) to read and output a file\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "html" } }, "outputs": [], "source": [ "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "html" } }, "outputs": [], "source": [ "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%%js \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display Frames in Sprite File\n", "\n", "The next code block contains logic to extract frames within the sprite sheet. This is a more practical game enhancement compared to the previous example.\n", "\n", "Here are terms to describe key elements in the code:\n", "\n", "- **MetaData**: Contains information about the sprite file, including its name, source URL, and orientation details.\n", " - **orientation**: Describes the layout of the sprite in the PNG file.\n", " - **header**: Size of the area of description above the sprite.\n", " - **pad**: Size of the area between the sprites.\n", " - **jagged**: Indicates that each row can contain a different number of sprites.\n", "- **drawImage**: In the 9-property format, it provides the ability to scale the source into the destination.\n", "- **class**: Continues using the constructor and draw methods for source and output; adds math to abstract each frame independently.\n", "- **for-loops**: Demonstrates nested for loops to process each frame within the 2D sprite sheet.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "html" } }, "outputs": [], "source": [ "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hack #2: Display Individual Sprites\n", "\n", "Create new code cell(s) to display individual sprites from a sprite sheet. This sprite sheet will potentially be used in your game.\n", "\n", "**Challenge**: Use the concepts of 2D arrays, nested loops, and sprite metadata to extract and display individual sprites. Think about how you can manage and display different frames or animations for your game characters or objects.\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }