简体   繁体   中英

make bouncing turtle with python

I am a beginner with python and I wrote this code to make bouncing ball with python turtle it works but have some errors like the ball disappearing

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()

Although your approach to the problem works (my rework):

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()

In the long run, it's the wrong approach to take. In this case, due to the infinite loop while True , the mainloop() method is never called so no other turtle event handlers are active. For example, if we wanted to use exitonclick() instead of mainloop() , it wouldn't work. Instead consider:

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()

Here we've turned control back over to the mainloop and the motion is on an event timer. Other turtle events can execute so exitonclick() works. Just something to think about going forward before you program yourself, and your turtle, into a corner.

You need window_width()/2 and window_height()/2 to keep inside window.

ie.

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

You should put turtle.penup() Before the while loop to make your code better and a little bit faster. It is almost a bug!

You can bounce your wall, if you want to bounce it from upper wall, the screen width is 800 and length is 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()

This code is running because I did it with inheritance. You need to create a class then inherit all the properties and then create two methods there and then call those functions in the main class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM