Skip to content

Commit

Permalink
path to files changed
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielDekhtyar committed Jan 26, 2024
1 parent 06fede7 commit 5208b7b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 21 deletions.
3 changes: 2 additions & 1 deletion font/get_font.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from utils.resource_path import resource_path


def font(font_file):
Expand All @@ -10,7 +11,7 @@ def font(font_file):
:return: the full path to the font file by joining the font folder path and the font filename.
"""
# The path to the folder containing the font file
font_folder = r"CS50P\Final Project\font\fonts"
font_folder = resource_path(r"Hangman-with-Pygame\font\fonts")

# The name of the TTF file
font_filename = font_file
Expand Down
2 changes: 1 addition & 1 deletion project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Tuple

import pygame

from utils.resource_path import resource_path
from src import start_screen
from src import game_loop
from src import hangman_game
Expand Down
11 changes: 6 additions & 5 deletions sounds/play_sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"""

import pygame.mixer
from utils.resource_path import resource_path


def correct_answer() -> None:
# Load the sound
correct_answer_sound = pygame.mixer.Sound("CS50P/Final Project/sounds/correct answer.mp3")
correct_answer_sound = pygame.mixer.Sound(resource_path("Hangman-with-Pygame/sounds/correct answer.mp3"))

# Set volume 40%
correct_answer_sound.set_volume(0.4)
Expand All @@ -20,7 +21,7 @@ def correct_answer() -> None:

def wrong_answer() -> None:
# Load the sound
wrong_answer_sound = pygame.mixer.Sound("CS50P/Final Project/sounds/wrong answer.mp3")
wrong_answer_sound = pygame.mixer.Sound(resource_path("Hangman-with-Pygame/sounds/wrong answer.mp3"))

# Set volume 50%
wrong_answer_sound.set_volume(0.5)
Expand All @@ -33,7 +34,7 @@ def wrong_answer() -> None:

def yay() -> None:
# Load the sound
game_win_sound = pygame.mixer.Sound("CS50P/Final Project/sounds/yay.mp3")
game_win_sound = pygame.mixer.Sound(resource_path("Hangman-with-Pygame/sounds/yay.mp3"))

# Set volume 35%
game_win_sound.set_volume(0.35)
Expand All @@ -46,7 +47,7 @@ def yay() -> None:

def game_over() -> None:
# Load the sound
game_over_sound = pygame.mixer.Sound("CS50P/Final Project/sounds/game over.mp3")
game_over_sound = pygame.mixer.Sound(resource_path("Hangman-with-Pygame/sounds/game over.mp3"))

# Set volume 60%
game_over_sound.set_volume(0.6)
Expand All @@ -59,7 +60,7 @@ def game_over() -> None:

def game_start() -> None:
# Load the sound
game_start_sound = pygame.mixer.Sound("CS50P/Final Project/sounds/game start.mp3")
game_start_sound = pygame.mixer.Sound(resource_path("Hangman-with-Pygame/sounds/game start.mp3"))

# Set volume 70%
game_start_sound.set_volume(0.7)
Expand Down
11 changes: 6 additions & 5 deletions src/game_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pygame
import string

from utils.resource_path import resource_path
from src import start_screen
from classes.word import Word
from classes.button import Button
Expand Down Expand Up @@ -90,8 +91,8 @@ def render_restart_button(screen: pygame.Surface) -> Button:
the `rect` attribute of the `button` object.
"""
# Load the image
image_folder_path: int = "CS50P/Final Project/images/"
image: pygame.Surface = pygame.image.load(f"{image_folder_path}restart.png")
image_folder_path: str = resource_path("Hangman-with-Pygame/images/")
image: pygame.Surface = pygame.image.load(resource_path(f"{image_folder_path}restart.png"))

button_width = int(screen.get_width() * 0.035)
button_height = int(screen.get_height() * 0.055)
Expand Down Expand Up @@ -196,7 +197,7 @@ def render_letter_buttons(screen: pygame.Surface, all_button_instances: list[But
letter_images: dict[str, pygame.Surface] = {}
for letter in string.ascii_uppercase:
letter_images[letter] = pygame.image.load(
f"CS50P/Final Project/images/letters/{letter}.png"
resource_path(f"Hangman-with-Pygame/images/letters/{letter}.png")
)

# The dimensions of the buttons
Expand Down Expand Up @@ -292,7 +293,7 @@ def render_v_or_x_image(screen: pygame.Surface, button: Button, image_name) -> N
image_name: The `image_name` parameter is a string that represents the name of the image file to
be loaded. It should not include the file extension (e.g., ".png").
"""
image = pygame.image.load(f"CS50P/Final Project/images/{image_name}.png")
image = pygame.image.load(resource_path(f"Hangman-with-Pygame/images/{image_name}.png"))

