// This file is part of www.nand2tetris.org // and the book "The Elements of Computing Systems" // by Nisan and Schocken, MIT Press. // File name: projects/11/Square/Square.jack /** Implements a graphical square. The square has top-left x and y coordinates, and a size. */ class Square { field int x, y; // screen location of the top-left corner of this square field int size; // length of this square, in pixels /** Constructs and draws a new square with a given location and size. */ constructor Square new(int ax, int ay, int asize) { let x = ax; let y = ay; let size = asize; do draw(); return this; } /** Disposes this square. */ method void dispose() { do Memory.deAlloc(this); return; } /** Draws this square in its current (x,y) location */ method void draw() { // Draws the square using the color black do Screen.setColor(true); do Screen.drawRectangle(x, y, x + size, y + size); return; } /** Erases this square. */ method void erase() { // Draws the square using the color white (background color) do Screen.setColor(false); do Screen.drawRectangle(x, y, x + size, y + size); return; } /** Increments the square size by 2 pixels (if possible). */ method void incSize() { if (((y + size) < 254) & ((x + size) < 510)) { do erase(); let size = size + 2; do draw(); } return; } /** Decrements the square size by 2 pixels (if possible). */ method void decSize() { if (size > 2) { do erase(); let size = size - 2; do draw(); } return; } /** Moves this square up by 2 pixels (if possible). */ method void moveUp() { if (y > 1) { // Erases the bottom two rows of this square in its current location do Screen.setColor(false); do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size); let y = y - 2; // Draws the top two rows of this square in its new location do Screen.setColor(true); do Screen.drawRectangle(x, y, x + size, y + 1); } return; } /** Moves the square down by 2 pixels (if possible). */ method void moveDown() { if ((y + size) < 254) { do Screen.setColor(false); do Screen.drawRectangle(x, y, x + size, y + 1); let y = y + 2; do Screen.setColor(true); do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size); } return; } /** Moves the square left by 2 pixels (if possible). */ method void moveLeft() { if (x > 1) { do Screen.setColor(false); do Screen.drawRectangle((x + size) - 1, y, x + size, y + size); let x = x - 2; do Screen.setColor(true); do Screen.drawRectangle(x, y, x + 1, y + size); } return; } /** Moves the square right by 2 pixels (if possible). */ method void moveRight() { if ((x + size) < 510) { do Screen.setColor(false); do Screen.drawRectangle(x, y, x + 1, y + size); let x = x + 2; do Screen.setColor(true); do Screen.drawRectangle((x + size) - 1, y, x + size, y + size); } return; } }