-
Notifications
You must be signed in to change notification settings - Fork 0
/
bouncing_dvd_logo.py
138 lines (108 loc) · 4.14 KB
/
bouncing_dvd_logo.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sys, random, time
try:
import bext
except ImportError:
print('This program requires the bext module, which you')
print('can install by following the instructions at')
print('https://pypi.org/project/Bext/')
sys.exit()
WIDTH, HEIGHT = bext.size()
WIDTH -= 1
NUMBER_OF_LOGOS = 5
PAUSE_AMOUNT = 0.2
COLORS = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
UP_RIGHT = 'ur'
UP_LEFT = 'ul'
DOWN_RIGHT = 'dr'
DOWN_LEFT = 'dl'
DIRECTIONS = (UP_RIGHT, UP_LEFT, DOWN_RIGHT, DOWN_LEFT)
COLOR = 'color'
X = 'x'
Y = 'y'
DIR = 'direction'
def main():
bext.clear()
logos = []
for i in range(NUMBER_OF_LOGOS):
logos.append({COLOR: random.choice(COLORS),
X: random.randint(1, WIDTH - 4),
Y: random.randint(1, HEIGHT - 4),
DIR: random.choice(DIRECTIONS)})
if logos[-1][X] % 2 == 1:
logos[-1][X] -= 1
cornerBounces = 0
while True:
for logo in logos:
bext.goto(logo[X], logo[Y])
print(' ', end='')
originalDirection = logo[DIR]
if logo[X] == 0 and logo[Y] == 0:
logo[DIR] = DOWN_RIGHT
cornerBounces += 1
elif logo[X] == 0 and logo[Y] == HEIGHT - 1:
logo[DIR] = UP_RIGHT
cornerBounces += 1
elif logo[X] == WIDTH - 3 and logo[Y] == 0:
logo[DIR] = DOWN_LEFT
cornerBounces += 1
elif logo[X] == WIDTH - 3 and logo[Y] == HEIGHT - 1:
logo[DIR] = UP_LEFT
cornerBounces += 1
elif logo[X] == 0 and logo[DIR] == UP_LEFT:
logo[DIR] = UP_RIGHT
elif logo[X] == 0 and logo[DIR] == DOWN_LEFT:
logo[DIR] = DOWN_RIGHT
elif logo[X] == WIDTH - 3 and logo [DIR] == UP_RIGHT:
logo[DIR] = UP_LEFT
elif logo[X] == WIDTH - 3 and logo[DIR] == DOWN_RIGHT:
logo[DIR] = DOWN_LEFT
elif logo[Y] == 0 and logo[DIR] == UP_LEFT:
logo[DIR] = DOWN_LEFT
elif logo[Y] == 0 and logo[DIR] == UP_RIGHT:
logo[DIR] = DOWN_RIGHT
elif logo[Y] == HEIGHT - 1 and logo[DIR] == DOWN_LEFT:
logo[DIR] = UP_LEFT
elif logo[Y] == HEIGHT - 1 and logo[DIR] == DOWN_RIGHT:
logo[DIR] = UP_RIGHT
if logo[DIR] != originalDirection:
logo[COLOR] = random.choice(COLORS)
if logo[DIR] == UP_RIGHT:
logo[X] += 2
logo[Y] -= 1
elif logo[DIR] == UP_LEFT:
logo[X] -= 2
logo[Y] -= 1
elif logo[DIR] == DOWN_RIGHT:
logo[X] += 2
logo[Y] += 1
elif logo[DIR] == DOWN_LEFT:
logo[X] -= 2
logo[Y] += 1
bext.goto(5, 0)
bext.fg('white')
print('Corner bounces:', cornerBounces, end='')
for logo in logos:
bext.goto(logo[X], logo[Y])
bext.fg(logo[COLOR])
print('DVD', end='')
bext.goto(0, 0)
sys.stdout.flush()
time.sleep(PAUSE_AMOUNT)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print()
print('Bouncing DVD Logo, by Al Sweigart')
sys.exit()
'''
Exploring the Program
What happens if you change WIDTH, HEIGHT = bext.size() WIDTH, HEIGHT = 10, 5?
The width and height shrink from the terminal size to a width of 5 and height of 10.
What happens if you replace DIR: random.choice(DIRECTIONS) on line 52 with DIR: DOWN_RIGHT?
Everything bounces up and down in a linear fashion from top left of the terminal to the top right, and then bounces its way back to the left.
How can you make the 'Corner bounces:' text not appear on the screen?
Comment out: print('Corner bounces:', cornerBounces, end='')
What error message do you get if you delete or comment out cornerBounces = 0?
UnboundLocalError: cannot access local variable 'cornerBounces' where it is not associated with a value
'''