Python Turtle: Starting Countdown gets stuck - python

I am making a dodging game in which the turtle (shaped like a turtle) has to avoid other turtles (shaped like crocodiles). I got the game working but am trying to add in a countdown before the game starts which goes 3,2,1,GO!
My thinking is that since the countdown_number variable I have goes 3, 2, 1, and then GO!, I can get it to come before the rest of the game by having everything else dependent on the value of the countdown_number being "GO!".
Right now, however, when I start the program it gets stuck on 3 and after the update interval starts over again instead of continuing through 2, 1, and GO! so the game cannot start.
I've seen a lot of questions on Stack Overflow that have to do with running timers and things concurrent to the game but I need this to occur before the game starts.
import turtle
import random
import pygame
window_height = 600
window_width = 600
update_interval = 25
countdown_number = 3
river_width = 400
minimum_river_width = 200
border_height = 600
river_width_update = 0.5
safe_distance_from_border = border_height / 2 + 3
number_of_enemies = 10
enemies = []
enemy_speeds = []
enemy_width = 100
enemy_height = 40
enemy_speed_min, enemy_speed_max = 10, 20
safe_distance_from_enemy = 15
def startgame():
global countdown_number
if countdown_number >= 1:
turtle.home()
turtle.left(270)
turtle.clear()
turtle.speed(0)
for _ in range(0,100,5):
turtle.forward(1)
turtle.color("grey"+str(_))
turtle.write(countdown_number, align="center", font=("Arial", 200, "normal"))
if countdown_number == 0:
countdown_number = "GO!"
turtle.home()
turtle.left(270)
turtle.clear()
turtle.speed(0)
for _ in range(0,100,5):
turtle.forward(1)
turtle.color("grey"+str(_))
turtle.write(countdown_number, align="center", font=("Arial", 200, "normal"))
turtle.clear()
#print(number)
def gameover(msg):
print(msg) # Probably comment this out later
turtle.home()
turtle.write(msg, align="center", font=("Arial", 24, "normal"))
def moveplayerturtle(x, y):
if x > -window_width / 2 and x < window_width / 2:
turtle.goto(x, y)
def updatescreen():
global river_width
global countdown_number
if countdown_number != "GO!":
startgame()
if countdown_number == "GO!":
if upper_river_border.ycor() >= window_height-200:
upper_river_border.sety(upper_river_border.ycor() - river_width_update)
if lower_river_border.ycor() <= -window_height+200:
lower_river_border.sety(lower_river_border.ycor() + river_width_update)
if upper_river_border.ycor() - turtle.ycor() < safe_distance_from_border:
gameover("Game over!")
return
if turtle.ycor() - lower_river_border.ycor() < safe_distance_from_border:
gameover("Game over!")
return
for i in range(number_of_enemies):
enemies[i].forward(enemy_speeds[i])
if enemies[i].xcor() > (window_width+enemy_width)/2:
x = -(window_width+enemy_width)/2
y = int(turtle.ycor())
enemies[i].goto(x, y)
s=1
enemy_speeds[i]=int(random.randrange(10+s, 20+s))
if turtle.distance(enemies[i]) < safe_distance_from_enemy:
gameover("You lose!")
return
turtle.ontimer(updatescreen, update_interval)
turtle.update()
turtle.setup(window_width, window_height) # Set the window size
turtle.bgcolor("DarkBlue")
turtle.tracer(False)
upper_river_border = turtle.Turtle()
upper_river_border.up()
lower_river_border = turtle.Turtle()
lower_river_border.up()
upper_river_border.shape("square")
lower_river_border.shape("square")
upper_river_border.color("DarkOrange4")
lower_river_border.color("DarkOrange4")
upper_river_border.shapesize(30, 40)
lower_river_border.shapesize(30, 40)
upper_river_border.sety((border_height + river_width) / 2)
lower_river_border.sety(-(border_height + river_width) / 2)
turtle.addshape("crocodile.gif")
for _ in range(number_of_enemies):
enemy = turtle.Turtle()
turtle.addshape("crocodile.gif")
enemy.shape("crocodile.gif")
enemy.up()
x = -(window_width + enemy_width) / 2
y = random.randint(-(river_width-enemy_height)/2, (river_width-enemy_height)/2)
enemy.goto(x, y)
enemies.append(enemy)
enemy_speeds.append(random.randint(enemy_speed_min, enemy_speed_max))
turtle.shape("turtle")
turtle.left(180)
turtle.color("GreenYellow")
turtle.up()
turtle.ondrag(moveplayerturtle)
turtle.ontimer(updatescreen, update_interval)
turtle.done()
I've gotten the countdown to work perfectly fine on its own when I've written it as it's own program so I think it must just have to do with transitioning from the countdown sequence to the game loop itself that's causing the problem.
Any help would be appreciated.

You've got your wires crossed as far as the splash screen countdown -- calling the wrong thing at the wrong time. Below's my suggestion on a minimal fix. First, redefine startgame() as follows:
def startgame():
global countdown_number
turtle.home()
turtle.clear()
turtle.left(270)
if countdown_number > 0:
for grey in range(0, 100, 5):
turtle.forward(1)
turtle.color("grey" + str(grey))
turtle.write(countdown_number, align="center", font=("Arial", 200, "normal"))
countdown_number -= 1
turtle.ontimer(startgame, countdown_interval)
else:
for grey in range(0, 100, 5):
turtle.forward(1)
turtle.color("grey" + str(grey))
turtle.write("GO!", align="center", font=("Arial", 200, "normal"))
turtle.clear()
turtle.ontimer(updatescreen, countdown_interval)
Next some small changes to accomodate this rewrite. At the top of your code, add the definition:
countdown_interval = 1000
near the definition of update_interval.
At the bottom of your code, instead of this:
turtle.ontimer(updatescreen, update_interval)
turtle.done()
Do this:
turtle.ontimer(startgame, update_interval)
turtle.done()
Finally, in updatescreen() remove all code dealing with countdown_number:
global countdown_number
if countdown_number != "GO!":
startgame()
if countdown_number == "GO!":
and reindent the body of this function as needed.
That should do it. Happy dodging!

Related

I have problem with my game, when the ball goes over the block, it is not deleting from my screen

Someone can help me please?
I spent many hours to understand the problem.. (Without success 🤣)
I tried to built a 80s hit game.
The problem is that when the ball goes on one of the stones, I catch the event (shows me in the console) and update the list of blocks but the block is not deleted from the screen as if nothing happened and I did not update any list
this is my code:
import time
from turtle import Screen, Turtle
import random
screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.tracer(0)
player = Turtle("square")
player.shapesize(1.0, 4.0, 0)
player.color("blue")
player.penup()
player.goto(0, -250)
screen.tracer(0)
blue_turtle = []
blue_positions = []
for i in range(7):
turtle = Turtle("square")
turtle.shapesize(2.0 ,4.0, 0)
turtle.color("blue")
turtle.penup()
blue_positions.append((-300 + i * 100, 200))
blue_turtle.append(turtle)
def renderBlocks(b):
for i in range(len(b) - 1):
if b[i] == None:
print("Skipped")
continue
else:
x, y= blue_positions[i]
b[i].goto(x=x, y=y)
# total += 100
ball = Turtle("circle")
ball.color("white")
game_on = True
def move():
x = ball.xcor() + 10
y = ball.ycor()+ 10
ball.goto(x=x, y=y)
while game_on:
move()
renderBlocks(blue_turtle)
for i in range(len(blue_turtle) - 1):
if (blue_turtle[i] != None) and ball.distance(blue_turtle[i]) < 20:
blue_turtle[i] = (None)
time.sleep(0.1)
screen.update()
screen.exitonclick()
I believe the problem is here:
blue_turtle[i] = (None)
You tell your code that the turtle has been eliminated but you don't tell turtle graphics that it should be removed. Instead consider:
blue_turtle[i].hideturtle()
blue_turtle[i] = None
My rework of your code addressing this issue and some others:
from turtle import Screen, Turtle
def renderBlocks(b):
for i in range(len(b) - 1):
if b[i]:
x, y = blue_positions[i]
b[i].goto(x, y)
def move():
if game_on:
x = ball.xcor() + 10
y = ball.ycor() + 10
ball.goto(x, y)
renderBlocks(blue_turtles)
for i in range(len(blue_turtles) - 1):
if blue_turtles[i] and ball.distance(blue_turtles[i]) < 20:
blue_turtles[i].hideturtle()
blue_turtles[i] = None
screen.update()
screen.ontimer(move, 100) # milliseconds
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor('black')
screen.tracer(0)
player = Turtle('square')
player.shapesize(1, 4, 0)
player.color('blue')
player.penup()
player.sety(-250)
blue_turtles = []
blue_positions = []
for i in range(7):
turtle = Turtle('square')
turtle.shapesize(2, 4, 0)
turtle.color('blue')
turtle.penup()
blue_positions.append((-300 + i * 100, 200))
blue_turtles.append(turtle)
ball = Turtle('circle')
ball.color('white')
game_on = True
move()
screen.exitonclick()

One object gets stuck with two while loops

