Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

python - How do you move a turtle horizontally without changing it's heading?

[Gravity Code][1] I am making a game called Gravity code. And when the turtle catches falling "code", I need it to move horizontally, but to do that I have to turn the turtle to the right, move forward, then turn left. Because settiltangle doesn't work in trinket.

import turtle

user = turtle.Turtle()
user.speed("fastest")
user.shape("turtle")
user.fillcolor("white")
user.left(90)
user.penup()
user.goto(0, -192)

def left():
  user.left(90)
  user.forward(10)
  user.right(90)

def right():
  user.right(90)
  user.forward(10)
  user.left(90)

# --- events ---

screen.onkey(left, 'Left')
screen.onkey(left, 'a')
screen.onkey(right, 'Right')
screen.onkey(right, 'd')
screen.listen()
screen.mainloop()
question from:https://stackoverflow.com/questions/65646245/how-do-you-move-a-turtle-horizontally-without-changing-its-heading

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
# --- imports ---

import turtle
import random

# --- variables ---

x_title = -70
y_title = 60
game_title = "Gravity Code"
title = turtle.Turtle()
font_setup = ("Verdana", 25, "normal")
screen = turtle.Screen()

# --- main ---

screen.colormode(255)
title.speed("fastest")
title.hideturtle()
title.penup()
title.goto(x_title, y_title)

for i in range(len(game_title)):
    r = random.randint(1, 255)
    g = random.randint(1, 255)
    b = random.randint(1, 255)
    if game_title[i] != " ":
        title.color((r, g, b))
        x_title += 25
        title.write(game_title[i], font = font_setup)
        title.goto(x_title, y_title)
    else:
        x_title = -80
        y_title = 30
        title.goto(x_title, y_title)

# --- events ---

screen.mainloop()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...