[Python] Inheritance from Python turtle screen

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página