Advertisement
furas

Python - (zelle) graphics.py - bouncing ball - (Stackoverflow)

Jun 15th, 2025 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # date: 2025.06.15
  4.  
  5. # [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)
  6.  
  7. # [Teaching with Python](https://mcsp.wartburg.edu/zelle/python/)
  8. # https://mcsp.wartburg.edu/zelle/python/graphics.py
  9.  
  10. from graphics import *
  11.  
  12. # --- constants ---   # PEP8: UPPER_CASE_NAMES
  13.  
  14. WIDTH  = 400*2
  15. HEIGHT = 400
  16.  
  17. SIZE_X = 10*2
  18. SIZE_Y = 10
  19.  
  20. RADIUS = 2
  21.  
  22. # --- functions ---
  23.  
  24. def ball():
  25.     print("This program will show a ball bouncing around within the window")
  26.  
  27.     win = GraphWin("Ball", WIDTH, HEIGHT)  # PEP8: space after `,`
  28.     win.setBackground("white")
  29.     win.setCoords(-SIZE_X, -SIZE_Y, SIZE_X, SIZE_Y)  # PEP8: space after `,`
  30.  
  31.     dx = 1
  32.     dy = 1
  33.  
  34.     pos_x = SIZE_X//16
  35.     pos_y = SIZE_Y//2
  36.  
  37.     ball = Circle(Point(pos_x, pos_y), RADIUS)
  38.     ball.setFill("black")
  39.     ball.draw(win)
  40.  
  41.     while True:
  42.     #for _ in range(10000):
  43.  
  44.         #ball = Circle(Point(pos_x, pos_y), RADIUS)
  45.         #ball.setFill("black")
  46.         #ball.draw(win)
  47.  
  48.         ball.move(dx, dy)
  49.         pos_x += dx
  50.         pos_y += dy
  51.  
  52.         if pos_x >= SIZE_X-RADIUS or pos_x <= -(SIZE_X-RADIUS):
  53.             dx = -dx
  54.  
  55.         if pos_y >= SIZE_Y-RADIUS or pos_y <= -(SIZE_Y-RADIUS):
  56.             dy = -dy
  57.  
  58.         update(100)
  59.  
  60.         #ball.undraw()
  61.  
  62.         # exit when clicked mouse or pressed key
  63.         if win.checkMouse() or win.checkKey():
  64.             break
  65.  
  66. # --- main ---
  67.  
  68. ball()
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement