Snake Project in Python

Below is a Python code for my “Snake Project” (with Turtle and Tkinter).

The purpose of this program is to create a chain of turtles, called followers, that follow each other, with the first turtle in the chain following a special turtle: the leader. The leader itself follows the movement of the mouse.

It removes the last follower from the chain when the “Del” key is pressed.

Since it is not possible to physically delete Followers from memory, when the last Follower in the chain is removed, it is added in the first position of another chain that contains all the “deleted” Followers.
When a new Follower needs to be added to the chain, we first check if there are any in the deleted list and reuse one if available; otherwise, a new one must be created.

However, with my Python code, only the last Turtle is deleted when I click on the “Del” key, even if I press it several times. (Normally, it should delete each time the last follower).

I think the problem has to do with the method:

remove_last_follower(self, event)

of the “Leader” class.

Do you have any idea how to resolve this problem? Thanks a lot for you support.

My Python code:

from  turtle import Turtle, Screen, window_width, window_height # Importation du module Turtle

D = 30 # Distance entre les tortues

screen = Screen()

class Follower(Turtle):
    def __init__(self, name):
        Turtle.__init__(self)
        self.shape("circle")
        self.color("lightblue")
        self.penup()
        self.Prev = None
        self.Next = None
        self.Name = "T" + str(name)  
        self.coucou = False
   
   
    def move(self, x, y):    
        angle = self.towards(x, y)
        self.setheading(angle)
        self.setposition(x,y)
        self.back(20)
        if self.Next:
            x, y = self.pos()
            self.Next.move(x,y)
       

class Leader(Follower): 
    def __init__(self):
        super().__init__('tkz')
        self.shape("turtle")
        self.color("lightgreen")
        self.Last = None
        self.is_moving = False
        self.freeze = False
        self.deleted_followers = [] 
   
    def freeeze(self, event): 
        self.freeze = not self.freeze
   
   
    def add_follower(self, name):
        if self.deleted_followers:
            new_follower = self.deleted_followers.pop()
            new_follower.showturtle()
            new_follower.goto(new_follower.Prev.pos())
            new_follower.setheading(new_follower.Prev.heading())
        else:
            new_follower = Follower(name)
    
        if self.Last == None:
            self.Last = new_follower
            self.Next = new_follower
            new_follower.Prev = self
        else:
            self.Last.Next = new_follower
            self.Last = new_follower
            new_follower.Prev = self.Last
    
        new_follower.Next = None
       
       
    def move(self, event):
        if self.freeze == False:
            x, y = event.x - window_width()/2, -event.y + window_height()/2
            angle = self.towards(x, y) 
            self.setheading(angle)
            self.goto(x, y)
            #self.forward(-20)
            if self.Next:
                self.Next.move(x, y)
    

   
    def on_move(self, event):
        if self.is_moving:
            return
        self.is_moving = True
        self.move(event)
        self.is_moving = False
   
    
    def remove_last_follower(self, event):
        if self.Last:
            last_follower = self.Last
        if last_follower.Prev:
            self.Last = last_follower.Prev
            self.Last.Next = None
        else:
            self.Last = None
            self.Next = None
        last_follower.hideturtle() 
        self.deleted_followers.append(last_follower)
    screen.delay(0)



try:
    le = Leader()
except:
    le = Leader()

   


screen.cv.bind("<Button-1>", le.add_follower)
screen.cv.bind("<Button-3>", le.freeeze)
screen.cv.bind("<Delete>", le.remove_last_follower)

screen.cv.bind("<Motion>", le.on_move)
screen.listen()
screen.mainloop()    

New contributor

Number_Pi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

The linked list in add_follower isn’t updated correctly.

Change:

self.Last.Next = new_follower
self.Last = new_follower          # self.Last updated
new_follower.Prev = self.Last     # should be the *old* version of self.Last

To:

self.Last.Next = new_follower
temp = self.Last                 # save previous Last
self.Last = new_follower         # assign new Last
new_follower.Prev = temp         # assign previous correctly

Or even:

self.Last.Next = new_follower
self.Last, new_follower.Prev = new_follower, self.Last

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật