D
D
DesZaz2021-04-24 23:31:04
PHP
DesZaz, 2021-04-24 23:31:04

Turtle not responding when running while loop, how to fix?

from turtle import *
from random import randint

class Game(Turtle):
    def __init__(self,x,y,step = 10, shape = "circle", color = "black"):
        super().__init__()
        self.penup()
        self.speed(0)
        self.goto(x,y)
        self.color(color)
        self.shape(shape)
        self.step = step
    def move_up(self):
        self.goto(self.xcor(), self.ycor() + 10)
    def move_down(self):
        self.goto(self.xcor(), self.ycor() - 10)
    def move_left(self):
        self.goto(self.xcor() - 10, self.ycor())
    def move_right(self):
        self.goto(self.xcor() + 10, self.ycor())
    def Kasanie(self,Sprite1,Sprite2):
        dis1 = self.distance(Sprite1.xcor(), Sprite1.ycor())
        dis2 = self.distance(Sprite2.xcor(), Sprite2.ycor())
        if dis1 < 30 or dis2 < 30:
            return True
        else:
            return False
    def Kasanie2(self,goal):
        dis1 = self.distance(goal.xcor(), goal.ycor())
        if dis1 < 30:
            return True
        else:
            return False

player = Game(0, -100, 10, 'circle', 'orange')
Sprite1 = Game(-200, 200, 10, 'square', 'red')
Sprite2 = Game(200, 100, 10, 'square', 'red')
End = Game (0, 300, 10, 'triangle', 'Lime')

scr = player.getscreen()
scr.listen()
scr.onkey(player.move_up, "w")
scr.onkey(player.move_down, "s")
scr.onkey(player.move_left, "a")
scr.onkey(player.move_right, "d")


Inf1 = player.Kasanie(Sprite1,Sprite2)
Inf2 = player.Kasanie2(End)
total_score = 0
while total_score < 3:
    if Inf1:
        player.goto(0,-100)
        total_score += 1
    if Inf2:
        End.hideturtle()
        break
Sprite2.hideturtle()
Sprite1.hideturtle()


player.getscreen()._root.mainloop()
player.done()

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ukolov, 2018-08-22
@alexey-m-ukolov

In the first case, not a comparison , but an assignment . As a result, the value of the variable a is used in the condition , which is equal to 1 , which is converted to true .
In the second case, on the first line, you are doing a comparison , not an assignment . Therefore, in the second line, the variable $a has no value, that is, it is definitely not equal to 1 .
With php everything is ok, the problem is with your code.

R
Roman Terekhin, 2018-08-22
@RomaZveR

$a = 0;
if ($a == 1) {
   echo "yes";
} else {
   echo "no";
}

A
Alan Gibizov, 2021-04-25
@phaggi

I guess you'll find examples here that will help you build your game more successfully without those endless loops.

K
Koipse, 2021-04-25
@Koipse

A possible problem is that the thread "walks" through the code and already checks the player's action:

scr.listen()
scr.onkey(player.move_up, "w")
scr.onkey(player.move_down, "s")
scr.onkey(player.move_left, "a")
scr.onkey(player.move_right, "d")

However, stopping at:
while total_score < 3:
    if Inf1:
        player.goto(0,-100)
        total_score += 1
    if Inf2:
        End.hideturtle()
        break

total_score will never be greater than 3, because the check is infinite and will never exit the while, which is why the application hangs. You don't have a "way out" in the if in the while loop, so there is an endless polling from if1 to if2. The conditions don't match. We return to the beginning of the while, and what is there? If1. Further If2. They do not satisfy the conditions, we return ... The essence is clear?
You can try:
while total_score < 3:
    if Inf1:
        player.goto(0,-100)
        total_score += 1
    else:
        End.hideturtle()
        break

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question