Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So, I am making a tower defense game using Python's turtle module and I just started off. For some reason though, the .tracer at the very beginning is creating an extra turtle for me for some reason. I don't know why. I have 3 classes, and so far, I have only defined 4 sprites: Helper, Enemy, Enemy, Enemy. When I remove the tracer, the turtle suddenly disappears. I accessed my turtle list and started to delete them. That didn't work. Here is the code:

import turtle as t
import random as r

wn = t.Screen()
wn.tracer(0)
class Sprite():
    def __init__(self, color, speed, shape, ento, x, y):
        self.sprite = t.Turtle()
        self.color = color
        self.speed = speed
        self.shape = shape
        self.ento = ento
        self.x = x
        self.y = y


    def render(self, pen):
        pen.speed(0)
        pen.color(self.color)
        pen.shape(self.shape)
        pen.up()
        pen.goto(self.x, self.y)

class Helper(Sprite):
    def __init__(self, color, speed, damage, shape, x, y):
        self.sprite = Sprite(color, speed, shape, 'Helper', x, y)
        self.helper = t.Turtle()
        self.color = color
        self.speed = speed
        self.shape = shape
        self.ento = 'Helper'
        self.damage = damage
        self.x = x
        self.y = y

    def fire(self):
        pass

class Enemy(Sprite):
    def __init__(self, color, speed, health, shape, x, y):
        self.sprite = Sprite(color, speed, shape, 'Enemy', x, y)
        self.enemy = t.Turtle()
        self.color = color
        self.speed = speed
        self.shape = shape
        self.ento = 'Enemy'
        self.health = health
        self.x = x
        self.y = y
        self.state = 'frozen'
        self.tick = 0


    def move(self):
        if self.state == 'frozen':
            self.state = 'moving'
            self.tick = 0
        self.enemy.forward(self.speed)
        self.x = self.enemy.xcor()
        self.y = self.enemy.ycor()
        self.tick += 1
helpers = []
for i in range(1):
    test = Helper('green', 1, 1, 'square', 0, 0)
enemies = []
for i in range(3):
    enemies.append(Enemy('red', 1, 1, 'circle', r.randint(-100, 100), r.randint(-100, 100)))

print(wn._turtles)
while True:
    wn.update()
    test.render(test.helper)
    for enemy in enemies:
        enemy.render(enemy.enemy)
        enemy.move()

Why is this not working. I work on a windows computer. I am not really sure if this applies to only me or to everyone.

question from:https://stackoverflow.com/questions/66064061/why-is-the-tracer-making-another-turtle-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your object code is a disaster. Consider this logic:

class Sprite():
    def __init__(self, color, speed, shape, ento, x, y):
        self.sprite = t.Turtle()
    #...

class Helper(Sprite):
    def __init__(self, color, speed, damage, shape, x, y):
        self.sprite = Sprite(color, speed, shape, 'Helper', x, y)

Helper is a Sprite but it doesn't call super(), and instead has a Sprite inside it where the turtle that Sprite creates would have been? I'm lost. I think we need to make the code object-oriented instead of simply object-based:

from turtle import Screen, Turtle
from random import randint

class Sprite(Turtle):
    def __init__(self, color, pace, shape, ento, x, y):
        super().__init__()

        self.color(color)
        self.shape(shape)

        self.penup()

        self.pace = pace
        self.ento = ento
        self.x = x
        self.y = y

    def render(self):
        self.goto(self.x, self.y)

class Helper(Sprite):
    def __init__(self, color, pace, damage, shape, x, y):
        super().__init__(color, pace, shape, 'Helper', x, y)

        self.damage = damage

    def fire(self):
        pass

class Enemy(Sprite):
    def __init__(self, color, pace, health, shape, x, y):
        super().__init__(color, pace, shape, 'Enemy', x, y)

        self.health = health
        self.state = 'frozen'
        self.tick = 0

    def move(self):
        if self.state == 'frozen':
            self.state = 'moving'
            self.tick = 0

        self.forward(self.pace)
        self.x = self.xcor()
        self.y = self.ycor()
        self.tick += 1

screen = Screen()
screen.tracer(0)

helpers = []
for _ in range(1):
    helpers.append(Helper('green', 1, 1, 'square', 0, 0))

enemies = []
for _ in range(3):
    enemies.append(Enemy('red', 1, 1, 'circle', randint(-100, 100), randint(-100, 100)))

while True:
    helpers[0].render()

    for enemy in enemies:
        enemy.render()
        enemy.move()

    screen.update()

This clean up of your code appears to also clean up your extra turtle issue. I renamed speed to be pace to keep it from being confused with turtle's own speed method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...