# moving rectangle

 Classic List Threaded
7 messages
Reply | Threaded
Open this post in threaded view
|

## moving rectangle

 Can anyone tell me whats wrong with my code? It just extends downward and grows in length. It should move like an up and down pong bat.     method void moveDown() {         if(y < 254) {             do Screen.setColor(false);             do Screen.drawRectangle(x, y+1, width ,height + 1);             let y = y+2;             let height = height + 2;             do Screen.setColor(true);             do Screen.drawRectangle(x,y-1,width,height-1);         }         return;     }
Reply | Threaded
Open this post in threaded view
|

## Re: moving rectangle

 Administrator (It would be better to have width and height fixed and to code the draws as     drawRectangle(x, y+1, x+width, y+1+height) This way you don't have multiple variables the must be kept in lockstep.) You problem is that you are drawing the two rectangles exactly on top of each other.     draw(... y+1 ...)     yy = y+2     draw(... yy-1 ...) so yy-1 = y+2-1 = y+1. --Mark
Reply | Threaded
Open this post in threaded view
|

## Re: moving rectangle

 thanks. Funny, my implementation of moveUp worked… I had already gone down a rabbit hole by then and couldn’t see the obvious. On Nov 15, 2016, at 5:54 PM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote: (It would be better to have width and height fixed and to code the draws as     drawRectangle(x, y+1, x+width, y+1+height) This way you don't have multiple variables the must be kept in lockstep.) You problem is that you are drawing the two rectangles exactly on top of each other.     draw(... y+1 ...)     yy = y+2     draw(... yy-1 ...) so yy-1 = y+2-1 = y+1. --Mark If you reply to this email, your message will be added to the discussion below: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/moving-rectangle-tp4030537p4030538.html To unsubscribe from moving rectangle, click here. NAML
Reply | Threaded
Open this post in threaded view
|

## Re: moving rectangle

 In reply to this post by cadet1620 Could you tell me why this code is creating black streaks across the screen? I’m not really familiar with jack and affecting pixel movement:    method void moveUp() {        if(y > 2) {            do Screen.setColor(false);            do Screen.drawRectangle(x, y, width, height + 1);            let y = y - 2;            let height = height - 2;            do Screen.setColor(true);            do Screen.drawRectangle(x, y, width, height + 1);        }        return;    }    method void moveDown() {        if(height < 253) {            do Screen.setColor(false);            do Screen.drawRectangle(x, y, width ,height - 1);            let y = y + 2;            let height = height + 2;            do Screen.setColor(true);            do Screen.drawRectangle(x, y, width, height -1 );        }        return;    }    method void moveLeft() {        if(x > 2) {            do Screen.setColor(false);            do Screen.drawRectangle(x, y, width+1, height);            let x = x - 2;            let width = width - 2;            do Screen.setColor(true);            do Screen.drawRectangle(x, y, width+1, height);        }        return;    }    method void moveRight() {        if((width) < 509) {            do Screen.setColor(false);            do Screen.drawRectangle(x, y, width-1, height);            let x = x + 2;            let width = width + 2;            do Screen.setColor(true);            do Screen.drawRectangle(x, y, width-1, height);        }        return;    }On Nov 15, 2016, at 5:54 PM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote: (It would be better to have width and height fixed and to code the draws as     drawRectangle(x, y+1, x+width, y+1+height) This way you don't have multiple variables the must be kept in lockstep.) You problem is that you are drawing the two rectangles exactly on top of each other.     draw(... y+1 ...)     yy = y+2     draw(... yy-1 ...) so yy-1 = y+2-1 = y+1. --Mark If you reply to this email, your message will be added to the discussion below: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/moving-rectangle-tp4030537p4030538.html To unsubscribe from moving rectangle, click here. NAML
Reply | Threaded
Open this post in threaded view
|

## Re: moving rectangle

 In reply to this post by cadet1620 Width and height are fixed - and passed on construction. I just didn’t include that part of the code. On Nov 15, 2016, at 5:54 PM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote: (It would be better to have width and height fixed and to code the draws as     drawRectangle(x, y+1, x+width, y+1+height) This way you don't have multiple variables the must be kept in lockstep.) You problem is that you are drawing the two rectangles exactly on top of each other.     draw(... y+1 ...)     yy = y+2     draw(... yy-1 ...) so yy-1 = y+2-1 = y+1. --Mark If you reply to this email, your message will be added to the discussion below: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/moving-rectangle-tp4030537p4030538.html To unsubscribe from moving rectangle, click here. NAML
Reply | Threaded
Open this post in threaded view
|

## Re: moving rectangle

 Administrator In reply to this post by The_Larks The_Larks wrote Could you tell me why this code is creating black streaks across the screen? I’m not really familiar with jack and affecting pixel movement:     method void moveUp() {         if(y > 2) {             do Screen.setColor(false);             do Screen.drawRectangle(x, y, width, height + 1);             let y = y - 2;             let height = height - 2;             do Screen.setColor(true);             do Screen.drawRectangle(x, y, width, height + 1);         }         return;     }     method void moveDown() {         if(height < 253) {             do Screen.setColor(false);             do Screen.drawRectangle(x, y, width ,height - 1);             let y = y + 2;             let height = height + 2;             do Screen.setColor(true);             do Screen.drawRectangle(x, y, width, height -1 );         }         return;     } Screen.drawRectangle(x1, y1, x2, y2) draws a rectangle with corners (x1, y1) and (x2, y2). You need to draw the same sized rectangle in all four of these routines. The rectangles in moveDown() appear to be 2 pixels shorter than those in moveUp(). --Mark
Reply | Threaded
Open this post in threaded view
|

## Re: moving rectangle

 Thanks. Fixed. I got that by combining pong bat and squareGame. I must’ve missed something and thought it was integral part of creating movement. On Nov 16, 2016, at 2:15 PM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:The_Larks wroteCould you tell me why this code is creating black streaks across the screen? I’m not really familiar with jack and affecting pixel movement:     method void moveUp() {         if(y > 2) {             do Screen.setColor(false);             do Screen.drawRectangle(x, y, width, height + 1);             let y = y - 2;             let height = height - 2;             do Screen.setColor(true);             do Screen.drawRectangle(x, y, width, height + 1);         }         return;     }     method void moveDown() {         if(height < 253) {             do Screen.setColor(false);             do Screen.drawRectangle(x, y, width ,height - 1);             let y = y + 2;             let height = height + 2;             do Screen.setColor(true);             do Screen.drawRectangle(x, y, width, height -1 );         }         return;     }Screen.drawRectangle(x1, y1, x2, y2) draws a rectangle with corners (x1, y1) and (x2, y2). You need to draw the same sized rectangle in all four of these routines. The rectangles in moveDown() appear to be 2 pixels shorter than those in moveUp(). --Mark If you reply to this email, your message will be added to the discussion below:http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/moving-rectangle-tp4030537p4030542.htmlTo unsubscribe from moving rectangle, click here.NAML