Python Forum
Pong clone with classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pong clone with classes
#2
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:
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.
Reply


Messages In This Thread
Pong clone with classes - by OhNoSegFaultAgain - Mar-30-2019, 04:34 PM
RE: Pong clone with classes - by keames - May-11-2019, 07:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame Pong menator01 0 1,832 Dec-26-2022, 06:48 AM
Last Post: menator01
  Git clone all of a Github user's public repositories (download all repositories) rootVIII 31 11,777 Aug-10-2019, 04:10 PM
Last Post: rootVIII
  Pygame simple pong example metulburr 5 7,795 Nov-07-2016, 06:40 PM
Last Post: Kai.

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020