python海龟画图模块画布弹球程序

"""python海龟画图模块画布弹球程序"""

from turtle import *
from random import randint
from time import sleep

width,height=480,360

screen = Screen()
screen.bgcolor("navy")
screen.title("python海龟画图模块画布弹球程序by lixingqiu")
screen.setup(width,height)
canvas = screen.cv                                    # 获取画布
canvas.create_line(0, 0, 90, 90, fill="red", width=3) # 这条线看出坐标系
ball = canvas.create_oval(0,0,50,50,fill='cyan')      # 创建圆形,返回的是编号

xspeed = randint(-5,5)                                # 设定x速度
yspeed = randint(-5,5)                                # 设定y速度

while True:     
    canvas.move(ball,xspeed,yspeed)                   # 移动小球
    p = canvas.coords(ball)                           # 获取外接矩形坐标
    if p[0]<-width/2 or p[2] >width/2 :xspeed = -xspeed  # 水平反弹
    if p[1]<-height/2 or p[3]>height/2:yspeed = -yspeed  # 垂直反弹
    canvas.update()
    sleep(0.01)

python海龟画图模块画布弹球程序lixingqiu