简体   繁体   中英

How do I make a turtle move in OOP?

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: eg

````
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)


````

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM