Ejemplo n.º 1
0
def main():
    hurstExponent = float(sys.argv[1])
    stddraw.setPenRadius(0.0)
    stddraw.clear(stddraw.LIGHT_GRAY)
    scaleFactor = 2 ** (2.0 * hurstExponent)
    curve(0, .5, 1.0, .5, .01, scaleFactor)
    stddraw.show()
Ejemplo n.º 2
0
def plotLines(a):
    """
    Plot the elements of array a as line end-points.
    """
    n = len(a)
    stddraw.setXscale(-1, n)
    stddraw.setPenRadius(0.0)
    for i in range(1, n):
        stddraw.line(i-1, a[i-1], i, a[i])
Ejemplo n.º 3
0
def plotLines(a):
    """
    Plot the values of array 'a' as line end-points.
    """
    N = len(a)
    stddraw.setXscale(0, N-1)
    stddraw.setPenRadius()
    for i in range(1, N):
        stddraw.line(i-1, a[i-1], i, a[i])
Ejemplo n.º 4
0
def plotPoints(a):
    """
    Plot the values of array 'a' as points.
    """
    N = len(a)
    stddraw.setXscale(0, N-1)
    stddraw.setPenRadius(1.0 / (3.0 * N))
    for i in range(N):
        stddraw.point(i, a[i])
Ejemplo n.º 5
0
def plotPoints(a):
    """
    Plot the elements of array a as points.
    """
    n = len(a)
    stddraw.setXscale(-1, n)
    stddraw.setPenRadius(1.0 / (3.0 * n))
    for i in range(n):
        stddraw.point(i, a[i])
Ejemplo n.º 6
0
def tree(n, x, y, a, branchRadius):
    cx = x + math.cos(a) * branchRadius
    cy = y + math.sin(a) * branchRadius
    stddraw.setPenRadius(.001 * (n ** 1.2))
    stddraw.line(x, y, cx, cy)
    stddraw.show()
    if (n == 0):
        return

    tree(n-1, cx, cy, a + BEND_ANGLE - BRANCH_ANGLE, \
        branchRadius * BRANCH_RATIO)
    tree(n-1, cx, cy, a + BEND_ANGLE + BRANCH_ANGLE, \
        branchRadius * BRANCH_RATIO)
    tree(n-1, cx, cy, a + BEND_ANGLE, \
        branchRadius * (1 - BRANCH_RATIO))
Ejemplo n.º 7
0
def main():
    n = int(sys.argv[1])

    cx = [0.000, 1.000, 0.500]
    cy = [0.000, 0.000, 0.866]

    x = 0.0
    y = 0.0

    stddraw.setPenRadius(0.0)
    for i in range(n):
        r = stdrandom.uniformInt(0, 3)
        x = (x + cx[r]) / 2.0
        y = (y + cy[r]) / 2.0
        stddraw.point(x, y)
    stddraw.show()
Ejemplo n.º 8
0
def main(argv):
    n = float(argv[1])
    decay = float(argv[2])
    stddraw.createWindow()
    stddraw.setPenRadius(0)
    angle = 360.0 / n
    step = math.sin(math.radians(angle/2.0))
    t = turtle.Turtle(0.5, 0, angle/2.0)

    i = 0
    while i < 10.0 * 360.0 / angle:
        step /= decay
        t.goForward(step)
        t.turnLeft(angle)
        i += 1
    stddraw.show()
    stddraw.wait()
Ejemplo n.º 9
0
def draw(pole):

    n = len(pole) - 1

    # Draw 3 poles.
    stddraw.clear()
    stddraw.setPenColor(POLE_COLOR)
    stddraw.setPenRadius(POLE_WIDTH)
    for i in range(3):
        stddraw.line(i, 0, i, n)

    # Draw n discs.
    discs = stdarray.create1D(3, 0)   # discs[p] = # discs on pole p
    for i in range(n, 0, -1):
        stddraw.setPenColor(DISC_COLOR)
        stddraw.setPenRadius(0.035)   # magic constant
        size = 0.5 * i / n
        p = pole[i]
        stddraw.line(p-size/2, discs[p], p + size/2, discs[p])
        discs[p] += 1

    stddraw.sleep(500)
    stddraw.show()
