diff --git a/Breaker.spec b/Breaker.spec new file mode 100644 index 0000000..7c1a272 --- /dev/null +++ b/Breaker.spec @@ -0,0 +1,50 @@ +# -*- mode: python ; coding: utf-8 -*- + +block_cipher = None + +added_files = [ + + ("assets\\image", "assets\\image"), + ("assets\\audio", "assets\\audio") + +] + +a = Analysis( + ['main.py'], + pathex=[], + binaries=[], + datas=added_files, + hiddenimports=[], + hookspath=[], + hooksconfig={}, + runtime_hooks=[], + excludes=[], + win_no_prefer_redirects=False, + win_private_assemblies=False, + cipher=block_cipher, + noarchive=False, +) +pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) + +exe = EXE( + pyz, + a.scripts, + a.binaries, + a.zipfiles, + a.datas, + [], + name='Breaker', + debug=False, + bootloader_ignore_signals=False, + strip=False, + upx=True, + upx_exclude=[], + runtime_tmpdir=None, + console=False, + disable_windowed_traceback=False, + argv_emulation=False, + target_arch=None, + codesign_identity=None, + entitlements_file=None, + icon=['assets\\image\\ball.ico'] +) diff --git a/assets/image/ball.ico b/assets/image/ball.ico index de600f1..c474602 100644 Binary files a/assets/image/ball.ico and b/assets/image/ball.ico differ diff --git a/main.py b/main.py index 0fd190b..0b69710 100644 --- a/main.py +++ b/main.py @@ -2,11 +2,18 @@ import pickle import random +import sys, os + FPS_TARGET = 60 WIN_WIDTH = 480 WIN_HEIGHT = 640 UI_HEIGHT = 45 +def resource_path(relative_path): + """ Get absolute path to resource, works for dev and for PyInstaller """ + base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) + return os.path.join(base_path, relative_path) + def default_player_pos() -> pygame.rect.Rect: return pygame.rect.Rect(WIN_WIDTH * 0.5 - 30, WIN_HEIGHT - 55, 60, 15) @@ -55,7 +62,7 @@ def create_bricks(level_number: int) -> list[list]: return level def play_level_music(level_number: int) -> None: - pygame.mixer.music.load('assets/audio/level' + str(((level_number-1) % 5) + 1) + '.ogg') + pygame.mixer.music.load(resource_path(r'assets\audio\level' + str(((level_number-1) % 5) + 1) + '.ogg')) pygame.mixer.music.play(-1) class game: @@ -65,13 +72,13 @@ def __init__(self, screen) -> None: self.screen = screen play_level_music(1) - self.background_image: pygame.surface.Surface = pygame.image.load('assets/image/background.jpg') - self.frame_image: pygame.surface.Surface = pygame.image.load('assets/image/frame.png') - self.ball_image: pygame.surface.Surface = pygame.image.load('assets/image/ball.png') - self.paddle_image: pygame.surface.Surface = pygame.image.load('assets/image/paddle.png') - self.paddle_icon_image: pygame.surface.Surface = pygame.image.load('assets/image/paddle_icon.png') - self.powerup1_image: pygame.surface.Surface = pygame.image.load('assets/image/powerup1.png') # B - self.powerup2_image: pygame.surface.Surface = pygame.image.load('assets/image/powerup2.png') # L + self.background_image: pygame.surface.Surface = pygame.image.load(resource_path(r'assets\image\background.jpg')) + self.frame_image: pygame.surface.Surface = pygame.image.load(resource_path(r'assets\image\frame.png')) + self.ball_image: pygame.surface.Surface = pygame.image.load(resource_path(r'assets\image\ball.png')) + self.paddle_image: pygame.surface.Surface = pygame.image.load(resource_path(r'assets\image\paddle.png')) + self.paddle_icon_image: pygame.surface.Surface = pygame.image.load(resource_path(r'assets\image\paddle_icon.png')) + self.powerup1_image: pygame.surface.Surface = pygame.image.load(resource_path(r'assets\image\powerup1.png')) # B + self.powerup2_image: pygame.surface.Surface = pygame.image.load(resource_path(r'assets\image\powerup2.png')) # L self.fg_font: pygame.font.Font = pygame.font.SysFont(pygame.font.match_font("cascadiamonoregular"), 32) self.fg_font_color1: tuple[int, int, int] = (255, 0, 0) @@ -132,7 +139,7 @@ def start(self): # --- BRICK COLLIDE --- for block in self.bricks: if self.ball_pos.colliderect(block[0], block[1], 36, 15): - pygame.mixer.Sound.play(pygame.mixer.Sound("assets/audio/ping.wav")) + pygame.mixer.Sound.play(pygame.mixer.Sound(resource_path(r'assets\audio\ping.wav'))) if random.randint(1, 10) == 10: # 10% self.powerups.append([block[0], block[1]+10, 1]) if random.randint(1, 20) == 20: # 5% @@ -191,7 +198,7 @@ def start(self): self.powerups.remove(powerup) powerup[1] += 100 * self.dt - # --- PLAYER/BALL DRAW --- + # --- PLAYER\BALL DRAW --- self.screen.blit(self.ball_image, self.ball_pos) self.screen.blit(self.paddle_image, self.player_pos) @@ -201,7 +208,7 @@ def start(self): # --- BALL PHYSICS --- if self.ball_pos.centery > WIN_HEIGHT: # dead if self.lives > 0: - pygame.mixer.Sound.play(pygame.mixer.Sound("assets/audio/drop.wav")) + pygame.mixer.Sound.play(pygame.mixer.Sound(resource_path(r'assets\audio\drop.wav'))) self.lives -= 1 self.player_pos = default_player_pos() self.ball_pos = default_ball_pos() @@ -211,7 +218,7 @@ def start(self): self.ball_dir_y = 1 self.ball_ang = 0 else: - pygame.mixer.Sound.play(pygame.mixer.Sound("assets/audio/crash.wav")) + pygame.mixer.Sound.play(pygame.mixer.Sound(resource_path(r'assets\audio\crash.wav'))) self.__init__(self.screen) if self.ball_pos.centerx < 15: @@ -222,7 +229,7 @@ def start(self): if self.ball_pos.centery < UI_HEIGHT + 10: self.ball_dir_y *= -1 - # --- BALL/PLAYER COLLIDE --- + # --- BALL\PLAYER COLLIDE --- if self.ball_pos.colliderect(self.player_pos): self.ball_dir_y = -1 self.ball_dir_x = 1 @@ -258,7 +265,7 @@ def start(self): if __name__ == "__main__": pygame.init() pygame.display.set_caption('Breaker') - pygame.display.set_icon(pygame.image.load('assets/image/ball.ico')) + pygame.display.set_icon(pygame.image.load(resource_path(r'assets\image\ball.ico'))) screen: pygame.surface.Surface = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) new_game = game(screen) new_game.start() diff --git a/pack_executable.py b/pack_executable.py deleted file mode 100644 index 3a179ba..0000000 --- a/pack_executable.py +++ /dev/null @@ -1,10 +0,0 @@ -import cx_Freeze - -executables = [cx_Freeze.Executable("main.py", icon="assets/image/ball.ico", base = "Win32GUI", target_name="breaker")] - -cx_Freeze.setup( - name="Breaker", - options={"build_exe": {"packages":["pygame"], - "include_files":["assets/", "LICENSE"]}}, - executables = executables -) \ No newline at end of file