"""python simplest turtle graphics example"""

import turtle

turtle.fd(100)
"""python simple turtle graphics example"""

import turtle

for i in range(4):
    turtle.fd(100)
    turtle.rt(90)


"""python simple turtle graphics example with screen object"""

import turtle

width,height = 480,360

screen = turtle.getscreen()
screen.setup(width,height)
screen.title("Python海龟画正方形程序")

for i in range(4):
    turtle.fd(100)
    turtle.rt(90)

screen.mainloop()


"""python simple turtle graphics example with screen object and new instance of turtle named chinese_loong。
   this program will draw five angle star,这个程序会画一个五角星。
   Python简单的海龟画图示例程序。本程序会新建屏幕对象,并且实例化一个海龟名叫中国龙。

"""

import turtle

width,height = 480,360

screen = turtle.Screen()
screen.setup(width,height)
screen.title("Python海龟画五角星程序")

chinese_loong = turtle.Turtle(shape='turtle')
chinese_loong.color("brown")
chinese_loong.width(4)

for i in range(5):
    chinese_loong.fd(100)
    chinese_loong.rt(144)

screen.exitonclick()


"""python simple turtle graphics example with screen object and new instance of turtle named chinese_loong。
   
   you can move turtle with arrow keys。
   
   Python简单的海龟画图示例程序。本程序会新建屏幕对象,并且实例化一个海龟名叫中国龙。你可以通过按上下左右方向箭头操作海龟移动。

"""

import turtle

def move_left():
    chinese_loong.setheading(180)
    chinese_loong.fd(10)
    
def move_right():
    chinese_loong.setheading(00)
    chinese_loong.fd(10)
    
def move_up():
    chinese_loong.setheading(90)
    chinese_loong.fd(10)
    
def move_down():
    chinese_loong.setheading(-90)
    chinese_loong.fd(10)

width,height = 480,360

screen = turtle.Screen()
screen.bgcolor("black")
screen.delay(0)
screen.setup(width,height)
screen.title("Python海龟画图移动海龟,move turtle with arrow by lixingqiu")

chinese_loong = turtle.Turtle(shape='turtle')
chinese_loong.color("brown")
chinese_loong.width(4)

screen.onkey(move_left,"Left")   # when pressed left arror ,call move_left function
screen.onkey(move_right,"Right")
screen.onkey(move_up,"Up")
screen.onkey(move_down,"Down")

screen.listen()                  # 监听键盘
screen.exitonclick()             # 单击鼠标关闭窗口


"""python simple turtle graphics animation with screen object and new instance of turtle named ball。
   
   ball 's shape is circle,it will move and bounce on edge。
   
   Python简单的海龟画图示例程序。本程序会新建屏幕对象,并且实例化一个海龟名叫ball。
   这个球会移动,并且碰到屏幕边缘会反弹。
   

"""
import random
import turtle
 
width,height = 480,360

screen = turtle.Screen()
screen.bgcolor("black")
screen.delay(10)
screen.setup(width,height)
screen.title("Python海龟画图弹球动画,a bounce ball animation by lixingqiu ")

ball = turtle.Turtle(shape='circle')
ball.penup()
ball.color("cyan")
ball.dx = random.randint(-5,5)   # Custom Property  Horizontal direction of unit displacement
ball.dy = random.randint(-5,5)   # 自定义属性,垂直方向的单位位移量。

while True:
    x = ball.xcor() + ball.dx
    y = ball.ycor() + ball.dy
    ball.goto(x,y)
    if abs(x) > width//2 : ball.dx = -ball.dx
    if abs(y) > height//2 :ball.dy = -ball.dy
 


"""python simple turtle graphics animation with screen object and new instance of turtle named ball。
   
   ball 's shape is circle,it will move and bounce on edge, but use screen ontimer function。
   
   Python简单的海龟画图示例程序。本程序会新建屏幕对象,并且实例化一个海龟名叫ball。
   这个球会移动,并且碰到屏幕边缘会反弹。本程序使用了屏幕的ontimer定时器功能以实现“循环”。
   

"""
import random
import turtle
 
width,height = 480,360

screen = turtle.Screen()
screen.bgcolor("black")
screen.delay(0)
screen.setup(width,height)
screen.title("Python海龟画图弹球动画用screen的ontimer实现 ,a bounce ball animation by lixingqiu ")

ball = turtle.Turtle(shape='circle')
ball.penup()
ball.color("cyan")
ball.dx = random.randint(-5,5)   # Custom Property  Horizontal direction of unit displacement
ball.dy = random.randint(-5,5)   # 自定义属性,垂直方向的单位位移量。

def move():    
    x = ball.xcor() + ball.dx
    y = ball.ycor() + ball.dy
    ball.goto(x,y)
    if abs(x) > width//2 : ball.dx = -ball.dx
    if abs(y) > height//2 :ball.dy = -ball.dy
    screen.ontimer(move,1)        # after on millionsecond,it will call move function.

move()
screen.exitonclick()
 


"""This program will define a Ball class,and it will make a lot of balls.

   本程序会定义一个Ball类,用它会生成很多球。
   
"""
import random
import turtle

class Ball(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self,visible=False,shape='circle')
        self.penup()
        self.dx =  random.randint(-5,5)       # Custom Property  Horizontal direction of unit displacement
        self.dy = random.randint(-5,5)        # 自定义属性,垂直方向的单位位移量。
        self.sw = self.screen.window_width()  # screen's width
        self.sh = self.screen.window_height() # screen's height
        self.color("cyan")
        self.st()
        self.move()

    def move(self):
        """移动弹球,move self"""            
        x = self.xcor() + self.dx
        y = self.ycor() + self.dy
        self.goto(x,y)
        if abs(x) > self.sw//2 : self.dx = -self.dx
        if abs(y) > self.sh//2 :self.dy = -self.dy
        self.screen.ontimer(self.move,1)    # after on millionsecond,it will call move function.
            
def main():
    
    width,height = 480,360

    screen = turtle.Screen()
    screen.bgcolor("black")
    screen.delay(0)
    screen.setup(width,height)
    screen.title("Python的turtle之Ball弹球类 ,a bounce Ball class by lixingqiu ")

    [Ball() for i in range(10)]           # 生成10个弹球

    screen.exitonclick()

if __name__ == "__main__":

    main()
     


"""This program will define a Ball class,and it will make a lot of balls.

   本程序会定义一个Ball类,用它会生成很多球。
   
"""
import time
import random
import turtle

class Ball(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self,visible=False,shape='circle')
        self.penup()
        self.radius = 10                      # use get_shapepoly() will know its radius
        self.shapesize(2,2)
        self.radius = 10 * 2
        self.dx =  random.randint(-5,5 )       # Custom Property  Horizontal direction of unit displacement
        self.dy = random.randint(-5,5)        # 自定义属性,垂直方向的单位位移量。
        self.sw = self.screen.window_width()  # screen's width
        self.sh = self.screen.window_height() # screen's height
        self.color("cyan")
        self.st()         

    def move(self):
        """移动弹球,move self"""            
        x = self.xcor() + self.dx
        y = self.ycor() + self.dy
        self.goto(x,y)
        self.bounce_on_edge(x,y)
        
    def bounce_on_edge(self,x,y):
        
        if x + self.radius > self.sw//2 or x - self.radius < -self.sw//2 : self.dx = -self.dx
        if y + self.radius > self.sh//2 or y - self.radius < -self.sh//2 : self.dy = -self.dy
            
def main():
    
    width,height = 480,360

    screen = turtle.Screen()
    screen.bgcolor("black")
    screen.delay(0)
    screen.setup(width,height)
    screen.title("Python的turtle之Ball弹球类with while game cycle ,a bounce Ball class by lixingqiu ")

    balls =  [Ball() for i in range(3)]           # 生成10个弹球
    
    while True:
        for ball in balls:
            ball.move()
            time.sleep(0.01)
  

if __name__ == "__main__":

    main()
     


"""This program will define a Ball class,and it will make a lot of balls.
   also will define a Rectangle class ,and operated by mouse .

   本程序会定义一个Ball类,用它会生成很多球。程序还会定义一个矩形类,用鼠标来控制它。
   
"""
import time
import random
import turtle

class Rectangle(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self,visible=False,shape='square')
        self.penup()
        self.shapesize(1,10)
        self._width = 200               #  use get_shapepoly() will know its width and height
        self._height = 20
        self.color("white")
        self.goto(0,-150)
        self.st()
        self.screen.cv.bind("<Motion>",self.move)
        
    def move(self,event):
        """conver coordinates,转换鼠标的坐标到海龟画图里的坐标"""
        x, y = (self.screen.cv.canvasx(event.x)/self.screen.xscale,
                        -self.screen.cv.canvasy(event.y)/self.screen.yscale)
        self.setx(x)
        
    
class Ball(turtle.Turtle):
    def __init__(self,board):
        turtle.Turtle.__init__(self,visible=False,shape='circle')
        self.penup()
        self.radius = 10                      # use get_shapepoly() will know its radius
        self.shapesize(2,2)
        self.radius = 10 * 2
        self.dx =  random.randint(-5,5 )       # Custom Property  Horizontal direction of unit displacement
        self.dy = random.randint(-5,5)        # 自定义属性,垂直方向的单位位移量。
        self.sw = self.screen.window_width()  # screen's width
        self.sh = self.screen.window_height() # screen's height
        self.color("cyan")
        self.board = board
        self.st()         

    def move(self):
        """移动弹球,move self"""            
        x = self.xcor() + self.dx
        y = self.ycor() + self.dy
        self.goto(x,y)
        self.bounce_on_edge(x,y)
        self.bounce_on_rect(x,y)
        
    def bounce_on_edge(self,x,y):
        
        if x + self.radius > self.sw//2 or x - self.radius < -self.sw//2 : self.dx = -self.dx
        if y + self.radius > self.sh//2 or y - self.radius < -self.sh//2 : self.dy = -self.dy  
        
    def bounce_on_rect(self,x,y):
        """碰到矩形就反弹"""
        if x > self.board.xcor() - self.board._width//2 and x < self.board.xcor() + self.board._width//2 :
            if y - self.radius <=  self.board.ycor() + self.board._height//2 :  self.dy = -self.dy
      
            
def main():
    
    width,height = 480,460

    screen = turtle.Screen()
    screen.bgcolor("black")
    screen.delay(0)
    screen.setup(width,height)
    screen.title("Python的turtle之Ball弹球类with while game cycle ,a bounce Ball class by lixingqiu ")

    board =   Rectangle()
    balls =  [Ball(board) for i in range(3)]           # 生成3个弹球    
    
    while True:
        for ball in balls:
            ball.move()
            time.sleep(0.01)
  

if __name__ == "__main__":

    main()
     


 

学本领,探索更大的世界!

李兴球博客 风火轮编程主页
error: Content is protected !!