Need help for enhanced Python Turtle code

I’ve been doing a course on Python. I’ve built the snake game – I’m amazed this could be done with just Turtle.

So I decided to enhance the game. I’ve spent the whole day on one extra enhancement: adding extra food.
What I wanted was, if the user scored x points, then they are rewarded with a big fruit that gives 5x points – but this fruit only sticks around for 5 seconds. The sticking around for 5 seconds is where I’ve got stuck.

I started by trying to keep things modular and put a loop in the newly created ‘super_food.py’ class. I think my logic was perfect. But when I ran the code, when the loop was running, the code slowed down to a really really low speed.

Then I moved the loop to the main.py file and I’ve been messing around with all day – but can’t figure out how to work the logic and have a timer that removes after x seconds the ‘super food’.

Here’s the code I have from main.py:

from turtle import Screen
from snake import Snake
from food import Food
from super_food import SuperFood
from scoreboard import Scoreboard
import time

SPEED_IN_TIME = 0.1
ACCURACY = 45 # This determines how close we get to the snake to determine a hit
ACCURACY_COLLISION = 10
BONUS_SCORE_OCCUR = 3
SUPER_FOOD_DURATION = 5

super_food_exists = False # Trying a hack to see if I can add super food - I've been struggling with this one all day
start_time = 0 # This is used in the super_food code

# All these we don't have in the Snake Class - these are separate to the snake
screen = Screen()
screen.setup(width=600, height=600)
Y_EDGE = screen.window_height() // 2
X_EDGE = screen.window_height() // 2

screen.bgcolor("black")
screen.title("My Snake Game with super food Gemini Version")
screen.tracer(0) # We turn off screen animation - this was making the movements look jagged.

snake = Snake()
food = Food()
super_food = SuperFood() # We add the new Super Food
scoreboard = Scoreboard()

screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")

game_is_on = True

temp_time_controller = False

while game_is_on:

    screen.update()
    time.sleep(SPEED_IN_TIME)

    snake.move()

    # Let's detect collision with food
    if snake.head.distance(food) < ACCURACY:
        # We have a hit! So we need to have the food appear elsewhere and increase the score
        food.refresh()

        # Now we increase the score
        scoreboard.increase_score()

        # We increase the lenght of the snake
        snake.extend()

    # print(snake.head.xcor()) # Just testing I can access the x coordinate

    if snake.head.xcor() > X_EDGE or snake.head.xcor() < -X_EDGE or snake.head.ycor() > Y_EDGE or snake.head.ycor() < -Y_EDGE:
        game_is_on = False
        scoreboard.game_over()

    # Nearly finished! Now we write code to detect if the snake colides with itself
    for segment in snake.turtle_segments[1:]:
        if snake.head.distance(segment) < ACCURACY_COLLISION:
            game_is_on = False
            scoreboard.game_over()

    # New part adding: super foods
    if scoreboard.score != 0: # Problem was 0 % BONUS_SCORE_OCCUR == 0
        if scoreboard.score % BONUS_SCORE_OCCUR == 0:

            super_food_exists = True

            start_time = time.time() # We start of the timer

        if super_food_exists:

            if time.time() - start_time < SUPER_FOOD_DURATION:
                print(f"Time check: {time.time() - start_time < SUPER_FOOD_DURATION}")
                print(f"Time check: {int(time.time() - start_time)}")
                super_food.unhide()
            else:
                super_food.hide()
                super_food_exists = False
                # start_time = 0
        # else:
        #     super_food.hide()

screen.exitonclick()

I’m guessing threading would help here – having the countdown run in its own thread?
Is it possible to do without threading? I’m completely saturated and can’t think straight.

Thanks.

I was expecting the code to run with my new addition – it nearly works!

New contributor

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

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