pythonturtle-graphics

Inheritance from Python turtle screen


I'm just trying to implement a pong game with turtle. My code consist of two files:

  1. main.py:
from playground import Playground
from rocket import Rocket
from ball import Ball
from score import Score
TBL_WID = 600
TBL_HEI = 500
ROCK_LEN = 50

table = Playground(TBL_WID, TBL_HEI, rock_p, rock_c, score_p, score_c, ball)
rock_p = Rocket(TBL_WID, TBL_HEI, ROCK_LEN)
rock_c = Rocket(TBL_WID, TBL_HEI, ROCK_LEN)
score_p = Score(TBL_WID, TBL_HEI)
score_c = Score(TBL_WID, TBL_HEI)
ball = Ball()
game_on = True

while game_on:
    table.rock_ball_hit()
  1. playground.py
from turtle import Screen, Turtle

class Playground(Screen):
    def __init__(self, w, h, rock_p, rock_c, score_p, score_c, ball):
        super().__init__()
        self.w = w
        self.h = h
        self.rock_p = rock_p
        self.rock_c = rock_c
        self.score_p = score_p
        self.score_c = score_c
        self.ball = ball
        self.bgcolor("black")
        self.screensize(w, h)
        draw_mid_line()

    def draw_mid_line(self):
        mid_line = Turtle()
        mid_line.pensize(20)
        mid_line.pencolor("white")
        mid_line.penup()
        mid_line.goto(0,self.h-10)
        mid_line.setheading(270)
        mid_line.pendown()

        for step in range(self.h/(2*10)):
            mid_line.forward(10)
            if min_line.pencolor() == 'white':
                min_line.pencolor('black')
            else:
                min_line.pencolor('white')

def rock_ball_hit(self, rock):
    return all([abs(ball.xcor()-rock.xcor()) < 10 , abs(ball.ycor()-rock.ycor()) < 15])

but when I run main.py it throws following error:

Traceback (most recent call last):
   File "E:/Python_Projects/Pong/main.py", line 1, in <module>
    from playground import Playground
   File "E:\Python_Projects/Pong\playground.py", line 5, in <module>
     class Playground(Screen):
TypeError: function() argument 'code' must be code, not str

I asked gemini but it couldn't figure out the problem.


Solution

  • in

    from turtle import Screen, Turtle
    
    class Playground(Screen):
    

    you try to create the class Playground inheriting Screen, but is is invalid because Screen is not a class but a function :

    bruno@raspberrypi:/tmp $ python 
    Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from turtle import Screen
    >>> type(Screen)
    <class 'function'>
    
    

    and then :

    bruno@raspberrypi:/tmp $ python 
    Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from turtle import Screen
    >>> 
    >>> class Playground(Screen):
    ...   None
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: function() argument 'code' must be code, not str
    >>> 
    

    As noticed by furas in a comment of your question the class for the screen is (re)named _Screen, so you can :

    from turtle import _Screen, Turtle
    
    class Playground(_Screen):
      ...
    

    but are you sure you need that supposing that will not creates problem because of the implementation of Turtle ?