0

I'm making a simple pong game and and trying to make it with OOP. I'm trying to get the turtles to move using ycor. It's intended to call the 'objects_up' method to move them up and do then ill do the same for x and y. I've tried all sorts of indentation, not using a method and moving wn.listen outside of the class. What am I doing wrong? I keep getting the error :

Edit1: Made Paddles a subclass of turtle. I'm getting a new, different error:

Edit2: Followed the advice of @OneCricketeer and I'm using a lambda now. The program runs fine but the keypress doesn't work and i'm getting a plethora of errors: e.g

````
File "C:\Users\okpla\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 1294, in _incrementudc
    raise Terminator
````

This is the code:

````
from turtle import Screen,Turtle

wn = Screen()
wn.title("Pong by CGGamer")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

class Paddles(Turtle):  
    def __init__(self,position,size):
        super().__init__()
        self.position = position
        self.size = size
        self.speed(0)
        self.shape("square")
        self.shape("square")
        self.color("white")
        self.shapesize(size,1)
        self.penup()
        self.setposition(position)
        wn.listen()
        wn.onkeypress(lambda self:self.sety(self.ycor() + 20),"w")

paddle_a = Paddles((-350,0),5)


paddle_b = Paddles((350,0),5)


ball = Paddles((0,0),1)


````
CGGamer
  • 1
  • 2
  • In `objects_up(self)` you call `y = self.ycor()` and there is no ycor anywhere in Paddles. `self.sety(y)` - there's also no sety in Paddles. You probably want a different ycor and different sety than one inside Paddle – Thomas Weller Dec 02 '22 at 20:39
  • `ycor()` is defined for `turtle`, as in you can access it via `turtle.ycor()`, but `Paddles` is a class you made yourself, which does not derive from `turtle`. So, for you to expect it to have `ycor` without you first defining it indicates that you are not understanding something about OOP. A bigger issue though is `self = turtle.Turtle()`. Overwriting the `self` parameter in the middle of the constructor makes absolutely no sense at all. – Random Davis Dec 02 '22 at 20:41
  • 3
    `self = turtle.Turtle()` just overwrites a local variable and leaks a turtle after the init completes. Try `self.turtle = turtle.Turtle()` using composition, then access it henceforth with `self.turtle.ycor()`. See also [How to create a subclass in python that is inherited from turtle Module](https://stackoverflow.com/questions/9441331/how-to-create-a-subclass-in-python-that-is-inherited-from-turtle-module/71003069#71003069) – ggorlen Dec 02 '22 at 20:43
  • Also if you used an actual IDE to write this code, like PyCharm (which is free), it would've warned you that reassigning `self` is a mistake. – Random Davis Dec 02 '22 at 20:44
  • @ggorlen No leak; as `self` was the only reference to that object, it's destroyed after its reference count goes to 0 when `Paddles.__init__` exits. – chepner Dec 02 '22 at 20:59
  • You want `self.sety(self.ycor() + 20)` – OneCricketeer Dec 02 '22 at 21:10
  • RE edit: Shouldn't `y = self.ycor` be `y = self.ycor()` ? – Nick ODell Dec 02 '22 at 21:14
  • @chepner I don't think so, because of the way turtle works. It has an internal list of turtles that are difficult to garbage collect. See [How to fully delete a turtle](https://stackoverflow.com/questions/43972351/how-to-fully-delete-a-turtle) and [Why is my Python Turtle program slowing down drastically the longer it runs?](https://stackoverflow.com/questions/73261924/why-is-my-python-turtle-program-slowing-down-drastically-the-longer-it-runs/73263441#73263441) – ggorlen Dec 02 '22 at 21:34
  • You are totally in a wrong direction. To implement a game, you usually define a model(state) for the game world(coordinates of ball and paddles in your case), implement a function to render a game scene which draws objects according to the model, and a game/event loop where you update the model according to the user input and call the rendering function in each loop. See [Tetris samples](https://python-forum.io/thread-37212.html?highlight=tetris) for example. – relent95 Dec 03 '22 at 05:24

1 Answers1

0

Thanks guys! Solved the problem, was sooo much easier than I thought. Here's the new code:

from turtle import Screen,Turtle

wn = Screen()
wn.title("Pong by CGGamer")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

class Paddles(Turtle):  
    def __init__(self,position,size):
        super().__init__()
        self.position = position
        self.size = size
        self.speed(0)
        self.shape("square")
        self.shape("square")
        self.color("white")
        self.y = 20
        self.x = 20
        self.shapesize(size,1)
        self.penup()
        self.setposition(position)
    
    def moving_on_y_up(self):
          newy = self.ycor() + self.y
          self.goto(self.xcor(),newy)
   
    def moving_on_x_right(self):
        newx = self.xcor() + self.x
        self.goto(newx,self.ycor())

    def moving_on_y_down(self):
        newy = self.ycor() - self.y
        self.goto(self.xcor(),newy)
    
    def moving_on_x_left(self):
        newx = self.xcor() - self.x
        self.goto(newx,self.ycor())


paddle_a = Paddles((-350,0),5)
wn.listen()
wn.onkeypress(paddle_a.moving_on_y_up, "w")
wn.onkeypress(paddle_a.moving_on_x_right, "d")
wn.onkeypress(paddle_a.moving_on_y_down, "s")
wn.onkeypress(paddle_a.moving_on_x_left, "a")


paddle_b = Paddles((350,0),5)
wn.listen()
wn.onkeypress(paddle_a.moving_on_y_up, "w")
wn.onkeypress(paddle_a.moving_on_x_right, "d")
wn.onkeypress(paddle_a.moving_on_y_down, "s")
wn.onkeypress(paddle_a.moving_on_x_left, "a")



ball = Paddles((0,0),1)



while True:
    wn.update()
CGGamer
  • 1
  • 2