diff options
Diffstat (limited to 'projects/11/Square')
-rw-r--r-- | projects/11/Square/Main.jack | 17 | ||||
-rw-r--r-- | projects/11/Square/Main.vm | 11 | ||||
-rw-r--r-- | projects/11/Square/Square.jack | 110 | ||||
-rw-r--r-- | projects/11/Square/Square.vm | 298 | ||||
-rw-r--r-- | projects/11/Square/SquareGame.jack | 81 | ||||
-rw-r--r-- | projects/11/Square/SquareGame.vm | 168 |
6 files changed, 0 insertions, 685 deletions
diff --git a/projects/11/Square/Main.jack b/projects/11/Square/Main.jack deleted file mode 100644 index 0753893..0000000 --- a/projects/11/Square/Main.jack +++ /dev/null @@ -1,17 +0,0 @@ -// 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/Main.jack
-
-// (same as projects/09/Square/Main.jack)
-
-/** Initializes a new Square Dance game and starts running it. */
-class Main {
- function void main() {
- var SquareGame game;
- let game = SquareGame.new();
- do game.run();
- do game.dispose();
- return;
- }
-}
diff --git a/projects/11/Square/Main.vm b/projects/11/Square/Main.vm deleted file mode 100644 index a3b355b..0000000 --- a/projects/11/Square/Main.vm +++ /dev/null @@ -1,11 +0,0 @@ -function Main.main 1 -call SquareGame.new 0 -pop local 0 -push local 0 -call SquareGame.run 1 -pop temp 0 -push local 0 -call SquareGame.dispose 1 -pop temp 0 -push constant 0 -return diff --git a/projects/11/Square/Square.jack b/projects/11/Square/Square.jack deleted file mode 100644 index 5a92838..0000000 --- a/projects/11/Square/Square.jack +++ /dev/null @@ -1,110 +0,0 @@ -// 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 - -// (same as projects/09/Square/Square.jack) - -/** Implements a graphical square. */ -class Square { - - field int x, y; // screen location of the square's top-left corner - field int size; // length of this square, in pixels - - /** Constructs 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 the square on the screen. */ - method void draw() { - do Screen.setColor(true); - do Screen.drawRectangle(x, y, x + size, y + size); - return; - } - - /** Erases the square from the screen. */ - method void erase() { - do Screen.setColor(false); - do Screen.drawRectangle(x, y, x + size, y + size); - return; - } - - /** Increments the square size by 2 pixels. */ - 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. */ - method void decSize() { - if (size > 2) { - do erase(); - let size = size - 2; - do draw(); - } - return; - } - - /** Moves the square up by 2 pixels. */ - method void moveUp() { - if (y > 1) { - do Screen.setColor(false); - do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size); - let y = y - 2; - do Screen.setColor(true); - do Screen.drawRectangle(x, y, x + size, y + 1); - } - return; - } - - /** Moves the square down by 2 pixels. */ - 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. */ - 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. */ - 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; - } -} diff --git a/projects/11/Square/Square.vm b/projects/11/Square/Square.vm deleted file mode 100644 index 871ccf7..0000000 --- a/projects/11/Square/Square.vm +++ /dev/null @@ -1,298 +0,0 @@ -function Square.new 0 -push constant 3 -call Memory.alloc 1 -pop pointer 0 -push argument 0 -pop this 0 -push argument 1 -pop this 1 -push argument 2 -pop this 2 -push pointer 0 -call Square.draw 1 -pop temp 0 -push pointer 0 -return -function Square.dispose 0 -push argument 0 -pop pointer 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function Square.draw 0 -push argument 0 -pop pointer 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 0 -return -function Square.erase 0 -push argument 0 -pop pointer 0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 0 -return -function Square.incSize 0 -push argument 0 -pop pointer 0 -push this 1 -push this 2 -add -push constant 254 -lt -push this 0 -push this 2 -add -push constant 510 -lt -and -not -if-goto incSize.Else0 -push pointer 0 -call Square.erase 1 -pop temp 0 -push this 2 -push constant 2 -add -pop this 2 -push pointer 0 -call Square.draw 1 -pop temp 0 -label incSize.Else0 -push constant 0 -return -function Square.decSize 0 -push argument 0 -pop pointer 0 -push this 2 -push constant 2 -gt -not -if-goto decSize.Else0 -push pointer 0 -call Square.erase 1 -pop temp 0 -push this 2 -push constant 2 -sub -pop this 2 -push pointer 0 -call Square.draw 1 -pop temp 0 -label decSize.Else0 -push constant 0 -return -function Square.moveUp 0 -push argument 0 -pop pointer 0 -push this 1 -push constant 1 -gt -not -if-goto moveUp.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 2 -add -push constant 1 -sub -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 1 -push constant 2 -sub -pop this 1 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push constant 1 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveUp.Else0 -push constant 0 -return -function Square.moveDown 0 -push argument 0 -pop pointer 0 -push this 1 -push this 2 -add -push constant 254 -lt -not -if-goto moveDown.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push constant 1 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 1 -push constant 2 -add -pop this 1 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 2 -add -push constant 1 -sub -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveDown.Else0 -push constant 0 -return -function Square.moveLeft 0 -push argument 0 -pop pointer 0 -push this 0 -push constant 1 -gt -not -if-goto moveLeft.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 2 -add -push constant 1 -sub -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 0 -push constant 2 -sub -pop this 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push constant 1 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveLeft.Else0 -push constant 0 -return -function Square.moveRight 0 -push argument 0 -pop pointer 0 -push this 0 -push this 2 -add -push constant 510 -lt -not -if-goto moveRight.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push constant 1 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 0 -push constant 2 -add -pop this 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 2 -add -push constant 1 -sub -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveRight.Else0 -push constant 0 -return diff --git a/projects/11/Square/SquareGame.jack b/projects/11/Square/SquareGame.jack deleted file mode 100644 index 4fe7e39..0000000 --- a/projects/11/Square/SquareGame.jack +++ /dev/null @@ -1,81 +0,0 @@ -// 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/SquareGame.jack
-
-// (same as projects/09/Square/SquareGame.jack)
-
-/**
- * Implements the Square Dance game.
- * This simple game allows the user to move a black square around
- * the screen, and change the square's size during the movement.
- * When the game starts, a square of 30 by 30 pixels is shown at the
- * top-left corner of the screen. The user controls the square as follows.
- * The 4 arrow keys are used to move the square up, down, left, and right.
- * The 'z' and 'x' keys are used, respectively, to decrement and increment
- * the square's size. The 'q' key is used to quit the game.
- */
-
-class SquareGame {
- field Square square; // the square of this game
- field int direction; // the square's current direction:
- // 0=none, 1=up, 2=down, 3=left, 4=right
-
- /** Constructs a new Square Game. */
- constructor SquareGame new() {
- // Creates a 30 by 30 pixels square and positions it at the top-left
- // of the screen.
- let square = Square.new(0, 0, 30);
- let direction = 0; // initial state is no movement
- return this;
- }
-
- /** Disposes this game. */
- method void dispose() {
- do square.dispose();
- do Memory.deAlloc(this);
- return;
- }
-
- /** Moves the square in the current direction. */
- method void moveSquare() {
- if (direction = 1) { do square.moveUp(); }
- if (direction = 2) { do square.moveDown(); }
- if (direction = 3) { do square.moveLeft(); }
- if (direction = 4) { do square.moveRight(); }
- do Sys.wait(5); // delays the next movement
- return;
- }
-
- /** Runs the game: handles the user's inputs and moves the square accordingly */
- method void run() {
- var char key; // the key currently pressed by the user
- var boolean exit;
- let exit = false;
-
- while (~exit) {
- // waits for a key to be pressed
- while (key = 0) {
- let key = Keyboard.keyPressed();
- do moveSquare();
- }
- if (key = 81) { let exit = true; } // q key
- if (key = 90) { do square.decSize(); } // z key
- if (key = 88) { do square.incSize(); } // x key
- if (key = 131) { let direction = 1; } // up arrow
- if (key = 133) { let direction = 2; } // down arrow
- if (key = 130) { let direction = 3; } // left arrow
- if (key = 132) { let direction = 4; } // right arrow
-
- // waits for the key to be released
- while (~(key = 0)) {
- let key = Keyboard.keyPressed();
- do moveSquare();
- }
- } // while
- return;
- }
-}
-
-
-
diff --git a/projects/11/Square/SquareGame.vm b/projects/11/Square/SquareGame.vm deleted file mode 100644 index f2dd542..0000000 --- a/projects/11/Square/SquareGame.vm +++ /dev/null @@ -1,168 +0,0 @@ -function SquareGame.new 0 -push constant 2 -call Memory.alloc 1 -pop pointer 0 -push constant 0 -push constant 0 -push constant 30 -call Square.new 3 -pop this 0 -push constant 0 -pop this 1 -push pointer 0 -return -function SquareGame.dispose 0 -push argument 0 -pop pointer 0 -push this 0 -call Square.dispose 1 -pop temp 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function SquareGame.moveSquare 0 -push argument 0 -pop pointer 0 -push this 1 -push constant 1 -eq -not -if-goto moveSquare.Else0 -push this 0 -call Square.moveUp 1 -pop temp 0 -label moveSquare.Else0 -push this 1 -push constant 2 -eq -not -if-goto moveSquare.Else1 -push this 0 -call Square.moveDown 1 -pop temp 0 -label moveSquare.Else1 -push this 1 -push constant 3 -eq -not -if-goto moveSquare.Else2 -push this 0 -call Square.moveLeft 1 -pop temp 0 -label moveSquare.Else2 -push this 1 -push constant 4 -eq -not -if-goto moveSquare.Else3 -push this 0 -call Square.moveRight 1 -pop temp 0 -label moveSquare.Else3 -push constant 5 -call Sys.wait 1 -pop temp 0 -push constant 0 -return -function SquareGame.run 2 -push argument 0 -pop pointer 0 -push constant 0 -pop local 1 -label run.While0 -push local 1 -not -not -if-goto run.EndWhile0 -label run.While0.While0 -push local 0 -push constant 0 -eq -not -if-goto run.While0.EndWhile0 -call Keyboard.keyPressed 0 -pop local 0 -push pointer 0 -call SquareGame.moveSquare 1 -pop temp 0 -goto run.While0.While0 -label run.While0.EndWhile0 -push local 0 -push constant 81 -eq -not -if-goto run.While0.Else1 -push constant 1 -neg -pop local 1 -label run.While0.Else1 -push local 0 -push constant 90 -eq -not -if-goto run.While0.Else2 -push this 0 -call Square.decSize 1 -pop temp 0 -label run.While0.Else2 -push local 0 -push constant 88 -eq -not -if-goto run.While0.Else3 -push this 0 -call Square.incSize 1 -pop temp 0 -label run.While0.Else3 -push local 0 -push constant 131 -eq -not -if-goto run.While0.Else4 -push constant 1 -pop this 1 -label run.While0.Else4 -push local 0 -push constant 133 -eq -not -if-goto run.While0.Else5 -push constant 2 -pop this 1 -label run.While0.Else5 -push local 0 -push constant 130 -eq -not -if-goto run.While0.Else6 -push constant 3 -pop this 1 -label run.While0.Else6 -push local 0 -push constant 132 -eq -not -if-goto run.While0.Else7 -push constant 4 -pop this 1 -label run.While0.Else7 -label run.While0.While8 -push local 0 -push constant 0 -eq -not -not -if-goto run.While0.EndWhile8 -call Keyboard.keyPressed 0 -pop local 0 -push pointer 0 -call SquareGame.moveSquare 1 -pop temp 0 -goto run.While0.While8 -label run.While0.EndWhile8 -goto run.While0 -label run.EndWhile0 -push constant 0 -return |