repl.it
@Charanpreet/

Lab 4.02 Example - Moving balls

Python (with Turtle)

No description

fork
loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import random
import time
import turtle


balls = []
for i in range(9):
  ball = turtle.Turtle()
  red_component = random.randint(0, 255)
  green_component = random.randint(0, 255)
  blue_component = random.randint(0, 255)
  ball.color(red_component, green_component, blue_component)
  ball.shape('circle')
  ball.speed(10)
  ball.penup()
  ball.goto(-200 + 50 * i, -200)
  balls.append(ball)

for ball in balls:
  ball_x = ball.xcor()
  ball_y = ball.ycor()
  # Switch the x and y coordinates.
  ball.goto(ball_y, ball_x)


# Use a loop to move all the balls to the right side of the screen.
for ball in balls:
  ball_x = ball.xcor()
  ball_y = ball.ycor()
  # Switch the x and y coordinates.
  ball.goto(ball_x + 400, ball_y)

# Use a loop to move all the balls to the top side of the screen.
for ball in balls:
  ball_x = ball.xcor()
  ball_y = ball.ycor()
  # Switch the x and y coordinates.
  ball.goto(ball_y, ball_x)

# Use a loop to move all the balls back to their orignal places.
for ball in balls:
  ball_x = ball.xcor()
  ball_y = ball.ycor()
  # Switch the x and y coordinates.
  ball.goto(ball_x, ball_y - 400)

# Use a loop to make each ball move up and then back down so that
# it looks like each ball is bouncing one after the other.
for ball in balls:
  ball_x = ball.xcor()
  ball_y = ball.ycor()
  # Switch the x and y coordinates.
  ball.goto(ball_x , ball_y + 60)
  ball.goto(ball_x , ball_y - 60)
result
console