def instructions(): stddraw.clear() stddraw.setPenColor(stddraw.BLACK) stddraw.setFontSize(30) stddraw.text(3.5,7,"To Win This Game All You Have To Do") stddraw.text(3.5,6,"Is Get Your Score Over 100 in under 25 Moves.") stddraw.text(3.5,5,"That's It!. Good Luck!")
def win(): stddraw.clear() stddraw.setPenColor(stddraw.GREEN) stddraw.setFontSize(36) stddraw.text(4.5, -4.5, "Congrats! You win!") stddraw.show(1000) sys.exit()
def computation(number): ''' This function compute the steps which takes that for all distance from vertices of a polygon to its centroid are smaller than 0.001 This function will also plot a new polygon ''' # x and y are arrays to hold x coordinates and y coordinates of a vertex x,y=generate_polygon(number) x_average=average(x) # calculate x bar y_average=average(y) # calculate y bar diff=difference(x,y,x_average,y_average) # if diff is true, means distance of (x_bar,y_bar) and all vertices of a polygon are smaller than 0.001 k=0 while not diff: k+=1 stddraw.clear() stddraw.polygon(x,y) stddraw.show(20) x=average_coordinates(x) # calculate the new x coordinates y=average_coordinates(y) # calculate the new y coordinates x_average=average(x) # x_average is the x bar of this new polygon y_average=average(y) # y_average is the y bar of this new polygon diff=difference(x,y,x_average,y_average) return k
def main(): #n = int(sys.argv[1]) #p = float(sys.argv[2]) #trials = int(sys.argv[3]) n = 50 trials = 1 moveStep = 999 for i in range(trials): #isAlive = lifeio.random(n, p) #滑翔机 isAlive = [[False] * 2*n for i in range(2*n)] isAlive[10][20] = True isAlive[11][21] = True isAlive[12][19] = True isAlive[12][20] = True isAlive[12][21] = True #isAlive[10][21] = True ''' #高斯帕滑翔机枪 isAlive = GosperGliderGun.gun ''' stddraw.clear() stddraw.setPenColor(stddraw.BLACK) lifeio.draw(isAlive, True) stddraw.show(1000) for j in range(moveStep): stddraw.clear() alive = move(isAlive) isAlive = alive stddraw.show()
def drawScene(black_hole_x, black_hole_y, ball_x, ball_y, goal_x, goal_y, neg_x, neg_y): ''' handles the drawing of the ball, black box, goal box, and negative box ''' stddraw.clear() stddraw.setPenColor(stddraw.BLACK) #gridlines for i in range(1, GRID_NUM): stddraw.filledRectangle(GRID_SIZE * i, 0, 0, HEIGHT) stddraw.filledRectangle(0, GRID_SIZE * i, WIDTH, 0) #black rectangle stddraw.filledRectangle(black_hole_x * GRID_SIZE, black_hole_y * GRID_SIZE, GRID_SIZE, GRID_SIZE) #Ball stddraw.setPenColor(stddraw.GREEN) stddraw.filledCircle(((ball_x * GRID_SIZE) + (GRID_SIZE / 2)), ((ball_y * GRID_SIZE) + (GRID_SIZE / 2)), GRID_SIZE / 2) #Goal stddraw.setPenColor(stddraw.GREEN) stddraw.rectangle(goal_x * GRID_SIZE, goal_y * GRID_SIZE, GRID_SIZE, GRID_SIZE) #Negative box stddraw.setPenColor(stddraw.RED) stddraw.rectangle(neg_x * GRID_SIZE, neg_y * GRID_SIZE, GRID_SIZE, GRID_SIZE)
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()
def drawGridScene(self): """ Draws the gridlines and current position of the ball and robot """ stddraw.clear() stddraw.setPenColor(stddraw.BLACK) #gridlines for i in range(1, self.GRID_NUM_HEIGHT): stddraw.filledRectangle(0, self.GRID_SIZE * i, self.WIDTH, 0) for i in range(1, self.GRID_NUM_WIDTH): stddraw.filledRectangle(self.GRID_SIZE * i, 0, 0, self.HEIGHT) #goalie stddraw.setPenColor(stddraw.BLUE) stddraw.filledRectangle(self.goalie_pos * self.GRID_SIZE, 0, self.GRID_SIZE, self.GRID_SIZE) #x,y,size_x,size_y #Ball stddraw.setPenColor(stddraw.GREEN) stddraw.filledCircle( ((self.ball_x * self.GRID_SIZE) + (self.ball_radius)), (self.HEIGHT - ((self.ball_y * self.GRID_SIZE) + (self.ball_radius))), self.ball_radius) stddraw.show(0)
def fall(): m = 1 while m == 1: m = 0 for i in range(1, 9): for j in range(9): if game[8][j] == "null": game[8][j] = blocks[random.randrange(0, 6)] if game[i][j] != "null" and game[i - 1][j] == "null": a = game[i][j] b = game[i - 1][j] game[i][j] = b game[i - 1][j] = a m = 1 stddraw.clear() update() if x: stddraw.show(2) update() for i in range(9): for j in range(9): if game[i][j] == "null": game[i][j] = blocks[random.randrange(0, 6)]
def win(winner, turn): stddraw.clear() stddraw.setFontSize(30) if winner == 'X' or winner == 'O': stddraw.text(0.0, 0.0, "'" + winner + "'" + " player wins") if turn >= 9 and winner != 'X' and winner != 'O': stddraw.text(0.0, 0.0, " It's a tie!")
def main(): hurstExponent = float(sys.argv[1]) stddraw.setPenRadius(0.0) stddraw.clear(stddraw.LIGHT_GRAY) scaleFactor = 2**(2.0 * hurstExponent) fill_brownian(array, 0, 128, 0.05, scaleFactor) stddraw.show()
def draw(pole): POLE_WIDTH = 0.01 POLE_COLOR = stddraw.RED DISC_COLOR = stddraw.BLUE 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.show(500.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)
def swap(mx1, mx2, my1, my2): a = game[my1][mx1] b = game[my2][mx2] game[my1][mx1] = b game[my2][mx2] = a stddraw.clear() update()
def there_is_no_winner(game_playertwo_click, game_playerOne_click): if len(game_playertwo_click) + len(game_playerOne_click) >= 9: stddraw.clear() stddraw.setFontSize(30) stddraw.text(1.5, 2.5, "There is no winner ! ") stddraw.text(1.5, 1.5, "please close you window !") stddraw.show(1) exit()
def hanoi(n): DT = 1000 t = 1.4 l = [] tow = [] for i in range(3): tow.append([]) for j in range(n): tow[i].append(0) for i in range(n): l.append(t**i) stddraw.setXscale(-1, t**n * 4) stddraw.setYscale(-1, n + 2) stddraw.line(t**n * 0.5, -1, t**n * 0.5, n + 1) stddraw.line(t**n * 1.5, -1, t**n * 1.5, n + 1) stddraw.line(t**n * 2.5, -1, t**n * 2.5, n + 1) tow[0] = l for i in range(n): stddraw.line(t**n * 0.5 - tow[0][i] / 2, n - i, t**n * 0.5 + tow[0][i] / 2, n - i) stddraw.show(DT) global r for a in range(len(r)): stddraw.clear() stddraw.line(t**n / 2, -1, t**n / 2, n + 1) stddraw.line(t**n * 1.5, -1, t**n * 1.5, n + 1) stddraw.line(t**n * 2.5, -1, t**n * 2.5, n + 1) p = r[a][0] d = r[a][1] for i in range(3): flag = 0 for j in range(n): #if j+1 == p: if tow[i][j] == t**(p - 1): flag = 1 if d == 'l': num = (i - 1) % 3 else: num = (i + 1) % 3 temp = tow[i][j] tow[i][j] = 0 tt = n - 1 while tow[num][tt] != 0: tt -= 1 tow[num][tt] = temp #if flag == 1: break if flag == 1: break #print(tow) for i in range(3): for j in range(n): if tow[i][j]: stddraw.line(t**n * (i + 0.5) - tow[i][j] / 2, n - j, t**n * (i + 0.5) + tow[i][j] / 2, n - j) #print(i,j,'||||',t**n*(i+0.5)-tow[i][j]/2,n-j,t**n*(i+0.5)+tow[i][j]/2,n-j) stddraw.show(DT)
def redraw_board(): stddraw.clear() for i in range(9): for j in range(7): if board_array[i][j] != 6: stddraw.picture( shapes_array[board_array[i][j]], padding_horizontal + j * taffy_spacing_horizontal, 1 - (padding_vertical + i * taffy_spacing_vertical))
def main(): n = int(sys.argv[1]) turtle = Turtle(.5, .0, 180.0 / n) stepSize = math.sin(math.radians(180.0 / n)) stddraw.clear(stddraw.LIGHT_GRAY) for i in range(n): turtle.goForward(stepSize) turtle.turnLeft(360.0 / n) stddraw.show()
def main(): n = int(sys.argv[1]) turtle = Turtle(.5, .0, 180.0/n) stepSize = math.sin(math.radians(180.0/n)) stddraw.clear(stddraw.LIGHT_GRAY) for i in range(n): turtle.goForward(stepSize) turtle.turnLeft(360.0/n) stddraw.show()
def main(argv): var = float(argv[1]) n = int(argv[2]) stddraw.createWindow() stddraw.clear() stddraw.setXscale(-1, +1) stddraw.setYscale(-1, +1) midpoint(0, 0, 0, 0, var / math.sqrt(2), n) stddraw.wait()
def main(): filename = sys.argv[1] dt = float(sys.argv[2]) universe = Universe(filename) while True: universe.increaseTime(dt) stddraw.clear() universe.draw() stddraw.show(10)
def draw_world(world, wait=-1): """ Draws the present state of Karel's world to stddraw. :param world: object of type World to draw :return: """ stddraw.clear() _draw_labels(world.num_avenues, world.num_streets) _draw_status(world.num_avenues, world.num_streets, world.karel) for i in range(1, world.num_avenues + 1): for j in range(1, world.num_streets + 1): x = float(i) - 0.5 y = float(j) - 0.5 # draw beeper(s) if present stddraw.setPenRadius() b = world.beepers[i][j] if b > 0: stddraw.picture(_beeper_picture, x, y) if b > 1: stddraw.text(x, y, str(b)) else: d = 0.5 * _intersection_size stddraw.line(x - d, y, x + d, y) stddraw.line(x, y - d, x, y + d) # draw walls x1 = x + 0.5 x0 = x1 - 1.0 y1 = y + 0.5 y0 = y1 - 1.0 walls = world.walls[i][j] stddraw.setPenRadius(0.5 * _wall_thickness) if walls[constants.NORTH]: stddraw.line(x0, y1, x1, y1) if walls[constants.SOUTH]: stddraw.line(x0, y0, x1, y0) if walls[constants.EAST]: stddraw.line(x1, y0, x1, y1) if walls[constants.WEST]: stddraw.line(x0, y0, x0, y1) # draw Karel the Robot if world.karel is not None: x = float(world.karel.location_avenue) - 0.5 y = float(world.karel.location_street) - 0.5 d = world.karel.facing stddraw.picture(_karel_pictures[d], x, y) if world.karel.error: stddraw.picture(_error_picture, x, y) if wait >= 0: stddraw.show(wait) else: stddraw.show()
def main(argv): stddraw.createWindow() newton = Universe() dt = float(argv[1]) while True: newton.increaseTime(dt) newton.draw() stddraw.sleep(10) stddraw.show() stddraw.clear()
def main(): n = int(sys.argv[1]) #n = 5 turtle = Turtle(.5, .0, 90 - (180.0 / n / 2)) stepSize = math.sin(math.radians(180.0 / n)) * n / 3 stddraw.clear(stddraw.LIGHT_GRAY) for i in range(n): turtle.goForward(stepSize) turtle.turnLeft((1 - 1 / n) * 180.0) stddraw.show(1000) stddraw.show()
def load_more_taffies(): shift_taffies_down() for j in range(7): stddraw.clear() redraw_board() stddraw.show(100) if board_array[0][j] == 6: board_array[0][j] = randint(0, 5) if board_array[1][j] == 6: shift_taffies_down() load_more_taffies()
def _init_canvas(minX, maxX, minY, maxY): """ @param minX: smallest x value @param maxX: largest x value @param minY: smallest y value @param maxY: largest y value Initialises a canvas with given scale """ stddraw.clear(color.DARK_GRAY) stddraw.setXscale(minX - 1, maxX + 1) # -4 bis 3 stddraw.setYscale(minY - 1, maxY + 1) # -1 bis 8
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)
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)
def exch(a, i, j): tmp = a[i] a[i] = a[j] a[j] = tmp stddraw.clear() for n in range(length): if n == j or n == i: stddraw.setPenColor(stddraw.RED) else: stddraw.setPenColor(stddraw.BLUE) stddraw.filledRectangle(n, 0, 0.5, a[n]) stddraw.show(300)
def determine_the_winner(game_player_click, playernumber, game_win): if len(game_player_click) >= 3: win = check_weather_win(game_player_click) playernumber_str = str(playernumber) if win == True: stddraw.clear() stddraw.setFontSize(30) stddraw.text(1.5, 2.5, "player" + playernumber_str + " win !") stddraw.text(1.5, 1.5, "please close you window !") game_win = False stddraw.show() return game_win
def display(self): # clear the background canvas to empty_cell_color stddraw.clear(self.empty_cell_color) # draw the game grid self.draw_grid() # draw the current (active) tetromino if self.current_tetromino != None: self.current_tetromino.draw() # draw a box around the game grid self.draw_boundaries() # show the resulting drawing with a pause duration = 250 ms stddraw.show(250)
def draw_new_things(game_array,score): stddraw.clear() stddraw.setFontSize(30) stddraw.text(2.2, 9.2, "Your current Score:") stddraw.text(9, 8, "Remaining Moves") score1 = str(score) stddraw.text(5.5, 9.2, score1) draw_a_circle(game_array) draw_a_inclined(game_array) draw_a_square(game_array) draw_a_regular_triangle(game_array) draw_a_pentagon(game_array) draw_a_parallelogram(game_array)
def main(): n = int(sys.argv[1]) # number of biased coin flips per trial p = float(sys.argv[2]) # heads with probability p trialCount = int(sys.argv[3]) # number of trials histogram = Histogram(n + 1) for trial in range(trialCount): heads = stdrandom.binomial(n, p) histogram.addDataPoint(heads) stddraw.setCanvasSize(500, 200) stddraw.clear(stddraw.LIGHT_GRAY) histogram.draw() stddraw.show()
def main(): n = int(sys.argv[1]) p = float(sys.argv[2]) trials = int(sys.argv[3]) for i in range(trials): isOpen = percolationio.random(n, p) stddraw.clear() stddraw.setPenColor(stddraw.BLACK) percolationio.draw(isOpen, False) stddraw.setPenColor(stddraw.BLUE) full = percolation.flow(isOpen) percolationio.draw(full, True) stddraw.show(1000.0) stddraw.show()
def main(argv): n = int(argv[1]) p = float(argv[2]) t = int(argv[3]) stddraw.createWindow() for i in range(t): open = percolation.random(n, p) stddraw.clear() stddraw.setPenColor(stddraw.BLACK) percolation.show(open, False) stddraw.setPenColor(stddraw.BLUE) full = percolation.flow(open) percolation.show(full, True) stddraw.sleep(1000) stddraw.show() stddraw.wait()
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()
def main(argv): lamb = float(argv[1]) # Arrival rate mu = float(argv[2]) # Service rate hist = histogram.Histogram(60 + 1) q = linkedlistqueue.Queue() stddraw.createWindow(700, 500) nextArrival = stdrandom.exp(lamb) # Time of next arrival nextService = nextArrival + 1.0/mu # Time of next completed service # Simulate the M/D/1 queue while True: # Next event is an arrival. while nextArrival < nextService: # Simulate an arrival q.enqueue(nextArrival) nextArrival += stdrandom.exp(lamb) # Next event is a service completion. arrival = q.dequeue() wait = nextService - arrival # Update the histogram. stddraw.clear() hist.addDataPoint(min([60, int(wait+0.5)])) hist.draw() #stddraw.sleep(20) stddraw.sleep(20) stddraw.show() # Update the queue. if q.isEmpty(): nextService = nextArrival + 1.0/mu else: nextService = nextService + 1.0/mu
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
import stddraw # Draw a bouncing ball to standard draw. RADIUS = .05 DT = 20.0 stddraw.setXscale(-1.0, 1.0) stddraw.setYscale(-1.0, 1.0) rx = .480 ry = .860 vx = .015 vy = .023 while True: # 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.GRAY) stddraw.filledCircle(rx, ry, RADIUS) stddraw.show(0)
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 stddraw.clear(); for k in range(n): stddraw.line(k, 0, k, freq[k]) stddraw.show() # Print page ranks. for i in range(n): stdio.writef("%8.5f", float(freq[i]) / float(t)) stdio.writeln() stddraw.wait() #----------------------------------------------------------------------- # Example executions: # # python transition.py < tiny.txt | python randomsurferhistogram.py 1000000
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)
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