I'm just trying to implement a pong game with turtle. My code consist of two files:
- 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()
- 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.
2 Answers 2
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 ?
2 Comments
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.
Dividing an integer (
self.h) by2*10, butrange()needs an integer, not a float.Using
min_line; switch tomid_linerock_ball_hitoutside 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.
table = Playground(TBL_WID, TBL_HEI, rock_p, rock_c, score_p, score_c, ball)must be moved afterball = Ball()so after you initialize rock_p, rock_c, score_p, score_c and balldef Screen()which creates instance ofclass _Screen(TurtleScreen)_tg_classes = ['ScrolledCanvas', 'TurtleScreen', 'Screen', ...! Not only the doc has inconsistencies