0

I've been trying to use the super() command in my class but it isn't working. Here is the code:

from turtle import Turtle, Screen

screen=Screen()
screen.setup(600, 600)
screen.bgcolor("black")

class Food(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("circle")
food=Food()
screen.exitonclick()

I'm trying to get the shape to be a circle but that isn't working and all I can see is the default arrow shape. I also tried .color, .forward and none seem to work.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Adam
  • 21
  • 4
  • What's not working? From an inheritance perspective, nothing is wrong with this code. Are you creating an *instance* of `Food` that will draw itself? – chepner Mar 12 '23 at 21:01
  • This should work fine -- can you share the rest of the code (where you actually create the turtle and see what it looks like)? – Samwise Mar 12 '23 at 21:03
  • When i use food=Food() it doesn't work , I've already setup the screen but in the middle its still a default arrow even when i use self.forward in the class setting it doesn't seems to do anything – Adam Mar 12 '23 at 21:04
  • 1
    I created a small test program with your `Food` class and it does indeed show a circle-shaped turtle on the screen. Please update your question with the *exact* code you're running. – Samwise Mar 12 '23 at 21:07
  • @samwise besides the class setup the full code will be just food= Food() , I've already setup the screen class and it works just fine ( tested another turtle on it and it works fine) – Adam Mar 12 '23 at 21:08
  • Please provide a [mcve] of what you are actually doing. – chepner Mar 12 '23 at 21:08
  • @samwise yes this is indeed the issue im having that my code is written well but doesn't seems to work for me , is it a problem from my end , im using pycharm and the code I've written is from a tutorial, but it just doesn't seem to work for me any idea why ? – Adam Mar 12 '23 at 21:11
  • I updated the question with the full code – Adam Mar 12 '23 at 21:19
  • Your "full code" example does not have `food = Food()` (even though earlier you said you tried that), so it's hard for us to know what is the actual code you're using... – John Gordon Mar 12 '23 at 21:19
  • Yes sorry I just updated it , this is 100% the full code sorry about earlier – Adam Mar 12 '23 at 21:21
  • So any idea why it isn't working because i do believe that the class is written well but i dont know why it isn't working for me but works for anyone else – Adam Mar 12 '23 at 21:25
  • I think the `Screen` stuff is the problem. When I tested your code I just created a `Food()` and then called `mainloop()`, and that worked fine. With your `Screen` code I don't see any turtle at all; I'm not familiar enough with the turtle module to know how exactly you're supposed to use that class. – Samwise Mar 12 '23 at 21:31
  • Alright thanks for the help I'll see what i can do from my end – Adam Mar 12 '23 at 21:35
  • 1
    The problem may be that shapes are drawn in black by default, and so setting the background color to black makes the shape invisible. Try setting the foreground color to white. (Or don't set the background color to black.) – John Gordon Mar 12 '23 at 21:46
  • Yep still doesn't work i even made a fresh new code with just screen=Screen() and exit on click, i still see the default arrow shape, it probably something to do with pycharm and not the actual code because i did try everything – Adam Mar 12 '23 at 21:51
  • [I would avoid inheritance in turtle](https://stackoverflow.com/questions/9441331/how-to-create-a-subclass-in-python-that-is-inherited-from-turtle-module/71003069#71003069). `Turtle`s are not meant to be subclassed; they're magical, auto-rendering, ungarbage-collectable entities with over 150 properties with common names that should be kept at arm's length. Prefer composition over inheritance. That said, the code works fine for me once I remove the black background. – ggorlen Mar 12 '23 at 23:23

1 Answers1

0

whatever the question is trying to achieve, the default colour is black. So you could change the colour in the class like this:

from turtle import Turtle, Screen

screen=Screen()
screen.setup(600, 600)
screen.bgcolor("black")

class Food(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("circle")
        self.color('red')   # <-- add a colour

food=Food()
screen.exitonclick()

Which will then give this:

enter image description here

So you now have a red circle.

D.L
  • 4,339
  • 5
  • 22
  • 45