MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

barebones/bouncingdvd-barebones.py:14–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12
13
14def main():
15 bext.clear()
16 logo = {'color': random.choice(COLORS),
17 'x': random.randint(1, WIDTH - 4),
18 'y': random.randint(1, HEIGHT - 4),
19 'dir': random.choice(DIRECTIONS)}
20 if logo['x'] % 2 == 1:
21 logo['x'] -= 1 # Make sure X is even so it can hit the corner.
22
23 while True: # Main program loop.
24 # Erase the logo's current location:
25 bext.goto(logo['x'], logo['y'])
26 print(' ', end='') # (!) Try commenting this line out.
27
28 originalDirection = logo['dir']
29
30 # See if the logo bounces off the corners:
31 if logo['x'] == 0 and logo['y'] == 0:
32 logo['dir'] = 'DR'
33 elif logo['x'] == 0 and logo['y'] == HEIGHT - 1:
34 logo['dir'] = 'UR'
35 elif logo['x'] == WIDTH - 3 and logo['y'] == 0:
36 logo['dir'] = 'DL'
37 elif logo['x'] == WIDTH - 3 and logo['y'] == HEIGHT - 1:
38 logo['dir'] = 'UL'
39
40 # See if the logo bounces off the left edge:
41 elif logo['x'] == 0 and logo['dir'] == 'UL':
42 logo['dir'] = 'UR'
43 elif logo['x'] == 0 and logo['dir'] == 'DL':
44 logo['dir'] = 'DR'
45
46 # See if the logo bounces off the right edge:
47 # (WIDTH - 3 because 'DVD' has 3 letters.)
48 elif logo['x'] == WIDTH - 3 and logo['dir'] == 'UR':
49 logo['dir'] = 'UL'
50 elif logo['x'] == WIDTH - 3 and logo['dir'] == 'DR':
51 logo['dir'] = 'DL'
52
53 # See if the logo bounces off the top edge:
54 elif logo['y'] == 0 and logo['dir'] == 'UL':
55 logo['dir'] = 'DL'
56 elif logo['y'] == 0 and logo['dir'] == 'UR':
57 logo['dir'] = 'DR'
58
59 # See if the logo bounces off the bottom edge:
60 elif logo['y'] == HEIGHT - 1 and logo['dir'] == 'DL':
61 logo['dir'] = 'UL'
62 elif logo['y'] == HEIGHT - 1 and logo['dir'] == 'DR':
63 logo['dir'] = 'UR'
64
65 if logo['dir'] != originalDirection:
66 # Change color when the logo bounces:
67 logo['color'] = random.choice(COLORS)
68
69 # Move the logo. (X moves by 2 because the terminal
70 # characters are twice as tall as they are wide.)
71 if logo['dir'] == 'UR':

Callers 1

Calls 1

clearMethod · 0.80

Tested by

no test coverage detected