Ejemplo n.º 10
0
# p[i][j] = prob. that surfer moves from page i to page j
p = stdarray.create2D(n, n, 0.0)
for i in range(n):
    for j in range(n):
        p[i][j] = stdio.readFloat()

# freq[i] = # times surfer hits page i
freq = stdarray.create1D(n, 0)

# Start at page 0.
page = 0
stddraw.createWindow()
stddraw.setXscale(-1, n)
stddraw.setYscale(0, t)
#stddraw.setPenRadius(.5/float(n))
stddraw.setPenRadius()
for i in range(t):

    # Make one random move.
    r = random.random()
    sum = 0.0;
    for j in range(n):
        # Find interval containing r.
        sum += p[page][j]
        if r < sum:
            page = j
            break
    freq[page] += 1

    if i % 1000 == 0:
        # Plot histogram of frequencies
Ejemplo n.º 11
0
def Torso(guess):
    if guess <= 6:
        stddraw.setPenRadius(0.1)
        stddraw.line(-2, 4, -2, -4)
        stddraw.show(100)
Ejemplo n.º 12
0
        stddraw.line(x0, y0, x1, y1)
        stddraw.show(0.0)
        return
    curve(n, x0, y0, xm, fxm)
    stddraw.filledCircle(xm, fxm, .005)
    stddraw.show(0.0)
    curve(n, xm, fxm, x1, y1)


#-----------------------------------------------------------------------

# Accept integer n as a command-line argument. Plot to standard draw
# a graph that relates site vacancy probability (control variable) to
# percolation probability (experimental observations) for a
# n-by-n system.

n = int(sys.argv[1])
stddraw.setPenRadius(0.0)
stddraw.setXscale(-2, 5)
stddraw.setYscale(-2, 5)
curve(n, 0.0, math.sin(0), math.pi, math.sin(math.pi))
stddraw.line(-2, 0, 5, 0)
stddraw.line(0, -2, 0, 5)
stddraw.show()

#-----------------------------------------------------------------------

# python percplot.py 20

