// A graphic Pong bat. Has a location on the screen, width & height. // The bat has methods for drawing, erasing, moving left and right on the screen and // changing the width. // The bat is drawn in y=233, width = 32. class Bat { // the location on the screen field int x, y; // the width & height field int width, height; // The bat's movement direction field int direction; // 1=left,2=right // Constructs a new bat with the given location and width. constructor Bat new(int Ax, int Ay, int Awidth, int Aheight) { let x = Ax; let y = Ay; let width = Awidth; let height = Aheight; let direction = 2; do show(); return this; } // Deallocates the object's memory. method void dispose() { do Memory.deAlloc(this); return; } // Draws the bat on the screen. method void show() { do Screen.setColor(true); do draw(); return; } // Erases the bat from the screen. method void hide() { do Screen.setColor(false); do draw(); return; } // Draws the bat method void draw() { do Screen.drawRectangle(x, y, x + width, y + height); return; } // Sets the direction of the bat (0=stop, 1=left, 2=right). method void setDirection(int Adirection) { let direction = Adirection; return; } // returns the left edge of the bat. method void getLeft() { return x; } // returns the right edge of the bat. method void getRight() { return x + width; } // Sets the width. method void setWidth(int Awidth) { do hide(); let width = Awidth; do show(); return; } // Moves the bat according to its direction. method void move() { if (direction = 1) { let x = x - 4; if (x < 0) { let x = 0; } do Screen.setColor(false); do Screen.drawRectangle((x + width) + 1, y, (x + width) + 4, y + height); do Screen.setColor(true); do Screen.drawRectangle(x, y, x + 3, y + height); } else { let x = x + 4; if ((x + width) > 511) { let x = 511 - width; } do Screen.setColor(false); do Screen.drawRectangle(x - 4, y, x - 1, y + height); do Screen.setColor(true); do Screen.drawRectangle((x + width) - 3, y, x + width, y + height); } return; } }