当前位置: 首页>>代码示例>>Python>>正文


Python Turtle.clear方法代码示例

本文整理汇总了Python中turtle.Turtle.clear方法的典型用法代码示例。如果您正苦于以下问题:Python Turtle.clear方法的具体用法?Python Turtle.clear怎么用?Python Turtle.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在turtle.Turtle的用法示例。


在下文中一共展示了Turtle.clear方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
def main():
    t = Turtle()
    t.speed('fast')
    t.pensize(10)

    draw_loop(t, 20, 10)
    t.clear()
    draw_olympics_logo(t, 50)
    t.clear()
    draw_telerik_logo(t, 200)

    done()
开发者ID:wencakisa,项目名称:Softuni-Python3,代码行数:14,代码来源:drawing_loop.py

示例2: Game

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
class Game(object):

    def __init__(self):
        screen.tracer(False)
        screen.setup(width=MAX_X*SQUARE_WIDTH + DM,
                     height = (MAX_Y+FREEROWS)*SQUARE_WIDTH + DM,
                     startx = -20,
                     starty = 20)
        screen.screensize(MAX_X*SQUARE_WIDTH + DM - 50,
                          (MAX_Y+FREEROWS)*SQUARE_WIDTH + DM -50)
        self.designer = Turtle(visible=False)
        starttime = time.clock()
        self.messagero = Turtle(visible=False)
        self.messagero.penup()
        self.messagero.pencolor("blue")
        self.messagero.goto(0, -(MAX_Y+FREEROWS)*SQUARE_WIDTH/2+6)
        self.message("Please wait a moment!")
        self.designer.pencolor("gray90")
        w2 = SQUARE_WIDTH // 2
        for c in range(MAX_X+1):
            x1, y1 = coords(c,0)
            x2, y2 = coords(c, MAX_Y)
            x1, y1, y2 = x1 - w2, y1 - w2, y2 - w2
            self.line(x1, y1, x1, y2)
        for r in range(MAX_Y+1):
            x1, y1 = coords(0, r)
            x2, y2 = coords(MAX_X, r)
            x1, y1, x2 = x1 - w2, y1 - w2, x2 - w2
            self.line(x1, y1, x2, y1)
        screen.update()
        self.patches = {}
        for r in range(MAX_Y):
            for c in range(MAX_X):
                self.patches[(c, r)] = Patch(c, r)

        self.state = {(41,33):1, (42,33):1, (43,34):1,
                      (42,32):1, (42,34):1}
        for cell in self.state:
            self.patches[cell].showturtle()
        self.newstate = None

        stoptime = time.clock()
        print(stoptime - starttime)
        screen.update()
        screen.onkey(self.run, "space")
        screen.onkey(screen.bye, "Escape")
        screen.onkey(self.clear, "c")
        screen.listen()
        screen.onclick(self.toggle)
        self.message("spacebar:start/pause | left click:toggle cell | c:clear"
                     " | escape:quit")

    def message(self, txt):
        self.messagero.clear()
        self.messagero.write(txt, align="center", font=("Courier", 14, "bold"))
        

    def line(self, x1, y1, x2, y2):
        self.designer.penup()
        self.designer.goto(x1, y1)
        self.designer.pendown()
        self.designer.goto(x2, y2)

    def update_display(self):
        screen.tracer(False)
        for cell in self.state:
            if cell not in self.newstate:
                try:
                    self.patches[cell].hideturtle()
                except KeyError:
                    pass
        for cell in self.newstate:
            try:
                self.patches[cell].showturtle()
                self.patches[cell].color(calccolor(self.newstate[cell]))
            except KeyError:
                pass
        screen.tracer(True)

    def clear(self):
        self.newstate = {}
        self.update_display()
        self.state = {}

    def toggle(self, x, y):
        cell = cellindices(int(x), int(y))
        self.newstate = self.state.copy()
        if cell in self.newstate:
            del self.newstate[cell]
        else:
            self.newstate[cell] = 1
        self.update_display()
        self.state = self.newstate
         

    def run(self):
        starttime = time.clock()
        anzahl_generationen = 0
        screen.onkey(self.stop, "space")
        generation = generate(self.state)
#.........这里部分代码省略.........
开发者ID:1c71,项目名称:Program-Practice,代码行数:103,代码来源:tdemo_game_of_life_coloured.py

