0

I have this piece of code that has a block which moves towards the player. The player avoids this block by clicking to jump (basically Flappy Bird). if the player touches the ground, they lose, and if they touch the block, they also lose.

It was working fine, until I implemented the code that made the block/pipe move. While the block is moving, the player object doesn't move. Why?

Here is the code:

import turtle as t
import time
import math 
ii = 0
heading = 0
Screen = t.Screen()
Screen.title("Block")
Screen.bgcolor("white")
Screen.setup(1000, 1000, startx = 0, starty = 0)
xmin = -t.window_width()/2
xmax = t.window_width()/2
ymin = -t.window_height()/2
print(ymin)
ymax = t.window_height()/2
Block = t.Turtle()
Block.seth(heading)
Block.speed(0)
Block.penup()
pipe = t.Turtle()
pipe.shape('square')
pipe.color('green')
pipe.goto(x=500, y=0)
speed_y = 0
print("hi")
def flap(Block, p):
    global speed_y, heading
    speed_y = 0
    heading = 35
    pi = 0
    while True:
        if pi != 10:
            Block.sety(Block.ycor() + 5.5)
            pi += 1
        else:
            break
        
Screen.onkey(lambda:flap(Block, p), 'w')
Screen.onkey(lambda:flap(Block, p), 'space')
Screen.onkey(lambda:flap(Block, p), 'Up')
def isCollision(t1, t2):
    d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
    if d < 20:
        return True
    else:
        return False
def fun():
    global ii
    time.sleep(1)
    ii += 1
    json.dumps(ii)
    return ii
Screen.listen() 
def fall(Block, p):
    global speed_y, heading
    if speed_y == 0 or speed_y == 1:
        speed_y = speed_y + 2
    else:
        speed_y = speed_y+0.1
    y = Block.ycor()
    Block.sety(y-speed_y)
    heading = heading - 1
    return speed_y

def PipeLeft(pipe):
    x = pipe.ycor()
    pipe.setx(x-0.1)
while True:
    if isCollision(Block, pipe) == True:
        t.write("YOU LOSE", font = ("arial", 40, "bold"))
        time.sleep(4)
        break
    PipeLeft(pipe)
    fall(Block, p)
    if Block.ycor() <= -350.5 or Block.ycor() >= 600:
        Block.sety(-350.5)
        t.write("YOU LOSE", font = ("arial", 40, "bold"))
        time.sleep(4)
        break
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • while true loops and the sleep function are really bad ideas for most gui programs because while the interpreter is sleeping or while it is executing your while true loop it cannot accept input from the users interactions with the window and the window appears frozen and useless – Alexander Sep 18 '22 at 02:41
  • Well, the sleep function is only after the game has ended, so there is really no reason for the user give an input after that. About the loop, is there another way i can continuously check if it's touching the floor? thanks. – Synt4xErr0r 7 Sep 18 '22 at 02:49
  • Honestly I cant tell what the code is doing... Which part is the floor. And what is the moving block doing? – Alexander Sep 18 '22 at 03:19
  • I'm having a hard time following the code too. I suggest breaking the problem down and simplifying your design. It'll be extremely difficult to make a clean realtime Flappy Bird game without `turtle.tracer(0)` and `turtle.update()`. Avoid `while` loops in favor of `ontimer`. I suggest [this realtime skeleton](https://stackoverflow.com/questions/47879608/how-to-bind-several-key-presses-together-in-turtle-graphics/70979967#70979967). – ggorlen Sep 18 '22 at 21:17

0 Answers0