diff --git a/PyGame/Basics/000_Intro.py b/PyGame/Basics/000_Intro.py new file mode 100644 index 0000000..dc09bf1 --- /dev/null +++ b/PyGame/Basics/000_Intro.py @@ -0,0 +1,52 @@ +# These are the minimum suggested module imports to get started. +# "os" will be used to set a system environment variable +import os +# This environment variable, if present, will supress the printout +# of the PyGame welcome text each time the program is launched +os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" +# The pygame module itself +import pygame +# We use the sys module to cleanly exit the program +import sys + +# This initializes the back-end bits and pieces of pygame +pygame.init() + +# Some variables used to initialize the window +# The width and height are in pixels +win_width = 600 +win_height = 480 +running = True + +# We create a window object used to draw pygame things +# It is initialized with set_mode((width, height)) +window = pygame.display.set_mode((win_width, win_height)) +# This sets the text in the window's title bar +pygame.display.set_caption("PyGame Basics") +# this clock variable is just an instance of the +# pygame.time.Clock() class so it's less wordy when we +# need to do clock/fps related tasks +clock = pygame.time.Clock() + +# The main loop. This while loop runs until "running" is False +while running is True: + # pygame.event.get() returns any events that have happened + # since the last call to get(). This lets us catch any + # key presses/releases, mouse clicks, etc. + for event in pygame.event.get(): + # pygame.QUIT event happens when you click the "X" + # in the window title. + if event.type == pygame.QUIT: + running = False + # flip() updates the entire window with all changes since + # the last update + pygame.display.flip() + # tick() is typically used to set the frame rate of the + # application (fps) so it doesn't run too fast, and doesn't + # use excessive CPU cycles when not needed + 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() \ No newline at end of file diff --git a/PyGame/Basics/005_windowskin.py b/PyGame/Basics/005_windowskin.py new file mode 100644 index 0000000..1c20056 --- /dev/null +++ b/PyGame/Basics/005_windowskin.py @@ -0,0 +1,90 @@ +import os +os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" +import pygame +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 + +# We create a window object used to draw pygame things +# It is initialized with set_mode((width, height)) +window = pygame.display.set_mode((win_width, win_height)) +pygame.display.set_caption('PyGame Fancy Basics') +# In order to work with images they must be turned into +# an object. The image.load() method is used to read in an +# image and create an object which can be manipulated. +icon = pygame.image.load(os.path.join("img", "melon_icon_64.png")) +# If the image has transparent portions the convert_alpha() method +# must be called to properly render the image when used +icon.convert_alpha() +# This will set the icon used on the title bar and in the +# system tray. A 64x64 icon should be used. +pygame.display.set_icon(icon) +# This will fill the entire window with a single color +# The color parameter must be a three element tuple (r, g, b) +window.fill(win_bg_color) +# Here we use the Font() method to import a True Type font +# from the "font/" folder with a size of 200 +pixeboy_200 = pygame.font.Font(os.path.join("font", "Pixeboy.ttf"), 200) +# We create two lines of text with the font.render() method +# Parameters are: +# text to display (a string) +# is font antialiased (pretty much always True) +# color of text (this is a R, G, B tuple) +# This is called a "text surface" +title_text_1 = pixeboy_200.render("HELLO", True, text_color) +title_text_2 = pixeboy_200.render("WORLD", True, text_color) +# Text is drawn on the screen by specifying the top left +# corner of an imaginary rectangle that contains the text +# We want to center this text in the window so we +# subtract half the width of the text (get_width()) +# from half the width of the program window +text_x = (win_width // 2) - (title_text_1.get_width() // 2) +# We'll start 25 pixels from the top of the window +text_y = 25 +# The blit() method draws a text surface in the specified +# window at the coordinates specified (a (x, y) tuple) +# Note: This just draws the text. It won't show up until +# display.flip() or display.update() is called +window.blit(title_text_1, (text_x, text_y)) +# Now we do the same thing for the second line of text +# just moved down (y) by the height of the first line of +# text plus an extra 25 pixels for separation +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)) +# Just like the icon used for the title bar +# Any other images can be loaded into an object +emoji = pygame.image.load(os.path.join("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() +# And we blit/draw it onto the window the same way as we do text +window.blit(emoji, (emoji_x, emoji_y)) +clock = pygame.time.Clock() + +# The main loop. This while loop runs until "running" is False +while running is True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + # 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() \ No newline at end of file diff --git a/PyGame/Basics/010_mousesound.py b/PyGame/Basics/010_mousesound.py new file mode 100644 index 0000000..3cd3713 --- /dev/null +++ b/PyGame/Basics/010_mousesound.py @@ -0,0 +1,84 @@ +import os +os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" +import pygame +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 + +window = pygame.display.set_mode((win_width, win_height)) +pygame.display.set_caption('PyGame Fancy Basics') +icon = pygame.image.load(os.path.join("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(os.path.join("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(os.path.join("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(os.path.join("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 +audio = pygame.mixer.Sound(os.path.join("snd", "toot.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.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() \ No newline at end of file diff --git a/PyGame/Basics/font/Pixeboy.ttf b/PyGame/Basics/font/Pixeboy.ttf new file mode 100644 index 0000000..d303047 Binary files /dev/null and b/PyGame/Basics/font/Pixeboy.ttf differ diff --git a/PyGame/Basics/img/fruit_icons.jpg b/PyGame/Basics/img/fruit_icons.jpg new file mode 100644 index 0000000..66bb610 Binary files /dev/null and b/PyGame/Basics/img/fruit_icons.jpg differ diff --git a/PyGame/Basics/img/melon_icon_64.png b/PyGame/Basics/img/melon_icon_64.png new file mode 100644 index 0000000..f3bba53 Binary files /dev/null and b/PyGame/Basics/img/melon_icon_64.png differ diff --git a/PyGame/Basics/img/poop_emoji.png b/PyGame/Basics/img/poop_emoji.png new file mode 100644 index 0000000..f03904a Binary files /dev/null and b/PyGame/Basics/img/poop_emoji.png differ diff --git a/PyGame/Basics/img/poop_emoji_sm.png b/PyGame/Basics/img/poop_emoji_sm.png new file mode 100644 index 0000000..192762e Binary files /dev/null and b/PyGame/Basics/img/poop_emoji_sm.png differ diff --git a/PyGame/Basics/snd/toot.ogg b/PyGame/Basics/snd/toot.ogg new file mode 100644 index 0000000..96f1c66 Binary files /dev/null and b/PyGame/Basics/snd/toot.ogg differ