Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # date: 2025.06.15
- # [python - is the code on the right track if exercise ask to have just a ball bouncing around on the window ? some clues ? please no solutions - Stack Overflow](https://stackoverflow.com/questions/79666758/is-the-code-on-the-right-track-if-exercise-ask-to-have-just-a-ball-bouncing-arou)
- # [Teaching with Python](https://mcsp.wartburg.edu/zelle/python/)
- # https://mcsp.wartburg.edu/zelle/python/graphics.py
- from graphics import *
- # --- constants --- # PEP8: UPPER_CASE_NAMES
- WIDTH = 400*2
- HEIGHT = 400
- SIZE_X = 10*2
- SIZE_Y = 10
- RADIUS = 2
- # --- functions ---
- def ball():
- print("This program will show a ball bouncing around within the window")
- win = GraphWin("Ball", WIDTH, HEIGHT) # PEP8: space after `,`
- win.setBackground("white")
- win.setCoords(-SIZE_X, -SIZE_Y, SIZE_X, SIZE_Y) # PEP8: space after `,`
- dx = 1
- dy = 1
- pos_x = SIZE_X//16
- pos_y = SIZE_Y//2
- ball = Circle(Point(pos_x, pos_y), RADIUS)
- ball.setFill("black")
- ball.draw(win)
- while True:
- #for _ in range(10000):
- #ball = Circle(Point(pos_x, pos_y), RADIUS)
- #ball.setFill("black")
- #ball.draw(win)
- ball.move(dx, dy)
- pos_x += dx
- pos_y += dy
- if pos_x >= SIZE_X-RADIUS or pos_x <= -(SIZE_X-RADIUS):
- dx = -dx
- if pos_y >= SIZE_Y-RADIUS or pos_y <= -(SIZE_Y-RADIUS):
- dy = -dy
- update(100)
- #ball.undraw()
- # exit when clicked mouse or pressed key
- if win.checkMouse() or win.checkKey():
- break
- # --- main ---
- ball()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement