Square program moving across the board.

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Square program moving across the board.

biglustypanda
So when i was running the square game provided and try to move the square around the board. I notice that the square keep moving constantly like a snake game rather than move it by 2 pixel. Is there a reason why that happen and how can I make it only move 2 pixel to a certain direction.
Reply | Threaded
Open this post in threaded view
|

Re: Square program moving across the board.

cadet1620
Administrator
That is the way that the square game was programmed to run.

The keyboard control is all in the SquareGame.run() method. You can experiment with modifying the Square game to learn more about Jack programming.

For instance comment out this line and the square will only move while a key is down.
        while (~exit) {
            // waits for a key to be pressed.
            while (key = 0) {
                let key = Keyboard.keyPressed();
//              do moveSquare();
            }

After making that change, try this:

Holding down 'z' and 'x' (grow and shrink) causes the square to also move as it grows and shrinks.
Can you fix the code so that it only moves the square when an arrow key is down?

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Square program moving across the board.

biglustypanda
to fix that code to make the key move only when the arrow key is down
can we just move the 2nd movesquare method into each if statement after assigning direction.

but then the square move really slow. but when i tried to move the pixel to 5. it leaves a trails behind when it move around
Reply | Threaded
Open this post in threaded view
|

Re: Square program moving across the board.

cadet1620
Administrator
You might also want to look at moveSquare() and see how it handles direction==0.

That suggests the alternate solution of setting direction=0 in the changing size cases.

It's unclear which is the better "bug fix" to the existing game. If the game was being written from scratch with the intention that it would only move the square when an arrow was pressed, then your solution would be a better design, possibly adding a direction parameter to moveBlock() thus eliminating the direction state variable.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Square program moving across the board.

cadet1620
Administrator
In reply to this post by biglustypanda
biglustypanda wrote
but then the square move really slow. but when i tried to move the pixel to 5. it leaves a trails behind when it move around
Are you running the simulator speed set to fast speed with animation set to none?

When you change the move distance to 5 you also need to increase the width of the white rectangle that is drawn to erase the trailing edge of the square.

--Mark