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()
原创文章/代码,转载请注明出处。
本文介绍了一个使用Python的turtle模块实现的简单弹球游戏。通过定义Ball类继承自Turtle并设置球的速度和方向,实现了球在窗口内壁上的反弹效果。此示例适合初学者了解turtle绘图库的基本用法。
706

被折叠的 条评论
为什么被折叠?