pos_x = button.x
pos_y = button.y
Expand All @@ -319,7 +320,7 @@ def render_the_hangman(screen: pygame.Surface, hangman_image_number: int) -> Non
file from the "CS50P/Final Project/images/hangman/" directory.
"""
hangman_image = pygame.image.load(
f"CS50P/Final Project/images/hangman/{hangman_image_number}.png"
resource_path(f"Hangman-with-Pygame/images/hangman/{hangman_image_number}.png")
)

image_height = int(screen.get_height() * 0.3)
Expand Down
3 changes: 2 additions & 1 deletion src/hangman_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import random
import pygame

from utils.resource_path import resource_path
from classes.word import Word
from classes.button import Button
from src import game_screen
Expand Down Expand Up @@ -127,7 +128,7 @@ def get_random_word(level: str) -> tuple[str, str]:
"""

# Path where all the csv files with the words are located within the project
csv_path = f"CS50P/Final Project/data/{level}.csv"
csv_path = resource_path(resource_path(f"Hangman-with-Pygame/data/{level}.csv"))

# Open the CSV file
with open(csv_path, "r") as file:
Expand Down
17 changes: 9 additions & 8 deletions src/start_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import pygame

from utils.resource_path import resource_path
from font import set_font
from classes.button import Button

Expand Down Expand Up @@ -83,7 +84,7 @@ def render_bg(screen: pygame.Surface) -> None:
screen_width, screen_height = screen.get_width(), screen.get_height()

# Select the background image
bg_image_path: str = "CS50P/Final Project/images/background_image.png"
bg_image_path: str = resource_path("Hangman-with-Pygame/images/background_image.png")

# Load the background image
bg_image: pygame.Surface = pygame.image.load(bg_image_path)
Expand Down Expand Up @@ -160,11 +161,11 @@ def render_buttons(screen: pygame.Surface, title_rect: pygame.Rect) -> tuple[But
`level_1_button`, `level_2_button`, `level_3_button`, and `level_4_button`.
"""
# Load the level button images
image_folder_path: str = "CS50P/Final Project/images/level buttons/"
level_1_img: pygame.Surface = pygame.image.load(f"{image_folder_path}easy.png")
level_2_img: pygame.Surface = pygame.image.load(f"{image_folder_path}medium.png")
level_3_img: pygame.Surface = pygame.image.load(f"{image_folder_path}hard.png")
level_4_img: pygame.Surface = pygame.image.load(f"{image_folder_path}very hard.png")
image_folder_path: str = resource_path("Hangman-with-Pygame/images/level buttons/")
level_1_img: pygame.Surface = pygame.image.load(resource_path(f"{image_folder_path}easy.png"))
level_2_img: pygame.Surface = pygame.image.load(resource_path(f"{image_folder_path}medium.png"))
level_3_img: pygame.Surface = pygame.image.load(resource_path(f"{image_folder_path}hard.png"))
level_4_img: pygame.Surface = pygame.image.load(resource_path(f"{image_folder_path}very hard.png"))

# Size the images
image_width, image_height = int(screen.get_width() * 0.25), int(
Expand Down Expand Up @@ -227,8 +228,8 @@ def render_exit_button(screen: pygame.Surface) -> Button:
the `rect` attribute of the `button` object.
"""
# Load the image
image_folder_path: int = "CS50P/Final Project/images/"
image: pygame.Surface = pygame.image.load(f"{image_folder_path}exit button.png")
image_folder_path: int = resource_path("Hangman-with-Pygame/images/")
image: pygame.Surface = pygame.image.load(resource_path(f"{image_folder_path}exit button.png"))

button_width = int(screen.get_width() * 0.03)
button_height = int(screen.get_height() * 0.055)
Expand Down
12 changes: 12 additions & 0 deletions utils/resource_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys, os


def resource_path(relative_path: str):
""" Get the absolute path to the resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)

0 comments on commit 5208b7b

Please sign in to comment.