Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

Variables [13/14]

In this example, the purple disc moves across the screen. The disc starts the motion from the coordinates (460, 300). Run the program.

The disc's motion vector is (+5, -3); it is given by variables vx and vy. The numbers +5 and -3 are called vector's components.

Vectors are explained in great detail in one of the future chapters. For the present time, it doesn't matter if you don't know what vectors are.

Try changing the motion vector by modifying the inital values of variables vx and vy , so that the disc moves in some other direction and with different speed. Try out several combinations of values in the range from -10 to 10.

Vectors are commonly used to set speed and direction.

Exercise 1:

Change the motion vector so that the disc moves downward.

Solution

The variable vx should be set to zero. The variable vy should be set to a positive number.

Exercise 2:

Change the motion vector so that the disc ends the motion at the top left corner of the screen.

Solution

#num vx = -4.6;  << vx = -460 / 100
#num vy = -3;    << vy = -300 / 100  

Loading...