示例3: Game

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
class Game(object):

    NBADDR = ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1))
    
    def __init__(self):
        screen.tracer(False)
        screen.setup(width=MAX_X*SQUARE_WIDTH + DM,
                     height = (MAX_Y+FREEROWS)*SQUARE_WIDTH + DM,
                     startx = -20,
                     starty = 20)
        screen.screensize(MAX_X*SQUARE_WIDTH + DM - 50,
                          (MAX_Y+FREEROWS)*SQUARE_WIDTH + DM -50)
        self.designer = Turtle(visible=False)
        starttime = time.clock()
        self.messagero = Turtle(visible=False)
        self.messagero.penup()
        self.messagero.pencolor("blue")
        self.messagero.goto(0, -(MAX_Y+FREEROWS)*SQUARE_WIDTH/2+6)
        self.message("Please wait a moment!")
        self.designer.pencolor("gray90")
        for c in range(MAX_X+1):
            self.line((-MAX_X/2. -.5 + c)*SQUARE_WIDTH + MARG_X, 
                      ((-MAX_Y + FREEROWS)/2. -.5 + 0)*SQUARE_WIDTH + MARG_Y,
                      (-MAX_X/2. -.5 + c)*SQUARE_WIDTH + MARG_X,
                      ((-MAX_Y + FREEROWS)/2. -.5 + MAX_Y)*SQUARE_WIDTH + MARG_Y)
        for r in range(MAX_Y+1):
            self.line((-MAX_X/2. -.5 + 0)*SQUARE_WIDTH + MARG_X, 
                      ((-MAX_Y + FREEROWS)/2. -.5 + r)*SQUARE_WIDTH + MARG_Y,
                      (-MAX_X/2. -.5 + MAX_X)*SQUARE_WIDTH + MARG_X,
                      ((-MAX_Y + FREEROWS)/2. -.5 + r)*SQUARE_WIDTH + MARG_Y)
        screen.update()
        self.patches = {}
        for r in range(MAX_Y):
            for c in range(MAX_X):
                self.patches[(c, r)] = Patch(c, r)

        self.state = set([(41,33), (42,33), (43,34), (42,32), (42,34)])
        for cell in self.state:
            self.patches[cell].showturtle()
        self.newstate = None

        stoptime = time.clock()
        print(stoptime - starttime)
        screen.update()
        screen.onkey(self.run, "space")
        screen.onkey(screen.bye, "Escape")
        screen.onkey(self.clear, "c")
        screen.listen()
        screen.onclick(self.toggle)
        self.message("spacebar:start/pause | left click:toggle cell | c:clear"
                     " | escape:quit")

    def message(self, txt):
        self.messagero.clear()
        self.messagero.write(txt, align="center", font=("Courier", 14, "bold"))
        

    def line(self, x1, y1, x2, y2):
        self.designer.penup()
        self.designer.goto(x1, y1)
        self.designer.pendown()
        self.designer.goto(x2, y2)

    def calcnext(self):
        cd = {}
        for (x,y) in self.state:
            for dx, dy in Game.NBADDR:
                xx, yy = x+dx, y+dy
                cd[(xx,yy)] = cd.get((xx,yy), 0) + 1
            cd[(x,y)] = cd.get((x,y), 0) + 10
        td = []
        for c in cd:
            if cd[c] not in [3, 12, 13]:
                td.append(c)
        for c in td: del cd[c]
        return set(cd.keys())

    def update_display(self):
        screen.tracer(False)
        for cell in self.newstate - self.state:
            try:
                self.patches[cell].showturtle()
            except:
                pass
        for cell in self.state - self.newstate:
            try:
                self.patches[cell].hideturtle()
            except:
                pass
        screen.tracer(True)

    def clear(self):
        self.newstate = set()
        self.update_display()
        self.state = set()

    def toggle(self, x, y):
        cell = cellindices(x, y)
        self.newstate = self.state.copy()
        if cell in self.newstate:
#.........这里部分代码省略.........
开发者ID:DestinyHe,项目名称:python-turtle-demo,代码行数:103,代码来源:tdemo_game_of_life.py

示例4: TetrisBoard

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
class TetrisBoard(object):
    def __init__(self, cols, rows):
        self.cols, self.rows = cols, rows
        self.screen = Screen()
        self.screen.screensize(BLOCKWIDTH*cols-50, BLOCKWIDTH*rows-50)
        self.screen.setup(BLOCKWIDTH*cols+12, BLOCKWIDTH*rows+12)
        self.screen.title("Turtle Tetris")
        self.screen.bgcolor("black")
        self.writer = Turtle()
        self.writer.ht()        
        self.label = None
        self.grid = {}
        self.screen.tracer(False)
        for row in range(rows):
            for col in range(cols):
                self.grid[(col, row)] = TetrisTurtle(col, row)
        self.screen.tracer(True)
        self.brick = TetrisBrick(self)
        self.result = 0
        self.LEVEL = 0.6
        self.keybuffer = KeyBuffer(self.screen,
                            ["Right", "Left", "Up", "Down", "space", "Escape"])
        self.reset()
        self.screen.listen()
        self.t1 = time()
        
    def reset(self):
        self.result = 0
        self.LEVEL = 0.600
        self.screen.tracer(False)
        self.writer.clear()
        if self.label:
            self.writer.clearstamp(self.label)
        for x in range(COLUMNS):
            for y in range(ROWS):
                self.grid[(x,y)].fillcolor("")
        self.screen.tracer(True)
        self.state = "NEWBRICK"
        
    def blink(self, y, n=1):
        for _ in range(n):
            for color in ("white", "black"):
                self.screen.tracer(False)
                for x in range(COLUMNS):
                    self.grid[(x,y)].pencolor(color)
                    sleep(self.LEVEL/10.0)
                self.screen.tracer(True)
            
    def display_result(self):
        tb = self
        tb.writer.color("white", "gray20")
        tb.writer.shape("square")
        tb.writer.shapesize(5, 15)
        tb.writer.goto(-4 ,0)
        self.label = tb.writer.stamp()
        tb.writer.goto(-2,3)
        tb.writer.write(str(tb.result) + " rows!",
                        align="center", font = ("Courier", 24, "bold") )
        tb.writer.goto(-2,-22)
        tb.writer.write("New game : <spacebar>",
                        align="center", font = ("Courier", 16, "bold") )
        tb.writer.goto(-2,-42)
        tb.writer.write("Quit : <escape>",
                        align="center", font = ("Courier", 16, "bold") )
        
    def getcolor(self, col, row):
        return self.grid[(col, row)].fillcolor()
    def setcolor(self, col, row, color):
        return self.grid[(col, row)].fillcolor(color)
    def rowfree(self, row):
        return not any([self.getcolor(col, row) for col in range(COLUMNS)])
    def rowfull(self, row):
        return all([self.getcolor(col, row) for col in range(COLUMNS)])

    def cleanup(self, shp):
        try:
            ymax = max([y for (x,y) in shp])
        except ValueError:
            self.state = "FINIS"
            return
        currenty = ymax
        while currenty > 0:
            if self.rowfull(currenty):
                self.blink(currenty, 2)
                self.result += 1
                if self.result == 8:
                    self.LEVEL = 0.4
                elif self.result == 20:
                    self.LEVEL = 0.25
                y = currenty
                while True:
                    self.screen.tracer(False)
                    for c in range(COLUMNS):
                        self.setcolor(c, y, self.getcolor(c, y-1))
                    self.screen.tracer(True)
                    if self.rowfree(y):
                        break
                    else:
                        y -= 1
            else:
#.........这里部分代码省略.........
开发者ID:DestinyHe,项目名称:python-turtle-demo,代码行数:103,代码来源:pytetris.py

示例5: Drawing

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
class Drawing():

    def __init__(self):
        """
        This drawing class is for practicing drawing simple or even geometrical 
        pictures with turtle module.
        Let's apply this method with easy examples.
        gb = turtle.Turtle() ; gb.rigth, left, forward, backward, circle, shape, 
        shapesize, penup, pendown, home, clear
        You can get a lot of detail information by googling 'python turtle'.
        """
        self.gb = Turtle()
        self.gb.shape('turtle')
        self.gb.speed(6)
        #self.screen = self.gb.getscreen()
        #w = 150
        #self.screen.setworldcoordinates(-w,-w,w,w)
        print 'A cute turtle is ready to draw!'
    def swirl(self,shape='turtle',speed=0):
        self.gb.shape(shape)
        self.gb.speed(speed)
        self.gb.color('black')
        for i in range(500):
            self.gb.forward(i)
            self.gb.right(98)

    def color_swirl(self,shape='turtle',speed=0):
        self.gb.shape(shape)
        self.gb.speed(speed)
        for i in range(500):
            color = colorsys.hsv_to_rgb(i/1000.0,1.0,1.0)
            self.gb.color(color)
            self.gb.forward(i)
            self.gb.right(98)

    def zigzag(self,shape='turtle',speed=0):
        self.gb.shape(shape)
        self.gb.speed(speed)
        for i in range(180):
            self.gb.forward(100)
            self.gb.right(30)
            self.gb.forward(20)
            self.gb.left(60)
            self.gb.forward(50)
            self.gb.right(30)
            self.gb.penup()
            self.gb.setposition(0,0)
            self.gb.pendown()
            self.gb.right(2)
            
    def square(self,shape='turtle',speed=0):
        self.gb.shape(shape)
        self.gb.speed(speed)
        for i in range(400):
            self.gb.forward(i)
            self.gb.left(90.5)

    def gohome(self):
        wiggle = [30,30,30,30]
        self.gb.shape('turtle')
        self.gb.speed(6)
        self.gb.clear()
        self.gb.penup()
        for tick in wiggle:
            self.gb.right(tick)
            self.gb.left(tick)
        self.gb.home()
        self.gb.clear()
        self.gb.pendown()
        self.gb.color('black')
开发者ID:EMCSlabs,项目名称:Programs,代码行数:72,代码来源:funfun.py

示例6: draw_bullseye

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
	turtle.goto(x, y-radius)
	turtle.pendown()
	turtle.fill(True)
	turtle.circle(radius)
	turtle.fill(False)

def draw_bullseye(turtle, x, y, wn):
	color = True
	for radius in range(circle_radius, circle_inner_radius, -40):
		if color:
			turtle.fillcolor("red")
		else:
			turtle.fillcolor("white")
		origin_circle(turtle,radius,x,y,wn)
		color = not color

while True:
	t1.speed(0)
	wn = t1.getscreen()
	t1.ht()
	width = wn.window_width()
	height = wn.window_height()
	wn.screensize(width, height, None)
	x = randint( -(width/2) + circle_radius, (width/2) - circle_radius)
	y = randint( -(height/2) + circle_radius, (height/2) - circle_radius)
	print x, y
	print width, height
	draw_bullseye(t1, x, y, wn)
	time.sleep(3)
	t1.clear()
开发者ID:camlesom,项目名称:Target-Practice,代码行数:32,代码来源:target.py

示例7: random_color

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
from turtle import Turtle
from random import random

def random_color():
    return(random(),random(),random())

MorganFreeman = Turtle()
MorganFreeman.pensize(15)
MorganFreeman.speed(0)

while True:
    for counter in range(225):
        MorganFreeman.pencolor(random_color())
        MorganFreeman.forward(counter)
        MorganFreeman.right(198)
        MorganFreeman.forward(counter)
        MorganFreeman.right(72)
        #The previous "turn right" command can be replaced with 70
        #for a different "rotating-star" effect.
    MorganFreeman.penup()
    MorganFreeman.home()
    MorganFreeman.pendown()
    MorganFreeman.clear()


        
开发者ID:donovank,项目名称:python-2,代码行数:25,代码来源:PythonArt.py

示例8:

# 需要导入模块: from turtle import Turtle [as 别名]
# 或者: from turtle.Turtle import clear [as 别名]
cursor.speed(2)
 
cursor.penup()
cursor.setposition(-70,0)
cursor.color('red')
cursor.pendown()
cursor.left(90)
cursor.forward(100)
cursor.right(150)
cursor.forward(115)
cursor.left(120)
cursor.forward(115)
cursor.right(150)
cursor.forward(100)
time.sleep(0.5)
cursor.clear()
 
cursor.penup()
cursor.setposition(-70,0)
cursor.right(180)
cursor.color('blue')
cursor.pendown()
cursor.right(30)
cursor.forward(115)
cursor.right(120)
cursor.forward(115)
cursor.right(180)
cursor.forward(57.5)
cursor.left(60)
cursor.forward(60)
time.sleep(0.5)
开发者ID:mani1996,项目名称:Remember-My-Name,代码行数:33,代码来源:script.py


注:本文中的turtle.Turtle.clear方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。