def test_Card_Guess_Tree(): """ Function that tests all the base functions of implementing a Card_Guess_Tree: creating the treem adding new cards, and extracting cards with a memory limit. """ test_tree = Card_Guess_Tree("test", 3) card1 = card_g = game_classes.Card("g_1", "small_cards/green_0.png", None) card2 = card_g = game_classes.Card("g_2", "small_cards/green_0.png", None) card3 = card_g = game_classes.Card("r_r", "small_cards/green_0.png", None) test_tree.update_card_tree(card3) test_tree.update_card_tree(card2) test_tree.update_card_tree(card1) test_tree.update_card_tree(card3) test_tree.update_card_tree(card2) test_tree.update_card_tree(card1) test_tree.update_card_tree(card3) test_tree.update_card_tree(card2) test_tree.update_card_tree(card1) test_tree.update_card_tree(card3) test_tree.update_card_tree(card2) test_tree.update_card_tree(card1) print(travel_Card_Guess_Tree(test_tree.Guess_Tree, 3)) print(travel_Card_Guess_Tree(test_tree.Guess_Tree, 4)) print(travel_Card_Guess_Tree(test_tree.Guess_Tree, 5)) print(travel_Card_Guess_Tree(test_tree.Guess_Tree, 6)) print(travel_Card_Guess_Tree(test_tree.Guess_Tree, 7))
def draw_winners(winners): """ Function that draws the winners in win placement from left to right. Left being the first winner and right being last place. O(n) runtime where n is the size of the list winners """ # clear screen (top half) screen.fill(black) # get a "middle" start postion for bliting cards start_pos = ((screen_width) // 2) - \ (200 * (len(winners) - 1) + card_width) // 2 target_index = 0 for player in winners: # O(n) player_num = str(player.name[7]) card_disp = game_classes.Card( "red", "small_cards/green_" + player_num + ".png", None) card_disp.rect = def_rect card_disp.rect = card_disp.rect.move(start_pos, 300) card_disp.rect = card_disp.rect.move(200 * target_index, 0) scale_card_blit(card_disp.card_data, card_disp.rect) target_index += 1 # refresh the screen pygame.display.flip()
def redraw_hand_nonvisble(player, start_horz, start_vert=0): """ Draws a players hand to be non-visible (face down cards). O(n) runtime where n is the size of the players hand """ # placeholder player num graphics player_num = str(player.name[7]) card_disp = game_classes.Card("red", "small_cards/red_" + player_num + ".png", None) card_disp.rect = card_disp.rect.move(start_horz, start_vert) # dynamic card spacing player_handsize = len(player.hand) if player_handsize <= 7: iterating_fact = 80 else: iterating_fact = 550 // player_handsize card_index = 0 for card in player.hand: # O(n) card.rect = def_rect card.rect = card.rect.move(start_horz, start_vert) card.rect = card.rect.move(iterating_fact * card_index, 0) scale_card_blit(face_down_card.card_data, card.rect) card_index += 1 # displaying the placeholder player num graphics scale_card_blit(card_disp.card_data, card_disp.rect, True)
def generate_cards(): """ Generate one set of all uno cards by iterating through all the possible subtrings that relate to the filepath of the cards image in small_cards. This method thus limits to only having the PY-UNO game run within its own containing folder. Returns: An "ordered" list of all cards that are possible to create with the images within small_cards. Cards are defined by the Card class in game_classes. O(m*n) runtime where n is the number of colors and m is the number of types. However, if the card game requires a small vairance of cards the impact of function is greatly reduced """ cards = [] # predefined cards substrings colors = ["blue_", "red_", "green_", "yellow_"] colors_name = ["b", "r", "g", "y"] card_type = [ "picker", "skip", "reverse", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ] # make predefined cards for i in range(len(colors)): # O(m*n) for ct in card_type: filename_str = "small_cards/" + colors[i] + ct + ".png" name_str = colors_name[i] + "_" + ct cards.append(game_classes.Card(name_str, filename_str, None)) # make four wild pick 4 cards.append( game_classes.Card("w_d1", "small_cards/wild_pick_four.png", None)) cards.append( game_classes.Card("w_d2", "small_cards/wild_pick_four.png", None)) cards.append( game_classes.Card("w_d3", "small_cards/wild_pick_four.png", None)) cards.append( game_classes.Card("w_d4", "small_cards/wild_pick_four.png", None)) # make four wild color cards.append( game_classes.Card("w_c1", "small_cards/wild_color_changer.png", None)) cards.append( game_classes.Card("w_c2", "small_cards/wild_color_changer.png", None)) cards.append( game_classes.Card("w_c3", "small_cards/wild_color_changer.png", None)) cards.append( game_classes.Card("w_c4", "small_cards/wild_color_changer.png", None)) return cards
def redraw_screen_menu_color(selected=None): """ Draws a simple color menu with placeholder graphics. Function clears the top half of the screen and clears display of nonvisible hands while it runs. O(1) runtime as the number of colors is 4 thus the for loop only runs 4 times thus being negligible """ # zero input catch if selected is None: selected = 0 # clear screen pygame.draw.rect(screen, black, (0, 0, screen_width, int(600 * scale_y)), 0) # get a "middle" start postion for bliting cards start_pos = ((screen_width) // 2) - ((300 * 2 + card_width) // 2) # placeholders for color slection graphics card_g = game_classes.Card("green", "small_cards/green_0.png", None) card_b = game_classes.Card("blue", "small_cards/blue_0.png", None) card_y = game_classes.Card("yellow", "small_cards/yellow_0.png", None) card_r = game_classes.Card("red", "small_cards/red_0.png", None) color_array = [card_g, card_b, card_y, card_r] color_index = 0 for card_c in color_array: # O(4) card_c.rect = def_rect if color_index == selected: card_c.rect = card_c.rect.move(start_pos, 200) else: card_c.rect = card_c.rect.move(start_pos, 300) card_c.rect = card_c.rect.move(200 * color_index, 0) scale_card_blit(card_c.card_data, card_c.rect) color_index += 1 # refresh the screen pygame.display.flip()
def redraw_screen_menu_target(players, selected=None): """ Draws a simple menu with placeholder graphics (red number cards) that refrences a target player to use a card effect on. Thus function clears the top half of the screen and clears display of nonvisible hands while it runs. O(n) runtime where n is the number of players that can be a proper target """ # zero input catch if selected is None: selected = 0 # clear screen (top half) pygame.draw.rect(screen, black, (0, 0, screen_width, int(600 * scale_y)), 0) # get a "middle" start postion for bliting cards start_pos = ((screen_width) // 2) - \ (200 * (len(players) - 1) + card_width) // 2 target_index = 0 for player in players: # O(n) player_num = str(player.name[7]) card_disp = game_classes.Card("red", "small_cards/red_" + player_num + ".png", None) card_disp.rect = def_rect if target_index == selected: card_disp.rect = card_disp.rect.move(start_pos, 200) else: card_disp.rect = card_disp.rect.move(start_pos, 300) card_disp.rect = card_disp.rect.move(200 * target_index, 0) scale_card_blit(card_disp.card_data, card_disp.rect) target_index += 1 # refresh the screen pygame.display.flip()
def redraw_hand_visble(player, selected=None): """ Redraws a players hand to be face up. O(n) runtime where n is the size of the players hand """ # player playing indicator placeholder graphic player_num = str(player.name[7]) card_disp = game_classes.Card("red", "small_cards/red_" + player_num + ".png", None) card_disp.rect = card_disp.rect.move(0, screen_height - card_height) # dynamic card spacing player_handsize = len(player.hand) if player_handsize <= 9: iterating_fact = 100 else: iterating_fact = (3 * (screen_width // 4)) // player_handsize # get a "middle" start postion for bliting cards start_pos = (screen_width - 100 * len(player.hand)) // 2 if start_pos < 150: start_pos = 150 card_index = 0 for card in player.hand: # O(n) card.rect = def_rect if card_index == selected: card.rect = card.rect.move(start_pos, 600) else: card.rect = card.rect.move(start_pos, 700) card.rect = card.rect.move(iterating_fact * card_index, 0) scale_card_blit(card.card_data, card.rect) card_index += 1 # displaying the placeholder playing player number scale_card_blit(card_disp.card_data, card_disp.rect)
# scaling factors that are initially set the scale value of 1 (native to # 1600x900 pixel resolution) scale_x = 1 scale_y = 1 # defining the global pygame screen value to be used within PY-UNO screen = pygame.display.set_mode(size_o, HWSURFACE | DOUBLEBUF | RESIZABLE) screen.fill(black) # default card rectangle size and card size and default card pygame rectangle card_width = 130 card_height = 182 def_rect = pygame.Rect(0, 0, card_width, card_height) # defining the default facedown card value face_down_card = game_classes.Card("face_down", "small_cards/card_back.png", None) def handle_resize(event): """ Small function that is called when the PY-GAME window is resized. Functinon updates the scale_x and scale_y globals for easy resizing display functionality. O(1) runtime """ # grabbing the newely resized windows size scale_size = event.dict['size'] # save these scaling factors globally as they affect the global screen