/* A Square Game. In this game you can move a black square around the screen and change its size during the movement. In the beggining, the square is located at the top left corner. Use the arrow keys to move the square. Use 'z' & 'x' to decrement & increment the size. Use 'q' to quit. */ class SquareGame { // The square field Square square; // The square's movement direction field int direction; // 0=none,1=up,2=down,3=left,4=right // Constructs a new Square Game. constructor SquareGame new() { let square = Square.new(0, 0, 30); let direction = 0; return this; } // Deallocates the object's memory. method void dispose() { do square.dispose(); do Memory.deAlloc(this); return; } // Starts the game. Handles inputs from the user that controls // the square movement direction and size. method void run() { var char key; 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; } if (key = 90) { do square.decSize(); } if (key = 88) { do square.incSize(); } if (key = 131) { let direction = 1; } if (key = 133) { let direction = 2; } if (key = 130) { let direction = 3; } if (key = 132) { let direction = 4; } // waits for the key to be released. while (~(key = 0)) { let key = Keyboard.keyPressed(); do moveSquare(); } } return; } // Moves the square by 2 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; } }