# python percplot.py 100
Ejemplo n.º 13
0
def display(playing_field, x, y, lent, totalscore, finalscore, clickcoords,
            clicklocal, attempts, level, background, win):
    stddraw.clear()
    image(background)
    stddraw.setXscale(-lent, (2 * lent * x) - lent)
    stddraw.setYscale(-(2 * lent * y) - 4 * lent, lent)
    stddraw.setPenColor(stddraw.PINK)
    stddraw.filledRectangle(-lent, -(2 * lent * y) - 4 * lent, (2 * lent * x),
                            5 * lent)
    stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE)
    stddraw.filledRectangle(-lent, -(2 * lent * y) - 3 * lent, (2 * lent * x),
                            lent)
    stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE)
    stddraw.filledRectangle(-lent, -(2 * lent * y) - 1.5 * lent,
                            (2 * lent * x), lent)
    stddraw.setPenColor(stddraw.GRAY)
    stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent,
                            (2 * lent * x), lent)
    if totalscore < finalscore:
        stddraw.setPenColor(stddraw.GREEN)
        stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent,
                                ((2 * lent * x) - lent) / 1.7 *
                                (totalscore / finalscore), lent)
    if totalscore >= finalscore:
        stddraw.setPenColor(stddraw.GREEN)
        stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent,
                                ((2 * lent * x) - lent), lent)
    stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE)
    stddraw.filledRectangle(x * lent + lent, -(2 * lent * y) - 3.5 * lent,
                            (lent * x), 3.5 * lent)
    stddraw.setPenRadius(0.010)
    stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE)
    stddraw.rectangle(-lent, -(2 * lent * y) - 4 * lent, (2 * lent * x),
                      5 * lent)
    stddraw.setFontSize(25)
    stddraw.setPenColor(stddraw.DARK_GREEN)
    stddraw.text((((2 * lent * x) - lent) / 1.3), -((2 * lent * y) + lent),
                 'Points: ' + str(totalscore) + ' / ' + str(finalscore))
    stddraw.setPenColor(stddraw.RED)
    stddraw.setFontSize(20)
    stddraw.text((((2 * lent * x) - lent) / 1.3),
                 -((2 * lent * y) + 2.7 * lent),
                 'Attempts Left: ' + str(attempts))
    stddraw.setPenColor(stddraw.BLUE)
    stddraw.setFontSize(25)
    stddraw.text((((2 * lent * x) - lent) / 20),
                 -((2 * lent * y) - 0.08 * lent), 'Level: ' + str(level))
    for i in range(len(clickcoords) - 1):
        found = False
        reps = -1
        while found == False and not reps >= (x * y - 1):
            reps += 1
            if clicklocal[reps][0] < clickcoords[i] < clicklocal[reps][
                    1] and clicklocal[reps][2] < clickcoords[
                        i + 1] < clicklocal[reps][3]:
                spotx = playing_field[reps][0]
                spoty = playing_field[reps][1]
                stddraw.setPenColor(stddraw.DARK_RED)
                stddraw.setPenRadius(0.05)
                stddraw.filledSquare(spotx, spoty, lent)
                found = True

    for i in range(x * y):
        spotx = playing_field[i][0]
        spoty = playing_field[i][1]

        if playing_field[i][2] == 1:

            stddraw.setPenColor(stddraw.MAGENTA)
            stddraw.setPenRadius(0.05)
            stddraw.filledCircle(spotx, spoty, lent / 1.2)

        if playing_field[i][2] == 2:
            stddraw.setPenColor(stddraw.YELLOW)
            stddraw.setPenRadius(0.05)
            stddraw.filledSquare(spotx, spoty, lent / 1.2)

        if playing_field[i][2] == 3:
            stddraw.setPenColor(stddraw.BLUE)
            stddraw.setPenRadius(0.05)
            stddraw.filledPolygon([(spotx - lent) + 0.2, (spotx - lent) + 0.3,
                                   (spotx - lent) + 0.7, (spotx - lent) + 0.9,
                                   (spotx - lent) + 0.6],
                                  [(spoty - lent) + 0.6, (spoty - lent) + 0.9,
                                   (spoty - lent) + 0.9, (spoty - lent) + 0.6,
                                   (spoty - lent) + 0.1])

        if playing_field[i][2] == 4:
            stddraw.setPenColor(stddraw.GREEN)
            stddraw.setPenRadius(0.05)
            stddraw.filledPolygon([
                (spotx - lent) + 0.1,
                (spotx - lent) + 0.4,
                (spotx - lent) + 0.9,
                (spotx - lent) + 0.4,
            ], [(spoty - lent) + 0.4, (spoty - lent) + 0.1,
                (spoty - lent) + 0.4, (spoty - lent) + 0.9])

        if playing_field[i][2] == 5:
            stddraw.setPenColor(stddraw.BLACK)
            stddraw.setPenRadius(0.05)
            stddraw.filledPolygon([(spotx - lent) + 0.5, (spotx - lent) + 0.1,
                                   (spotx - lent) + 0.9],
                                  [(spoty - lent) + 0.8, (spoty - lent) + 0.1,
                                   (spoty - lent) + 0.1])

        if playing_field[i][2] == 6:
            stddraw.setPenColor(stddraw.CYAN)
            stddraw.setPenRadius(0.05)
            stddraw.filledPolygon([(spotx - lent) + 0.5, (spotx - lent) + 0.1,
                                   (spotx - lent) + 0.9, (spotx - lent) + 0.3,
                                   (spotx - lent) + 0.6],
                                  [(spoty - lent) + 0.9, (spoty - lent) + 0.7,
                                   (spoty - lent) + 0.7, (spoty - lent) + 0.1,
                                   (spoty - lent) + 0.1])

    if attempts == 0 and totalscore < finalscore:
        while stddraw.mousePressed() == False:
            stddraw.setPenColor(stddraw.BLACK)
            stddraw.filledRectangle(-lent, -(lent * y) - 2 * lent,
                                    (2 * lent * x), 3.5 * lent)
            stddraw.setFontSize(20)
            stddraw.setPenColor(stddraw.RED)
            stddraw.text(lent * x - lent, -(lent * y),
                         'YOU LOSE, DOUBLE CLICK TO EXIT')
            stddraw.show(0)
            if stddraw.mousePressed() == True:
                win = True
                return win

    if totalscore >= finalscore:
        while stddraw.mousePressed() == False:
            stddraw.setPenColor(stddraw.BOOK_BLUE)
            stddraw.filledRectangle(-lent, -(lent * y) - 2 * lent,
                                    (2 * lent * x), 3.5 * lent)
            stddraw.setFontSize(20)
            stddraw.setPenColor(stddraw.GREEN)
            stddraw.text(lent * x - lent, -(lent * y),
                         'YOU WIN, DOUBLE CLICK TO PROCEED')
            stddraw.show(0)
            if stddraw.mousePressed() == True:
                win = False
                return win
    stddraw.show(250)
    return win
Ejemplo n.º 14
0
def main():

    stddraw.createWindow(1024, 256)
    stddraw.setPenRadius(0)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-.75, +.75)
    stddraw.show()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i-24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample);

        if t == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show()
            stddraw.clear()
            t = 0

        t += 1
Ejemplo n.º 15
0
import stddraw


def draw_tree(n, x, y, size):
    if n <= 1:
        return

    stddraw.line(x, y, x - size / 1.3, y - size)
    stddraw.filledCircle(x - size / 1.3, y - size, 0.008)

    stddraw.line(x, y, x + size / 1.3, y - size)
    stddraw.filledCircle(x + size / 1.3, y - size, 0.008)

    draw_tree(n - 1, x - size / 1.3, y - size, size / 2)
    draw_tree(n - 1, x + size / 1.3, y - size, size / 2)


stddraw.setPenRadius(0.0025)
draw_tree(5, 0.5, 1, .3)
stddraw.show()
Ejemplo n.º 16
0
    def draw(self):
        # draw the tile as a filled square
        if (self.number == 2):
            self.background_color = Color(238, 228,
                                          218)  # background (tile) color
        if (self.number == 4):
            self.background_color = Color(237, 224,
                                          200)  # background (tile) color
        if (self.number == 8):
            self.background_color = Color(242, 177,
                                          121)  # background (tile) color
        if (self.number == 16):
            self.background_color = Color(245, 149,
                                          99)  # background (tile) color
        if (self.number == 32):
            self.background_color = Color(246, 124,
                                          95)  # background (tile) color
        if (self.number == 64):
            self.background_color = Color(246, 94,
                                          59)  # background (tile) color
        if (self.number == 128):
            self.background_color = Color(237, 207,
                                          114)  # background (tile) color
        if (self.number == 256):
            self.background_color = Color(237, 204,
                                          97)  # background (tile) color
        if (self.number == 512):
            self.background_color = Color(237, 200,
                                          80)  # background (tile) color
        if (self.number == 1024):
            self.background_color = Color(237, 197,
                                          63)  # background (tile) color
        if (self.number == 2048):
            self.background_color = Color(237, 194,
                                          46)  # background (tile) color
        #used proper colors until 2048
        #used remainder in order to get a proper color for every number
        #with this the value will never exceed 255 however better solution may be proposed
        if (self.number > 2048):
            self.background_color = Color(
                (self.number % 255), (self.number % 20), (self.number % 255))

        stddraw.setPenColor(self.background_color)
        stddraw.filledSquare(self.position.x, self.position.y, 0.5)
        # draw the bounding box of the tile as a square
        stddraw.setPenColor(self.boundary_color)
        stddraw.setPenRadius(Tile.boundary_thickness)
        stddraw.square(self.position.x, self.position.y, 0.5)
        stddraw.setPenRadius()  # reset the pen radius to its default value

        # draw inner lines
        x = self.position.x
        y = self.position.y
        stddraw.setPenColor(self.foreground_color)
        stddraw.square(x, y, 0.3)
        stddraw.setPenRadius(Tile.boundary_thickness)

        stddraw.line(x - 0.5, y - 0.5, x + 0.5, y + 0.5)
        stddraw.line(x - 0.5, y + 0.5, x + 0.5, y - 0.5)

        #draw inner box
        stddraw.setPenRadius()
        stddraw.setPenColor(self.background_color)
        stddraw.filledSquare(self.position.x, self.position.y, 0.3)

        ###############################
        #stddraw.setPenColor(stddraw.RED)
        #stddraw.filledCircle(0.5, 0.5, 0.3)
        #stddraw.filledCircle(-0.5,0.5,0.3)

        # draw the number on the tile
        stddraw.setPenColor(self.foreground_color)
        stddraw.setFontFamily(Tile.font_family)
        stddraw.setFontSize(Tile.font_size)
        stddraw.boldText(self.position.x, self.position.y, str(self.number))
