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!
MotionX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.