Clean up capitalization of globals and variable placement.

This commit is contained in:
Junior 2024-06-27 09:36:06 -04:00
parent e447a24348
commit 0b2ffe39a4
4 changed files with 52 additions and 52 deletions

View File

@ -12,15 +12,14 @@ import sys
# This initializes the back-end bits and pieces of pygame # This initializes the back-end bits and pieces of pygame
pygame.init() pygame.init()
# Some variables used to initialize the window # Some global variables used to initialize the window
# The width and height are in pixels # The width and height are in pixels
win_width = 600 WIN_WIDTH = 600
win_height = 480 WIN_HEIGHT = 480
running = True
# We create a window object used to draw pygame things # We create a window object used to draw pygame things
# It is initialized with set_mode((width, height)) # It is initialized with set_mode((width, height))
window = pygame.display.set_mode((win_width, win_height)) window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
# This sets the text in the window's title bar # This sets the text in the window's title bar
pygame.display.set_caption("PyGame Basics") pygame.display.set_caption("PyGame Basics")
# this clock variable is just an instance of the # this clock variable is just an instance of the
@ -29,6 +28,7 @@ pygame.display.set_caption("PyGame Basics")
clock = pygame.time.Clock() clock = pygame.time.Clock()
# The main loop. This while loop runs until "running" is False # The main loop. This while loop runs until "running" is False
running = True
while running is True: while running is True:
# pygame.event.get() returns any events that have happened # pygame.event.get() returns any events that have happened
# since the last call to get(). This lets us catch any # since the last call to get(). This lets us catch any

View File