I'm trying to add physics to the paddle object whenever I press "w", but the ball object gets stuck until the while loop is finished in the paddle_up() function. How do I make the ball keep moving while the paddle is jumping?
The point of this game is avoiding the ball coming from the right. Whenever the user avoids the ball he or she gets one point, but whenever it collide's with the paddle, they lose one point:
import random
import turtle
# window setup
wn = turtle.Screen()
wn.title("Jump Game")
wn.bgcolor("black")
wn.setup(width=1000, height=750)
wn.tracer(0)
# Key Biding Functions
def paddle_up():
paddle_a.dy = -0.1
while paddle_a.ycor() < 0:
paddle_a.sety(paddle_a.ycor() - paddle_a.dy)
wn.update()
def paddle_down():
y = paddle_a.ycor()
y -= 10
paddle_a.sety(y)
def paddle_left():
x = paddle_a.xcor()
x -= 10
paddle_a.setx(x)
def paddle_right():
x = paddle_a.xcor()
x += 10
paddle_a.setx(x)
# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(1)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=2, stretch_len=2, outline=1)
paddle_a.penup()
paddle_a.goto(-480, -350)
# Ball
ball = turtle.Turtle()
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(-350, 0)
ball_speed = random.random()
ball_position_y = random.randint(-350, -300)
# Score System
Score = 0
Scr = turtle.Turtle()
Scr.goto(0, 360)
Scr.color("white")
Scr.penup()
Scr.hideturtle()
wn.listen()
wn.onkeypress(paddle_up, "w")
wn.onkeypress(paddle_down, "s")
wn.onkeypress(paddle_left, "a")
wn.onkeypress(paddle_right, "d")
# Main Game Loop
while True:
wn.update()
ball.setx(ball.xcor() - ball_speed * 0.3)
if ball_speed <= 0.3:
ball_speed = 0.3
if ball.xcor() < paddle_a.xcor() + 20 and ball.xcor() > paddle_a.xcor() - 20 and ball.ycor() < paddle_a.ycor() +20 and ball.ycor() > paddle_a.ycor() - 20:
Score = Score - 1
Scr.clear()
Scr.write("Score{}".format(Score), align="center", font=("Courier", 24))
ball.setx(490)
ball_position_y = random.randint(-350, -300)
ball.sety(ball_position_y)
elif ball.xcor() <= -490 and ball.ycor() != paddle_a.ycor():
Score = Score + 1
Scr.clear()
Scr.write("Score{}".format(Score), align="center", font=("Courier", 24))
ball.setx(490)
ball_position_y = random.randint(-350, -300)
ball.sety(ball_position_y)
You're using while loops where you should be using timer events. SO is full of Python turtle game answers that say, "while True: has no business in an event-driven world like turtle." How could you miss them?
I've reworked your code below to make it basically playable:
from random import randint
from turtle import Screen, Turtle
FONT = ('Courier', 24)
# Key Biding Functions
def paddle_up():
def paddle_rise():
if paddle.ycor() < 0:
paddle.sety(paddle.ycor() + paddle.dy)
screen.update()
screen.ontimer(paddle_rise, 10)
paddle_rise()
def paddle_down():
paddle.sety(paddle.ycor() - 10)
def paddle_left():
paddle.setx(paddle.xcor() - 10)
def paddle_right():
paddle.setx(paddle.xcor() + 10)
def update_score():
global ball_speed
scorer.clear()
scorer.write("Score: {}".format(score), align='center', font=FONT)
ball.setposition(490, randint(-350, -300))
ball_speed = randint(1, 3)
# Main Game Loop
def play():
global score
ball.setx(ball.xcor() - ball_speed)
if ball.distance(paddle) <= 20:
score -= 1
update_score()
elif ball.xcor() <= -490 and ball.ycor() != paddle.ycor():
score += 1
update_score()
screen.update()
screen.ontimer(play, 10)
# window setup
screen = Screen()
screen.title("Jump Game")
screen.bgcolor('black')
screen.setup(width=1000, height=800)
screen.tracer(0)
# Paddle A
paddle = Turtle()
paddle.shape('square')
paddle.color('white')
paddle.shapesize(stretch_wid=2, stretch_len=2, outline=1)
paddle.speed('slowest')
paddle.penup()
paddle.goto(-480, -350)
paddle.dy = 2
# Ball
ball = Turtle()
ball.shape('circle')
ball.color('white')
ball.penup()
ball.setposition(490, randint(-350, -300))
ball_speed = randint(1, 3)
# Score System
score = 0
scorer = Turtle()
scorer.hideturtle()
scorer.sety(360)
scorer.color('white')
scorer.penup()
scorer.write("Score: {}".format(score), align='center', font=FONT)
screen.onkeypress(paddle_up, 'w')
screen.onkeypress(paddle_down, 's')
screen.onkeypress(paddle_left, 'a')
screen.onkeypress(paddle_right, 'd')
screen.listen()
play()
screen.mainloop()

Start the game when SPACE is pressed (Turtle module)

I've been trying to code the Pong minigame using Turtle Graphics. Everything seems to work perfectly except for the beginning. I want the main loop to start iterating ONLY once the space key is pressed.
Here is the part i've been having trouble with:
## Start the game
start = False
def start_game():
startmessage.clear() #This is a turtle i created to show the message "Press SPACE to start"
start = True
ball_start() #This is a function i created to get the ball moving
wn.onkeypress(start_game, "space")
## Main loop
while start == True:
I've included .listen() and .mainloop(), so that's not the problem.
The full code is below. If i run the program with that code, this is what it does:
It asks for the user to enter the max points. The black window appears, the score table also appears, but for some reason the border doesn't.
When the max points value is entered in the shell, the message "Press SPACE to start" appeats in the black screen.
As soon as Space is pressed, that message disappears and nothing else occurs.
Here is the full code in case I'm missing something important:
import turtle
import random
import time
## Screen setup
wn = turtle.Screen()
wn.title("Pong!")
wn.bgcolor("black")
wn.setup(width = 900, height = 700)
wn.tracer(0)
# Border
collisions = 0
border = turtle.Turtle()
border.penup()
border.color("white")
border.setposition(-400,-300)
border.pendown()
for side in range(2):
border.forward(800)
border.left(90)
border.forward(600)
border.left(90)
border.hideturtle()
# Scores
scoreA = 0
scoreB = 0
score_marker = turtle.Turtle()
score_marker.shape("blank")
score_marker.color("yellow")
score_marker.speed(0)
score_marker.penup()
score_marker.setposition(0, 310)
score_marker.pendown()
score_marker.write("Player A : {} Player B: {}".format(scoreA,scoreB), align = "center", font = ("Courier", 20, "bold"))
# Select number of points
maxpoints = int(input("Enter max points: "))
print("The player who first gets to {} points wins the game!".format(maxpoints))
# Start message
startmessage = turtle.Turtle()
startmessage.speed(0)
startmessage.color("white")
startmessage.shape("blank")
startmessage.penup()
startmessage.setposition(0,75)
startmessage.pendown()
startmessage.write("Press SPACE to start", align = "center", font = ("Courier", 20, "bold"))
# End message
endmessage = turtle.Turtle()
endmessage.speed(0)
endmessage.color("green")
endmessage.shape("blank")
endmessage.penup()
endmessage.setposition(0,0)
endmessage.pendown()
## Paddles
paddleB_speed = 25
paddleA_speed = paddleB_speed
wn.listen()
# Paddle A
paddleA = turtle.Turtle()
paddleA.speed(0)
paddleA.shape("square")
paddleA.color("white")
paddleA.shapesize(3.2,0.7)
paddleA.penup()
paddleA.goto(-350,0)
# Paddle A movement
def a_up():
y = paddleA.ycor() + paddleA_speed
paddleA.sety(y)
def a_down():
y = paddleA.ycor() - paddleA_speed
paddleA.sety(y)
wn.onkeypress(a_up,"w")
wn.onkeypress(a_down,"s")
# Paddle B
paddleB = turtle.Turtle()
paddleB.speed(0)
paddleB.shape("square")
paddleB.color("white")
paddleB.shapesize(3.2,0.7)
paddleB.penup()
paddleB.goto(350,0)
# Paddle B movement
def b_up():
y = paddleB.ycor() + paddleB_speed
paddleB.sety(y)
def b_down():
y = paddleB.ycor() - paddleB_speed
paddleB.sety(y)
wn.onkeypress(b_up,"Up")
wn.onkeypress(b_down,"Down")
## Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.shapesize(0.5,0.5)
ball.penup()
ball.setposition(0,0)
ballspeed = 0.3
ballspeed_increase = 0.01
# Ball starting movement
def ball_start():
angle_ranges = list(range(30,60)) + list(range(120,150)) + list(range(210,240)) + list(range(300,330))
angle = random.choice(angle_ranges)
ball.setheading(angle)
ball_start()
## Exit the game
def exit_game():
wn.bye()
## Start the game
start = False
def start_game():
startmessage.clear()
start = True
ball_start()
wn.onkeypress(start_game, "space")
## Main loop
while start == True:
wn.update()
### MOVEMENT
## Ball movement
ball.forward(ballspeed)
### COLLISIONS
## Paddles and border
# Paddle A
if paddleA.ycor() > 268:
paddleA.sety(268)
if paddleA.ycor() < -268:
paddleA.sety(-268)
# Paddle B
if paddleB.ycor() > 268:
paddleB.sety(268)
if paddleB.ycor() < -268:
paddleB.sety(-268)
## Ball and paddles
if paddleA.distance(ball) <= 10:
collisions += 1
#direction = ball.heading()
#ball.setheading(180 - random.randint(0,30) - direction)
ball.setheading(random.randint(110,250) + 180)
ball.forward(ballspeed + ballspeed_increase)
if paddleB.distance(ball) <= 10:
collisions += 1
#direction = ball.heading()
#ball.setheading(180 - random.randint(0,30) - direction)
ball.setheading(random.randint(110,250))
ball.forward(ballspeed + ballspeed_increase)
## Ball and border
# Top and bottom borders
if ball.ycor() < - 296 or ball.ycor() > 296:
collisions += 1
direction = ball.heading()
ball.setheading(360 - direction)
ball.forward(ballspeed + ballspeed_increase)
# Left and right borders
if ball.xcor() < -396: #--> Player B wins the point
delay = 0.1
time.sleep(1)
collisions += 1
ball.setposition(0,0)
scoreB += 1
score_marker.clear()
score_marker.write("Player A : {} Player B: {}".format(scoreA,scoreB), align = "center", font = ("Courier", 20, "bold"))
ball_start()
if ball.xcor() > 396: #--> Player A wins the poin
delay = 0.1
time.sleep(1)
collisions += 1
ball.setposition(0,0)
scoreA += 1
score_marker.clear()
score_marker.write("Player A : {} Player B: {}".format(scoreA,scoreB), align = "center", font = ("Courier", 20, "bold"))
ball_start()
# End of the game
if scoreA == maxpoints or scoreB == maxpoints:
if scoreA == maxpoints:
print("Player A wins the game!")
endmessage.write("Player A wins the game!\nA scored {} points\nB scored {} points".format(scoreA,scoreB), align = "center", font = ("Courier", 18, "bold"))
time.sleep(1)
endmessage.penup()
endmessage.setposition(0,-100)
endmessage.pendown()
endmessage.color("red")
endmessage.write("Press ESC to exit game", align = "center", font = ("Courier", 14, "normal"))
wn.onkeypress(exit_game, "Escape")
break
if scoreB == maxpoints:
print("Player B wins the game!")
endmessage.write("Player B wins the game!\nA scored {} points\nB scored {} points".format(scoreA,scoreB), align = "center", font = ("Courier", 18, "bold"))
time.sleep(1)
endmessage.penup()
endmessage.setposition(0,-100)
endmessage.pendown()
endmessage.color("red")
endmessage.write("Press ESC to exit game", align = "center", font = ("Courier", 14, "normal"))
wn.onkeypress(exit_game, "Escape")
break
wn.mainloop()
This code looks like it resets start but it doesn't -- it sets a local start and ignores the global start:
start = False
def start_game():
startmessage.clear()
start = True
ball_start()
You need a global statement:
start = False
def start_game():
global start
startmessage.clear()
start = True
ball_start()
The above is necessary but not sufficient. The next problem is the way you use a top level while loop instead of a timer event. The way I'd expect this game to start up and run would be more like:
from turtle import Screen, Turtle
running = False
def start_game():
global running
start_message.clear()
running = True
ball_start()
def ball_start():
start_message.write("We've started the game!", align="center", font=("Courier", 20, "bold"))
# what really ball_start() really does replaces the above...
def move_one_step():
if running:
screen.update()
# ...
screen.ontimer(move_one_step, 100)
screen = Screen()
start_message = Turtle()
start_message.hideturtle()
start_message.penup()
start_message.sety(75)
start_message.write("Press SPACE to start", align="center", font=("Courier", 20, "bold"))
screen.onkeypress(start_game, 'space')
screen.listen()
move_one_step()
screen.mainloop()
I think it might be the way you used the space key value. You put in space instead of return.
wn.onkeypress(start_game, "return")

