Python Forum

Full Version: turtle moving platform
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
working on python game using turtles.

have player object that moves up and down (jump) on key press. trying to add moving platform that the player has to jump on.

i tried putting moving platform in a while loop. problem is since the while loop is running to keep the platform moving, the program does not detect key press.

-tried moving turtle.listen() inside the main while loop but that didnt work.

How do I keep the platform moving in a while True loop and have the listener active?

import turtle


screen = turtle.Screen()
screen.screensize(1000, 1000)
screen.bgcolor("light green")

player = turtle.Turtle()
player.hideturtle()
player.penup()
player.shape("circle")
player.color("red")
player.fillcolor("yellow")
player.setposition(-50, -40)
player.showturtle()

s = turtle.Turtle()
s.hideturtle()
s.penup()
s.shape("square")
s.color("black")
s.fillcolor("white")
s.setposition(-50, -60)
s.showturtle()

#first tier
s1 = turtle.Turtle()
s1.hideturtle()
s1.penup()
s1.shape("square")
s1.color("black")
s1.fillcolor("white")
s1.showturtle()

s2 = turtle.Turtle()
s2.hideturtle()
s2.penup()
s2.shape("square")
s2.color("black")
s2.fillcolor("white")
s2.setposition(s1.xcor()+20, 0)
s2.showturtle()


s3 = turtle.Turtle()
s3.hideturtle()
s3.penup()
s3.shape("square")
s3.color("black")
s3.fillcolor("white")
s3.setposition(s2.xcor()+20, 0)
s3.showturtle()


s4 = turtle.Turtle()
s4.hideturtle()
s4.penup()
s4.shape("square")
s4.color("black")
s4.fillcolor("white")
s4.setposition(s3.xcor()+20, 0)
s4.showturtle()

#second tier
s5 = turtle.Turtle()
s5.hideturtle()
s5.penup()
s5.shape("square")
s5.color("black")
s5.fillcolor("white")
s5.setposition(s4.xcor()+30, 60)
s5.showturtle()

s6 = turtle.Turtle()
s6.hideturtle()
s6.penup()
s6.shape("square")
s6.color("black")
s6.fillcolor("white")
s6.setposition(s5.xcor()+20, 60)
s6.showturtle()

s7 = turtle.Turtle()
s7.hideturtle()
s7.penup()
s7.shape("square")
s7.color("black")
s7.fillcolor("white")
s7.setposition(s6.xcor()+20, 60)
s7.showturtle()

s8 = turtle.Turtle()
s8.hideturtle()
s8.penup()
s8.shape("square")
s8.color("black")
s8.fillcolor("white")
s8.setposition(s7.xcor()+20, 60)
s8.showturtle()

#third tier
s9 = turtle.Turtle()
s9.hideturtle()
s9.penup()
s9.shape("square")
s9.color("black")
s9.fillcolor("white")
s9.setposition(s8.xcor()+30, 120)
s9.showturtle()

s10 = turtle.Turtle()
s10.hideturtle()
s10.penup()
s10.shape("square")
s10.color("black")
s10.fillcolor("white")
s10.setposition(s9.xcor()+20, 120)
s10.showturtle()

s11 = turtle.Turtle()
s11.hideturtle()
s11.penup()
s11.shape("square")
s11.color("black")
s11.fillcolor("white")
s11.setposition(s10.xcor()+20, 120)
s11.showturtle()

s12 = turtle.Turtle()
s12.hideturtle()
s12.penup()
s12.shape("square")
s12.color("black")
s12.fillcolor("white")
s12.setposition(s11.xcor()+20, 120)
s12.showturtle()

s13 = turtle.Turtle()
s13.hideturtle()
s13.penup()
s13.shape("square")
s13.color("black")
s13.fillcolor("red")
s13.setposition(s11.xcor()+20, 180)
s13.showturtle()

#moving plaform
while True:
s13.backward(3)
if s13.xcor() > 250:
s13.setheading(0)
if s13.xcor() < -200:
s13.setheading(180)

squareList = [s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12]

falling = False
def jump():
speed = 2
while speed > 0:
y = player.ycor()
x = player.xcor()
y += 6
x += 1.3
speed -= 0.09
player.setposition(x, y)
if speed < 0:
falling = True
while falling:
y = player.ycor()
x = player.xcor()
y -= 2
x += 1
speed += .10
player.setposition(x, y)
for squares in squareList:
if (player.xcor() >= 0 and player.xcor() <= 80) and player.ycor() == 20:
falling = False
if (player.xcor() >= 80 and player.xcor() <= 160) and player.ycor() == 80:
falling = False
if(player.xcor() >= 160 and player.xcor() <= 240) and player.ycor() == 140:
falling = False

def right():
x = player.xcor()
x += 2
player.setx(x)

turtle.listen()
turtle.onkey(jump, "Up")
turtle.onkey(right, "Right")






turtle.mainloop()
In short, how do you make an object move continually via a main while loop, yet be able to detect keyboard events? Seems logical to put the turtle.listen() and turtle.onkey(...) in the main while loop but that's doesnt work (cant see subfunctions in the onkey(..., left())

I moved the main while loop below the turtle.onkey(), and it recognized the key press, however, the platform stops moving when you press a key (it must interrupt the main while loop).

tried putting main while loop in a subfunction and calling it in the key press subfunction but that didnt work either.