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

Function main

src/gamesbyexample/bouncingdots.py:45–116  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

43
44
45def main():
46 bext.clear()
47
48 # Generate some dots.
49 dots = []
50 for i in range(NUMBER_OF_DOTS):
51 dots.append({COLOR: random.choice(COLORS),
52 X: random.randint(1, WIDTH - 2),
53 Y: random.randint(1, HEIGHT - 2),
54 DIR: random.choice(DIRECTIONS)})
55
56 while True: # Main program loop.
57 for dot in dots: # Handle each dot in the dots list.
58 # Erase the dot's current location:
59 bext.goto(dot[X], dot[Y])
60 print(' ', end='')
61
62 # Move the dot:
63 if dot[DIR] == UP_RIGHT:
64 dot[X] += 1
65 dot[Y] -= 1
66 elif dot[DIR] == UP_LEFT:
67 dot[X] -= 1
68 dot[Y] -= 1
69 elif dot[DIR] == DOWN_RIGHT:
70 dot[X] += 1
71 dot[Y] += 1
72 elif dot[DIR] == DOWN_LEFT:
73 dot[X] -= 1
74 dot[Y] += 1
75
76 # Draw the dots at their new location:
77 bext.goto(dot[X], dot[Y])
78 bext.fg(dot[COLOR])
79 print(DOT_CHAR, end='')
80
81 # See if the dot bounces off the corners:
82 if dot[X] == 0 and dot[Y] == 0:
83 dot[DIR] = DOWN_RIGHT
84 elif dot[X] == 0 and dot[Y] == HEIGHT - 1:
85 dot[DIR] = UP_RIGHT
86 elif dot[X] == WIDTH - 1 and dot[Y] == 0:
87 dot[DIR] = DOWN_LEFT
88 elif dot[X] == WIDTH - 1 and dot[Y] == HEIGHT - 1:
89 dot[DIR] = UP_LEFT
90
91 # See if the dot bounces off the left edge:
92 elif dot[X] == 0 and dot[DIR] == UP_LEFT:
93 dot[DIR] = UP_RIGHT
94 elif dot[X] == 0 and dot[DIR] == DOWN_LEFT:
95 dot[DIR] = DOWN_RIGHT
96
97 # See if the dot bounces off the right edge:
98 elif dot[X] == WIDTH - 1 and dot[DIR] == UP_RIGHT:
99 dot[DIR] = UP_LEFT
100 elif dot[X] == WIDTH - 1 and dot[DIR] == DOWN_RIGHT:
101 dot[DIR] = DOWN_LEFT
102

Callers 1

bouncingdots.pyFile · 0.70

Calls 1

clearMethod · 0.80

Tested by

no test coverage detected