Detect collision of two Turtles using 'if' statements

I am trying to create a game where a turtle is confined in a box. A ball bounces around the box, and if it collides with the turtle, or if the turtle touches the perimeter of the box, the game is over. A red dot spawns in a random location in the box, and when the turtle runs over that dot, the dot is supposed to disappear and respawn somewhere else. However, this only works sometimes. Sometimes the dot will randomly disappear and spawn somewhere else even though the turtle isn't even close to it. How can I fix this?
from turtle import Screen, Turtle, mainloop
wn = Screen()
wn.bgcolor("light blue")
#CONTROLLABLE TURTLE
t = Turtle()
t.color('black')
t.pensize(10)
t.shape("turtle")
t.speed("fastest")
t.penup()
t.goto(-130,-200)
t.pendown()
global hit
global score
hit = False
score = 0
#MAKE SQUARE
for i in range(4):
t.forward(400)
t.left(90)
t.penup()
t.goto(50,0)
speed = 2
t.color("black")
#BALL
ball1 = turtle.Turtle()
ball1.color("blue")
ball1.speed(0)
ball1.penup()
ball1.shape("circle")
A = random.randint(30,60)
B = random.randint(120,150)
C = random.randint(210,240)
D = random.randint(300,330)
Directions = [A, B, C, D]
direct = random.choice(Directions)
def tDirection(direct):
ball1.right(direct)
tDirection(direct)
angle = 90
#DOT TURTLE
dot = turtle.Turtle()
dot.color("black")
dot.speed(0)
dot.hideturtle()
def createDot():
dotx = random.randint(-10,230)
doty = random.randint(-160,200)
dot.penup()
dot.goto(dotx,doty)
dot.pendown()
dot.pensize(3)
dot.fillcolor("Red")
dot.begin_fill()
dot.circle(7)
dot.end_fill()
createDot()
#make score function
while True:
if hit == False:
#moving ball
ty = ball1.ycor()
tx = ball1.xcor()
if ty < -183:
angleCurr = ball1.heading()
if(270>angleCurr>180):
ball1.right(angle)
else:
ball1.left(angle)
ball1.forward(2)
elif ty > 185:
angleCurr = ball1.heading()
if(0<angleCurr<90):
ball1.right(angle)
else:
ball1.left(angle)
ball1.forward(2)
elif tx < -115:
angleCurr = ball1.heading()
if(180<angleCurr<270):
ball1.left(angle)
else:
ball1.right(angle)
ball1.forward(2)
elif tx > 251:
angleCurr = ball1.heading()
if(0<angleCurr<90):
ball1.left(angle)
else:
ball1.right(angle)
ball1.forward(9)
wn.onkey(lambda: t.setheading(180), 'Left')
wn.onkey(lambda: t.setheading(0), 'Right')
wn.onkey(lambda: t.setheading(90), 'Up')
wn.onkey(lambda: t.setheading(270), 'Down')
w = turtle.Turtle()
w.hideturtle()
w.penup()
w.speed("fastest")
def end():
w.goto(-95,-20)
w.pendown()
w.write("GAME OVER", font=("Arial", 40, "normal"))
t.hideturtle()
ball1.hideturtle()
dot.hideturtle()
dot.clear()
speed = 2
def turtleMove():
t.forward(speed)
wn.ontimer(turtleMove, 10)
dodx = dot.xcor()
dody = dot.ycor()
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
hit = True
dot.clear()
elif abs(t.xcor() - tx) < 5 and abs(t.ycor() - ty) < 5:
end()
if t.xcor() > 253 or t.xcor() < -115 or t.ycor() > 185 or t.ycor() < -183:
end()
turtleMove()
wn.mainloop()
wn.listen()
if hit == True:
createDot()
score+=1
print(score)
hit = False
I think it might just be a typo:
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
Your collision detection is doing a comparison with the y coordinates instead of subtracting like you did with the x coordinates.
So this should fix it:
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() - dody) < 5:
I would avoid code like this:
dodx = dot.xcor()
dody = dot.ycor()
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
hit = True
dot.clear()
elif abs(t.xcor() - tx) < 5 and abs(t.ycor() - ty) < 5:
end()
And instead use turtle's distance() method:
if t.distance(dot) < 5:
hit = True
dot.clear()
elif t.distance(ball1) < 5:
end()
However, this only works sometimes. Sometimes the dot will randomly
disappear and spawn somewhere else even though the turtle isn't even
close to it.
Are you pretending that this code runs? It doesn't even start up due to two different import issues. Fixing those, the turtle controls don't work at all. And the ball doesn't bounce around the box!
This appears to be a random collection of bits of code borrowed from other programs patched together with wishful thinking. It simply doesn't work.
Below I've taken apart your code and put it back together again such that it basically runs. The blue ball does bounce around the box; you can move the turtle with the arrow keys; the red dot does disappear and reappear elsewhere when the turtle touches it:
from turtle import Screen, Turtle
from random import randint, choice
CURSOR_SIZE = 20
def tDirection(direct):
ball.right(direct)
def turtleMove():
turtle.forward(speed)
screen.ontimer(turtleMove, 10)
def createDot():
x, y = randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE), randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE)
dot.goto(x, y)
def end():
marker.write("GAME OVER", align='center', font=("Arial", 40, "normal"))
turtle.hideturtle()
ball.hideturtle()
dot.hideturtle()
screen = Screen()
screen.bgcolor("light blue")
score = 0
speed = 2
# CONTROLLABLE TURTLE
turtle = Turtle("turtle")
turtle.color('black')
turtle.pensize(10)
turtle.speed("fastest")
turtle.penup()
turtle.goto(-200, -200)
turtle.pendown()
# MAKE SQUARE
for i in range(4):
turtle.forward(400)
turtle.left(90)
turtle.penup()
turtle.goto(50, 0)
# BALL
ball = Turtle("circle")
ball.color("blue")
ball.speed('fastest')
ball.penup()
A = randint(30, 60)
B = randint(120, 150)
C = randint(210, 240)
D = randint(300, 330)
directions = [A, B, C, D]
direct = choice(directions)
tDirection(direct)
angle = 90
# DOT TURTLE
dot = Turtle('circle')
dot.color("red")
dot.speed('fastest')
dot.pensize(3)
dot.penup()
createDot()
marker = Turtle(visible=False)
marker.penup()
marker.sety(-20)
screen.onkey(lambda: turtle.setheading(0), 'Right')
screen.onkey(lambda: turtle.setheading(90), 'Up')
screen.onkey(lambda: turtle.setheading(180), 'Left')
screen.onkey(lambda: turtle.setheading(270), 'Down')
screen.listen()
turtleMove()
while True:
# moving ball
tx, ty = ball.position()
if ty < CURSOR_SIZE - 200:
angleCurr = ball.heading()
if 270 > angleCurr > 180:
ball.right(angle)
else:
ball.left(angle)
ball.forward(2)
elif ty > 200 - CURSOR_SIZE:
angleCurr = ball.heading()
if 0 < angleCurr < 90:
ball.right(angle)
else:
ball.left(angle)
ball.forward(2)
elif tx < CURSOR_SIZE - 200:
angleCurr = ball.heading()
if 180 < angleCurr < 270:
ball.left(angle)
else:
ball.right(angle)
ball.forward(2)
elif tx > 200 - CURSOR_SIZE:
angleCurr = ball.heading()
if 0 < angleCurr < 90:
ball.left(angle)
else:
ball.right(angle)
ball.forward(2)
ball.forward(9)
if turtle.distance(dot) < CURSOR_SIZE/2:
score += 1
dot.hideturtle()
createDot()
dot.showturtle()
elif turtle.distance(ball) < CURSOR_SIZE/2:
end()
if not CURSOR_SIZE - 200 < turtle.xcor() < 200 - CURSOR_SIZE or not CURSOR_SIZE - 200 < turtle.ycor() < 200 - CURSOR_SIZE:
end()
screen.mainloop()
When asking for help on SO, please be honest about the state of your code.
The above code isn't complete, there is still work to do. E.g. the while True: loop should be converted to a function and ontimer() event; the score needs to be displayed; the game should be made restartable.

