|
|
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;
}
|
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
|
|
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
|
|
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
|
|
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
|
Administrator
|
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
|
|
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 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
|
|