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. Bouncing Ball

Write a program that displays a bouncing ball. Each time it hits the ground, it should rebound with a slightly smaller velocity, so that it eventually comes to a stop.

3. 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.

Draw the orbits at a scale of 500,000 km/pixel. At that scale the planets would hardly be visible, so draw the planets at 1000x their actual size, and the sun at 25x its actual size. In your simulation, let one second represent 10 days of actual time.

body

diameter (km)

color (R,G,B)

dist from Sun (x 10km)

orbital period (days)

Sun

1,393,000

(254, 143, 54)

0

n/a

Mercury

4,879

(191, 144, 114)

57.9

88.0

Venus

12,104

(244, 190, 190)

108.2

224.7

Earth

12,756

(94, 192, 227)

149.6

365.2

Mars

3,933

(222, 70, 49)

227.9

687.0

4. Drawing Squares

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

5. 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.

6. 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.

7. 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. Draw a random starfield in the background.

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.