Esempio n. 1
16
class Material(object):
    def __init__(self):
        self.gods_hand = Turtle()
        self.gods_hand.hideturtle()
        self.gods_hand.speed(0)
        
    def draw_line(self):
        self.gods_hand.penup()
        self.gods_hand.tracer(0, 0)
        self.gods_hand.goto(100, 100)
        self.gods_hand.pendown()
        self.gods_hand.fill(True)
        self.gods_hand.setheading(0)
        self.gods_hand.forward(50)
        self.gods_hand.setheading(90)
        self.gods_hand.forward(50)
        self.gods_hand.setheading(180)
        self.gods_hand.forward(50)
        self.gods_hand.setheading(270)
        self.gods_hand.forward(50)
        self.gods_hand.fill(False)
        self.gods_hand.penup()
        self.gods_hand.goto(120, 120)
        self.gods_hand.pendown()
        self.gods_hand.pencolor("Blue")
        self.gods_hand.fillcolor("Blue")
        self.gods_hand.fill(True)
        self.gods_hand.setheading(0)
        self.gods_hand.forward(10)
        self.gods_hand.setheading(90)
        self.gods_hand.forward(10)
        self.gods_hand.setheading(180)
        self.gods_hand.forward(10)
        self.gods_hand.setheading(270)
        self.gods_hand.forward(10)
        
        self.gods_hand.fill(False)
        self.gods_hand.penup()
        self.gods_hand.tracer(1, 1)
        
    def build_box(self, pos_x, pos_y, length_x, length_y, do_fill, color):
        pos_x, pos_y = coordinate_converter((pos_x, pos_y))
        
        self.gods_hand.tracer(0, 0)
        
        self.gods_hand.penup()
        self.gods_hand.goto(pos_x, pos_y)
        self.gods_hand.pendown()
        
        if do_fill:
            self.gods_hand.fill(True)
            
        self.gods_hand.color(color)
            
        for x in range(4, 0, -1):
            heading = x * 90
            self.gods_hand.setheading(heading)    
            self.gods_hand.forward(length_x)
        
        if do_fill:
            self.gods_hand.fill(False)
        
        self.gods_hand.tracer(1, 1)
                 
    def draw_human(self, pos_x, pos_y):
        self.gods_hand.tracer(0, 0)
        self.gods_hand.penup()
        self.gods_hand.goto(pos_x, pos_y)
        self.gods_hand.color("blue")
        self.gods_hand.shape("circle")
        self.gods_hand.shapesize(0, 0, 5)
        stampid = self.gods_hand.stamp()
        self.gods_hand.color("black")
        self.gods_hand.tracer(1, 1)
        return stampid
        
    def erase_human(self, stampid):
        self.gods_hand.clearstamp(stampid)
Esempio n. 2
0
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:
                currenty -= 1
        tetris.state = "NEWBRICK"

    def run(self):
        tb = self
        b = self.brick
        ### actions to be done unconditionally
        if tb.state == "NEWBRICK":
            if b.reset():
                self.t1 = time()
                tb.state = "FALL"
            else:
                tb.state = "FINIS"
        t2 = time()
        if tb.state == "FALL" and t2 - self.t1 > self.LEVEL:
            b.down()
            b.apply("Step")
            self.t1 = t2
        ### actions bound to key events
        key = self.keybuffer.getkey()
        if key:
            if tb.state == "FALL":
                if key == "Left":
                    b.shiftleft()
                elif key == "Right":
                    b.shiftright()
                elif key == "Down":
                    b.drop()
                    tb.state = "CLEANUP"
                elif key == "Up":
                    b.turn()
                elif key == "space":
                    tb.state = "BREAK"
                b.apply(key)
            elif tb.state == "BREAK":
                if key == "space":
                    tb.state = "FALL"
            elif tb.state == "ADE":
                if key == "space":
                    tb.reset()
                    tb.state = "NEWBRICK"
                elif key == "Escape":
                    tb.screen.bye()
                
        if tb.state == "CLEANUP":
            tb.cleanup(b.shape1)
        if tb.state == "FINIS":
            tb.display_result()
            tb.state = "ADE"
        self.screen.ontimer(self.run, 100)    
