简体   繁体   English

为什么当我尝试向其形状添加更多内容时,我的乌龟停止移动?

[英]Why does my turtle stop moving when I try to add more to its shape?

I am trying to draw a shape and then move that shape around the screen with the arrow keys.我正在尝试绘制一个形状,然后使用箭头键在屏幕上移动该形状。 My goal is to make a 2D game in the end, but I can't figure out how to draw more than a circle and get it all to move.我的目标是最终制作一款 2D 游戏,但我想不出如何绘制一个圆圈以上的内容并使其全部移动。

I pasted my code so far.到目前为止,我粘贴了我的代码。 After the circle is drawn I tried going for a line and it doesn't seem to like that.画完圆圈后,我试着画一条线,但它似乎不喜欢那样。 If I take out that forward statement then I can move the circle around again.如果我删除那个forward声明,那么我可以再次移动圆圈。

import turtle

def Left():
  move.left(1)
  move.forward(1)

def Right():
  move.right(1)
  move.forward(1)

def Forwards():
  move.forward(1)

def Backwards():
  move.backward(1)

def moving_object(move):
    move.fillcolor('orange')
    move.begin_fill()
    move.circle(20)
    move.forward(10)
    move.end_fill()

screen = turtle.Screen()
screen.setup(600,600)
screen.bgcolor('green')
screen.tracer(0)

move = turtle.Turtle()
move.color('orange')
move.speed(0)
move.width(2)
move.hideturtle()
move.penup()
move.goto(-250, 0)
move.pendown()

screen.listen()
screen.onkeypress(Left, "Left")
screen.onkeypress(Right, "Right")
screen.onkeypress(Forwards, "Up")
screen.onkeypress(Backwards, "Down")

while True :
    move.clear()
    moving_object(move)
    screen.update()

The whole structure of your turtle code is incorrect.您的海龟代码的整个结构不正确。 There shouldn't be a while True: in an event-driven world like turtle.不应该有while True:在像 turtle 这样的事件驱动的世界中。 Your looping logic made any forward motion inside moving_object() repeat infinitely and push your turtle off the screen.您的循环逻辑使moving_object()内的任何向前运动无限重复并将乌龟推离屏幕。

Let's rebuild your program to exhibit tank-like movement where the circle is the tank and the line is its gun:让我们重建您的程序以展示类似坦克的运动,其中圆圈是坦克,直线是它的枪:

from turtle import Screen, Turtle

DIAMETER = 40

def left():
    turtle.left(15)
    moving_object(turtle)

def right():
    turtle.right(15)
    moving_object(turtle)

def forward():
    turtle.forward(5)
    moving_object(turtle)

def backward():
    turtle.backward(5)
    moving_object(turtle)

def moving_object(move):
    move.clear()

    move.begin_fill()
    move.dot(DIAMETER)  # dot makes position center of circle
    move.end_fill()

    move.forward(DIAMETER/2 + 25)
    move.backward(DIAMETER/2 + 25)  # return so we don't move object

    screen.update()

screen = Screen()
screen.setup(600, 600)
screen.bgcolor('green')
screen.tracer(0)

turtle = Turtle()
turtle.hideturtle()
turtle.color('orange')
turtle.width(5)

screen.onkeypress(left, 'Left')
screen.onkeypress(right, 'Right')
screen.onkeypress(forward, 'Up')
screen.onkeypress(backward, 'Down')
screen.listen()

moving_object(turtle)

screen.update()
screen.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我尝试运行 Turtle.shape("turtle") 时我的 turtle 模块崩溃了? - Why is my turtle module crashing when I try run Turtle.shape("turtle")? 如何让我的乌龟停止移动并结束活动 - How can I get my turtle to stop moving, and end the event 如何阻止我的蟒蛇移动? - How do I stop my python turtle from moving? 为什么按“ Shift”时我的Python乌龟形状尺寸会减小 - Why does my Python turtle shape size decrease when pressing 'shift' 尝试进行API调用时,为什么我的python脚本停止执行? - Why does my python script stop executing when I try to make my API call? 为什么当我尝试导入turtle时没有找到名为pygame的模块? - Why does it say that no module named pygame was found when I try to import turtle? 当移动的Python乌龟靠近另一只乌龟时将其停止 - Stop a moving Python turtle when it's close to another turtle 当我按下一个键时,所有其他先前移动的元素都停止在 python turtle 中移动 - When I press a key, all the other previously moving elements stop moving in python turtle 当我尝试将它添加到数组时,为什么我的 object 被复制了? - Why is my object being duplicated when I try to add it to an array? 当我尝试使用 corsheaders 时,为什么我的应用程序会中断? - Why does my application break when I try to use corsheaders?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM