A pyinstaller example
107
PyGame/Basics/015_pyinstaller.py
Normal file
|
@ -0,0 +1,107 @@
|
|||
# IMPORTANT NOTES:
|
||||
# 1) If any included files (fonts, images, sounds) are
|
||||
# changed then the .spec file MUST be edited to include them
|
||||
# 2) Build the executable with this command (saves to dist/ folder)
|
||||
# pyinstaller 015_pyinstaller.spec
|
||||
|
||||
import os
|
||||
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
|
||||
import pygame
|
||||
import random
|
||||
import sys
|
||||
|
||||
pygame.init()
|
||||
|
||||
# Some variables used to initialize the window
|
||||
# The width and height are in pixels
|
||||
win_width = 600
|
||||
win_height = 480
|
||||
win_bg_color = (26, 110, 43)
|
||||
text_color = (242, 238, 10)
|
||||
running = True
|
||||
emoji_hover = False
|
||||
|
||||
# This function takes a list of path arguments
|
||||
# and prepends the sys._MEIPASS variable if the
|
||||
# application has been run as a pyinstaller executable
|
||||
# returning the result as a joined system path
|
||||
def get_real_path(*path_args: str):
|
||||
if getattr(sys, "frozen", False):
|
||||
my_path = os.path.join(sys._MEIPASS, *path_args)
|
||||
else:
|
||||
my_path = os.path.join(*path_args)
|
||||
return my_path
|
||||
|
||||
window = pygame.display.set_mode((win_width, win_height))
|
||||
pygame.display.set_caption('PyGame Fancy Basics')
|
||||
icon = pygame.image.load(get_real_path("img", "melon_icon_64.png"))
|
||||
icon.convert_alpha()
|
||||
pygame.display.set_icon(icon)
|
||||
window.fill(win_bg_color)
|
||||
# Import font
|
||||
pixeboy_200 = pygame.font.Font(get_real_path("font", "Pixeboy.ttf"), 200)
|
||||
# Create two lines of text
|
||||
title_text_1 = pixeboy_200.render("HELLO", True, text_color)
|
||||
title_text_2 = pixeboy_200.render("WORLD", True, text_color)
|
||||
# Draw first line of text
|
||||
text_x = (win_width // 2) - (title_text_1.get_width() // 2)
|
||||
text_y = 25
|
||||
window.blit(title_text_1, (text_x, text_y))
|
||||
# Draw second line of text
|
||||
text_x = (win_width // 2) - (title_text_2.get_width() // 2)
|
||||
text_y = 25 + title_text_2.get_height() + 25
|
||||
window.blit(title_text_2, (text_x, text_y))
|
||||
# Import image
|
||||
emoji = pygame.image.load(get_real_path("img", "poop_emoji_sm.png"))
|
||||
emoji.convert_alpha()
|
||||
# Let's place the image 25 pixels from the bottom of the window
|
||||
emoji_x = (win_width // 2) - (emoji.get_width() // 2)
|
||||
emoji_y = win_height - 25 - emoji.get_height()
|
||||
window.blit(emoji, (emoji_x, emoji_y))
|
||||
pixeboy_20 = pygame.font.Font(get_real_path("font", "Pixeboy.ttf"), 20)
|
||||
click_text = pixeboy_20.render("Click Me", True, text_color)
|
||||
text_x = (win_width // 2) - (click_text.get_width() // 2)
|
||||
text_y = win_height - 5 - click_text.get_height()
|
||||
window.blit(click_text, (text_x, text_y))
|
||||
# We can import audio files with mixer.Sound()
|
||||
# Only .ogg (Ogg Vorbis) format files should be used
|
||||
# The audio program Audacity can be used to convert files to .ogg
|
||||
# Here we will import several and put them in a list
|
||||
audio = []
|
||||
audio.append(pygame.mixer.Sound(get_real_path("snd", "toot.ogg")))
|
||||
audio.append(pygame.mixer.Sound(get_real_path("snd", "toot_echo.ogg")))
|
||||
audio.append(pygame.mixer.Sound(get_real_path("snd", "toot_trip.ogg")))
|
||||
audio.append(pygame.mixer.Sound(get_real_path("snd", "toot_wet.ogg")))
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
# The main loop. This while loop runs until "running" is False
|
||||
while running is True:
|
||||
# We'll get the current mouse cursor position
|
||||
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||
# If the rectangle bounding the emoji is currently
|
||||
# colliding with the current mouse cursor position
|
||||
# Then change cursor
|
||||
if emoji.get_rect(topleft=(emoji_x, emoji_y)).collidepoint(mouse_x, mouse_y):
|
||||
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND)
|
||||
emoji_hover = True
|
||||
else:
|
||||
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW)
|
||||
emoji_hover = False
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
# pygame.MOUSEBUTTONDOWN happens the moment a mouse button is clicked
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and emoji_hover is True:
|
||||
audio[random.randrange(0, len(audio))].play()
|
||||
# pygame.KEYDOWN happens the moment a key is pressed down
|
||||
if event.type == pygame.KEYDOWN:
|
||||
# If the pressed key is "ESC" button, then let's exit
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
running = False
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
# We get to this poince once "running" is False.
|
||||
# Clean up pygame with the quit() method, then exit program.
|
||||
pygame.quit()
|
||||
sys.exit()
|
39
PyGame/Basics/015_pyinstaller.spec
Normal file
|
@ -0,0 +1,39 @@
|
|||
# -*- mode: python ; coding: utf-8 -*-
|
||||
|
||||
|
||||
a = Analysis(
|
||||
['015_pyinstaller.py'],
|
||||
pathex=[],
|
||||
binaries=[],
|
||||
datas=[('font\\Pixeboy.ttf', 'font'), ('img\\melon_icon_64.png', 'img'), ('img\\poop_emoji_sm.png', 'img'), ('snd\\toot_echo.ogg', 'snd'), ('snd\\toot_trip.ogg', 'snd'), ('snd\\toot_wet.ogg', 'snd'), ('snd\\toot.ogg', 'snd')],
|
||||
hiddenimports=[],
|
||||
hookspath=[],
|
||||
hooksconfig={},
|
||||
runtime_hooks=[],
|
||||
excludes=[],
|
||||
noarchive=False,
|
||||
optimize=0,
|
||||
)
|
||||
pyz = PYZ(a.pure)
|
||||
|
||||
exe = EXE(
|
||||
pyz,
|
||||
a.scripts,
|
||||
a.binaries,
|
||||
a.datas,
|
||||
[],
|
||||
name='015_pyinstaller',
|
||||
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=['img\\poop_emoji.ico'],
|
||||
)
|
BIN
PyGame/Basics/img/poop_emoji.ico
Normal file
After Width: | Height: | Size: 332 KiB |
Before Width: | Height: | Size: 364 KiB After Width: | Height: | Size: 365 KiB |
BIN
PyGame/Basics/img/poop_emoji_16.png
Normal file
After Width: | Height: | Size: 3.6 KiB |
BIN
PyGame/Basics/img/poop_emoji_256.png
Normal file
After Width: | Height: | Size: 53 KiB |
BIN
PyGame/Basics/img/poop_emoji_32.png
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
PyGame/Basics/img/poop_emoji_64.png
Normal file
After Width: | Height: | Size: 10 KiB |