Esempio n. 3
0
class Material(object):
    def __init__(self):
        self.gods_hand = Turtle()
        self.gods_hand.hideturtle()
        self.gods_hand.speed(0)

    def draw_line(self):
        self.gods_hand.penup()
        self.gods_hand.tracer(0, 0)
        self.gods_hand.goto(100, 100)
        self.gods_hand.pendown()
        self.gods_hand.fill(True)
        self.gods_hand.setheading(0)
        self.gods_hand.forward(50)
        self.gods_hand.setheading(90)
        self.gods_hand.forward(50)
        self.gods_hand.setheading(180)
        self.gods_hand.forward(50)
        self.gods_hand.setheading(270)
        self.gods_hand.forward(50)
        self.gods_hand.fill(False)
        self.gods_hand.penup()
        self.gods_hand.goto(120, 120)
        self.gods_hand.pendown()
        self.gods_hand.pencolor("Blue")
        self.gods_hand.fillcolor("Blue")
        self.gods_hand.fill(True)
        self.gods_hand.setheading(0)
        self.gods_hand.forward(10)
        self.gods_hand.setheading(90)
        self.gods_hand.forward(10)
        self.gods_hand.setheading(180)
        self.gods_hand.forward(10)
        self.gods_hand.setheading(270)
        self.gods_hand.forward(10)

        self.gods_hand.fill(False)
        self.gods_hand.penup()
        self.gods_hand.tracer(1, 1)

    def build_box(self, pos_x, pos_y, length_x, length_y, do_fill, color):
        pos_x, pos_y = coordinate_converter((pos_x, pos_y))

        self.gods_hand.tracer(0, 0)

        self.gods_hand.penup()
        self.gods_hand.goto(pos_x, pos_y)
        self.gods_hand.pendown()

        if do_fill:
            self.gods_hand.fill(True)

        self.gods_hand.color(color)

        for x in range(4, 0, -1):
            heading = x * 90
            self.gods_hand.setheading(heading)
            self.gods_hand.forward(length_x)

        if do_fill:
            self.gods_hand.fill(False)

        self.gods_hand.tracer(1, 1)

    def draw_human(self, pos_x, pos_y):
        self.gods_hand.tracer(0, 0)
        self.gods_hand.penup()
        self.gods_hand.goto(pos_x, pos_y)
        self.gods_hand.color("blue")
        self.gods_hand.shape("circle")
        self.gods_hand.shapesize(0, 0, 5)
        stampid = self.gods_hand.stamp()
        self.gods_hand.color("black")
        self.gods_hand.tracer(1, 1)
        return stampid

    def erase_human(self, stampid):
        self.gods_hand.clearstamp(stampid)
Esempio n. 4
0
        stamps.append(stamp)
        t.forward(20)
    return stamps


t.home()
t.sety(-30)
stamp_range(t)

t.home()
t.sety(-60)
stamps = stamp_range(t)

for index, stamp in enumerate(stamps):
    if ((index % 3) == 0):
        t.clearstamp(stamp)

t.home()
t.sety(-90)
stamps = stamp_range(t)
t.clearstamps(len(stamps) // 3)
t.clearstamps(-len(stamps) // 3)

t.home()
t.sety(-120)
stamps = stamp_range(t)
t.undo()

t.home()
t.sety(-150)
undosize_before = t.undobufferentries()
Esempio n. 5
0
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:
                currenty -= 1
        tetris.state = "NEWBRICK"

    def run(self):
        tb = self
        b = self.brick
        ### actions to be done unconditionally
        if tb.state == "NEWBRICK":
            if b.reset():
                self.t1 = time()
                tb.state = "FALL"
            else:
                tb.state = "FINIS"
        t2 = time()
        if tb.state == "FALL" and t2 - self.t1 > self.LEVEL:
            b.down()
            b.apply("Step")
            self.t1 = t2
        ### actions bound to key events
        key = self.keybuffer.getkey()
        if key:
            if tb.state == "FALL":
                if key == "Left":
                    b.shiftleft()
                elif key == "Right":
                    b.shiftright()
                elif key == "Down":
                    b.drop()
                    tb.state = "CLEANUP"
                elif key == "Up":
                    b.turn()
                elif key == "space":
                    tb.state = "BREAK"
                b.apply(key)
            elif tb.state == "BREAK":
                if key == "space":
                    tb.state = "FALL"
            elif tb.state == "ADE":
                if key == "space":
                    tb.reset()
                    tb.state = "NEWBRICK"
                elif key == "Escape":
                    tb.screen.bye()

        if tb.state == "CLEANUP":
            tb.cleanup(b.shape1)
        if tb.state == "FINIS":
            tb.display_result()
            tb.state = "ADE"
        self.screen.ontimer(self.run, 100)
Esempio n. 6
0
def paint_canvas():
    # create a new turtle robot
    artist = Turtle(visible=False)

    painted = 0
    blobs = 0
    painted_over = 0
    most_drip_drops = 0

    # instantiate the 2-d array
    canvas = [0] * grid_size
    for i in range(grid_size):
        canvas[i] = [0] * grid_size

    # set the coordinates
    # of our first paint
    # blob at the center
    # of the canvas grid
    drip = grid_size // 2
    drop = grid_size // 2

    # turn the position of the rob
    # ot at the center of the grid
    drip, drop = artist.position()
    most_drip_drops = canvas[0][0]

    # while loop shuffles randomly through the
    # grid/canvas, leaping up from a random co
    # ordinate, then setting down into another
    # random coordinate. this logic is repeate
    # d until every cell has been painted upon
    while (painted < (grid_size * grid_size)):
        # set drip drop coordinates at random
        drip = random.randint(0, grid_size - 1)
        drop = random.randint(0, grid_size - 1)

        # set the turtle robot
        # as a stamp so user c
        # an visualize where t
        # he randomization wit
        # hin the grid is occu
        # ring
        artist.color("#000000")
        artist.shape("turtle")
        brush = artist.stamp()

        artist.penup()
        artist.goto((drip - grid_size // 2) * cell_size,
                    (drop - grid_size // 2) * cell_size)

        # if the cell has not been
        # painted upon, and the dr
        # rip drop has selected it
        # we add the count of that
        # cell into the correct x,
        # y coordinates of our 2-d
        # array and fill the cell
        # with a randomized color
        if canvas[drip][drop] == 0:
            # uncomment the commented line below for testing
            # print("drip: {}, drop: {}".format(drip,drop))
            canvas[drip][drop] = 1

            # fills cell with a
            # random hexidecimal
            # value from function
            # drip_color
            color = drip_color()
            artist.begin_fill()
            artist.color(color)
            artist.circle(random.randint(10, 20))
            artist.end_fill()
            painted += 1

        # if there already has be
        # en a drip drop, or rand
        # om traversal over the a
        # ctive cell, add one to
        # the active x,y coordina
        # te of the 2d array
        if canvas[drip][drop] > 0:
            canvas[drip][drop] += 1
            # update the x,y coordinate with the max
            # number of drip drops for later stats
            if canvas[drip][drop] > most_drip_drops:
                most_drip_drops = canvas[drip][drop]
            painted_over += 1

        # clear the turtle logo
        # stamp, which can be th
        # ought of as resetting
        # the turtle logo stamp
        # to the new random cell
        artist.clearstamp(brush)
        blobs += 1

    # clear the turtle graphic, r
    # eturn the aggregate of blob
    # bs dropped on the canvas, o
    # r grid, and return the most
    # drips dropped in one cell.
    artist.clear()
    return blobs, most_drip_drops