Created
October 25, 2025 01:32
-
-
Save dominikwilkowski/c8a4360d4b07346005da209510b27ae5 to your computer and use it in GitHub Desktop.
python fun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tkinter as tk | |
| from time import sleep | |
| root = tk.Tk() | |
| canvas = tk.Canvas(root, height=800, width=800, bg="black") | |
| canvas.pack (anchor=tk.CENTER, expand=True) | |
| FPS=80 | |
| x_left, y_left = 50, 50 | |
| x_right, y_right = 730, 50 | |
| # left_vy = -7 | |
| right_vy = -7 | |
| pressed = set() | |
| root.focus_set() # ensure we receive key events | |
| def on_press(e): | |
| pressed.add(e.keysym.lower()) | |
| def on_release(e): | |
| pressed.discard(e.keysym.lower()) | |
| root.bind("<KeyPress>", on_press) | |
| root.bind("<KeyRelease>", on_release) | |
| def tick(): | |
| global y_left, y_right, right_vy | |
| dy_left = 0 | |
| if "a" in pressed: | |
| dy_left -= SPEED | |
| if "d" in pressed: | |
| dy_left += SPEED | |
| y_left += dy_left | |
| y_left = max(0, min(CANVAS_H - PADDLE_H, y_left)) | |
| # Right paddle auto-bounce | |
| y_right += right_vy | |
| if y_right <= 0 or y_right >= CANVAS_H - PADDLE_H: | |
| right_vy = -right_vy | |
| y_right += right_vy # nudge back inside bounds | |
| # Render | |
| canvas.delete("all") | |
| canvas.create_rectangle( | |
| x_left, y_left, x_left + PADDLE_W, y_left + PADDLE_H, fill="white", outline="" | |
| ) | |
| canvas.create_rectangle( | |
| x_right, y_right, x_right + PADDLE_W, y_right + PADDLE_H, fill="white", outline="" | |
| ) | |
| # Schedule next frame | |
| root.after(DT_MS, tick) | |
| # Start loop | |
| tick() | |
| root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment