Example #1
0
    def move(self, dir, apple):
        """ Changes the direction of the snake.

        Keyword parameters:
        dir   -- That key that will change the direction of the snake (a string).
        apple -- The apple that the snake is trying to eat.
        """
        x, y = self.body[0]
        if dir == 'a':
            self.body.insert(0, (x - 1, y))
        elif dir == 'w':
            self.body.insert(0, (x, y + 1))
        elif dir == 'd':
            self.body.insert(0, (x + 1, y))
        elif dir == 's':
            self.body.insert(0, (x, y - 1))

        front = self.body[0]
        stddraw.setPenColor(stddraw.PURPLE)
        stddraw.filledSquare(front[0], front[1], 0.5)

        if front != apple.location:
            back = self.body.pop()
            stddraw.setPenColor(stddraw.BLACK)
            stddraw.filledSquare(back[0], back[1], 0.5)
        else:
            apple.new_location(self)
            config.PTS += 1
Example #2
0
def draw(a, which):
    n = len(a)
    stddraw.setXscale(-.5, n)
    stddraw.setYscale(-.5, n)
    for i in range(n):
        for j in range(n):
            if a[i][j] == which:
                stddraw.filledSquare(j, n-i-1, .5)
Example #3
0
def draw(a, which):
    n = len(a)
    stddraw.setXscale(-.5, n)
    stddraw.setYscale(-.5, n)
    for i in range(n):
        for j in range(n):
            if a[i][j] == which:
                stddraw.filledSquare(j, n - i - 1, .49)
Example #4
0
def draw_blanks():
    new_array = array
    for i in range(len(new_array)):
        for j in range(len(new_array[i])):
            y = -i + 1 - 1.5
            x = j + 1 - .5
            if new_array[i][j] == 0:
                stddraw.setPenColor(stddraw.WHITE)
                stddraw.filledSquare(x, y, 0.5)
def reSquares(n, x, y, r):
    if n == 0: return
    stddraw.setPenColor(stddraw.GRAY)
    stddraw.filledSquare(x, y, r)
    stddraw.setPenColor()
    stddraw.square(x, y, r)
    reSquares(n - 1, x - r, y + r, r / 2.2)
    reSquares(n - 1, x - r, y - r, r / 2.2)
    reSquares(n - 1, x + r, y + r, r / 2.2)
    reSquares(n - 1, x + r, y - r, r / 2.2)
Example #6
0
def win(chosen_word):
    stddraw.clear()
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.filledSquare(0.0, 0.0, 10)
    stddraw.setFontSize(70)
    stddraw.setPenColor(stddraw.MAGENTA)
    stddraw.text(0.0, 0.0, "YOU WIN xD")
    stddraw.setFontSize(30)
    stddraw.text(0.0, -4, "The word was: ")
    stddraw.text(0.0, -5, str(''.join(chosen_word)))
    stddraw.show(9000)
Example #7
0
def draw(a, which):
    #print(a)
    m = len(a)
    n = len(a[0])
    scale_length = max(m, n)
    stddraw.setXscale(-.5, scale_length)
    stddraw.setYscale(-.5, scale_length)
    for i in range(m):
        for j in range(n):
            if a[i][j] == which:
                stddraw.filledSquare(j, m - i - 1, .49)
Example #8
0
def lose(chosen_word):
    stddraw.clear()
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.filledSquare(0.0, 0.0, 10)
    stddraw.setFontSize(70)
    stddraw.setPenColor(stddraw.BLUE)
    stddraw.text(0.0, 0.0, "Oops :(")
    stddraw.setFontSize(30)
    stddraw.text(0.0, -4, "You couldn't guess the word!")
    stddraw.text(0.0, -5, "The word was: ")
    stddraw.text(0.0, -6, str(''.join(chosen_word)))
    stddraw.show(9000)
Example #9
0
 def new_location(self, snake):
     """Creates a new location for an apple after it is eaten.
        Technically, a new apple object is not created. It is the
        same apple object with a new location.
     """
     x, y = self.location
     while self.location in snake.body:
         x = random.randint(0, config.N - 1) + .5
         y = random.randint(0, config.N - 1) + .5
         self.location = (x, y)
     stddraw.setPenColor(stddraw.RED)
     stddraw.filledSquare(x, y, .5)
Example #10
0
def draw(N):

    d.setXscale(0, N)
    d.setYscale(0, N)
    for i in range(N):
        for j in range(N):
            if ((i + j) % 2) != 0:
                d.setPenColor(d.BLACK)
            else:
                d.setPenColor(d.RED)
            d.filledSquare(i + .5, j + .5, .5)
            d.show(0)
def drawSelect(point, score, time): #[x, y], score
    x = point[0]
    y = point[1]
    stddraw.clear()
            
    LIGHT_BLUE = Color(190,240,255)
    stddraw.setPenColor(LIGHT_BLUE)
    stddraw.filledSquare(x-1+0.5, y+0.5, 0.5) #the 4 adjacent squares to the one you clicked
    stddraw.filledSquare(x+1+0.5, y+0.5, 0.5)
    stddraw.filledSquare(x+0.5, y-1+0.5, 0.5)
    stddraw.filledSquare(x+0.5, y+1+0.5, 0.5)

    drawTop(score, time)
    drawBoard()

    stddraw.setPenColor(stddraw.BLACK)
    stddraw.setPenRadius(0.04)
    
    stddraw.line(x, y, x+0.25, y) #the 4 corners around the selected square
    stddraw.line(x, y, x, y+0.25) #bottom left

    stddraw.line(x+0.75, y, x+1, y) #bottom right
    stddraw.line(x+1, y, x+1, y+0.25)

    stddraw.line(x, y+0.75, x, y+1) #top left
    stddraw.line(x, y+1, x+0.25, y+1)

    stddraw.line(x+0.75, y+1, x+1, y+1) #top right
    stddraw.line(x+1, y+0.75, x+1, y+1)
Example #12
0
def main(argv):
    r1 = int(argv[1])
    g1 = int(argv[2])
    b1 = int(argv[3])

    c1 = color.Color(r1, g1, b1)

    r2 = int(argv[4])
    g2 = int(argv[5])
    b2 = int(argv[6])

    c2 = color.Color(r2, g2, b2)
    stddraw.createWindow()
    stddraw.setPenColor(c1)
    stddraw.filledSquare(.25, .5, .2)

    stddraw.setPenColor(c2)
    stddraw.filledSquare(.25, .5, .1)

    stddraw.setPenColor(c2)
    stddraw.filledSquare(.75, .5, .2)

    stddraw.setPenColor(c1)
    stddraw.filledSquare(.75, .5, .1)
    stddraw.show()
    stddraw.wait()
Example #13
0
 def draw(self):
     # draw the tile as a filled square
     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 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))
Example #14
0
def draw_squares(n, center_x, center_y, size):
    if n <= 1:
        return

    draw_squares(n - 1, center_x - size, center_y - size, size / 2.1)
    draw_squares(n - 1, center_x + size, center_y - size, size / 2.1)
    draw_squares(n - 1, center_x - size, center_y + size, size / 2.1)
    draw_squares(n - 1, center_x + size, center_y + size, size / 2.1)

    stddraw.square(center_x, center_y, size)
    stddraw.setPenColor(color.GRAY)
    stddraw.filledSquare(center_x, center_y, size - 0.002)
    # you may need to change the value of 0.002 because of your screen resolution

    stddraw.setPenColor(color.BLACK)
Example #15
0
def main():
    stddraw.createWindow()
    stddraw.setXscale(0, 15)
    stddraw.setYscale(0, 15)
    for i in range(16):
        for j in range(16):
            #val = i + 16*j
            val = 8 * i + 8 * j
            #c1 = color.Color(0, 0, 255-val)
            c1 = color.Color(255 - val, 255 - val, 255)
            c2 = color.Color(val, val, val)
            stddraw.setPenColor(c1)
            stddraw.filledSquare(i, j, 0.5)
            stddraw.setPenColor(c2)
            stddraw.filledSquare(i, j, 0.25)
            stddraw.show()
    stddraw.wait()
Example #16
0
def main():
    stddraw.createWindow()
    stddraw.setXscale(0, 15)
    stddraw.setYscale(0, 15)
    for i in range(16):
        for j in range (16):
            #val = i + 16*j
            val = 8*i + 8*j
            #c1 = color.Color(0, 0, 255-val)
            c1 = color.Color(255-val, 255-val, 255)            
            c2 = color.Color(val, val, val)
            stddraw.setPenColor(c1)
            stddraw.filledSquare(i, j, 0.5)
            stddraw.setPenColor(c2)
            stddraw.filledSquare(i, j, 0.25)
            stddraw.show()
    stddraw.wait()
Example #17
0
def main():
    r = 8
    stddraw.setXscale(-(r+2), r+2)
    stddraw.setYscale(-(r+2), r+2)
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.filledSquare(0,0,r+2)
    xH1 = r
    yH1 = r
    xH2 = -r
    yH2 = r
    xH3 = -r
    yH3 = -r
    xH4 = r
    yH4 = -r

    nesting(xH1, yH1, xH2, yH2, xH3, yH3, xH4, yH4)
    stddraw.show()
Example #18
0
def draw_cells(cells):
    '''
    Draw the cells of a 1- or 2-dimensional Game of Life.

    Alive cells are drawn in red and dead cells are drawn in
    white.

    For a 1-dimensional Game of Life the cells are drawn
    as a single row centered in the image window.

    For a 2-dimensional Game of Life the cells occupy the
    entire canvas of the window.

    This function does nothing if `cells` is empty.

    Parameters
    ----------
    cells : list of bool or list-of-list of bool
        The cells in a Game of Life.
    '''

    if not cells:
        return
    n = len(cells)
    if type(cells[0]) is bool:
        stddraw.setXscale(0, n)
        stddraw.setYscale(-n / 2, n / 2)
        stddraw.clear(stddraw.WHITE)
        stddraw.setPenColor(stddraw.RED)
        for col in range(0, n):
            if cells[col]:
                stddraw.filledSquare(col + 0.5, 0, 0.5)
    else:
        stddraw.setXscale(0, n)
        stddraw.setYscale(0, n)
        stddraw.clear(stddraw.WHITE)
        stddraw.setPenColor(stddraw.RED)
        for row in range(0, n):
            for col in range(0, n):
                if cells[row][col]:
                    stddraw.filledSquare(col + 0.5, n - row - 0.5, 0.5)
def eliminate(score, bonus):
    toRemove = []

    for row in range(9):
        length = 1
        for column in range(7):
            if board[row][column] == board[row][column+1]: #if there is a chain of same squares
                length += 1
            elif length >= 3: #chain breaks here
                for b in range(length): 
                    toRemove += [ [row, column-b] ] # y,x location
                length = 1
            else: #chain didn't reach at least 3 so reset it
                length = 1

    for column in range(7):
        length = 1
        for row in range(9):
            if board[row][column] == board[row+1][column]: #same as above, but check horizontally
                length += 1
            elif length >= 3: #chain breaks here
                for b in range(length): 
                    toRemove += [ [row-b, column] ] # y,x location
                length = 1
            else: #chain didn't reach at least 3 so reset it
                length = 1

    LIGHT_GREEN = Color(230,255,230)
    stddraw.setPenColor(LIGHT_GREEN)

    for point in toRemove:
        if board[point[0]][point[1]] != "":
            board[point[0]][point[1]] = ""
            stddraw.setPenColor(LIGHT_GREEN)
            stddraw.filledSquare(point[1]+0.5, point[0]+0.5, 0.5)
            score += 10 + bonus #the more you get in a row the higher score you get
            bonus += 1
    stddraw.show(150) #briefly flash light green where tiles connected and disapeared
    return score, bonus
Example #20
0
def drawShapes(number,x,y):
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.setPenRadius(0.01)
    if number == 0:
        stddraw.square(x,y,0.4)
        stddraw.setPenColor(stddraw.YELLOW)
        stddraw.filledSquare(x,y,0.4)
    if number == 1:
        stddraw.setPenColor(stddraw.RED)
        stddraw.filledCircle(x,y,0.4)
        stddraw.setPenColor(stddraw.BLACK)
        stddraw.setPenRadius(0.004)
        stddraw.circle(x,y,0.4)
    if number == 2:
        xs = [x-0.4,x+0.4,x]
        ys = [y-0.4,y-0.4,y+0.4]
        stddraw.polygon(xs,ys)
        stddraw.setPenColor(stddraw.ORANGE)
        stddraw.filledPolygon(xs,ys)
    if number == 3:
        xs = [x-0.4,x,x+0.4,x]
        ys = [y,y+0.4,y,y-0.4]
        stddraw.polygon(xs,ys)
        stddraw.setPenColor(stddraw.GREEN)
        stddraw.filledPolygon(xs,ys)
    if number == 4:
        xs = [x-0.4,x-0.2,x+0.2,x+0.4,x]
        ys = [y,y+0.4,y+0.4,y,y-0.4]
        stddraw.polygon(xs,ys)
        stddraw.setPenColor(stddraw.MAGENTA)
        stddraw.filledPolygon(xs,ys)
    if number == 5:
        xs = [x-0.4,x-0.2,x+0.4,x+0.2]
        ys = [y-0.4,y+0.4,y+0.4,y-0.4]
        stddraw.polygon(xs,ys)
        stddraw.setPenColor(stddraw.BLUE)
        stddraw.filledPolygon(xs,ys)
Example #21
0
def printcheckboard(values): 
    n = 8
    stddraw.setXscale(0, n)
    stddraw.setYscale(0, n)
    z = 0 ##z is the spot in values string
    rows = 'ABCDEFGH'
    cols = '12345678'
        
    for i in range(n):
        for j in range(n): 
            if ((i + j) % 2) != 0:
                stddraw.setPenColor(stddraw.BLACK)
            else:
                stddraw.setPenColor(stddraw.RED)
            stddraw.filledSquare(j + .5, i + .5, .5)
            stddraw.setPenColor(stddraw.YELLOW)
            location = rows[i] + cols[j]
            stddraw.text(j+.15, i+.1, location)
            if((i + j) % 2) == 0:
                if(values[z] == '1'):           ##player 1 #or values[z] == '3'
                    stddraw.setPenColor(stddraw.BLUE)
                    stddraw.filledCircle(j + .5, i + .5, .25)
                elif(values[z] == '2'):         ##player 2
                    stddraw.setPenColor(stddraw.WHITE)
                    stddraw.filledCircle(j + .5, i + .5, .25)
                elif(values[z] == '3'):         ##player 1 king
                    stddraw.setPenColor(stddraw.WHITE)
                    stddraw.filledCircle(j + .5, i + .5, .25)
                    stddraw.setPenColor(stddraw.BLACK)
                    stddraw.text(j+.5, i+.5, "K")
                elif(values[z] == '4'):         ##player 2 king
                    stddraw.setPenColor(stddraw.WHITE)
                    stddraw.filledCircle(j + .5, i + .5, .25)
                    stddraw.setPenColor(stddraw.BLACK)
                    stddraw.text(j+.5, i+.5, "K")
                z+=1
    stddraw.show(500)
Example #22
0
def clear_taffy(x, y):
    stddraw.setPenColor(stddraw.WHITE)
    stddraw.setPenRadius(0.005)
    stddraw.filledSquare(x, y, 1.5)
    stddraw.show(20.0)
Example #23
0
#-----------------------------------------------------------------------
# checkerboard.py
#-----------------------------------------------------------------------

import stddraw
import sys

# Accept integer command-line argument n. Draw an n-by-n checkerboard.

n = int(sys.argv[1])

stddraw.setXscale(0, n)
stddraw.setYscale(0, n)

for i in range(n):
    for j in range(n):
        if ((i + j) % 2) != 0:
            stddraw.setPenColor(stddraw.BLACK)
        else:
            stddraw.setPenColor(stddraw.RED)
        stddraw.filledSquare(i + .5, j + .5, .5)

stddraw.show()

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

# python checkerboard.py 8

Example #24
0
import stddraw
from color import Color

# Display Albers squares corresponding to each of 256 levels
# of blue (blue-to-white in row-major order) and gray
# (black-to-white in column-major order).

stddraw.setXscale(-1, 16)
stddraw.setYscale(-1, 16)

for i in range(16):
    for j in range(16):     
        c = Color((15-j)*255/15, (15-j)*255/15, 255)            
        stddraw.setPenColor(c)
        stddraw.filledSquare(i, j, 0.5)
        #stddraw.show(0.0)
        
for i in range(16):
    for j in range(16):     
        c = Color(i*255/15, i*255/15, i*255/15)
        stddraw.setPenColor(c)
        stddraw.filledSquare(i, j, 0.25)
        #stddraw.show(0.0)        
        
stddraw.show()

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

# python colorstudy.py
Example #25
0
 def __init__(self):
     x = random.randint(0, config.N - 1) + .5
     y = random.randint(0, config.N - 1) + .5
     self.body = [(x, y)]
     stddraw.setPenColor(stddraw.PURPLE)
     stddraw.filledSquare(x, y, .5)
#stddraw.setCanvasSize(512, 256)
#stddraw.setYscale(.25*2, .75*2)
stddraw.setYscale(-1, 7)
stddraw.setXscale(-4, 27 * 2)
'''
stddraw.setPenColor(c1)
stddraw.filledSquare(.25, .5, .3)

stddraw.setPenColor(c2)
stddraw.filledSquare(.25, .5, .2)

stddraw.setPenColor(c3)
stddraw.filledSquare(.25, .5, .1)
'''
f = list(permutations('012', 3))

for i in range(6):
    stddraw.setPenColor(cs[eval(f[i][0])])
    stddraw.filledSquare(i * 3 * 2 + i, 3, 3)
    stddraw.setPenColor(cs[eval(f[i][1])])
    stddraw.filledSquare(i * 3 * 2 + i, 3, 2)
    stddraw.setPenColor(cs[eval(f[i][2])])
    stddraw.filledSquare(i * 3 * 2 + i, 3, 1)
stddraw.show()

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

# python alberssquares.py 9 90 166 100 100 100

# python alberssquares.py 0 174 239 147 149 252
Example #27
0
#-----------------------------------------------------------------------
# shapestext.py
#-----------------------------------------------------------------------

import stddraw

# Draw some shapes and some text.
stddraw.createWindow()
stddraw.square(.2, .8, .1)

stddraw.filledSquare(.8, .8, .2)

stddraw.circle(.8, .2, .2)

xd = [.1, .2, .3, .2]
yd = [.2, .3, .2, .1]
stddraw.filledPolygon(xd, yd)

stddraw.setFontFamily('Times')
stddraw.setFontSize(40)
stddraw.text(.2, .5, 'black')

stddraw.setPenColor(stddraw.WHITE)
stddraw.setFontFamily('Courier')
stddraw.setFontSize(30)
stddraw.text(.8, .8, 'white')

stddraw.show()
stddraw.wait()
Example #28
0
def removed_taffy(x, y):
    stddraw.setPenColor(stddraw.WHITE)
    stddraw.filledSquare(x, y, .5)
import stddraw as d
import sys

d.square(.2, .8, .1)

d.filledSquare(.8, .8, .2)

d.circle(.8, .2, .2)

d.show(0)

c = ''
while c != '.':
    c = input()
Example #30
0
vy = 0.023

radius = 0.05
dt = 20
start = time.time()

t = 0
while t < 10000:
    # Update ball position and draw it there.
    if abs(rx + vx) + radius > 1.0:
        vx = -vx
    if abs(ry + vy) + radius > 1.0:
        vy = -vy
    rx = rx + vx
    ry = ry + vy

    # stddraw.clear()

    stddraw.setPenColor(stddraw.GRAY)
    stddraw.filledSquare(0, 0, 1.0)

    stddraw.setPenColor(stddraw.BLACK)
    stddraw.filledCircle(rx, ry, radius)

    # stddraw.sleep(dt)
    stddraw.show()
    t += 1
finish = time.time()

print finish - start
parser = argparse.ArgumentParser(
    description='Draw a chrozoid graph using probability to draw lines')
parser.add_argument('n', type=int, help='number of subdivisions of circle')
parser.add_argument('prob',
                    type=float,
                    help='probability for drawing the line: 0 <= prob <= 1')
args = parser.parse_args()
n = args.n
p = args.prob
if not 0 <= p <= 1:
    parser.print_help()
    sys.exit(1)

stddraw.setXscale(-n - 1, n + 1)
stddraw.setYscale(-n - 1, n + 1)

points = gen_polygon_coordinates(n, scale=n, angle_offset=math.pi / 2)

for i in range(n):
    stddraw.setPenColor(stddraw.RED)
    stddraw.filledSquare(*points[i], 0.1)

for i in range(n):
    for j in range(n):
        if random.random() <= p:
            stddraw.setPenColor(stddraw.GRAY)
            stddraw.line(*points[i], *points[j])

stddraw.show()
Example #32
0
#-----------------------------------------------------------------------
# checkerboard.py
#-----------------------------------------------------------------------

import stddraw
import sys

# Accept integer command-line argument n. Draw an n-by-n checkerboard.

n = int(sys.argv[1])
stddraw.createWindow()

stddraw.setXscale(0, n)
stddraw.setYscale(0, n)

for i in range(n):
    for j in range(n):
        if ((i + j) % 2) != 0:
            stddraw.setPenColor(stddraw.BLACK)
        else:
            stddraw.setPenColor(stddraw.RED)
        stddraw.filledSquare(i + .5, j + .5, .5)
        stddraw.show()

stddraw.wait()
g1 = readint()
b1 = readint()


c1 = Color(r1, g1, b1) #Colores cuadrado 1

r2 = readint()
g2 = readint()
b2 = readint()


c2 = Color(r2, g2, b2) #Colores cuadrado 2

stddraw.setCanvasSize(512, 256)
stddraw.setYscale(.25, .75)

stddraw.setPenColor(c1)
stddraw.filledSquare(.25, .5, .2)

stddraw.setPenColor(c2)
stddraw.filledSquare(.25, .5, .1)

stddraw.setPenColor(c2)
stddraw.filledSquare(.75, .5, .2)

stddraw.setPenColor(c1)
stddraw.filledSquare(.75, .5, .1)

stddraw.show()
print("End")
def drawSquare(x, y, size):
    stddraw.setPenColor(stddraw.LIGHT_GRAY)
    stddraw.filledSquare(x, y, size / 2)
    stddraw.setPenColor(stddraw.BLACK)
    stddraw.square(x, y, size / 2)
Example #35
0
r1 = int(sys.argv[1])
g1 = int(sys.argv[2])
b1 = int(sys.argv[3])
c1 = color.Color(r1, g1, b1)

r2 = int(sys.argv[4])
g2 = int(sys.argv[5])
b2 = int(sys.argv[6])
c2 = color.Color(r2, g2, b2)

stddraw.setCanvasSize(512, 256)
stddraw.setYscale(.25, .75)

stddraw.setPenColor(c1)
stddraw.filledSquare(.25, .5, .2)

stddraw.setPenColor(c2)
stddraw.filledSquare(.25, .5, .1)

stddraw.setPenColor(c2)
stddraw.filledSquare(.75, .5, .2)

stddraw.setPenColor(c1)
stddraw.filledSquare(.75, .5, .1)

stddraw.show()

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

# python alberssquares.py 9 90 166 100 100 100
import stddraw as d
import sys

N = int(input())

d.setXscale(0, N)
d.setYscale(0, N)

for i in range(N):
    for j in range(N):
        if ((i + j) % 2) != 0:
            d.setPenColor(d.BLACK)
        else:
            d.setPenColor(d.RED)
        d.filledSquare(i + .5, j + .5, .5)

d.show(0)