[Tutor] How to make object disappear?
boB Stepp
robertvstepp at gmail.com
Mon May 9 11:55:38 EDT 2016
Hello!
I am not one of the experts; instead, I am one of the learners. But
if you are seeking assistance on Tutor, it is necessary to post the
actual code that you successfully or unsuccessfully ran. If you were
unsuccessful, you need to copy and paste the full text of the error
messages you received.
On Mon, May 9, 2016 at 6:59 AM, Lisa Hasler Waters
<lwaters at flinthill.org> wrote:
The code you give below does not run when I copy and paste it into my
environment. The indentation (Which is CRITICAL in Python.) is
inconsistent and in places wrong. It is typical to use 4 spaces for
each level of indentation. If your email client is mangling your
indentations, then you need to determine how to correct this prior to
posting! To see what your code looks like from my perspective, go to
https://mail.python.org/pipermail/tutor/2016-May/108832.html
> 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')
After I corrected the indentation errors, the above code still did not
run as you omitted the statement to get everything going, that is:
tk.mainloop()
Now I can see your maze game! It looks potentially very fun and cool.
As to your actual problem, I have not gone through your "if"
conditions, but obviously you need to accurately detect when the
"edge" of the ball collides with the nearest side of one of the walls.
If such an event happens the canvas would have to be redrawn with the
walls in place, but without the ball. If the ball is not
disappearing, then my initial thoughts would be:
1) Your if/elif statements are not correctly detecting the collisions.
2) You are not actually "capturing" the collision event as the
ball moves; that is, you may not be successfully detecting the ball's
*current* coordinates.
3) You are not correctly redrawing the window at the point where
the ball must "disappear".
There are various stylistic issues. You and your students might want
to glance through the portions of PEP 8 similar to the Python that you
are currently coding:
https://www.python.org/dev/peps/pep-0008/
PEP 8 is the Python style-guide, which many if not most Python coders
strive to emulate.
Good luck!
boB
More information about the Tutor
mailing list