Dear Tutor, My students and I are creating a maze game in tkinter. We are trying to make the ball disappear when it hits the wall (black lines). We are not having much luck. The following code has run the best, but still, the ball does not disappear. Any advice would be appreciated!
from tkinter import * import random import time tk = Tk() tk.title("Game") tk.resizable(0, 0) tk.wm_attributes("-topmost", 1) canvas = Canvas(tk, width=1400, height=835, bd=0, highlightthickness=0) canvas.pack() tk.update() class dot: def __init__(self, canvas, color): self.canvas = canvas self.id = canvas.create_oval(15, 15, 30, 30, fill='Blue', tags='dot1') this = dot(canvas, 'blue') def ball(n, x, y): canvas.move(n, x, y) def mt(event): if event.keysym == 'Left': ball(1, -30, 0) restart() elif event.keysym == 'Up': ball(1, 0, -30) restart() elif event.keysym == 'Down': ball(1, 0, 30) restart() elif event.keysym == 'Right': ball(1, 30, 0) restart() else: ball(1, 30, 0) restart() canvas.bind_all('<KeyPress-Up>', mt) canvas.bind_all('<KeyPress-Down>', mt) canvas.bind_all('<KeyPress-Left>', mt) canvas.bind_all('<KeyPress-Right>', mt) dot_bbox = canvas.coords('dot1') x = dot_bbox[0] y = dot_bbox[1] x2 = dot_bbox[2] y2 = dot_bbox[3] canvas.create_line(0, 0, 0, 300, width=20) canvas.create_line(0, 300, 300, 300, width=10) canvas.create_line(80, 240, 80, 0, width=10) canvas.create_line(160, 300, 160, 60, width=10) canvas.create_line(240, 240, 240, 0, width=10) canvas.create_line(300, 300, 300, 150, width=10) canvas.create_line(300, 150, 600, 150, width=10) canvas.create_line(80, 0, 2000, 0, width=30) canvas.create_line(300, 75, 600, 75, width=10) canvas.create_line(760, 0, 760, 300, width=10) canvas.create_line(600, 75, 680, 75, width=10) def restart(): if (canvas.find_overlapping(x, y, x2, y2) == (1, 2)) or (canvas.find_overlapping(x, y, x2, y2) == (1, 3)) or (canvas.find_overlapping(x, y, x2, y2) == (1, 4)) or (canvas.find_overlapping(x, y, x2, y2) == (1, 5)) or (canvas.find_overlapping(x, y, x2, y2) == (1, 6)) == True: canvas.delete('dot1') Shell: Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. Visit http://www.python.org/download/mac/tcltk/ for current information. >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> =============================== RESTART: Shell =============================== >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> =============================== RESTART: Shell =============================== >>> from tkinter import * >>> import random >>> import time >>> tk = Tk() >>> >>> tk.title("Game") '' >>> tk.resizable(0, 0) '' >>> tk.wm_attributes("-topmost", 1) '' >>> canvas = Canvas(tk, width=1400, height=835, bd=0, highlightthickness=0) >>> canvas.pack() >>> tk.update() >>> class dot: def __init__(self, canvas, color): self.canvas = canvas self.id = canvas.create_oval(10, 10, 25, 25, fill='Blue', tags='dot1') >>> this = dot(canvas, 'blue') >>> def ball(n, x, y): canvas.move(n, x, y) >>> def mt(event): if event.keysym == 'Left': ball(1, -30, 0) restart() elif event.keysym == 'Up': ball(1, 0, -30) restart() elif event.keysym == 'Down': ball(1, 0, 30) restart() elif event.keysym == 'Right': ball(1, 30, 0) restart() else: ball(1, 30, 0) restart() >>> canvas.bind_all('<KeyPress-Up>', mt) '4300440008mt' >>> canvas.bind_all('<KeyPress-Down>', mt) '4329736584mt' >>> canvas.bind_all('<KeyPress-Left>', mt) '4380738824mt' >>> canvas.bind_all('<KeyPress-Right>', mt) '4383283336mt' >>> dot_bbox = canvas.coords('dot1') >>> x = dot_bbox[0] >>> y = dot_bbox[1] >>> x2 = dot_bbox[2] >>> y2 = dot_bbox[3] >>> print(canvas.find_overlapping(x, y, x2, y2)) (1,) >>> print(canvas.find_overlapping(x, y, x2, y2)) (1,) >>> canvas.create_line(0, 0, 0, 300, width=20) 2 >>> print(canvas.find_overlapping(x, y, x2, y2)) (1, 2) >>> canvas.create_line(600, 75, 680, 75, width=10) 3 >>> print(canvas.find_overlapping(x, y, x2, y2)) (1, 2) >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> =============================== RESTART: Shell =============================== >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> =============================== RESTART: Shell =============================== >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> =============================== RESTART: Shell =============================== >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> ================ RESTART: /Users/BScherer/Desktop/MazeTest.py ================ >>> -- Lisa Waters, PhD Technology Integration Flint Hill School _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor