# 继承Turtle类的随机移动的地球

## 如本文章标有价格,需议价或其它事情商议请加微信pythonxia

"""
继承自Turtle类的碰到边缘有会反弹随机移动的球.
"""
from turtle import *
from random import randint
from time import sleep

class Ranger(Turtle):
def __init__(self,image,x,y):
Turtle.__init__(self,shape = image,visible=False)
self.penup()
self.screen_width = self.screen.window_width()
self.screen_height = self.screen.window_height()
self.xspeed = randint(-10,10)
self.yspeed = randint(-10,10)
self.timecounter = 0              # 延迟计数器
self.timeamount = 3               #  总共延迟时间
self.goto(x,y)
self.onclick(self.hide)
self.st()
self.move()

def move(self):
if randint(1,30) == 1 : self.set_random_heading()
self.setx( self.xcor() + self.xspeed)
self.sety( self.ycor() + self.yspeed)
x = self.xcor()
y =  self.ycor()
if abs(x) > self.screen_width//2 - 50:
self.xspeed = - self.xspeed

if abs(y) > self.screen_height//2 - 50:
self.yspeed = - self.yspeed
self.screen.ontimer(self.move,10)

def hide(self,x,y):
"""隐藏自己"""

"""随机设定水平速度和垂直速度"""
self.xspeed = randint(-10,10)
self.yspeed = randint(-10,10)

if __name__ == "__main__":

screen = Screen()
screen.bgpic('太空.png')
screen.title(' 继承自Turtle类的碰到边缘有会反弹随机移动的球')
screen.delay(0)
image = "星球\\0.gif"