def main(argv): fileName = argv[1] w = int(argv[2]) h = int(argv[3]) source = picture.Picture() source.load(fileName) target = picture.Picture(w, h) for ti in range(w): for tj in range(h): si = ti * source.width() // w sj = tj * source.height() // h target.set(ti, tj, source.get(si, sj)) maxHeight = max(source.height(), target.height()) stddraw.createWindow(source.width() + target.width(), maxHeight) stddraw.setXscale(0, source.width() + target.width()) stddraw.setYscale(0, maxHeight) stddraw.picture(source, source.width() / 2, maxHeight / 2) stddraw.picture(target, source.width() + target.width() / 2, maxHeight / 2) stddraw.show() stddraw.wait()
def main_menu(): stddraw.picture(images.background, 250, 400) stddraw.picture(images.welcome, 250, 680) stddraw.picture(images.button, 250, 420) stddraw.picture(images.mario, 130, 150) stddraw.picture(images.luigi, 370, 150) stddraw.show(0.0)
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 slideOne(source,target,n,w,h): pic = Picture(w,h) for t in range(n): for col in range(w): for row in range(h): c0 = source.get(col, row) cn = target.get(col, row) alpha = float(t) / float(n) c = blend(c0, cn, alpha) pic.set(col, row, c) stddraw.picture(pic) stddraw.show(2000.0/n) #2秒一张为2000/n
def slideShow(p,n,w,h): l = len(p) #创建黑色图像 blackP = Picture(w,h) for col in range(w): for row in range(h): blackP.set(col,row,color.BLACK) for i in range(l-1): slideOne(p[i],blackP,n,w,h) slideOne(blackP,p[i+1],n,w,h) stddraw.picture(p[l-1]) stddraw.show()
def main_menu_click(): global Clicked while not Clicked: if stddraw.mousePressed(): mx = stddraw.mouseX() my = stddraw.mouseY() # determines where and what to print if 446 > mx > 54 and 539 > my > 301: stddraw.picture(images.gameBackground,250,400) stddraw.picture(images.scoreOverlay,327,725) stddraw.setFontSize(25) stddraw.setPenColor(color.WHITE) stddraw.text(327,725, '0') Clicked = True stddraw.show(0.0)
def finish_game(grid_height, grid_width): # colors used for the menu background_color = Color(28, 27, 36) # clear the background canvas to background_color stddraw.clear(background_color) # get the directory in which this python code file is placed current_dir = os.path.dirname(os.path.realpath(__file__)) # path of the image file img_file = current_dir + "/game_over.png" # center coordinates to display the image img_center_x, img_center_y = (grid_width + 2) / 2, grid_height - 10 # image is represented using the Picture class image_to_display = Picture(img_file) # display the image stddraw.picture(image_to_display, img_center_x, img_center_y) stddraw.show(1000)
def display_game_over(grid_height, grid_width): background_color = Color(42, 69, 99) button_color = Color(25, 255, 228) text_color = Color(31, 160, 239) # clear the background canvas to background_color stddraw.clear(background_color) # get the directory in which this python code file is placed current_dir = os.path.dirname(os.path.realpath(__file__)) # path of the image file img_file = current_dir + "/menu_image.png" # center coordinates to display the image img_center_x, img_center_y = (grid_width - 1) / 2, grid_height - 7 # image is represented using the Picture class image_to_display = Picture(img_file) # display the image stddraw.picture(image_to_display, img_center_x, img_center_y) # dimensions of the start game button button_w, button_h = grid_width - 1.5, 2 # coordinates of the bottom left corner of the start game button button_blc_x, button_blc_y = img_center_x - button_w / 2, 4 # display the start game button as a filled rectangle stddraw.setPenColor(button_color) stddraw.filledRectangle(button_blc_x, button_blc_y, button_w, button_h) # display the text on the start game button stddraw.setFontFamily("Arial") stddraw.setFontSize(25) stddraw.setPenColor(text_color) text_to_display = "Game Over" stddraw.text(img_center_x, 5.5, text_to_display) stddraw.text(img_center_x, 4.5, "Score : " + str(grid.total_score)) # menu interaction loop while True: # display the menu and wait for a short time (50 ms) stddraw.show(50) # check if the mouse has been left-clicked if stddraw.mousePressed(): # get the x and y coordinates of the location at which the mouse has # most recently been left-clicked mouse_x, mouse_y = stddraw.mouseX(), stddraw.mouseY() if mouse_x >= button_blc_x and mouse_x <= button_blc_x + button_w: if mouse_y >= button_blc_y and mouse_y <= button_blc_y + button_h: return True
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 dog(x, y): stddraw.picture(picture.Picture("dog.jpg"), x, y)
bodies = self._bodies[:] net_force = Vector([0, 0]) bodies.remove(b) for other in bodies: net_force = net_force + b.force_from(other) net_forces.append(net_force) for i in range(len(self._bodies)): self._bodies[i].move(net_forces[i], dt) def draw(self): for b in self._bodies: b.draw() # some options: 8star-rotation.txt, binaryStars.txt, planets.txt, or anything in space_files # space_files data provided by Robert Sedgewick, Kevin Wayne, and Robert Dondero from their book Introduction to Programming with Python universe = Universe('space_files\\binaryStars.txt') # adjust dt to adjust speed of simulation t = 0 dt = 10000 T = 100000 while True: universe.increase_time(dt) stddraw.clear() stddraw.picture(Picture('space_files\\starfield.jpg'), 0, 0) universe.draw() stddraw.show(1) t += dt
def image(background): if background == 1: jim = Picture('jimmy.jpg') stddraw.picture(jim) if background == 2: jim = Picture('a.png') stddraw.picture(jim) if background == 3: jim = Picture('b.png') stddraw.picture(jim) if background == 4: jim = Picture('c.png') stddraw.picture(jim) if background == 5: jim = Picture('d.png') stddraw.picture(jim) if background == 6: jim = Picture('e.png') stddraw.picture(jim) if background == 7: jim = Picture('f.png') stddraw.picture(jim) if background == 8: jim = Picture('g.png') stddraw.picture(jim) if background == 9: jim = Picture('h.png') stddraw.picture(jim) if background == 10: jim = Picture('i.png') stddraw.picture(jim) if background == 11: jim = Picture('j.png') stddraw.picture(jim) if background >= 12: jim = Picture('k.png') stddraw.picture(jim)
def draw(self, x, y): """ draws the Jewel onto the screen at location (x,y) """ image = picture.Picture(self.image) sd.picture(image, x+.5, y+.5)
def pika(x, y): stddraw.picture(picture.Picture("pika.jpg"), x, y)
def guy(x, y): stddraw.picture(picture.Picture("guy.jpg"), x, y)
def pic1(x, y): stddraw.picture(picture.Picture("pic1.png"), x, y) stddraw.show(0.0)
picture data type(from booksite library) - 2d array of colors Creating data types define an Api for the data type for a stopwatch constructor stopwatch() start timing s.start() reset to zero s.zero() class stopwatch turtle graphics - turtle tobot for drawing -very simple API -create a turtle at (x,y) -tell turtle to turn left by some angle - go forward , drawing a line along the way """ from picture import Picture import stddraw my_picture = Picture("karel1.jpg") stddraw.picture(my_picture, 0.5, 0.5) stddraw.setFontSize(64) stddraw.text(0.5, 0.2, "I live !") stddraw.show()
def draw(self): location = 'space_files\\' + self._name stddraw.picture(Picture(location), self._r[0], self._r[1]) stddraw.point(self._r[0], self._r[1])
def sonny(x, y): stddraw.picture(picture.Picture("sonny.jpg"), x, y)
# and the waved image. pic1 = Picture(sys.argv[1]) width = pic1.width() height = pic1.height() pic2 = Picture(width, height) # Apply the wave filter. for col in range(width): for row in range(height): cc = col rr = int(row + 20.0 * math.sin(col * 2.0 * math.pi / 64.0)) if (rr >= 0) and (rr < height): pic2.set(col, row, pic1.get(cc, rr)) stddraw.setCanvasSize(width, height) stddraw.picture(pic2) stddraw.show() #----------------------------------------------------------------------- # python wave.py mandrill.jpg # python wave.py mandrill.png # python wave.py darwin.jpg # python wave.py darwin.png
def roblox(x, y): stddraw.picture(picture.Picture("roblox.png"), x, y)
def picwhite(x, y): stddraw.picture(picture.Picture("picwhite.png"), x, y) stddraw.show(0.0)
def trash(x, y): stddraw.picture(picture.Picture("trash.jpg"), x, y)
MAX_GRAY_SCALE = 255 p = Picture() stddraw.setCanvasSize(p.width(),p.height()) for t in range(100): # Compute the picture p. for col in range(p.width()): for row in range(p.height()): # Compute pixel color. x = 1.0 * col / p.width() y = 1.0 * row / p.height() v = 0.0 for i in range(3): v += a[i].potentialAt(x, y) v = (MAX_GRAY_SCALE / 2.0) + (v / 2.0e10) if v < 0: grayScale = 0 elif v > MAX_GRAY_SCALE: grayScale = MAX_GRAY_SCALE else: grayScale = int(v) color = Color(grayScale, grayScale, grayScale) p.set(col, p.height()-1-row, color) stddraw.clear() stddraw.picture(p) stddraw.show(0) a[1].increaseCharge(-2.0) #print(a[1]._q)
p = 'me.jpg' source = Picture(p) #压缩或放大图片到w*h def scale(source, w, h): target = Picture(w, h) for tCol in range(w): for tRow in range(h): sCol = tCol * source.width() // w sRow = tRow * source.height() // h target.set(tCol, tRow, source.get(sCol, sRow)) return target #缩小后的图片 scPicture = scale(source, w, h) #大图片,也就是m*n的小图片的集合 TaPicture = Picture(w * m, n * h) for i in range(m): for j in range(n): for col in range(w): for row in range(h): TaPicture.set(i * w + col, j * h + row, scPicture.get(col, row)) stddraw.picture(TaPicture) stddraw.show()
pic = Picture() for col in range(pic.width()): for row in range(pic.height()): # Compute pixel color. x = 1.0 * col / pic.width() y = 1.0 * row / pic.height() v = 0.0 for i in range(n): v += charges[i].potentialAt(x, y) v = (MAX_GRAY_SCALE / 2.0) + (v / 2.0e10) if v < 0: grayScale = 0 elif v > MAX_GRAY_SCALE: grayScale = MAX_GRAY_SCALE else: grayScale = int(v) color = Color(grayScale, grayScale, grayScale) pic.set(col, pic.height()-1-row, color) # Draw the Picture. stddraw.setCanvasSize(pic.width(), pic.height()) stddraw.picture(pic) stddraw.show() #----------------------------------------------------------------------- # python potential.py < charges.txt
import sys import stddraw import luminance from picture import Picture pic = Picture(sys.argv[1]) for col in range(pic.width()): for row in range(pic.height()): pixel = pic.get(col, row) gray = luminance.toGray(pixel) pic.set(col, row, gray) stddraw.setCanvasSize(pic.width(), pic.height()) stddraw.picture(pic) stddraw.show()
fileName = sys.argv[1] w = int(sys.argv[2]) h = int(sys.argv[3]) source = Picture(fileName) target = Picture(w, h) for tCol in range(w): for tRow in range(h): sCol = tCol * source.width() // w sRow = tRow * source.height() // h target.set(tCol, tRow, source.get(sCol, sRow)) stddraw.setCanvasSize(w, h) stddraw.picture(target) stddraw.show() #----------------------------------------------------------------------- # python scale.py mandrill.jpg 800 800 # python scale.py mandrill.jpg 600 300 # python scale.py mandrill.jpg 200 400 # python scale.py mandrill.jpg 200 200 # python scale.py mandrill.png 200 200 # python scale.py darwin.jpg 200 200
def update(): stddraw.setFontFamily("WORDSOFLOVE") stddraw.setFontSize(70) stddraw.picture(pic7, 4.5, 5) stddraw.setFontSize(70) for i in range(9): for j in range(9): if game[i][j] == "strawberry": stddraw.picture(pic1, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "lemon": stddraw.picture(pic2, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "orange": stddraw.picture(pic3, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "pear": stddraw.picture(pic4, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "grapes": stddraw.picture(pic5, (j + 1) * 0.9, i + 0.5) elif game[i][j] == "watermelon": stddraw.picture(pic6, (j + 1) * 0.9, i + 0.5) stddraw.text(1, 9.5, ":Score: ") stddraw.text(4.4, 9.5, ":Moves: ") stddraw.text(8, 9.5, ":Goal: ") stddraw.setFontFamily("Blah blah bang") stddraw.setFontSize(40) stddraw.text(8.7, 9.5, "700") stddraw.text(1.8, 9.5, str(score)) stddraw.text(5.3, 9.5, str(number_of_moves)) if x: stddraw.show(0.0)
def find_match(): hmatches = [] vmatches = [] matches = 0 global score global combo temp_score = score for i in range(9): for j in range(8): if game[i][j] == game[i][j + 1] != "null": matches += 1 if matches == 2: hmatches.append(i) hmatches.append(j - 1) if j == 7 and matches >= 2: hmatches.append(matches + 1) elif matches >= 2: hmatches.append(matches + 1) matches = 0 elif matches < 2: matches = 0 matches = 0 matches = 0 for i in range(9): for j in range(8): if game[j][i] == game[j + 1][i] != "null": matches += 1 if matches == 2: vmatches.append(j - 1) vmatches.append(i) if j == 7 and matches >= 2: vmatches.append(matches + 1) elif matches >= 2: vmatches.append(matches + 1) matches = 0 elif matches < 2: matches = 0 matches = 0 if len(hmatches) > 0: for i in range(1, (len(hmatches) // 3) + 1): if hmatches[(i * 3) - 1] > 2: for j in range(hmatches[(i * 3) - 1]): if x: stddraw.show(60) game[hmatches[i * 3 - 3]][hmatches[i * 3 - 2] + j] = "null" score += 10 * (combo + 2) combo += 1 y_s = (hmatches[i * 3 - 3] + 0.5) x_s = (hmatches[i * 3 - 2] + 1) * 0.9 + 1 delta_y = 9.5 - y_s delta_x = x_s - 1 if x: channel2.play(match) while y_s < 9 and x: x_s -= (delta_x / 10) y_s += (delta_y / 10) stddraw.clear() stddraw.text(1.8, 9.5, str(temp_score)) update_without_score() stddraw.text(1.8, 9.5, str(temp_score)) stddraw.picture(pic9, x_s, y_s) stddraw.show(0) temp_score = score temp_score = score if len(vmatches) > 0: for i in range(1, (len(vmatches) // 3) + 1): for j in range(vmatches[(i * 3) - 1]): if x: stddraw.show(60) game[vmatches[i * 3 - 3] + j][vmatches[i * 3 - 2]] = "null" score += 10 * (combo + 2) combo += 1 y_s = (vmatches[i * 3 - 3] + 1.5) x_s = (vmatches[i * 3 - 2] + 1) * 0.9 delta_y = 9.5 - y_s delta_x = x_s - 1 if x: channel2.play(match) while y_s < 9 and x: x_s -= (delta_x / 10) y_s += (delta_y / 10) stddraw.clear() stddraw.text(1.8, 9.5, str(temp_score)) update_without_score() stddraw.text(1.8, 9.5, str(temp_score)) stddraw.picture(pic9, x_s, y_s) stddraw.show(0) temp_score = score stddraw.clear() update() if x: stddraw.show(5) if len(hmatches) > 0 or len(vmatches) > 0: return True else: return False
mx1 = 2 elif stddraw.mouseX() < 4.0666: mx1 = 3 elif stddraw.mouseX() < 4.9555: mx1 = 4 elif stddraw.mouseX() < 5.8444: mx1 = 5 elif stddraw.mouseX() < 6.7333: mx1 = 6 elif stddraw.mouseX() < 7.6222: mx1 = 7 else: mx1 = 8 my1 = int(stddraw.mouseY()) stddraw.setPenColor(stddraw.RED) stddraw.picture(pic8, (mx1 + 1.01) * 0.9, my1 - 0.02 + 0.5) stddraw.setPenColor(stddraw.MAGENTA) first_click = False stddraw.show(0.0) while second_click: stddraw.show(0.0) if stddraw.mousePressed() and stddraw.mouseY() < 9: if stddraw.mouseX() < 1.4: mx2 = 0 elif stddraw.mouseX() < 2.289: mx2 = 1 elif stddraw.mouseX() < 3.17777: mx2 = 2 elif stddraw.mouseX() < 4.0666: mx2 = 3