Ejemplo n.º 17
0
 def draw(self):
     stddraw.setPenRadius(0.0125)
     stddraw.point(self._r[0], self._r[1])
Ejemplo n.º 18
0
import stddraw
import math

# Draw an animation of the second, minute, and hour hands of an
# analog clock.
stddraw.createWindow()
t = 0
while True:

    # Remainder operator with floats so all hands move every second.
    seconds = t % 60
    minutes = (t / 60.0) % 60
    hours = (t / 3600.0) % 12

    stddraw.clear()
    stddraw.setPenRadius()

    # Draw clock face.
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.filledCircle(0.5, 0.5, 0.45)

    # Draw hour markers.
    stddraw.setPenColor(stddraw.BLUE)
    for i in range(12):
        theta = math.radians(i * 30)
        stddraw.filledCircle(0.5 + 0.4 * math.cos(theta), \
                             0.5 + 0.4 * math.sin(theta), .025)

    # Draw second hand.
    stddraw.setPenRadius(.01)
    stddraw.setPenColor(stddraw.YELLOW)
Ejemplo n.º 19
0
def main():
    n = int(sys.argv[1])
    stddraw.setPenRadius(0.0)
    draw(n, .5, .5, .5)
    stddraw.show()
Ejemplo n.º 20
0
import stddraw
import math

# Draw an animation of the second, minute, and hour hands of an
# analog clock.
stddraw.createWindow()
t = 0
while True:

    # Remainder operator with floats so all hands move every second.
    seconds = t % 60
    minutes = (t / 60.0) % 60
    hours   = (t / 3600.0) % 12

    stddraw.clear()
    stddraw.setPenRadius()

    # Draw clock face.
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.filledCircle(0.5, 0.5, 0.45)

    # Draw hour markers.
    stddraw.setPenColor(stddraw.BLUE)
    for i in range(12):
        theta = math.radians(i * 30)
        stddraw.filledCircle(0.5 + 0.4 * math.cos(theta), \
                             0.5 + 0.4 * math.sin(theta), .025)

    # Draw second hand.
    stddraw.setPenRadius(.01)
    stddraw.setPenColor(stddraw.YELLOW)
Ejemplo n.º 21
0
def main():

    stddraw.createWindow(1024, 256)
    stddraw.setPenRadius(0)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-0.75, +0.75)
    stddraw.show()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample)

        if t == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show()
            stddraw.clear()
            t = 0

        t += 1
Ejemplo n.º 22
0
 def draw(self):
     stddraw.setPenRadius(0.0125)
     stddraw.point(self._r[0], self._r[1])
Ejemplo n.º 23
0
def main():
    stddraw.clear()
    n = int(sys.argv[1])
    stddraw.setPenRadius(0.0)
    draw(n, .5, .5, .5)
    stddraw.show()
Ejemplo n.º 24
0
# p[i][j] = prob. that surfer moves from page i to page j
p = stdarray.create2D(n, n, 0.0)
for i in range(n):
    for j in range(n):
        p[i][j] = stdio.readFloat()

# freq[i] = # times surfer hits page i
freq = stdarray.create1D(n, 0)

# Start at page 0.
page = 0
stddraw.createWindow()
stddraw.setXscale(-1, n)
stddraw.setYscale(0, t)
#stddraw.setPenRadius(.5/float(n))
stddraw.setPenRadius()
for i in range(t):

    # Make one random move.
    r = random.random()
    sum = 0.0
    for j in range(n):
        # Find interval containing r.
        sum += p[page][j]
        if r < sum:
            page = j
            break
    freq[page] += 1

    if i % 1000 == 0:
        # Plot histogram of frequencies
Ejemplo n.º 25
0
def Head(guess):
    if guess <= 7:
        stddraw.setPenRadius(0.09 * 400 / ((10 - -10) * 256))
        stddraw.circle(-2, 6, 2.0)
        stddraw.show(100)
Ejemplo n.º 26
0
#-----------------------------------------------------------------------
# plotfilter.py
#-----------------------------------------------------------------------

import stdio
import stddraw

# Plot the points read from standard input.

x0 = stdio.readFloat()
y0 = stdio.readFloat()
x1 = stdio.readFloat()
y1 = stdio.readFloat()

stddraw.createWindow()
stddraw.setXscale(x0, x1)
stddraw.setYscale(y0, y1)
stddraw.setPenRadius(0.001)

# Read and plot the points.
while not stdio.isEmpty():
    x = stdio.readFloat()
    y = stdio.readFloat()
    stddraw.point(x, y)

