Commits

César Bustíos committed fbfa96e

Generate the board according to the new game option

Comments (0)

Files changed (1)

 
 
 def main():
+    """
+    Main shit
+    """
     pygame.display.set_caption('Geezle - Memory Puzzle')
 
-    board = generate_board(False)
+    # Initial data
+    logos_path = join(IMAGES_PATH, 'logos/distros')
+    board = generate_board(logos_path, False)
     new_game = True
     running = True
     box_img = BOX_IMG
 
         # New board?
         if new_game:
-            board = generate_board(False)
-            display_new_game_settings()
-            #~ new_game = False
+            new_game_options = display_new_game_settings()
 
         clicked = False
         pygame.display.update()
                 x, y = event.pos
                 clicked = True
 
-        if clicked and new_game is not True:
-            # New game?
-            if options['new_game'].collidepoint(x, y):
-                new_game = True
-                score = 0
-            # Quit!!!???
-            elif options['quit'].collidepoint(x, y):
-                pygame.quit()
-                sys.exit()
-            elif options['blue_box'].collidepoint(x, y):
-                path = join(IMAGES_PATH, 'box1.png')
-                box_img = pygame.image.load(path).convert()
-            elif options['green_box'].collidepoint(x, y):
-                path = join(IMAGES_PATH, 'box2.png')
-                box_img = pygame.image.load(path).convert()
-            elif options['purple_box'].collidepoint(x, y):
-                path = join(IMAGES_PATH, 'box3.png')
-                box_img = pygame.image.load(path).convert()
-            elif options['tux_box'].collidepoint(x, y):
-                path = join(IMAGES_PATH, 'box4.png')
-                box_img = pygame.image.load(path).convert_alpha()
-
         # Get box at pixel (x,y)
         row, col = get_box(board, x, y)
         if row != None and col != None and new_game is not True:
                     score += 1
                     first_choice = None
 
+        if clicked and new_game is not True:
+            # New game?
+            if options['new_game'].collidepoint(x, y):
+                new_game = True
+                score = 0
+            # Quit!!!???
+            elif options['quit'].collidepoint(x, y):
+                pygame.quit()
+                sys.exit()
+            elif options['blue_box'].collidepoint(x, y):
+                path = join(IMAGES_PATH, 'box1.png')
+                box_img = pygame.image.load(path).convert()
+            elif options['green_box'].collidepoint(x, y):
+                path = join(IMAGES_PATH, 'box2.png')
+                box_img = pygame.image.load(path).convert()
+            elif options['purple_box'].collidepoint(x, y):
+                path = join(IMAGES_PATH, 'box3.png')
+                box_img = pygame.image.load(path).convert()
+            elif options['tux_box'].collidepoint(x, y):
+                path = join(IMAGES_PATH, 'box4.png')
+                box_img = pygame.image.load(path).convert_alpha()
+
+        if clicked and new_game is True:
+            if new_game_options['distros']['rect'].collidepoint(x,y):
+                logos_path = new_game_options['distros']['path']
+                board = generate_board(logos_path, False)
+                new_game = False
+            elif new_game_options['open_source']['rect'].collidepoint(x,y):
+                logos_path = new_game_options['open_source']['path']
+                board = generate_board(logos_path, False)
+                new_game = False
+            elif new_game_options['programming']['rect'].collidepoint(x,y):
+                logos_path = new_game_options['programming']['path']
+                board = generate_board(logos_path, False)
+                new_game = False
+
         # Check if game is over
         if all_matched(board['revealed']):
             display_game_over_message()
 
 def display_new_game_settings():
     """
-    For choosing icon theme
+    For choosing icon theme. Returns a dict of rects for colliding process
     """
     # Temporal surface for transparency
     tmp_surface = pygame.Surface(SCREEN_RESOLUTION)
     SCREEN.blit(open_source_img, open_source_rect)
     SCREEN.blit(programming_img, programming_rect)
 
+    # Return each option with its rect and path to images
+    distros_path = join(IMAGES_PATH, 'logos/distros')
+    open_source_path = join(IMAGES_PATH, 'logos/tools')
+    programming_path = join(IMAGES_PATH, 'logos/languages')
+
+    options = {
+        'distros': {'rect': distros_rect, 'path': distros_path},
+        'open_source': {'rect': open_source_rect, 'path': open_source_path},
+        'programming': {'rect': programming_rect, 'path': programming_path},
+    }
+
     # Now, fonts for describing the icons. Rects and positions
     font = pygame.font.Font(join(FONTS_PATH, FONT_STYLE), 20)
     distros_font = font.render('Linux Distributions', True, WHITE)
     SCREEN.blit(open_source_font, open_source_rect)
     SCREEN.blit(programming_font, programming_rect)
 
+    return options
+
 
 def reveal_box(board, row, col, box_img):
     """
     return True
 
 
-def get_logos_images():
+def get_logos_images(path):
     """
     Returns a shuffled list of logos images
     """
     logos = []
-    logos_path = join(IMAGES_PATH, 'logos/distros')
 
-    for img in os.listdir(logos_path):
+    for img in os.listdir(path):
         try:
             # Go Archie!
-            logos.append(pygame.image.load(join(logos_path, img)).convert_alpha())
+            logos.append(pygame.image.load(join(path, img)).convert_alpha())
         except pygame.error, e:
             continue
     random.shuffle(logos)
     return logos
 
 
-def generate_board(reveal=False):
+def generate_board(logos_path, reveal=False):
     """
     Generate all the necessary data: the image logos, the rect that
     represents each image on the surface and the revealed data.
     revealed = []
 
     # Get the logos images
-    images = get_logos_images()[:IMAGES_NEEDED]
+    images = get_logos_images(logos_path)[:IMAGES_NEEDED]
     assert len(images) >= IMAGES_NEEDED, 'Not enough logos'
     images *= 2
     random.shuffle(images)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.