Reputation: 1
in my code, which is for a game, I want to increase the speed of my randomly moving object ( here I call it squidward ) every time a certain amount of clicks and level is reached. However, my function is not working. I think the function itself makes sense and maybe I forgot to add something, however, I looked over it several times, but at this point I don't know what else I could possibly do in order to fix it.
Here is my code:
import turtle
from tkinter import*
from random import randint
# set up
win = turtle.Screen()
win.title("Squid-game")
win.bgcolor("black")
win.register_shape("squid_big.gif")
# variabelen
clicks = -1
level = 1
Speed = 1
# dem Turtle-objekt in Thadäus formen
squidward = turtle.Turtle()
squidward.shape("squid_big.gif")
squidward.penup()
squidward.speed(Speed)
def Speed():
global Speed, level, clicks
if level == 1 and clicks == 5:
level += 1
Speed += 1
if level == 2 and clicks == 10:
level += 1
Speed += 5
if level == 3 and clicks == 15:
level += 1
Speed += 2
squidward.speed(Speed)
squidward.onclick(Speed())
# zeigt an wie viele Clicks man hat
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
pen.penup
pen.goto(0, 400)
pen.write (f"Clicks: {clicks}", align="center", font=("Courier New", 32, "normal"))
# Funtion für das Clicken und den random Bewegung
def clicked (x,y):
global clicks
clicks += 1
pen.clear()
pen.write (f"Clicks: {clicks}", align="center", font=("Courier New", 32, "normal"))
while TRUE: squidward.goto(randint(-1200, 1200), randint(-1200, 1200))
# die Geschwindigkeit muss ich ab einer bestimmten Aazahl bis 10 steigen// Level-up
squidward.onclick(clicked)
win.mainloop()
Upvotes: 0
Views: 205
Reputation: 34
Your problem comes in that you only define the speed of the turtle once. Instead you should reset the speed every level like so;
def Speed():
global Speed, level, clicks, squidward
if level == 1 and clicks == 5:
level += 1
Speed += 1
if level == 2 and clicks == 10:
level += 1
Speed += 5
if level == 3 and clicks == 15:
level += 1
Speed += 2
squidward.speed(Speed)
In addition, the clicked() function is never ended as it goes into an infinite loop
Upvotes: 1