This week's pygame tuto was how-to add sounds to a game.
The recommended source for sounds is the following:
Leshy SFMaker - Online Sound Effect Generator (leshylabs.com)
Below, the current main.py of my pygame tuto. I have added sound effects and music.
And, of course, these have been added to my game file so that the code can reference them...
import pygame
# Pygame Setup Stuff
pygame.init()
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 500
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Codemy.com Pygame Tutorial')
clock = pygame.time.Clock()
running = True
dt = 0
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
# Load our sound effects
sound_1 = pygame.mixer.Sound('Sounds/sound_1.wav')
# Play our sound effects
#pygame.time.delay(3000)
#sound_1.play()
# Change the volume of the sound effect
#sound_1.set_volume(.2)
# Time delay
#pygame.time.delay(2000)
#sound_1.play()
# Load BG Music
pygame.mixer.music.load('Sounds/bg.wav')
# Play the BG music
pygame.mixer.music.play(-1, 0.0) # Repeats, and where to start playing
# Delay then stop music
pygame.time.delay(15000)
pygame.mixer.music.stop()
# Display All our Fonts (or not...)
#fonts = pygame.font.get_fonts()
#for font in fonts:
#print(font)
# Define the fonts
system_font = pygame.font.SysFont('impact', 80)
downloaded_font = pygame.font.Font('DangerNightPersonalUse-owdl4.otf', 80)
# Render the text as surface: Text, boolean for antialiasing, text-color, bg-color
system_font = system_font.render("This is Impact", True, "blue", "silver")
downloaded_font = downloaded_font.render("This is Danger!!", "true", "blue", "silver")
# Get Rect
system_font_rect = system_font.get_rect()
downloaded_font_rect = downloaded_font.get_rect()
# Position the text
system_font_rect.center = (WINDOW_WIDTH/2, 100)
downloaded_font_rect.center = (WINDOW_WIDTH/2, 200)
# load our images
hero_right = pygame.image.load("hero_right.png")
hero_left = pygame.image.load("hero_left.png")
#f_hero_right = pygame.image.load("f_hero_right.png")
# get rect surrounding our images
hero_right_rect = hero_right.get_rect()
hero_left_rect = hero_left.get_rect()
#f_hero_right_rect = f_hero_right.get_rect()
# position our images
hero_right_rect.topleft = (0,0)
hero_left_rect.topright = (WINDOW_WIDTH, 0)
#f_hero_right_rect.topleft = (0,0)
while running:
# poll for events
# pygame.QUIT event means that the user clicked the X to close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Pick the screen color
screen.fill("silver")
# UNDERSTAND CO-ORDINATES
# Top Left Corner is 0,0
# As we move -> X increases, as you go down Y increases
# Draw a line
# (screen, color, starting point(x,y), ending point(x,y), thickness)
# pygame.draw.line(screen, "black", (0,50), (800,50), 2)
# Draw a circle
# (screen, color, center(x,y), radius, thickness: 0=fill)
# pygame.draw.circle(screen, "black", (WINDOW_WIDTH/2, WINDOW_HEIGHT/2), 100, 5)
# Draw a rectangle
# (screen, color, (top-left x, top-left y, width, height))
# pygame.draw.rect(screen, "red", (100, 200, 100, 100))
# RENDER OUR GAME HERE
#pygame.draw.circle(screen, "#033660", player_pos, 40)
# Blit the text onto the screen. The blit order determines what ends up on top on
# the screen...
screen.blit(system_font, system_font_rect)
screen.blit(downloaded_font, downloaded_font_rect)
# Blit (copy) screen object at given coordinates
screen.blit(hero_right, hero_right_rect)
screen.blit(hero_left, hero_left_rect)
# Move hero-right
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
hero_right_rect.y -= 300 * dt
if keys[pygame.K_DOWN]:
hero_right_rect.y += 300 * dt
sound_1.play()
if keys[pygame.K_LEFT]:
hero_right_rect.x -= 300 * dt
if keys[pygame.K_RIGHT]:
hero_right_rect.x += 300 * dt
"""
# Check to see if mouse has been pressed
if pygame.mouse.get_pressed()[0]:
# Move the circle
if event.type == pygame.MOUSEMOTION:
pos = pygame.mouse.get_pos()
player_pos.x = pos[0]
player_pos.y = pos[1]
if pygame.mouse.get_pressed()[2]:
pygame.draw.circle(screen, "red", player_pos, 40)
# Use the mouse!!
if event.type == pygame.MOUSEBUTTONDOWN:
#print(event)
pos = pygame.mouse.get_pos()
# Move the circle
player_pos.x = pos[0]
player_pos.y = pos[1]
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
pygame.draw.circle(screen, "red", player_pos, 40)
# MOTION!
if event.type == pygame.MOUSEMOTION:
pos = pygame.mouse.get_pos()
# Move the circle
player_pos.x = pos[0]
player_pos.y = pos[1]
"""
# flip the display to output our work to the screen
pygame.display.flip()
# Set the clock stuff / delta time in seconds since the last frame
# used for framerate independent physics
dt = clock.tick(60) / 1000
pygame.quit()
No comments:
Post a Comment