from turtle import *
from random import randint
s = Screen()
s.title("turtle模块制作的弹球_作者:李兴球")
s.setup(800,600)
s.delay(0)
class Ball(Turtle):
def __init__(self,x,y):
Turtle.__init__(self)
self.shape('circle')
self.visible = False
self.penup()
self.speed(0)
self.xspeed=randint(-20,20)
self.yspeed=randint(-20,20)
self.setheading(90)
self.showturtle()
self.move()
def move(self):
x=(self.xcor() + self.xspeed)
y=(self.ycor() + self.yspeed)
self.goto(x,y)
if abs(self.xcor())>=380:self.xspeed = - self.xspeed
if abs(self.ycor())>=280:self.yspeed = - self.yspeed
s.ontimer(self.move,1)
[Ball(0,0),Ball(0,0),Ball(0,0),Ball(0,0)]
s.mainloop()
原创文章/代码,转载请注明出处。