May-11-2019, 07:44 PM
I just tried it. The collision detection is a little wonky but it works. When I ran it in IDLE, I got an error traceback when I closed it. The error wasn't in your code, it was in turtle. I suspect that the module itself might be buggy. People who write a lot of games all swear by a third party library called pygame. I'm not very familiar with turtle, but when I see tracebacks coming from within a module, I take it as a sign that I should be using different software. Nothing can be more frustrating than spending all day trying to debug your code all with the suspicion that a bug in somebody else's code is giving you problems.
The traceback I got when I ran your program:
Instead of this:
The traceback I got when I ran your program:
Error:Traceback (most recent call last):
File "C:\Users\seame\Desktop\pong.pyw", line 105, in <module>
b.ball.setx(b.ball.xcor() + b.vx)
File "C:\Users\seame\AppData\Local\Programs\Python\Python37-32\lib\turtle.py", line 1808, in setx
self._goto(Vec2D(x, self._position[1]))
File "C:\Users\seame\AppData\Local\Programs\Python\Python37-32\lib\turtle.py", line 3158, in _goto
screen._pointlist(self.currentLineItem),
File "C:\Users\seame\AppData\Local\Programs\Python\Python37-32\lib\turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "C:\Users\seame\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2469, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"
As for the object oriented code, I would recommend always using the instance variables once they're created up in your constructor rather than using the parameters they're assigned.Instead of this:
class Window: def __init__(self, width, height, bgc, title): self.width = width self.height = height self.bgc = bgc self.title = title self.window = turtle.Screen() self.window.bgcolor(bgc) self.window.title(title) self.window.setup(width=self.width, height=self.height) . . .Do this:
class Window: def __init__(self, width, height, bgc, title): self.width = width self.height = height self.bgc = bgc self.title = title self.window = turtle.Screen() self.window.bgcolor(self.bgc) self.window.title(self.title) self.window.setup(width=self.width, height=self.height)Admittedly, it still works the same; it's just a convention/style thing. It makes your code look better.