@ -7,15 +7,14 @@ pygame.init()
# Some variables used to initialize the window # Some variables used to initialize the window
# The width and height are in pixels # The width and height are in pixels
win_width = 600 WIN_WIDTH = 600
win_height = 480 WIN_HEIGHT = 480
win_bg_color = (26, 110, 43) WIN_BG_COLOR = (26, 110, 43)
text_color = (242, 238, 10) TEXT_COLOR = (242, 238, 10)
running = True
# We create a window object used to draw pygame things # We create a window object used to draw pygame things
# It is initialized with set_mode((width, height)) # It is initialized with set_mode((width, height))
window = pygame.display.set_mode((win_width, win_height)) window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('PyGame Fancy Basics') pygame.display.set_caption('PyGame Fancy Basics')
# In order to work with images they must be turned into # In order to work with images they must be turned into
# an object. The image.load() method is used to read in an # an object. The image.load() method is used to read in an
@ -29,7 +28,7 @@ icon.convert_alpha()
pygame.display.set_icon(icon) pygame.display.set_icon(icon)
# This will fill the entire window with a single color # This will fill the entire window with a single color
# The color parameter must be a three element tuple (r, g, b) # The color parameter must be a three element tuple (r, g, b)
window.fill(win_bg_color) window.fill(WIN_BG_COLOR)
# Here we use the Font() method to import a True Type font # Here we use the Font() method to import a True Type font
# from the "font/" folder with a size of 200 # from the "font/" folder with a size of 200
pixeboy_200 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 200) pixeboy_200 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 200)
@ -39,14 +38,14 @@ pixeboy_200 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 200)
# is font antialiased (pretty much always True) # is font antialiased (pretty much always True)
# color of text (this is a R, G, B tuple) # color of text (this is a R, G, B tuple)
# This is called a "text surface" # This is called a "text surface"
title_text_1 = pixeboy_200.render("HELLO", True, text_color) title_text_1 = pixeboy_200.render("HELLO", True, TEXT_COLOR)
title_text_2 = pixeboy_200.render("WORLD", True, text_color) title_text_2 = pixeboy_200.render("WORLD", True, TEXT_COLOR)
# Text is drawn on the screen by specifying the top left # Text is drawn on the screen by specifying the top left
# corner of an imaginary rectangle that contains the text # corner of an imaginary rectangle that contains the text
# We want to center this text in the window so we # We want to center this text in the window so we
# subtract half the width of the text (get_width()) # subtract half the width of the text (get_width())
# from half the width of the program window # from half the width of the program window
text_x = (win_width // 2) - (title_text_1.get_width() // 2) text_x = (WIN_WIDTH // 2) - (title_text_1.get_width() // 2)
# We'll start 25 pixels from the top of the window # We'll start 25 pixels from the top of the window
text_y = 25 text_y = 25
# The blit() method draws a text surface in the specified # The blit() method draws a text surface in the specified
@ -57,7 +56,7 @@ window.blit(title_text_1, (text_x, text_y))
# Now we do the same thing for the second line of text # Now we do the same thing for the second line of text
# just moved down (y) by the height of the first line of # just moved down (y) by the height of the first line of
# text plus an extra 25 pixels for separation # text plus an extra 25 pixels for separation
text_x = (win_width // 2) - (title_text_2.get_width() // 2) text_x = (WIN_WIDTH // 2) - (title_text_2.get_width() // 2)
text_y = 25 + title_text_2.get_height() + 25 text_y = 25 + title_text_2.get_height() + 25
window.blit(title_text_2, (text_x, text_y)) window.blit(title_text_2, (text_x, text_y))
# Just like the icon used for the title bar # Just like the icon used for the title bar
@ -65,13 +64,14 @@ window.blit(title_text_2, (text_x, text_y))
emoji = pygame.image.load(os.path.join("img", "poop_emoji_sm.png")) emoji = pygame.image.load(os.path.join("img", "poop_emoji_sm.png"))
emoji.convert_alpha() emoji.convert_alpha()
# Let's place the image 25 pixels from the bottom of the window # Let's place the image 25 pixels from the bottom of the window
emoji_x = (win_width // 2) - (emoji.get_width() // 2) emoji_x = (WIN_WIDTH // 2) - (emoji.get_width() // 2)
emoji_y = win_height - 25 - emoji.get_height() emoji_y = WIN_HEIGHT - 25 - emoji.get_height()
# And we blit/draw it onto the window the same way as we do text # And we blit/draw it onto the window the same way as we do text
window.blit(emoji, (emoji_x, emoji_y)) window.blit(emoji, (emoji_x, emoji_y))
clock = pygame.time.Clock() clock = pygame.time.Clock()
# The main loop. This while loop runs until "running" is False # The main loop. This while loop runs until "running" is False
running = True
while running is True: while running is True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:

View File

@ -8,43 +8,41 @@ pygame.init()
# Some variables used to initialize the window # Some variables used to initialize the window
# The width and height are in pixels # The width and height are in pixels
win_width = 600 WIN_WIDTH = 600
win_height = 480 WIN_HEIGHT = 480
win_bg_color = (26, 110, 43) WIN_BG_COLOR = (26, 110, 43)
text_color = (242, 238, 10) TEXT_COLOR = (242, 238, 10)
running = True
emoji_hover = False
window = pygame.display.set_mode((win_width, win_height)) window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('PyGame Fancy Basics') pygame.display.set_caption('PyGame Fancy Basics')
icon = pygame.image.load(os.path.join("img", "melon_icon_64.png")) icon = pygame.image.load(os.path.join("img", "melon_icon_64.png"))
icon.convert_alpha() icon.convert_alpha()
pygame.display.set_icon(icon) pygame.display.set_icon(icon)
window.fill(win_bg_color) window.fill(WIN_BG_COLOR)
# Import font # Import font
pixeboy_200 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 200) pixeboy_200 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 200)
# Create two lines of text # Create two lines of text
title_text_1 = pixeboy_200.render("HELLO", True, text_color) title_text_1 = pixeboy_200.render("HELLO", True, TEXT_COLOR)
title_text_2 = pixeboy_200.render("WORLD", True, text_color) title_text_2 = pixeboy_200.render("WORLD", True, TEXT_COLOR)
# Draw first line of text # Draw first line of text
text_x = (win_width // 2) - (title_text_1.get_width() // 2) text_x = (WIN_WIDTH // 2) - (title_text_1.get_width() // 2)
text_y = 25 text_y = 25
window.blit(title_text_1, (text_x, text_y)) window.blit(title_text_1, (text_x, text_y))
# Draw second line of text # Draw second line of text
text_x = (win_width // 2) - (title_text_2.get_width() // 2) text_x = (WIN_WIDTH // 2) - (title_text_2.get_width() // 2)
text_y = 25 + title_text_2.get_height() + 25 text_y = 25 + title_text_2.get_height() + 25
window.blit(title_text_2, (text_x, text_y)) window.blit(title_text_2, (text_x, text_y))
# Import image # Import image
emoji = pygame.image.load(os.path.join("img", "poop_emoji_sm.png")) emoji = pygame.image.load(os.path.join("img", "poop_emoji_sm.png"))
emoji.convert_alpha() emoji.convert_alpha()
# Let's place the image 25 pixels from the bottom of the window # Let's place the image 25 pixels from the bottom of the window
emoji_x = (win_width // 2) - (emoji.get_width() // 2) emoji_x = (WIN_WIDTH // 2) - (emoji.get_width() // 2)
emoji_y = win_height - 25 - emoji.get_height() emoji_y = WIN_HEIGHT - 25 - emoji.get_height()
window.blit(emoji, (emoji_x, emoji_y)) window.blit(emoji, (emoji_x, emoji_y))
pixeboy_20 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 20) pixeboy_20 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 20)
click_text = pixeboy_20.render("Click Me", True, text_color) click_text = pixeboy_20.render("Click Me", True, TEXT_COLOR)
text_x = (win_width // 2) - (click_text.get_width() // 2) text_x = (WIN_WIDTH // 2) - (click_text.get_width() // 2)
text_y = win_height - 5 - click_text.get_height() text_y = WIN_HEIGHT - 5 - click_text.get_height()
window.blit(click_text, (text_x, text_y)) window.blit(click_text, (text_x, text_y))
# We can import audio files with mixer.Sound() # We can import audio files with mixer.Sound()
# Only .ogg (Ogg Vorbis) format files should be used # Only .ogg (Ogg Vorbis) format files should be used
@ -58,6 +56,8 @@ audio.append(pygame.mixer.Sound(os.path.join("snd", "toot_wet.ogg")))
clock = pygame.time.Clock() clock = pygame.time.Clock()
# The main loop. This while loop runs until "running" is False # The main loop. This while loop runs until "running" is False
running = True
emoji_hover = False
while running is True: while running is True:
# We'll get the current mouse cursor position # We'll get the current mouse cursor position
mouse_x, mouse_y = pygame.mouse.get_pos() mouse_x, mouse_y = pygame.mouse.get_pos()

View File

@ -14,12 +14,10 @@ pygame.init()
# Some variables used to initialize the window # Some variables used to initialize the window
# The width and height are in pixels # The width and height are in pixels
win_width = 600 WIN_WIDTH = 600
win_height = 480 WIN_HEIGHT = 480
win_bg_color = (26, 110, 43) WIN_BG_COLOR = (26, 110, 43)
text_color = (242, 238, 10) TEXT_COLOR = (242, 238, 10)
running = True
emoji_hover = False
# This function takes a list of path arguments # This function takes a list of path arguments
# and prepends the sys._MEIPASS variable if the # and prepends the sys._MEIPASS variable if the
@ -32,36 +30,36 @@ def get_real_path(*path_args: str):
my_path = os.path.join(*path_args) my_path = os.path.join(*path_args)
return my_path return my_path
window = pygame.display.set_mode((win_width, win_height)) window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('PyGame Fancy Basics') pygame.display.set_caption('PyGame Fancy Basics')
icon = pygame.image.load(get_real_path("img", "melon_icon_64.png")) icon = pygame.image.load(get_real_path("img", "melon_icon_64.png"))
icon.convert_alpha() icon.convert_alpha()
pygame.display.set_icon(icon) pygame.display.set_icon(icon)
window.fill(win_bg_color) window.fill(WIN_BG_COLOR)
# Import font # Import font
pixeboy_200 = pygame.font.Font(get_real_path("font", "Pixeboy.ttf"), 200) pixeboy_200 = pygame.font.Font(get_real_path("font", "Pixeboy.ttf"), 200)
# Create two lines of text # Create two lines of text
title_text_1 = pixeboy_200.render("HELLO", True, text_color) title_text_1 = pixeboy_200.render("HELLO", True, TEXT_COLOR)
title_text_2 = pixeboy_200.render("WORLD", True, text_color) title_text_2 = pixeboy_200.render("WORLD", True, TEXT_COLOR)
# Draw first line of text # Draw first line of text
text_x = (win_width // 2) - (title_text_1.get_width() // 2) text_x = (WIN_WIDTH // 2) - (title_text_1.get_width() // 2)
text_y = 25 text_y = 25
window.blit(title_text_1, (text_x, text_y)) window.blit(title_text_1, (text_x, text_y))
# Draw second line of text # Draw second line of text
text_x = (win_width // 2) - (title_text_2.get_width() // 2) text_x = (WIN_WIDTH // 2) - (title_text_2.get_width() // 2)
text_y = 25 + title_text_2.get_height() + 25 text_y = 25 + title_text_2.get_height() + 25
window.blit(title_text_2, (text_x, text_y)) window.blit(title_text_2, (text_x, text_y))
# Import image # Import image
emoji = pygame.image.load(get_real_path("img", "poop_emoji_sm.png")) emoji = pygame.image.load(get_real_path("img", "poop_emoji_sm.png"))
emoji.convert_alpha() emoji.convert_alpha()
# Let's place the image 25 pixels from the bottom of the window # Let's place the image 25 pixels from the bottom of the window
emoji_x = (win_width // 2) - (emoji.get_width() // 2) emoji_x = (WIN_WIDTH // 2) - (emoji.get_width() // 2)
emoji_y = win_height - 25 - emoji.get_height() emoji_y = WIN_HEIGHT - 25 - emoji.get_height()
window.blit(emoji, (emoji_x, emoji_y)) window.blit(emoji, (emoji_x, emoji_y))
pixeboy_20 = pygame.font.Font(get_real_path("font", "Pixeboy.ttf"), 20) pixeboy_20 = pygame.font.Font(get_real_path("font", "Pixeboy.ttf"), 20)
click_text = pixeboy_20.render("Click Me", True, text_color) click_text = pixeboy_20.render("Click Me", True, TEXT_COLOR)
text_x = (win_width // 2) - (click_text.get_width() // 2) text_x = (WIN_WIDTH // 2) - (click_text.get_width() // 2)
text_y = win_height - 5 - click_text.get_height() text_y = WIN_HEIGHT - 5 - click_text.get_height()
window.blit(click_text, (text_x, text_y)) window.blit(click_text, (text_x, text_y))
# We can import audio files with mixer.Sound() # We can import audio files with mixer.Sound()
# Only .ogg (Ogg Vorbis) format files should be used # Only .ogg (Ogg Vorbis) format files should be used
@ -75,6 +73,8 @@ audio.append(pygame.mixer.Sound(get_real_path("snd", "toot_wet.ogg")))
clock = pygame.time.Clock() clock = pygame.time.Clock()
# The main loop. This while loop runs until "running" is False # The main loop. This while loop runs until "running" is False
running = True
emoji_hover = False
while running is True: while running is True:
# We'll get the current mouse cursor position # We'll get the current mouse cursor position
mouse_x, mouse_y = pygame.mouse.get_pos() mouse_x, mouse_y = pygame.mouse.get_pos()