-3

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.

Barmar
789k57 gold badges554 silver badges669 bronze badges
asked Jul 2 at 14:20
19
  • 1
    at least table = Playground(TBL_WID, TBL_HEI, rock_p, rock_c, score_p, score_c, ball) must be moved after ball = Ball() so after you initialize rock_p, rock_c, score_p, score_c and ball Commented Jul 2 at 14:34
  • 1
    @bruno it is also suggestion for next time :) Commented Jul 2 at 17:01
  • 2
    funny thing: documentation also shows class turtle.Screen. But in source code you may find def Screen() which creates instance of class _Screen(TurtleScreen) Commented Jul 2 at 17:06
  • 1
    @furas yes this is a way to debug, an other is to use pdb (I just found it, thinking how is it possible there is no equivalent of gdb for Python ;-) ) Commented Jul 2 at 17:09
  • 1
    @furas and in the source : _tg_classes = ['ScrolledCanvas', 'TurtleScreen', 'Screen', ... ! Not only the doc has inconsistencies Commented Jul 2 at 17:22

2 Answers 2

4

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 ?

answered Jul 2 at 15:27
Sign up to request clarification or add additional context in comments.

2 Comments

Notice the code I give with the inheritance is a what we name a minimal reproducible example producing your problem. In your question there are a lot of code where almost all has no link with your problem, when you write a question you must simplify the most possible to give a minimal reproducible example
comments done before to add the last part in my answer, here I speak about the (wrong) inheritance with Screen of course
2

You have a couple errors in your code pertaining to this issue.

Unrelated (will throw error in future), but first, in main.py, you're using rock_p, rock_c, score_p, score_c, and ball before they are defined. Make sure you define them before using it.

Second, (also unrelated and will be a problem in future), you're using draw_mid_line in playground.py without the constructor self . Add self. right before when you're calling draw_mid_line to prevent any further error.

Third, your draw_mid_line function contains multiple errors.

  1. Dividing an integer (self.h) by 2*10, but range() needs an integer, not a float.

  2. Using min_line; switch to mid_line

  3. rock_ball_hit outside the class

As for the error you described, make sure that there is no file overwrite; check if you have any duplicate file names. Avoid naming your files after standard modules.

You might want to delete your pycache and restarting your editor.

Make sure to check both paddles for collision, as you are only doing one.

Hope this helps.

answered Jul 2 at 14:37

2 Comments

thanks a lot, your points are right and i correct them in my code, but how can i delete my pycache ?
you can manually delete it by navigating to the folder, locating the pycache and select delete

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.