Python turtle | Display won't show

I was working on fixing up some code to make it look better and when I finished copying the code over and finishing fixing it, I ran it and nothing happened. Two things loaded then everything clears. I can't find the problem.
Original code works
import turtle
from random import randint
import time
"""-------------"""
t=turtle.Turtle()
s=turtle.Screen()
cube=turtle.Turtle()
title=turtle.Turtle()
"""--------------"""
WIDTH, HEIGHT=300, 300
LENGTH=(5)
"""--------------"""
x1 = randint(cube.xcor()/2 - WIDTH/2, WIDTH/2 - cube.xcor()/2)
y1 = randint(cube.ycor()/2 - HEIGHT/2, HEIGHT/2 - cube.ycor()/2)
"""------------"""
s.setup(WIDTH,HEIGHT)
"""------------"""
t.width(1)
s.bgcolor("dark green")
"""-----------------"""
#Title
title.color("Red")
title.hideturtle()
title.penup()
title.goto(0,130)
title.pendown()
title.write("SNAK3",align="center", font=("Lobster", 20, "Bold"))
"""-------------"""
class Food():
def block():
cube.hideturtle()
cube.color("red")
cube.penup()
cube.goto(x1,y1)
cube.speed(0)
cube.begin_fill()
for i in range(4):
cube.pendown()
cube.forward(10)
cube.right(90)
cube.end_fill()
"""---------------"""
t.shape("square")
class Player():
def move_up():
player=False
while player==False:
for i in range(10):
t.forward(LENGTH)
t.delay(25)
t.clear()
x, y = t.position()
if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT / 2:
player=True
t.hideturtle()
cube.clear()
title.clear()
title.penup()
title.goto(0,0)
title.pendown()
title.write("GAMEOVER", align="center", font=("lobster", 18, "bold"))
def move_left():
t.speed(0)
t.left(90)
t.speed(3)
def move_right():
t.speed(0)
t.right(90)
t.speed(3)
"""------------"""
food=Food.block()
player1=Player()
"""----------------"""
s.onkey(Player.move_up,"up")
s.onkey(Player.move_left,"left")
s.onkey(Player.move_right,"right")
s.listen()
"""--------------"""
collision=Collision_check.check()
"""--------------------"""
t1=t.xcor(),t.ycor()
cube1=cube.xcor(),cube.ycor()
class Collision_check():
def check():
crash=True
if t1.xcor()>=cube.xcor() and t1.xcor()<=cube.xcor()+10 and t1.ycor>=cube.ycor() and t1.ycor()<=cube.ycor+10:
cube.clear()
cube.goto(x1,y1)
LENGTH=+1
elif t1!=cube1:
crash=False
return crash
Fixed code with slight changes
import turtle
from random import randint
t=turtle.Turtle()
s=turtle.Screen()
cube=turtle.Turtle()
title=turtle.Turtle()
t.width(1)
s.bgcolor("dark green")
#Title
title.color("red")
title.hideturtle()
title.penup()
title.goto(0,130)
title.write("SNAK3",align="center",font=("lobster",20,"Bold"))
WIDTH, HEIGHT=300, 300
LENGTH=[5]
s.setup(WIDTH,HEIGHT)
#Random cords
x1=randint(cube.xcor()/2-WIDTH/2, WIDTH/2-cube.xcor()/2)
y1=randint(cube.ycor()/2-HEIGHT/2, HEIGHT/2-cube.ycor()/2)
class Food():
def block():
cube.hideturtle()
cube.color("red")
cube.penup()
cube.goto(x1,y1)
cube.speed(0)
cube.begin_fill()
cube.pendown()
for i in range(4):
cube.forward(10)
cube.right(90)
cube.end_fill()
t.shape("square")
class Player():
def forward():
player=False
while player==False:
for i in range(10):
t.forward(LENGTH)
t.delay(25)
t.clear
x, y=t.position()
if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT/2:
player=True
cube.clear()
t.hideturtle()
title.clear()
title.penup()
title.goto(0,0)
title.pendown()
title.write("GAMEOVER", align="center", font=("lobster",18,"bold"))
def left():
t.speed(0)
t.left(90)
t.speed(3)
def right():
t.speed(0)
t.right(90)
t.speed(3)
food=Food.block()
player1=Player()
s.onkey(player1.forward,("up"))
s.onkey(player1.left,("left"))
s.onkey(player1.left,("right"))
class Collision_check():
def check():
crash=True
if t.xcor()>=cube.xcor() and t.xcor()<=cube.xcor()+10 and t.ycor()>=cube.ycor() and t.ycor()<=cube.ycor()+10:
cube.clear()
cube.goto(x1,y1)
LENGTH+=1
if cube.xcor()>=t.xcor() and cube.xcor()<=t.xcor()+10 and cube.ycor()>=t.ycor() and cube.ycor()<=t.ycor()+10:
cube.clear()
cube.goto(x1,y1)
LENGTH+=1
else:
crash=False
return crash
collision=Collision_check.check()
Everything in the code above is nearly exactly the same as the original. Ive gone over the code several times and can not find the problem.
I've ruled out that the collision_check is not the problem, all the classes are in order and aligned. The only other thing I could think of is that i have spelled something wrong.
Let's first address some basic facts:
Neither version of this code works
The slight changes to make it look better don't make it look better
and aren't slight, there are significant differences
Neither version of the code uses Python classes correctly
repl.it provides its own implementation of turtle which is generally
a subset of the original
A program is like a story, you first explain the background of events and then you proceed to tell the story in an order that makes logical sense.
Here's my rework of your code to try to tell a better story, but it still doesn't work completely as there are parts of the story missing:
from turtle import Turtle, Screen
from random import randint
WIDTH, HEIGHT = 300, 300
class Food():
def block():
cube.penup()
cube.goto(x1, y1)
cube.pendown()
cube.begin_fill()
for _ in range(4):
cube.forward(10)
cube.right(90)
cube.end_fill()
class Player():
def forward():
for _ in range(10):
t.forward(LENGTH)
# t.delay(25) # no such turtle method, perhaps repl.it addition
t.clear()
x, y = t.position()
if not -WIDTH / 2 < x < WIDTH / 2 or not -HEIGHT / 2 < y < HEIGHT/2:
cube.clear()
t.hideturtle()
title.clear()
title.penup()
title.goto(0, 0)
title.pendown()
title.write("GAMEOVER", align='center', font=('lobster', 18, 'bold'))
def left():
t.speed(0)
t.left(90)
t.speed(3)
def right():
t.speed(0)
t.right(90)
t.speed(3)
class Collision_check():
def check():
global LENGTH
crash = True
if t.xcor() >= cube.xcor() and t.xcor() <= cube.xcor() + 10 and t.ycor() >= cube.ycor() and t.ycor() <= cube.ycor() + 10:
cube.clear()
cube.goto(x1, y1)
LENGTH += 1
if cube.xcor() >= t.xcor() and cube.xcor() <= t.xcor()+10 and cube.ycor() >= t.ycor() and cube.ycor() <= t.ycor()+10:
cube.clear()
cube.goto(x1, y1)
LENGTH += 1
else:
crash = False
return crash
s = Screen()
s.setup(WIDTH, HEIGHT)
s.bgcolor('dark green')
t = Turtle()
t.shape('square')
t.width(1)
cube = Turtle()
cube.hideturtle()
cube.color('red')
cube.speed('fastest')
# Title
title = Turtle()
title.hideturtle()
title.color('red')
title.penup()
title.goto(0, 130)
title.write("SNAK3", align='center', font=('lobster', 20, 'bold'))
LENGTH = 5
# Random cords
x1 = randint(cube.xcor()/2 - WIDTH/2, WIDTH/2 - cube.xcor()/2)
y1 = randint(cube.ycor()/2 - HEIGHT/2, HEIGHT/2 - cube.ycor()/2)
food = Food.block()
player1 = Player()
s.onkey(Player.forward, 'Up')
s.onkey(Player.left, 'Left')
s.onkey(Player.right, 'Right')
s.listen()
# this makes no sense here, needs to happen on a regular basis after moves
collision = Collision_check.check()
s.mainloop()

Categories

Resources