stddraw.show()
stddraw.wait()
Ejemplo n.º 27
0
    def display(self, delay=0):
        # clear the background canvas to empty_cell_color
        stddraw.clear(self.background_color)
        # draw a box around the game grid
        self.draw_boundaries()
        # draw the game grid
        self.draw_grid()
        # draw the current (active) tetromino ghost
        if self.current_ghost != None:
            self.current_ghost.draw()

        # draw the current (active) tetromino
        if self.current_tetromino != None:
            self.current_tetromino.draw()

        # draw the normal game cycle GUI
        if not self.game_over:
            # set pen color based on game mode
            if self.gamemode == "tetris":
                stddraw.setPenColor(stddraw.WHITE)
            else:
                stddraw.setPenColor(self.boundary_color)

            # set font and its size
            stddraw.setFontFamily("Arial")
            stddraw.setFontSize(24)

            # draw score
            if self.gamemode == "2048" and self.reached_2048:
                stddraw.boldText(13.75, 19, "Congrats!")
                stddraw.text(13.75, 17.75, "Score")
                stddraw.boldText(13.75, 16.75, str(self.score))
            else:
                stddraw.text(13.75, 19, "Score")
                stddraw.boldText(13.75, 18, str(self.score))

            # draw upcoming tetrominoes
            stddraw.text(13.75, 15, "Upcoming")
            stddraw.text(13.75, 14, "Tetrominoes")
            stddraw.setPenColor(self.boundary_color)
            stddraw.filledRectangle(12, -0.25, 3.5, 13.5)
            stddraw.setPenRadius(0.001)
            if self.gamemode == "tetris":
                stddraw.setPenColor(stddraw.DARK_GRAY)
            else:
                stddraw.setPenColor(self.empty_cell_color)
            stddraw.line(12.25, 8.75, 15.25, 8.75)
            stddraw.line(12.25, 4.25, 15.25, 4.25)
            self.next_tetromino1.copy(
                blcx=(14.25 - (self.next_tetromino1.column_count / 2)),
                blcy=9.5 + (4 - self.next_tetromino1.row_count) / 2,
                trim=True).draw()
            self.next_tetromino2.copy(
                blcx=(14.25 - (self.next_tetromino2.column_count / 2)),
                blcy=5 + (4 - self.next_tetromino2.row_count) / 2,
                trim=True).draw()
            self.next_tetromino3.copy(
                blcx=(14.25 - (self.next_tetromino3.column_count / 2)),
                blcy=0.5 + (4 - self.next_tetromino3.row_count) / 2,
                trim=True).draw()

            # show the canvas
            stddraw.show(delay)
        # draw the game over GUI
        else:
            # set pen color based on game mode
            if self.gamemode == "tetris":
                stddraw.setPenColor(stddraw.WHITE)
            else:
                stddraw.setPenColor(self.boundary_color)

            # set font and its size
            stddraw.setFontFamily("Arial")
            stddraw.setFontSize(24)

            # draw game over text and and final score
            stddraw.text(13.75, 12, "Game Over!")
            stddraw.text(13.75, 10, "Final Score:")
            stddraw.boldText(13.75, 9, str(self.score))

            # draw the high score conclusion
            stddraw.setFontSize(18)
            if self.score > self.old_high_score:
                stddraw.text(13.75, 7.75, "New High Score!")
                self.new_high_score = self.score
            else:
                stddraw.text(13.75, 7.75, "High Score:")
                stddraw.boldText(13.75, 7, str(self.old_high_score))

            # draw the after-game controls
            stddraw.setFontSize(16)
            stddraw.text(13.75, 2, "Press R to")
            stddraw.text(13.75, 1.5, "restart the game,")
            stddraw.text(13.75, 1, "or press Enter to")
            stddraw.text(13.75, 0.5, "return to the")
            stddraw.text(13.75, 0, "main manu.")

            # show the canvas
            stddraw.show(delay)
Ejemplo n.º 28
0
        return  
    koch(n-1, stepSize, myTurtle)
    myTurtle.turnLeft(60.0)
    koch(n-1, stepSize, myTurtle)
    myTurtle.turnLeft(-120.0)
    koch(n-1, stepSize, myTurtle)
    myTurtle.turnLeft(60.0)
    koch(n-1, stepSize, myTurtle)
 
# Accept integer n as a command-line argument. Plot a Koch curve of 
# order n to standard draw.

n = int(sys.argv[1])
stddraw.setCanvasSize(512, 256)
stddraw.setYscale(-.1, 0.4)
stddraw.setPenRadius(0.0)
stddraw.clear(stddraw.LIGHT_GRAY)
stepSize = 1.0 / (3.0 ** n)
myTurtle = Turtle(0.0, 0.0, 0.0)
koch(n, stepSize, myTurtle)
stddraw.show()

#-----------------------------------------------------------------------

# python koch.py 0

# python koch.py 1

# python koch.py 2

# python koch.py 3
Ejemplo n.º 29
0
 def draw(self):
     stddraw.setPenRadius(0.025)
     stddraw.point(self._r.cartesian(0), self._r.cartesian(1))
Ejemplo n.º 30
-1
def main():
    n = int(sys.argv[1])
    dist = stdarray.readFloat1D()
    cx = stdarray.readFloat2D()
    cy = stdarray.readFloat2D()
    x = 0.0
    y = 0.0
    stddraw.setPenRadius(0.0)
    for i in range(n):
        r = stdrandom.discrete(dist)
        x0 = cx[r][0]*x + cx[r][1]*y + cx[r][2]
        y0 = cy[r][0]*x + cy[r][1]*y + cy[r][2]
        x = x0
        y = y0
        stddraw.point(x, y)
    stddraw.show()