Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

while Loop and Keyboard [7/11]

This program moves a disc when the left or right arrow key is pressed.

Movement is explained in more detail in chapter on animations.

Exercise 1

Try insering an if statement in order to prevent the x coordinate getting smaller than 50. This will prevent the disc from moving outside the left side of screen.

Solution

Insert this statement before the disc function call: if x<50
: x=50;

Exercise 2

Reload the original program. Try using the getter atleast to prevent the x coordinate getting smaller than 50.

Hint

The getter atleast cannot change the value of the variable x by itself. Using a statement of assignment is the only way to change the value of a variable.

Solution

Insert this statement before the disc function call: x = x.atleast(15);

Loading...