def showScore(score, x=0): stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(-350, 450, 700, 100) score = "Score: " + str(score) stddraw.setFontSize(60) stddraw.setPenColor(stddraw.BLACK) stddraw.text(-210 + x, 500, score)
def draw_score(self, score): stddraw.setPenColor(self.boundary_color) # using boundary_color # set the pen radius stddraw.setPenRadius(self.box_thickness) # coordinates of the bottom left corner of the game grid pos_x, pos_y = self.grid_width - 0.5, -0.5 stddraw.rectangle(pos_x, pos_y, 3.5, self.grid_height) stddraw.setPenColor(Color(167, 160, 151)) stddraw.filledRectangle(pos_x + 0.03, pos_y + 0.09, 3.4, self.grid_height - 0.2) # set the text text_color = Color(0, 0, 0) stddraw.setFontFamily("Arial") stddraw.setFontSize(30) stddraw.setPenColor(text_color) text_to_display = "SCORE" text_to_display2 = "NEXT" stddraw.text(12 + 1.2, 5, text_to_display2) stddraw.text(self.grid_width + 1.2, 15, text_to_display) stddraw.text(self.grid_width + 1.2, 14, str(score)) # get the tetromino's type to create next tetromino to show in the next section tet_type = self.current_tetromino.get_type() if tet_type == 'I': width = 4 height = 11 elif tet_type == 'O': width = 2 height = 12 else: width = 3 height = 11 next_tetromino = Tetromino(tet_type, height, width) next_tetromino.draw_next_tet(self.current_tetromino) stddraw.setPenRadius() # reset the pen radius to its default value
def score(score): stddraw.setPenColor(stddraw.WHITE) stddraw.setFontSize(26) stddraw.filledRectangle(7.2, -2.5, 2, 1) stddraw.setPenColor(stddraw.RED) stddraw.text(8.25, -1.75, "SCORE:") stddraw.text(8.25, -2.25, str(score) + "/1000")
def wrong_guesses(i): stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(-1, -1, 2, 0.2) stddraw.setPenColor(stddraw.RED) stddraw.setFontSize(40) stddraw.text(-0.2,-0.9,i) stddraw.show(0)
def plotBars(a): """ Plot the values of array 'a' as bars. """ N = len(a) stddraw.setXscale(0, N - 1) for i in range(N): stddraw.filledRectangle(i, a[i] / 2, .25, a[i])
def plotBars(a): """ Plot the elements of array a as bars. """ n = len(a) stddraw.setXscale(-1, n) for i in range(n): stddraw.filledRectangle(i - 0.25, 0.0, 0.5, a[i])
def draw_turn(turn_number): stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(0, 0.96, 80, 20) stddraw.setPenColor(stddraw.BLACK) if turn_number % 2 != 0: stddraw.text((1 / 3) / 2, 0.98, "Player 1's Turn") else: stddraw.text((1 / 3) / 2, 0.98, "Player 2's Turn")
def plotBars(a): """ Plot the elements of array a as bars. """ n = len(a) stddraw.setXscale(-1, n) for i in range(n): stddraw.filledRectangle(i-0.25, 0.0, 0.5, a[i])
def white_key(x, y, is_pressed): stddraw.setPenColor(stddraw.BLACK) stddraw.rectangle(x, y, 48, 300) if is_pressed: stddraw.setPenColor(stddraw.RED) else: stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(x, y, 48, 300)
def draw_heart(): stddraw.setPenColor(stddraw.RED) stddraw.filledCircle(2.5, 7.5, 2.5) stddraw.filledCircle(7.5, 7.5, 2.5) stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(0, 0, 10, 7.5) stddraw.setPenColor(stddraw.RED) stddraw.filledPolygon([5, 10, 0], [0, 7.5, 7.5])
def plotBars(a): """ Plot the values of array 'a' as bars. """ N = len(a) stddraw.setXscale(0, N-1) for i in range(N): stddraw.filledRectangle(i, a[i]/2, .25, a[i])
def goal(): stddraw.setPenColor(stddraw.WHITE) stddraw.setFontSize(26) stddraw.filledRectangle(7.2, -3.75, 2, 1.3) stddraw.setPenColor(stddraw.ORANGE) stddraw.text(8.25, -3, "GOAL:") stddraw.setFontSize(16) stddraw.text(8, -3.5, "Score 1000 points") stddraw.text(8, -3.75, "within 1 minute!")
def drawWinCondition(): stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(0.03, 4, 4.65, 2.97) stddraw.setFontSize(50) stddraw.setPenColor(stddraw.MAGENTA) stddraw.text(2.25, 5.75, 'YOU WIN!') stddraw.setFontSize(25) stddraw.text(2.25, 4.75, 'Click to exit') stddraw.show(0)
def drawLossCondition(): stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(0.03, 4, 4.65, 2.97) stddraw.setFontSize(50) stddraw.setPenColor(stddraw.RED) stddraw.text(2.25, 5.75, 'YOU LOSE!') stddraw.setFontSize(25) stddraw.text(2.25, 4.75, 'Click to exit') stddraw.show(0)
def _draw_bars(array, barcolor): """ @param array: Array with points @param barcolor: Color of bars Draws a rectangle in a given color with a width of 0.6 from the x axis to a given point (TO BE USED FOR draw_data_random() FUNCTION) """ stddraw.setPenColor(barcolor) for point in array: yStart = 0 if point[1] > 0 else point[1] # Bar starts at y=0 if y is positive and at y if y is negative stddraw.filledRectangle(point[0] - 0.3, yStart, 0.6, abs(point[1]))
def drawbcs(p): stddraw.setXscale(-1, 100) stddraw.setYscale(0, 5) x = 1 stddraw.filledRectangle(0, 0, 0.5, 2) #左护栏 for t in p: print(t) drawbc(x, t) x += 5 stddraw.filledRectangle(x, 0, 0.5, 2) #右护栏 stddraw.show()
def tie_game(x,y): stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(0,0,1,.09) stddraw.setPenColor(stddraw.PINK) stddraw.text(0.5,.0625, "It's a tie!") stddraw.text(.8,.0625, "Exit") stddraw.setFontSize(24) if x>.6 and x <1: if y > 0 and y <.125: sys.exit() stddraw.show(1000)
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 player_two_win(x,y): stddraw.setFontSize(36) stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(0,0,1,.09) stddraw.setPenColor(stddraw.PINK) stddraw.text(0.5,.0625,"Player two wins!") stddraw.setFontSize(24) stddraw.text(.8,.0625, "Exit") if x>.6 and x <1: if y > 0 and y <.125: sys.exit() stddraw.show(1000)
def draw_lowest_elevation_path(a, row): # calculates and draws an optimal greedy path across a 2D # array, a, starting at row # print(row) height = len(a) y_pos = row stddraw.setPenColor(stddraw.GREEN) stddraw.filledRectangle(0, height - y_pos, 1, 1) temp = 0 d_elevation = 0 for n in range(1, len(a[row])-1): forward = abs(a[y_pos][n] - a[y_pos][n-1]) if y_pos > 0: forward_up = abs(a[y_pos-1][n] - a[y_pos][n-1]) else: forward_up = find_max(a) if y_pos < len(a)-1: forward_down = abs(a[y_pos+1][n] - a[y_pos][n-1]) else: forward_down = find_max(a) if forward <= forward_up and forward <= forward_down: d_elevation += forward elif forward_up < forward_down: y_pos -= 1 d_elevation += forward_up elif forward_down < forward_up: y_pos += 1 d_elevation += forward_down else: choice = random.randint(0,1) # print(choice) if choice == 0: y_pos -= 1 d_elevation += forward_up else: y_pos += 1 d_elevation += forward_down stddraw.filledRectangle(n, height - y_pos, 1, 1) if d_elevation < temp: print('col: ', n, ' y_pos: ', y_pos) print("CHANGE IN ELEVATION DECREASED") temp = d_elevation # stddraw.show(0) return d_elevation
def draw_boundaries(self): # draw a bounding box around the game grid as a rectangle stddraw.setPenColor(self.boundary_color) # using boundary_color # set the pen radius as box_thickness (half of this thickness is visible # for the bounding box as its lines lie on the boundaries of the canvas) #stddraw.setPenRadius(self.box_thickness) # coordinates of the bottom left corner of the game grid pos_x, pos_y = -0.75, -0.75 stddraw.filledRectangle(pos_x, pos_y, self.grid_width + 0.50, self.grid_height + 0.50) stddraw.setPenColor(self.empty_cell_color) pos_x, pos_y = -0.5, -0.5 stddraw.filledRectangle(pos_x, pos_y, self.grid_width, self.grid_height) stddraw.setPenRadius() # reset the pen radius to its default value
def drawTop(score, time): stddraw.setPenColor(stddraw.DARK_BLUE) stddraw.filledRectangle(0,9,9,2) stddraw.setPenColor(stddraw.WHITE) stddraw.setFontSize(15) stddraw.text(5, 10.3, "SCORE:") stddraw.text(2, 10.3, "TIME REMAINING:") stddraw.setFontSize(40) if time != 9999: stddraw.text(5, 9.8, str(score)+"/1000") stddraw.text(2, 9.8, str(25-int(time))) else: stddraw.text(5, 9.8, str(score)) stddraw.text(2, 9.8, "∞")
def draw_clock(game_start): #https://introcs.cs.princeton.edu/python/15inout/clock.py.html current_time = time.time() time_elapsed = current_time - game_start - 5 timer = '%02d:%02d' % (0, time_elapsed) stddraw.setFontSize(26) stddraw.setPenColor(stddraw.WHITE) stddraw.filledRectangle(7.2, -1.2, 2, 1) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.text(8.25, -.5, "TIME:") stddraw.text(8.25, -1, str(timer)) stddraw.show(1) if time_elapsed >= 60: stddraw.clear() stddraw.setPenColor(stddraw.RED) stddraw.setFontSize(36) stddraw.text(4.5, -4.5, "Time's up! Game over!") stddraw.show(1000) sys.exit()
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 drawNoGridScene(self): """ Draws the current position of the ball and robot """ stddraw.clear() stddraw.setPenColor(stddraw.BLACK) #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.HEIGHT - self.ball_y, self.ball_radius) stddraw.show(0)
def draw_map(a): # draws and displays a map based on a 2D array, a, # using the max and min to set the grayscale amin = find_min(a) amax = find_max(a) stddraw.setXscale(-10, len(a[0]) + 10) stddraw.setYscale(-5, len(a) + 5) stddraw.setCanvasSize(int(512*1.5), int(512*len(a)/len(a[0])*1.5)) m = 255 / (amax - amin) b = -m*amin for i in range(len(a)): for j in range(len(a[i])): value = int(m*a[i][j] + b) # print(a[i][j], value) gray = Color(value, value, value) stddraw.setPenColor(gray) stddraw.filledRectangle(j, len(a) - i, 1, 1)
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 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 visualize_q_table(self, width): qt = sorted(self.q_table.items()) GRID_SIZE_X = width / len(self.actions) GRID_SIZE_Y = width / len(qt) for i in range(0, len(self.q_table.items())): for j in range(0, len(self.actions)): #print(str(qt[i][1][j]) + ' ',end='') current_val = (qt[i][1][j]) sigmoid = 1 / (1 + math.exp(-current_val) ) #get a range between 0 and 1 state_name = qt[i][0] + " - " + str(round(qt[i][1][j], 2)) if current_val < 0: new_color = color.Color(int(sigmoid * 255), 0, 0) elif current_val == 0.0: new_color = color.Color(0, 0, 0) else: new_color = color.Color(0, int(sigmoid * 255), 0) #stddraw.setPenColor(color.BLACK) #stddraw.text((GRID_SIZE_X * i) + (GRID_SIZE/2),(GRID_SIZE * j) + (GRID_SIZE / 2), state_name) stddraw.setPenColor(new_color) stddraw.filledRectangle( (GRID_SIZE_X * j), width - (GRID_SIZE_Y * i) - GRID_SIZE_Y, GRID_SIZE_X, GRID_SIZE_Y) stddraw.setPenColor(color.WHITE) stddraw.text((GRID_SIZE_X * j) + (GRID_SIZE_X / 2), width - (GRID_SIZE_Y * i) - GRID_SIZE_Y + (GRID_SIZE_Y / 2), state_name) stddraw.setPenColor(color.BLACK) #gridlines for i in range(1, len(self.q_table.items())): stddraw.filledRectangle(0, GRID_SIZE_Y * i, width, 0) for i in range(1, len(self.actions)): stddraw.filledRectangle(GRID_SIZE_X * i, 0, 0, width) stddraw.show(0)
def drawScene(goalie_pos, ball_x, ball_y): stddraw.clear() stddraw.setPenColor(stddraw.BLACK) #gridlines for i in range(1, GRID_NUM_HEIGHT): stddraw.filledRectangle(0, GRID_SIZE * i, WIDTH, 0) for i in range(1, GRID_NUM_WIDTH): stddraw.filledRectangle(GRID_SIZE * i, 0, 0, HEIGHT) #goalie stddraw.setPenColor(stddraw.BLUE) stddraw.filledRectangle(goalie_pos * GRID_SIZE, 0, GRID_SIZE, GRID_SIZE) #x,y,size_x,size_y #Ball stddraw.setPenColor(stddraw.GREEN) stddraw.filledCircle(((ball_x * GRID_SIZE) + (GRID_SIZE / 2)), (HEIGHT - ((ball_y * GRID_SIZE) + (GRID_SIZE / 2))), GRID_SIZE / 2)
def drawbc(x, l): for k in range(len(l)): stddraw.filledRectangle(k + x, 0, 0.5, l[k])
import stddraw l = [1,2,1,1,2] stddraw.setXscale(-1,100) stddraw.setYscale(0,5) for k in range(len(l)): stddraw.filledRectangle(k,0,0.5,l[k]) stddraw.show()