[Tutor] How to make object disappear?

Bob Gailer bgailer at gmail.com
Mon May 9 10:56:04 EDT 2016


On May 9, 2016 8:01 AM, "Lisa Hasler Waters" <lwaters at flinthill.org> wrote:
>
> 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:

It is a Python convention to capitalize class names. In the future write
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()

This is my own opinion. It is important to separate data from logic. In
this case you are trying to map a key symbol to some numeric values:
key_to_val = {'Left' : (0, 30, 0), 'Right' etc}

(I realize you are probably introducing students to programming. So you
might first show the  then else logic, then introduce the dictionary
alternative.)

val = key_to_val.get(event.keysym, (30, 0))
ball(val)
restart()

15 lines of code replaced by 4. Easier to write,  easier to understand,
easier to maintain. Worth learning about dictionaries.
>
> 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')

I got a syntax error from that if statement.

It seems to me (obviously?) that canvas.delete is never called. This is a
great opportunity for reaching debugging. I'd add a print function call to
show the value of the canvas.find_overlapping call.

Also a good opportunity to read the manual. canvas.find_overlapping returns
a tuple of all matching items.

It would make code reading and maintenance easier if you assign
canvas.find_overlapping(x, y, x2, y2) to a name, then refer to that name in
the logic.
Revisiting the separation of data from logic:
if canvas.find_overlapping(x, y, x2, y2) in ((1,1),(1,2) etc.)

Also avoid things like if x ==True:
It is sufficient to write if x:

In English we say "if it is raining..." rather than " if it is raining is
True ".

Take what you want -l eave the rest.
>
>
>
>
>
>
> 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 at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list