@Charanpreet/

Lab 4.02 Example - Moving balls

Python (with Turtle)

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4374980802239792","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"aW1wb3J0IHJhbmRvbQppbXBvcnQgdGltZQppbXBvcnQgdHVydGxlCgoKYmFsbHMgPSBbXQpmb3IgaSBpbiByYW5nZSg5KToKICBiYWxsID0gdHVydGxlLlR1cnRsZSgpCiAgcmVkX2NvbXBvbmVudCA9IHJhbmRvbS5yYW5kaW50KDAsIDI1NSkKICBncmVlbl9jb21wb25lbnQgPSByYW5kb20ucmFuZGludCgwLCAyNTUpCiAgYmx1ZV9jb21wb25lbnQgPSByYW5kb20ucmFuZGludCgwLCAyNTUpCiAgYmFsbC5jb2xvcihyZWRfY29tcG9uZW50LCBncmVlbl9jb21wb25lbnQsIGJsdWVfY29tcG9uZW50KQogIGJhbGwuc2hhcGUoJ2NpcmNsZScpCiAgYmFsbC5zcGVlZCgxMCkKICBiYWxsLnBlbnVwKCkKICBiYWxsLmdvdG8oLTIwMCArIDUwICogaSwgLTIwMCkKICBiYWxscy5hcHBlbmQoYmFsbCkKCmZvciBiYWxsIGluIGJhbGxzOgogIGJhbGxfeCA9IGJhbGwueGNvcigpCiAgYmFsbF95ID0gYmFsbC55Y29yKCkKICAjIFN3aXRjaCB0aGUgeCBhbmQgeSBjb29yZGluYXRlcy4KICBiYWxsLmdvdG8oYmFsbF95LCBiYWxsX3gpCgoKIyBVc2UgYSBsb29wIHRvIG1vdmUgYWxsIHRoZSBiYWxscyB0byB0aGUgcmlnaHQgc2lkZSBvZiB0aGUgc2NyZWVuLgpmb3IgYmFsbCBpbiBiYWxsczoKICBiYWxsX3ggPSBiYWxsLnhjb3IoKQogIGJhbGxfeSA9IGJhbGwueWNvcigpCiAgIyBTd2l0Y2ggdGhlIHggYW5kIHkgY29vcmRpbmF0ZXMuCiAgYmFsbC5nb3RvKGJhbGxfeCArIDQwMCwgYmFsbF95KQoKIyBVc2UgYSBsb29wIHRvIG1vdmUgYWxsIHRoZSBiYWxscyB0byB0aGUgdG9wIHNpZGUgb2YgdGhlIHNjcmVlbi4KZm9yIGJhbGwgaW4gYmFsbHM6CiAgYmFsbF94ID0gYmFsbC54Y29yKCkKICBiYWxsX3kgPSBiYWxsLnljb3IoKQogICMgU3dpdGNoIHRoZSB4IGFuZCB5IGNvb3JkaW5hdGVzLgogIGJhbGwuZ290byhiYWxsX3ksIGJhbGxfeCkKCiMgVXNlIGEgbG9vcCB0byBtb3ZlIGFsbCB0aGUgYmFsbHMgYmFjayB0byB0aGVpciBvcmlnbmFsIHBsYWNlcy4KZm9yIGJhbGwgaW4gYmFsbHM6CiAgYmFsbF94ID0gYmFsbC54Y29yKCkKICBiYWxsX3kgPSBiYWxsLnljb3IoKQogICMgU3dpdGNoIHRoZSB4IGFuZCB5IGNvb3JkaW5hdGVzLgogIGJhbGwuZ290byhiYWxsX3gsIGJhbGxfeSAtIDQwMCkKCiMgVXNlIGEgbG9vcCB0byBtYWtlIGVhY2ggYmFsbCBtb3ZlIHVwIGFuZCB0aGVuIGJhY2sgZG93biBzbyB0aGF0CiMgaXQgbG9va3MgbGlrZSBlYWNoIGJhbGwgaXMgYm91bmNpbmcgb25lIGFmdGVyIHRoZSBvdGhlci4KZm9yIGJhbGwgaW4gYmFsbHM6CiAgYmFsbF94ID0gYmFsbC54Y29yKCkKICBiYWxsX3kgPSBiYWxsLnljb3IoKQogICMgU3dpdGNoIHRoZSB4IGFuZCB5IGNvb3JkaW5hdGVzLgogIGJhbGwuZ290byhiYWxsX3ggLCBiYWxsX3kgKyA2MCkKICBiYWxsLmdvdG8oYmFsbF94ICwgYmFsbF95IC0gNjAp"},"asBuffer":null},"loaded":true}}
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