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

Function main

src/gamesbyexample/bouncingdvd.py:43–138  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41
42
43def main():
44 bext.clear()
45
46 # Generate some logos.
47 logos = []
48 for i in range(NUMBER_OF_LOGOS):
49 logos.append({COLOR: random.choice(COLORS),
50 X: random.randint(1, WIDTH - 4),
51 Y: random.randint(1, HEIGHT - 4),
52 DIR: random.choice(DIRECTIONS)})
53 if logos[-1][X] % 2 == 1:
54 # Make sure X is even so it can hit the corner.
55 logos[-1][X] -= 1
56
57 cornerBounces = 0 # Count how many times a logo hits a corner.
58 while True: # Main program loop.
59 for logo in logos: # Handle each logo in the logos list.
60 # Erase the logo's current location:
61 bext.goto(logo[X], logo[Y])
62 print(' ', end='') # (!) Try commenting this line out.
63
64 originalDirection = logo[DIR]
65
66 # See if the logo bounces off the corners:
67 if logo[X] == 0 and logo[Y] == 0:
68 logo[DIR] = DOWN_RIGHT
69 cornerBounces += 1
70 elif logo[X] == 0 and logo[Y] == HEIGHT - 1:
71 logo[DIR] = UP_RIGHT
72 cornerBounces += 1
73 elif logo[X] == WIDTH - 3 and logo[Y] == 0:
74 logo[DIR] = DOWN_LEFT
75 cornerBounces += 1
76 elif logo[X] == WIDTH - 3 and logo[Y] == HEIGHT - 1:
77 logo[DIR] = UP_LEFT
78 cornerBounces += 1
79
80 # See if the logo bounces off the left edge:
81 elif logo[X] == 0 and logo[DIR] == UP_LEFT:
82 logo[DIR] = UP_RIGHT
83 elif logo[X] == 0 and logo[DIR] == DOWN_LEFT:
84 logo[DIR] = DOWN_RIGHT
85
86 # See if the logo bounces off the right edge:
87 # (WIDTH - 3 because 'DVD' has 3 letters.)
88 elif logo[X] == WIDTH - 3 and logo[DIR] == UP_RIGHT:
89 logo[DIR] = UP_LEFT
90 elif logo[X] == WIDTH - 3 and logo[DIR] == DOWN_RIGHT:
91 logo[DIR] = DOWN_LEFT
92
93 # See if the logo bounces off the top edge:
94 elif logo[Y] == 0 and logo[DIR] == UP_LEFT:
95 logo[DIR] = DOWN_LEFT
96 elif logo[Y] == 0 and logo[DIR] == UP_RIGHT:
97 logo[DIR] = DOWN_RIGHT
98
99 # See if the logo bounces off the bottom edge:
100 elif logo[Y] == HEIGHT - 1 and logo[DIR] == DOWN_LEFT:

Callers 1

bouncingdvd.pyFile · 0.70

Calls 1

clearMethod · 0.80

Tested by

no test coverage detected