def draw(self, pic): ''' Draw this Rectangle on the Picture provided. Use the data attributes of this Rectangle to specify the location, height, width and colour of the resulting rectangle on the image. ''' media.add_rect_filled(pic, self.x, self.y, self.width, self.height, self.col) return pic
def strikethrough(pic): '''(Pic) -> Picture Return a copy of the picture with a black line added horizontally through the middle of the picture. The line's thickness is 10% of the height of the picture. ''' p = media.copy(pic) height_rect = p.get_height() / 10 width_rect = p.get_width() y_upper_left = p.get_height() / 2 x_upper_left = 0 media.add_rect_filled(p, x_upper_left, y_upper_left, width_rect, height_rect, media.black) return p
def update_boards(self, x, y): ''' (self, x, y) -> NoneType Given an x and a y coordinate, turn the cooresponding board co-ordinate red if it is a hit, or white if it is a miss. ''' # tuple holding coordinates of attack coordinates = (x, y) # If attack coordinates correspond to a point at which # a ship is occupying on the opponent board, draw a red square. # if it doesn't, draw a white square. if coordinates not in self.opp_list: media.add_rect_filled(self.boards[1], x * 25 + 10,\ y * 25 + 10, 25, 25, media.white) else: media.add_rect_filled(self.boards[1], x * 25 + 10,\ y * 25 + 10, 25, 25, media.red) # Draw player's board using opponent's last move. # A white block is drawn if they missed, a red block if they hit. if self.attack != []: if self.attack[-1] not in self.coordinates: media.add_rect_filled(self.boards[0], self.attack[-1][0] * 25\ + 10, self.attack[-1][1] * 25 + 10, 25,\ 25, media.white) else: media.add_rect_filled(self.boards[0], self.attack[-1][0] * 25\ + 10, self.attack[-1][1] * 25 + 10, 25,\ 25, media.red) media.show(self.boards[0]) media.show(self.boards[1])
def process_user_command(game_tree, target_tree, curr, pic): '''Read and process one command from the user, modifying BTNode game_tree and current BTNode curr as appropriate and redrawing the new game_tree and BTNode target_tree on Picture pic. Return the new value of curr.''' d = {'Left': 'l', 'Right': 'r', 'Up': 'u', 'a': 'L', 's': 'R', 'q': 'q'} cmd = d.get(KD.moving_by_keys().key, 'm') # Only listen to valid commands. if len(cmd) != 1 or cmd[0] not in 'qulrLR': return curr # Erase the old tree and redraw target_tree halfway across the window. media.add_rect_filled(pic, 0, 0, WIDTH, HEIGHT, media.white) draw(pic, target_tree.root, 0, WIDTH / 2, curr) # Process user commands. if cmd == 'q': media.close(pic) sys.exit(0) elif cmd == 'u' and curr != None and curr.parent != None: curr = curr.parent elif cmd == 'l' and curr.left != None: curr = curr.left elif cmd == 'r' and curr.right != None: curr = curr.right elif cmd == 'L' and curr.right != None: curr = bst.rotate_left(game_tree, curr) elif cmd == 'R' and curr.left != None: curr = bst.rotate_right(game_tree, curr) # The parent attribute of the nodes of the new tree must be corrected. # If curr is at the top, a rotation may have moved it there. Set the # game_tree root to curr if that happened. if curr.parent == None: game_tree.root = curr # Draw the new game tree. draw(pic, game_tree.root, 0, 0, curr) media.update(pic) return curr
def play_game(tree_size): '''Play the rotation game with tree_size nodes in the tree.''' # Blank the screen and make the trees. media.add_rect_filled(pic, 0, 0, 400, 400, media.white) game_tree = random_tree(tree_size) target_tree = random_tree(tree_size) # Draw the new trees; the current node is initially the root. curr = game_tree.root draw(pic, game_tree.root, 0, 0, curr) draw(pic, target_tree.root, 0, WIDTH / 2, curr) media.show(pic) # Ask for user operations until the game tree and target tree are the # same. while game_tree != target_tree: curr = process_user_command(game_tree, target_tree, curr, pic) assert bst.is_valid_tree(game_tree.root)
def process_user_command(game_tree, target_tree, curr, pic): '''Read and process one command from the user, modifying BTNode game_tree and current BTNode curr as appropriate and redrawing the new game_tree and BTNode target_tree on Picture pic. Return the new value of curr.''' cmd = raw_input(prompt) # Only listen to valid commands. while cmd not in 'shqulrLR': cmd = raw_input(prompt) # Erase the old tree and redraw target_tree halfway across the window. media.add_rect_filled(pic, 0, 0, WIDTH, HEIGHT, media.white) draw(pic, target_tree.root, 0, WIDTH / 2, curr) # Process user commands. if cmd == 'q': media.close(pic) return None elif cmd == 'h': solver.help_solve(game_tree, target_tree) elif cmd== 's': while game_tree != target_tree: solver.help_solve(game_tree, target_tree) elif cmd == 'u' and curr != None and curr.parent != None: curr = curr.parent elif cmd == 'l' and curr.left != None: curr = curr.left elif cmd == 'r' and curr.right != None: curr = curr.right elif cmd == 'L' and curr.right != None: curr = bst.rotate_left(game_tree, curr) elif cmd == 'R' and curr.left != None: curr = bst.rotate_right(game_tree, curr) # Draw the new game tree. draw(pic, game_tree.root, 0, 0, curr) media.update(pic) return curr
def draw_board(self): '''(self) -> NoneType draw player board and opponent board and return in list''' # Each space will be 25 pixels long, with a 10 pixel border on top board_l = self.l * 25 + 10 # Each space will be 25 pixels wide, with a 10 pixel border on the side board_w = self.w * 25 + 10 list_boards = [] # list holding boards # Draw player's board board_player = media.create_picture(board_w, board_l, media.navy) # Draw opponent's board board_opponent = media.create_picture(board_w, board_l, media.gray) list_boards.append(board_player) # Add player board to list list_boards.append(board_opponent) # Add opponent board to list for item in list_boards: # loop through list of boards to add borders # Draw bar along top media.add_rect_filled(item, 0, 0, 10, board_l, media.white) media.add_rect_filled(item, 0, 0, board_w, 10, media.white) counter = 0 # counter for while loop to draw numbers to board while counter < self.w: # loop to draw x coordinates on board temp = str(counter) x_val = (counter) * 25 + 10 media.add_text(item, x_val, 2, temp, media.black) counter += 1 counter = 0 while counter < self.l: # draw y coordinates on board temp = str(counter) y_val = (counter) * 25 + 10 media.add_text(item, 2, y_val, temp, media.black) counter += 1 # Loop through list of player ships to place them on board for item in self.coordinates: media.add_rect_filled(list_boards[0], item[0] * 25 + 10,\ item[1] * 25 + 10, 25, 25, media.gray) media.show(list_boards[0]) media.show(list_boards[1]) self.boards = list_boards
# get 2 random numbers between 0 and 99 to use as coordinates x = random.randint(0, 99) y = random.randint(0, 99) # get the pixel at this x,y coordinate pix = media.get_pixel(pic, x, y) # get the red, blue and green values of this pixel red = media.get_red(pix) green = media.get_green(pix) blue = media.get_blue(pix) # introduce a new colour new_color = media.orange # make a 10 x 10 rectangle of the new colour inside our # picture, starting with our x and y as the upper # left corner. (In this case, it doesn't matter if some # of the rectangle is outside the picture, as long as # the x,y corner is inside.) media.add_rect_filled(pic, x, y, 10, 10, new_color) # display the picture media.show(pic) # the colours should have changed in our pixel red = media.get_red(pix) green = media.get_green(pix) blue = media.get_blue(pix)