Search code examples
pythonpython-turtlepong

'NoneType' object has no attribute 'ycor'


I trying to make a pong game using turtle library and I got this error: 'NoneType' object has no attribute 'ycor'.

This is my code:

#red, green, blue
import turtle

#Functions
def sprite(animation_speed, shape, color, x, y, size_width, size_height):
 sprite = turtle.Turtle()
 sprite.penup()
 sprite.speed(animation_speed)
 sprite.shape(shape)
 sprite.color(color)
 sprite.goto(x,y)
 sprite.shapesize(stretch_wid = size_height, stretch_len = size_width)

def paddle1Up():
 y = paddle1.ycor()
 y += 5
 paddle1.sety(y)

#Window settings
window = turtle.Screen()
window.title("Pong game")
window.bgcolor("#0096ff")
window.setup(width = 1920, height = 1080)
window.tracer(0)

#Start
paddle1 = sprite(0, "square", "purple", -800, 0, 3, 12)
paddle2 = sprite(0, "square", "purple", 800, 0, 3, 12)
ball = sprite(0, "circle", "white", 0, 0, 3, 3)

#keyboard biding
window.listen()
window.onkeypress(paddle1Up(), "w")

#Main game loop
while True:
 window.update()

The error is in this line: 'window.onkeypress(paddle1Up(), "w")'.


Solution

  • There are two critical errors in this code. First is that three variables are assigned to the result of a sprite() call:

    paddle1 = sprite(0, "square", "purple", -800, 0, 3, 12)
    paddle2 = sprite(0, "square", "purple", 800, 0, 3, 12)
    ball = sprite(0, "circle", "white", 0, 0, 3, 3)
    

    But the sprite() function doesn't return anything except None. The next problem is this call is incorrect:

    window.onkeypress(paddle1Up(), "w")
    

    We should be passing the function paddle1Up to onkeypress(), not the result of calling the function paddle1Up(). A less severe issue is that you have a variable and a function both called sprite. A dangerous practice, avoid it. Addressing all three issues above, we get the code:

    from turtle import Screen, Turtle
    
    # Functions
    def create_sprite(animation_speed, shape, color, x, y, size_width, size_height):
        sprite = Turtle()
        sprite.shape(shape)
        sprite.shapesize(stretch_wid=size_height, stretch_len=size_width)
        sprite.speed(animation_speed)
        sprite.color(color)
        sprite.penup()
        sprite.goto(x, y)
    
        return sprite
    
    def paddle1Up():
        paddle1.sety(paddle1.ycor() + 5)
    
    # Screen settings
    screen = Screen()
    screen.title("Pong game")
    screen.bgcolor('#0096ff')
    screen.setup(width=1920, height=1080)
    screen.tracer(0)
    
    paddle1 = create_sprite(0, 'square', 'purple', -800, 0, 3, 12)
    paddle2 = create_sprite(0, 'square', 'purple', 800, 0, 3, 12)
    ball = create_sprite(0, 'circle', 'white', 0, 0, 3, 3)
    
    # keyboard binding
    screen.onkeypress(paddle1Up, 'w')
    screen.listen()
    
    # Main game loop
    while True:
        screen.update()
    

    Which basically does what you set out to do but still has one issue that might become a problem as you expand on this code.