Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- DEBUG_accelerate_loss = False
- DEBUG_temp = None
- pygame.init() # initialize modules
- # pygame.font.init()
- # window dimensions
- screen_width = 300
- screen_height = 600
- def horizontal_center(dimension):
- return (screen_width - dimension) // 2
- def vertical_center(dimension):
- return (screen_height - dimension) // 2
- def cursor_in_region(coor_tuple, dim_tuple, mcoor_tuple):
- mx, my = mcoor_tuple[0], mcoor_tuple[1]
- xlo = coor_tuple[0]
- xhi = coor_tuple[0] + dim_tuple[0]
- ylo = coor_tuple[1]
- yhi = coor_tuple[1] + dim_tuple[1]
- boo = xlo <= mx <= xhi and ylo <= my <= yhi
- print(boo)
- return boo
- def render_end(is_game_won):
- end_str = "YOU LOSE!!"
- if is_game_won:
- end_str = "YOU WIN!!"
- game_screen.fill(black)
- # UI post-game display
- attempt = score_font.render("Guesses: {}".format(no_of_guess), True, gray)
- lose_message = game_font.render(end_str, True, gray)
- play_again_message = score_font.render("Play Again", True, gray)
- aw = attempt.get_rect().width
- game_screen.blit(attempt, (horizontal_center(aw), screen_height * (1 / 7)))
- lmw = lose_message.get_rect().width
- game_screen.blit(lose_message, (horizontal_center(lmw), screen_height * (1.5 / 7)))
- pamw = play_again_message.get_rect().width
- pamh = play_again_message.get_rect().height
- pamy = screen_height * (3 / 7)
- pampadding = 25
- pabcoor[0], pabcoor[1] = horizontal_center(pamw) - pampadding, pamy - pampadding
- pabdim[0], pabdim[1] = pamw + 2 * pampadding, pamh + 2 * pampadding
- pygame.draw.rect(game_screen,
- dark_gray, (
- pabcoor[0],
- pabcoor[1],
- pabdim[0],
- pabdim[1]))
- game_screen.blit(play_again_message, (horizontal_center(pamw), pamy))
- def calculate_grid_dimensions(colors, columns=3, cell_size=50, cell_spacing=10):
- row = 0
- grid_x = (cell_size + cell_spacing) * columns
- color = 0
- while color < len(colors):
- col = 0
- while col < columns:
- col += 1
- color += 1
- row += 1
- grid_y = (cell_size + cell_spacing) * row
- return grid_x, grid_y
- def render_color_grid(interface, x, y, colors, columns=3, cell_size=50, cell_spacing=10):
- """
- Renders a 3x3 grid of colors. This function is particular to this program.
- :param interface: Surface wherein the function will write the grid
- :param x: X coordinate
- :param y: Y coordinate
- :param colors: The list of colors to be used
- :param columns: 3 by default. The number of columns in the grid
- :param cell_size: 50 by default. The size of each individual cell in the grid.
- :param cell_spacing: 10 by default. The distance between all cells.
- :return: Tuple containing the x and y dimensions of the grid
- """
- row = 0
- grid_x = (cell_size + cell_spacing) * columns
- color = 0
- while color < len(colors):
- col = 0
- while col < columns:
- pygame.draw.rect(interface,
- colors[color],
- (
- x + (cell_size * col) + (cell_spacing * col),
- y + (cell_size * row) + (cell_spacing * row),
- cell_size,
- cell_size
- )
- )
- col += 1
- color += 1
- row += 1
- grid_y = (cell_size + cell_spacing) * row
- return grid_x, grid_y
- def calculate_row_dimensions(colors, cell_size=50, cell_spacing=10):
- return len(colors) * (cell_size + cell_spacing), cell_size
- def render_color_row(interface, x, y, colors, cell_size=50, cell_spacing=10):
- for c in range(len(colors)):
- pygame.draw.rect(interface,
- colors[c],
- (
- x + (cell_size * c) + (cell_spacing * c),
- y,
- cell_size,
- cell_size
- )
- )
- # colors
- black = (0, 0, 0, 255)
- white = (255, 255, 255, 255)
- gray = (128, 128, 128, 255)
- dark_gray = (50, 50, 50, 255)
- red = (255, 0, 0, 255)
- green = (0, 255, 0, 255)
- blue = (0, 0, 255, 255)
- yellow = (255, 255, 0, 255)
- orange = (255, 165, 0, 255)
- purple = (128, 0, 128, 255)
- maroon = (128, 0, 0, 255)
- pink = (255, 105, 80, 255)
- brown = (139, 69, 19, 255)
- # color translation
- color_directory = {
- # black: "black",
- # white: "white",
- gray: "gray",
- red: "red",
- green: "green",
- blue: "blue",
- yellow: "yellow",
- orange: "orange",
- purple: "purple",
- maroon: "maroon",
- pink: "pink",
- brown: "brown"
- }
- # color list
- color_list = [
- red,
- green,
- blue,
- yellow,
- orange,
- purple,
- maroon,
- pink,
- brown,
- ]
- # initialization block
- i = 1
- guess_list = []
- ans_list = []
- black_count = 0
- white_count = 0
- no_of_guess = 0
- is_win = 0 # 0: in-game, 1: Win, -1: Loss, -2: Waiting
- keys = pygame.key.get_pressed() # list of all keyboard keys
- display_bottom_screen = guess_processed = False
- # UI block
- game_screen = pygame.display.set_mode((screen_width, screen_height))
- pygame.display.set_caption("MasterMind")
- game_font = pygame.font.SysFont('Segoe UI Light', 60)
- score_font = pygame.font.SysFont('Segoe UI Light', 40)
- reminder_font = pygame.font.SysFont('Segoe UI Light', 40)
- display_text = game_font.render('MasterMind', False, white)
- clock = pygame.time.Clock()
- header_y_offset = 100
- color_grid_y_offset = -50
- guess_list_y_offset = 100
- guess_number_y = screen_height - 100
- score_y = screen_height - 50
- # play again button dimensions and coordinates
- pabdim = [0, 0]
- pabcoor = [0, 0]
- def init_game():
- global i, black_count, white_count, no_of_guess, is_win, display_bottom_screen
- print("[DEBUG] Game resetting...")
- is_win = -2
- i = 1
- guess_list.clear()
- ans_list.clear()
- black_count = 0
- white_count = 0
- no_of_guess = 0
- display_bottom_screen = False
- # randomization block
- for x in range(4): # loops from 1 to no_of_colors
- randomized_int = random.randint(0, 8)
- ans_list.append(color_list[randomized_int]) # 1 to 9
- print("Randomized Integer: {}".format(randomized_int))
- print("Appended color to answer: {}".format(color_directory[color_list[randomized_int]]))
- print("DEBUG")
- [print(color_directory[i], end=" ") for i in ans_list]
- print()
- is_win = 0
- print("[DEBUG] Game reset.")
- init_game()
- print("[DEBUG] ans_list: {}".format(ans_list))
- def render_game():
- if is_win == -2:
- game_screen.fill(black)
- elif is_win == 0:
- game_screen.fill(dark_gray) # black background
- # title
- dtxd = display_text.get_rect().width # display text x dimension
- game_screen.blit(display_text, (horizontal_center(dtxd), header_y_offset))
- # color grid
- cgd = calculate_grid_dimensions(color_list) # color grid dimensions
- render_color_grid(game_screen, horizontal_center(cgd[0]), vertical_center(cgd[1]) + color_grid_y_offset,
- color_list)
- # guess list
- grd = calculate_row_dimensions(guess_list, cell_size=25) # guess row dimensions
- render_color_row(game_screen, horizontal_center(grd[0]), vertical_center(grd[1]) + guess_list_y_offset,
- guess_list, cell_size=25)
- if len(guess_list) == 4:
- guess_display = score_font.render("Guess #{}:".format(no_of_guess), True, white)
- score = score_font.render(
- "{black}B - {white}W".format(black=black_count, white=white_count),
- True, white)
- gdw = guess_display.get_rect().width
- game_screen.blit(guess_display, (horizontal_center(gdw), guess_number_y))
- sw = score.get_rect().width
- game_screen.blit(score, (horizontal_center(sw), score_y))
- elif is_win == -1:
- render_end(False)
- elif is_win == 1:
- render_end(True)
- # main game loop
- execute = True
- while execute:
- # Render the game
- render_game()
- pygame.display.update()
- if no_of_guess >= 12:
- print("Loss.")
- print("is_win == -1")
- is_win = -1
- # event block
- for event in pygame.event.get(): # loop through user events i.e. keyboard, mouse
- if event.type == pygame.QUIT: # exit button
- execute = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- # Do the following events over the course of the game:
- if is_win == 0:
- # Start waiting
- if len(guess_list) >= 4:
- guess_list = []
- black_count = 0
- white_count = 0
- guess_processed = False
- print("[DEBUG] Mouse Clicked.")
- i += 1
- print("[DEBUG] Number of guesses: {}".format(no_of_guess))
- color_under_cursor = game_screen.get_at(pygame.mouse.get_pos())
- color_under_cursor = tuple(color_under_cursor)
- print("[DEBUG] Color under cursor: {}".format(color_under_cursor))
- if color_under_cursor in color_directory:
- print("[DEBUG] Color guessed: {}".format(color_directory[color_under_cursor]))
- guess_list.append(color_under_cursor)
- print("[DEBUG] guess_list: {}".format(guess_list))
- DEBUG_temp = (len(guess_list) == 4 and not guess_processed)
- print("[DEBUG] len(guess_list) == 4 and not guess_processed: {}".format(DEBUG_temp))
- if len(guess_list) == 4 and not guess_processed:
- [print(color_directory[i], end=" ") for i in guess_list]
- print()
- no_of_guess += 1
- print("[DEBUG] ans_list: {}".format(ans_list))
- print("[DEBUG] guess_list: {}".format(guess_list))
- # process guess
- for ans, guess in list(zip(ans_list, guess_list)):
- print("[DEBUG] {} == {}: {}".format(ans, guess, ans == guess))
- if ans == guess:
- black_count += 1
- print("[DEBUG] Incremented black count. Count is now {}".format(black_count))
- print("[DEBUG] guess in ans_list: {}".format(guess in ans_list))
- if guess in ans_list:
- white_count += 1
- print("[DEBUG] Incremented white count. Count is now {}".format(white_count))
- if black_count == 4 and white_count == 4:
- print("Guess #{}:".format(no_of_guess))
- print("4B - 4W")
- print("Win.")
- print("is_win = 1")
- is_win = 1
- else:
- print("Guess #{}:".format(no_of_guess))
- print("{black}B - {white}W".format(black=black_count, white=white_count))
- guess_processed = True
- # Do the following actions during game end prompt
- else:
- c_in_rgn = cursor_in_region(pabcoor, pabdim, pygame.mouse.get_pos())
- print("[DEBUG] cursor in region: ".format(c_in_rgn))
- if c_in_rgn:
- init_game()
- clock.tick(60) # sets fps
- pygame.time.delay(500) # allows
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement