Programming 1
Week 11: Exercises

1. Random Lines

Write a program that draws 1000 random lines of length 300 pixels, each with a random color. All line angles should be equally likely.

2. Solar System

Write a program that displays an animation of the inner Solar System, with the Sun at the center. Assume that planets' orbits are circular. Mark each planet's orbit with a gray circle.

Use the following data, which is based on these assumptions:

body

radius (pixels)

color

dist from Sun (pixels)

orbital period (sec)

Sun

35

(254, 143, 54)

0

n/a

Mercury

5

(191, 144, 114)

116

8.8

Venus

12

(244, 190, 190)

216

22.5

Earth

13

(94, 192, 227)

300

36.5

Mars

7

(222, 70, 49)

456

68.7

3. Drawing Squares

Write a program that lets the user draw squares by dragging the mouse. Each square should have a random color.

4. Dragging Squares

Extend the previous program so that the user can move a square to a new position by clicking and dragging it. A moved square should rise to the top of the Z-order.

5. Rotating Triangle

Write a program that displays an equilateral triangle at the center of the window. The user should be able to rotate the triangle by holding down the left or right arrow keys.

6. Spaceship

Extend the previous program to display a spaceship in the form of a elongated triangle. The arrow keys should rotate the ship, and the A key should thrust it forward.

7. Animated Sort

Write a program that animates a bubble sort. The application should display a row of random integers. As the bubble sort proceeds, adjacent integers should exchange positions by rotating around a point between them.

8. Towers of Hanoi

Write a program that displays the Towers of Hanoi, with 5 discs initially on the left peg. Each time the user presses the space bar, display the next step in the sequence of moves that will move all discs to the right peg.

9. Simon

Implement the game of Simon .

10. Tic Tac Toe

Write a program that lets two players play tic tac toe. A mouse click should place a mark (X or O) for the current player. If any player wins, highlight the winning squares.