【问题标题】:make bouncing turtle with python用python制作弹跳乌龟
【发布时间】:2016-10-01 15:17:26
【问题描述】:

我是 python 的初学者,我编写了这段代码来使用 python turtle 制作弹跳球,但它会出现一些错误,比如球消失

import turtle
turtle.shape("circle")
xdir = 1
x = 1
y = 1
ydir = 1
while True:
     x = x + 3 * xdir
     y = y + 3 * ydir
    turtle.goto(x , y)
    if x >= turtle.window_width():
        xdir = -1
    if x <= -turtle.window_width():
        xdir = 1
    if y >= turtle.window_height():
        ydir = -1
    if y <= -turtle.window_height():
        ydir = 1
    turtle.penup()
turtle.mainloop()

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    虽然你解决问题的方法有效(我的返工):

    import turtle
    
    turtle.shape("circle")
    turtle.penup()
    
    x, y = 0, 0
    xdir, ydir = 3, 3
    xlimit, ylimit = turtle.window_width() / 2, turtle.window_height() / 2
    
    while True:
        x = x + xdir
        y = y + ydir
    
        if not -xlimit < x < xlimit:
            xdir = -xdir
        if not -ylimit < y < ylimit:
            ydir = -ydir
    
        turtle.goto(x, y)
    
    turtle.mainloop()
    

    从长远来看,这是错误的做法。在这种情况下,由于无限循环while True,永远不会调用mainloop() 方法,因此没有其他海龟事件处理程序处于活动状态。例如,如果我们想使用exitonclick() 而不是mainloop(),它就行不通。而是考虑:

    import turtle
    
    turtle.shape("circle")
    turtle.penup()
    
    x, y = 0, 0
    xdir, ydir = 3, 3
    xlimit, ylimit = turtle.window_width() / 2, turtle.window_height() / 2
    
    def move():
        global x, y, xdir, ydir
    
        x = x + xdir
        y = y + ydir
    
        if not -xlimit < x < xlimit:
            xdir = -xdir
        if not -ylimit < y < ylimit:
            ydir = -ydir
    
        turtle.goto(x, y)
    
        turtle.ontimer(move, 5)
    
    turtle.ontimer(move, 5)
    
    turtle.exitonclick()
    

    在这里,我们将控制权交还给了主循环,并且动作在事件计时器上。其他海龟事件可以执行,所以exitonclick() 有效。只是在你把自己和你的乌龟编程到一个角落之前要考虑一下。

    【讨论】:

      【解决方案2】:

      您需要window_width()/2window_height()/2 才能保留在窗口内。

      即。

      if x >= turtle.window_width()/2:
          xdir = -1
      if x <= -turtle.window_width()/2:
          xdir = 1
      if y >= turtle.window_height()/2:
          ydir = -1
      if y <= -turtle.window_height()/2:
          ydir = 1
      

      【讨论】:

        【解决方案3】:

        你应该把 海龟.penup() 在while循环之前让你的代码更好更快一点。这几乎是一个错误!

        【讨论】:

          【解决方案4】:

          你可以反弹你的墙,如果你想从上墙反弹,屏幕宽度是800,长度是600

          from turtle import turtle
          turtle=Turtle()
              def move(self)://This will move your ball in diagonal direction
                  x_dir=self.xcor()+self.x
                  y_dir=self.ycor()+self.y
                  self.goto(x_dir,y_dir)
              def bounce(self)://This will bounce back
                  self.y *=-1
          turtle.bounce()
          

          这段代码正在运行,因为我是通过继承来完成的。您需要创建一个类,然后继承所有属性,然后在其中创建两个方法,然后在主类中调用这些函数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多