import pygame import sys import os # Initialize Pygame pygame.init() # Get the directory where the script is located script_dir = os.path.dirname(os.path.abspath(__file__)) # Function to get file path for assets in respective folders def get_asset_path(folder, filename): return os.path.join(script_dir, folder, filename) # Get system's screen width and height infoObject = pygame.display.Info() WIDTH, HEIGHT = infoObject.current_w, infoObject.current_h # Set up the screen screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN) pygame.display.set_caption("Clicker") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) GRAY = (200, 200, 200) # Load sounds click_sound_path = get_asset_path("sounds", "click_sound.mp3") if not os.path.isfile(click_sound_path): print(f"Sound file '{click_sound_path}' not found.") pygame.quit() sys.exit() click_sound = pygame.mixer.Sound(click_sound_path) # Variables clicks = 0 clicks_per_click = 1 click_upgrade_cost = 10 clicks_per_second = 0 cps_upgrade_cost = 50 last_click_time = pygame.time.get_ticks() clicked = False scale_factor = 1.0 # Load different skins for the click object click_skins = ["cookie.png", "diamond.png", "star.png", "dvd.png"] current_skin_index = 0 click_images = [pygame.image.load(get_asset_path("skins", skin)) for skin in click_skins] click_images = [pygame.transform.scale(image, (450, 450)) for image in click_images] click_image = click_images[current_skin_index] # Load font file custom_font_path = get_asset_path("fonts", "retro.ttf") if not os.path.isfile(custom_font_path): print(f"Font file '{custom_font_path}' not found.") pygame.quit() sys.exit() # Load fonts title_font = pygame.font.Font(custom_font_path, 72) # Load the font with a size of 72 font = pygame.font.Font(custom_font_path, 36) # Load the font with a size of 36 for button texts game_font = pygame.font.Font(custom_font_path, 64) # Just the in-game font # Function to cycle through skins def change_skin(direction): global current_skin_index, click_image if direction == "next": current_skin_index = (current_skin_index + 1) % len(click_skins) elif direction == "prev": current_skin_index = (current_skin_index - 1) % len(click_skins) click_image = pygame.image.load(get_asset_path("skins", click_skins[current_skin_index])) # Function to save the game state def save_game(): with open(os.path.join(script_dir, "save.txt"), "w") as file: file.write(f"{clicks}\n") file.write(f"{clicks_per_click}\n") file.write(f"{click_upgrade_cost}\n") file.write(f"{clicks_per_second}\n") file.write(f"{cps_upgrade_cost}\n") # Function to load the game state def load_game(): global clicks, clicks_per_click, click_upgrade_cost, clicks_per_second, cps_upgrade_cost save_path = os.path.join(script_dir, "save.txt") if os.path.exists(save_path): with open(save_path, "r") as file: lines = file.readlines() clicks = float(lines[0]) # Change int to float here clicks_per_click = int(lines[1]) click_upgrade_cost = int(lines[2]) clicks_per_second = int(lines[3]) cps_upgrade_cost = int(lines[4]) # Function to draw the main menu def main_menu(): while True: # Draw main menu background screen.fill(WHITE) draw_background("main_menu_background.jpg") # Title text title_text = title_font.render("Clicker", True, BLACK) title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 5)) screen.blit(title_text, title_rect) # Create "Play" button play_button = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2, 200, 50) pygame.draw.rect(screen, GRAY, play_button) play_text = font.render("Play", True, BLACK) play_text_rect = play_text.get_rect(center=play_button.center) screen.blit(play_text, play_text_rect) # Create "Quit" button quit_button = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + 100, 200, 50) pygame.draw.rect(screen, GRAY, quit_button) quit_text = font.render("Quit", True, BLACK) quit_text_rect = quit_text.get_rect(center=quit_button.center) screen.blit(quit_text, quit_text_rect) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if play_button.collidepoint(event.pos): return True # Start the game elif quit_button.collidepoint(event.pos): pygame.quit() sys.exit() # Function to draw the background def draw_background(image_name="game_background.jpg"): # Load game background image and scale it to cover the entire screen background_path = get_asset_path("backgrounds", image_name) print("Background path:", background_path) # Check the path if not os.path.isfile(background_path): print(f"Background image file '{background_path}' not found.") pygame.quit() sys.exit() background = pygame.image.load(background_path) print("Background loaded successfully!") # Check if the image is loaded background = pygame.transform.scale(background, (WIDTH, HEIGHT)) screen.blit(background, (0, 0)) # Define incremental cost and benefit increase factors CLICK_UPGRADE_COST_INCREMENT = 10 CLICK_BENEFIT_INCREMENT = 1 CPS_UPGRADE_COST_INCREMENT = 50 CPS_BENEFIT_INCREMENT = 1 # Function to draw upgrades and return their rects def draw_upgrades(): # Draw upgrade button upgrade_text = font.render(f"Upgrade Click {click_upgrade_cost} clicks", True, BLACK) upgrade_text_rect = upgrade_text.get_rect(midbottom=(WIDTH // 2, HEIGHT - 10)) screen.blit(upgrade_text, upgrade_text_rect) # Draw CPS upgrade button cps_upgrade_text = font.render(f"Upgrade CPS {cps_upgrade_cost} clicks", True, BLACK) cps_upgrade_text_rect = cps_upgrade_text.get_rect(midbottom=(WIDTH // 2, HEIGHT - 60)) screen.blit(cps_upgrade_text, cps_upgrade_text_rect) return upgrade_text_rect, cps_upgrade_text_rect # Handle upgrades def handle_upgrades(): global clicks, clicks_per_click, click_upgrade_cost, clicks_per_second, cps_upgrade_cost mouse_pos = pygame.mouse.get_pos() if upgrade_text_rect.collidepoint(mouse_pos): pygame.draw.rect(screen, GRAY, upgrade_text_rect, 3) if pygame.mouse.get_pressed()[0] and clicks >= click_upgrade_cost: clicks -= click_upgrade_cost clicks_per_click += CLICK_BENEFIT_INCREMENT click_upgrade_cost += CLICK_UPGRADE_COST_INCREMENT # Provide visual feedback here if desired elif cps_upgrade_text_rect.collidepoint(mouse_pos): pygame.draw.rect(screen, GRAY, cps_upgrade_text_rect, 3) if pygame.mouse.get_pressed()[0] and clicks >= cps_upgrade_cost: clicks -= cps_upgrade_cost clicks_per_second += 1 cps_upgrade_cost *= 2 # Function to draw the options menu def options_menu(): # Load options menu background image and scale it to cover the entire screen options_menu_background_path = get_asset_path("backgrounds", "options_menu_background.jpg") # Adjust the path as needed if not os.path.isfile(options_menu_background_path): print(f"Background image file '{options_menu_background_path}' not found.") pygame.quit() sys.exit() options_menu_background = pygame.image.load(options_menu_background_path) options_menu_background = pygame.transform.scale(options_menu_background, (WIDTH, HEIGHT)) while True: screen.fill(WHITE) # Draw options menu background screen.blit(options_menu_background, (0, 0)) # Title text title_text = title_font.render("Options", True, BLACK) title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 5)) # Moved up to one-fourth screen.blit(title_text, title_rect) # Create "Back to Game" button back_button = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2, 200, 50) # Center vertically pygame.draw.rect(screen, GRAY, back_button) back_text = font.render("Back to Game", True, BLACK) back_text_rect = back_text.get_rect(center=back_button.center) screen.blit(back_text, back_text_rect) # Create "Quit" button quit_button = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + 100, 200, 50) # Center vertically pygame.draw.rect(screen, GRAY, quit_button) quit_text = font.render("Quit", True, BLACK) quit_text_rect = quit_text.get_rect(center=quit_button.center) screen.blit(quit_text, quit_text_rect) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if back_button.collidepoint(event.pos): return # Return to the main game elif quit_button.collidepoint(event.pos): save_game() # Save the game state before quitting pygame.quit() sys.exit() # Load the game state if save file exists load_game() # Display main menu main_menu() # Draw the submenu button in the top right corner submenu_text = game_font.render("Options", True, BLACK) submenu_rect = submenu_text.get_rect(topright=(WIDTH - 10, 10)) # Main loop running = True click_rect = pygame.Rect(WIDTH // 2 - 225, HEIGHT // 2 - 225, 450, 450) original_click_rect = click_rect.copy() click_angle = 0 # Function to display the shop menu def shop_menu(): selected_skin_index = current_skin_index while True: screen.fill(WHITE) # Draw shop menu background draw_background("shop_menu_background.jpg") # Assuming you have a background image for the shop menu # Title text title_text = title_font.render("Shop", True, BLACK) title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 5)) # Adjust position as needed screen.blit(title_text, title_rect) # Display available skins for purchase for i, skin_image in enumerate(click_images): skin_rect = skin_image.get_rect(center=((i + 1) * WIDTH // (len(click_images) + 1), HEIGHT // 2)) screen.blit(skin_image, skin_rect) if i != current_skin_index: cost_text = font.render(f"{click_upgrade_cost} clicks", True, BLACK) cost_rect = cost_text.get_rect(midtop=(skin_rect.centerx, skin_rect.bottom + 10)) screen.blit(cost_text, cost_rect) # Create "Back to Game" button back_button = pygame.Rect(WIDTH // 2 - 100, HEIGHT - 100, 200, 50) # Adjust position as needed pygame.draw.rect(screen, GRAY, back_button) back_text = font.render("Back to Game", True, BLACK) back_text_rect = back_text.get_rect(center=back_button.center) screen.blit(back_text, back_text_rect) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if back_button.collidepoint(event.pos): return # Return to the main game for i, skin_image in enumerate(click_images): skin_rect = skin_image.get_rect(center=((i + 1) * WIDTH // (len(click_images) + 1), HEIGHT // 2)) if skin_rect.collidepoint(event.pos): selected_skin_index = i # Highlight the selected skin pygame.draw.rect(screen, BLACK, skin_rect, 3) pygame.display.flip() # Update the main loop to include the shop menu option while running: screen.fill(WHITE) # Draw background draw_background() # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: # Save the game state when the game is closed save_game() running = False elif event.type == pygame.MOUSEBUTTONDOWN: if click_rect.collidepoint(event.pos): clicks += clicks_per_click clicked = True elif submenu_rect.collidepoint(event.pos): options_menu() # Call the options menu function when submenu button is clicked elif shop_rect.collidepoint(event.pos): shop_menu() # Call the shop menu function when shop button is clicked elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: # Change to the previous skin change_skin("prev") elif event.key == pygame.K_RIGHT: # Change to the next skin change_skin("next") # Draw the submenu button screen.blit(submenu_text, submenu_rect) # Draw the shop button shop_text = game_font.render("Shop", True, BLACK) shop_rect = shop_text.get_rect(bottomleft=(10, HEIGHT - 10)) screen.blit(shop_text, shop_rect) # Calculate clicks per second current_time = pygame.time.get_ticks() time_diff = (current_time - last_click_time) / 1000.0 # Convert milliseconds to seconds clicks += clicks_per_second * time_diff last_click_time = current_time # Rotate and draw click rotated_click = pygame.transform.rotate(click_image, click_angle) scaled_click = pygame.transform.scale(rotated_click, (int(rotated_click.get_width() * scale_factor), int(rotated_click.get_height() * scale_factor))) screen.blit(scaled_click, scaled_click.get_rect(center=click_rect.center)) click_angle += 1 # Increase rotation angle # Draw click count text = game_font.render(f"Clicks {int(clicks)}", True, BLACK) screen.blit(text, (10, 10)) # Draw upgrades and get upgrade rects upgrade_text_rect, cps_upgrade_text_rect = draw_upgrades() # Make the click bigger when clicked if clicked: scale_factor = 1.1 clicked = False else: scale_factor = max(1.0, scale_factor - 0.05) # Handle upgrades handle_upgrades() # Update the display pygame.display.flip() # Cap the frame rate pygame.time.Clock().tick(60) # Quit Pygame pygame.quit() sys.exit()