Pygame play a sound when click on certain spot on GUI - audio

I'm an amateur, very inexperience programmer. I've been working on an art project that I am programming using Pygame. I've hit a road block, ad can't figure out how to do what I need it to do.
I need it to play a specific sound when clicking on a specific place on the GUI. For example, when you click on the red button, it plays an audio file that says "red"
I also need it to be able to play sounds with clicking and dragging on the canvas part.
I hope this is enough detail. Thanks for the help!
import pygame, sys, time, random
from pygame.locals import *
# set up pygame
pygame.init()
pygame.mixer.init
pygame.mixer.get_init
bgimg="GUIsmall.gif"
inst="instructionssm.gif"
white=(255,255,255)
screen=pygame.display.set_mode((800,600), pygame.RESIZABLE)
screen.fill(white)
bg=pygame.image.load(bgimg)
instrimg=pygame.image.load(inst)
screen.blit(bg, (0,0))
pygame.display.flip()
red=pygame.mixer.Sound("red.mp3")
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
raise SystemExit
elif event.type==pygame.MOUSEBUTTONDOWN:
red.play(0,0,0)

I think you should have a class button and a collection of buttons:
class Button:
__init__(self, name, position, image_file, sound_file):
self.name = name
self.image = pygame.image.load(image_file)
self.sound = pygame.mixer.Sound(sound_file)
self.position = position
self.rect = pygame.Rect(position, self.image.get_size())
buttons = []
buttons.add( Button("red", (0,0), "red.png", "red.mp3") )
...
Then you can use it in the main loop:
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
raise SystemExit
elif event.type==pygame.MOUSEBUTTONDOWN:
for b in buttons:
if b.rect.collidepoint(event.pos):
b.sound.play()

Related

How to prompt user to open a file with python3?

I'm making a game and I want the user to be able to click a button which opens the file manager and asks them to open a game save file. Is there a module / how could I put this in my game? If there's no good gui way how would I make a text input thing (without using the terminal)? Thanks!
Pygame is a low-level library, so it doesn't have the kind of built-in dialogs you're after. You can create such, but it will be quicker to use the tkinter module which is distributed with Python and provides an interface to the TK GUI libraries. I'd recommend reading the documentation, but here's a function that will pop up a file selection dialog and then return the path selected:
def prompt_file():
"""Create a Tk file dialog and cleanup when finished"""
top = tkinter.Tk()
top.withdraw() # hide window
file_name = tkinter.filedialog.askopenfilename(parent=top)
top.destroy()
return file_name
Here's a small example incorporating this function, pressing Spacebar will popup the dialog:
import tkinter
import tkinter.filedialog
import pygame
WIDTH = 640
HEIGHT = 480
FPS = 30
def prompt_file():
"""Create a Tk file dialog and cleanup when finished"""
top = tkinter.Tk()
top.withdraw() # hide window
file_name = tkinter.filedialog.askopenfilename(parent=top)
top.destroy()
return file_name
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
f = "<No File Selected>"
frames = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
f = prompt_file()
# draw surface - fill background
window.fill(pygame.color.Color("grey"))
## update title to show filename
pygame.display.set_caption(f"Frames: {frames:10}, File: {f}")
# show surface
pygame.display.update()
# limit frames
clock.tick(FPS)
frames += 1
pygame.quit()
Notes: This will pause your game loop, as indicated by the frame counter but as events are being handled by the window manager, this shouldn't be an issue.
I'm not sure why I needed the explicit import tkinter.filedialog, but I get an AttributeError if I don't.
As for string entry in pygame, you might want to do this natively, in which case you'd be handling KEYUP events for letter keys to build the string and perhaps finishing when the user presses Enter or your own drawn button. You could continue down the tk path, in which case you'll want to use something like tkinter.simpledialog.askstring(…)
It is possible to make a file dialogue (though not using the native file explorer) using the pygame_gui module.
Simply create an instance of UIFileDialog and grab the path when the user hits 'ok':
file_selection = UIFileDialog(rect=Rect(0, 0, 300, 300), manager=manager, initial_file_path='C:\\')
if event.ui_element == file_selection.ok_button:
file_path = file_selection.current_file_path
If you want to allow selecting a directory set allow_picking_directories to True, but note that it does not allow picking an initial_file_path.
file_selection = UIFileDialog(rect=Rect(0, 0, 300, 300), manager=manager, allow_picking_directories=True)
Here's the above code in a simple program that allows you to pick a file when the button is clicked:
#!/usr/bin/env python
import pygame
import pygame_gui
from pygame_gui.windows.ui_file_dialog import UIFileDialog
from pygame_gui.elements.ui_button import UIButton
from pygame.rect import Rect
pygame.init()
window_surface = pygame.display.set_mode((800, 600))
background = pygame.Surface((800, 600))
background.fill(pygame.Color('#000000'))
manager = pygame_gui.UIManager((800, 600))
clock = pygame.time.Clock()
file_selection_button = UIButton(relative_rect=Rect(350, 250, 100, 100),
manager=manager, text='Select File')
while 1:
time_delta = clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.USEREVENT:
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == file_selection_button:
file_selection = UIFileDialog(rect=Rect(0, 0, 300, 300), manager=manager, allow_picking_directories=True)
if event.ui_element == file_selection.ok_button:
print(file_selection.current_file_path)
manager.process_events(event)
manager.update(time_delta)
window_surface.blit(background, (0, 0))
manager.draw_ui(window_surface)
pygame.display.update()

Encountering a Syntax Error while attempting to blit an image in Python 3.8

I am encountering a syntax error after I blit an image on my display window. I made a separate module where inside of it, I created a class that would manage all the aspects(position, behaviour) of the image. I loaded the image and fetched its rect and finally I drew the image in it's desired position. The file had no errors and so I shifted to the main file that managed game assets and behaviours. In the main file, I imported the class that managed the image. Then, I made a call(after filling the background) to draw the image so it appears on top of the background. It gave me the error
line 46
self.ship.blitme()
^
SyntaxError: invalid syntax
Here's the code snippet to the image class
import pygame
class Ship:
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
Here's the main class for managing game assets and behaviour
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# Set the background color.
self.bg_color = (230, 230, 230)
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
self._update_events()
# Redraw the screen during each pass through the loop.
def _check_events(self):
# Respond for keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _update_events(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill((self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
You forgot a closing bracket in line 45, main.py:
self.screen.fill((self.settings.bg_color) ) # <-- this one
Unfortunately, Python often marks a line beneath the wrong one.

How does the ship object know to get displayed centred at the bottom of the main screen surface?

These are the relevant source files:
1. ship.py
#!/usr/bin/python3
import pygame
class Ship(object):
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
2. alien_invasion.py
#!/usr/bin/python3
import pygame
from sys import exit
from settings import Settings
from ship import Ship
class AlienInvasion(object):
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for the game"""
while True:
# Watch for keyboard and mouse events. ** The Event Loop **
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
Running alien_invasion.py produces the following output...
...which is the desired output. I just don't understand how the ship's rect is positioned (correctly) centered at the bottom when the Ship.blitme() method takes parameters image and rect (and not rect.midbottom). To my understanding, this doesn't use the self.rect.midbottom attribute anywhere to draw the image on screen after defining it.
TIA!
When you create an instance of the ship class, you move its rect to the bottom middle of the screen with self.rect.midbottom = self.screen_rect.midbottom. This moves rect.x and rect.y as well, so when you blit the ship with the rect, it is already positioned right

How can I fix the slowness of my pygame event loop?

I'm creating a button class for a game, and I'm using the pygame event loop to detect mouse clicks (specifically when the mouse is released) (I hear it is better the method of using pygame.mousemget_pressed()[0]). However, the event loop seems to be slow, not responding and performing the buttons function when it is clicked. I think it may be because relate to how I created the event loop in a class, but I'm not sure. Here's a sample of my code:
class Button:
"""A Button class, built for all kinds of purposes"""
def __init__(self, window, rect, message, off_color, on_color, message_color, message_font_size):
pass # just a bunch of variables that use the parameters given
def in_button(self):
mouse_pos = pygame.mouse.get_pos()
if pygame.Rect(self.rect).collidepoint(mouse_pos):
return True
def clicked(self):
if self.in_button():
pygame.event.pump()
for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONUP:
return True
# I proceed to create 5 instances using this class.
I removed some unnecessary method information in my code. If you need anything more, please help me.
You have to implement the clicked method in a different way, because there should be only one event loop in your application, not one in every button instance. pygame.event.get() empties the event queue, so calling it multiple times per frame will cause problems.
I suggest to pass the events to the buttons. In this (very simple) example I pass the pygame.MOUSEBUTTONDOWN events to the clicked method and then check if the event.pos (mouse position) of the event collides with the rect. If it returns True, I do something in the event loop in the main function.
import pygame as pg
class Button:
def __init__(self, pos):
self.image = pg.Surface((100, 40))
self.image.fill(pg.Color('dodgerblue1'))
self.rect = self.image.get_rect(center=pos)
def clicked(self, event):
"""Check if the user clicked the button."""
# pygame.MOUSE* events have an `event.pos` attribute, the mouse
# position. You can use `pygame.mouse.get_pos()` as well.
return self.rect.collidepoint(event.pos)
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
button = Button((100, 60))
number = 0
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
# Pass the MOUSEBUTTONDOWN event to the buttons.
if button.clicked(event):
number += 1 # Do something.
print('clicked', number)
screen.fill((30, 30, 30))
screen.blit(button.image, button.rect)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
If you want a button with several images, you can use something similar to the button class here (the first addendum) or search for more sophisticated Button classes for pygame.

Key Pressing System PyGame

I'm making a basic pong game and want to make a system where a button press makes the paddle goes up or down. I'm fairly new to PyGame and here's my code so far.
import pygame, sys
from pygame.locals import*
def Pong():
pygame.init()
DISPLAY=pygame.display.set_mode((600,400),0,32)
pygame.display.set_caption("Pong")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
DISPLAY.fill(BLACK)
while True:
def P(b):
pygame.draw.rect(DISPLAY,WHITE,(50,b,50,10))
xx=150
P(xx)
for event in pygame.event.get():
if event.type==KEYUP and event.key==K_W:
P(xx+10)
xx=xx+10
pygame.display.update()
elif event.type==QUIT:
pygame.quit()
sys.exit()
Please read some tutorials and/or docs. This is really basic and if you don't get this right it will bite you later.
Anyway, here's how it could look:
import pygame
pygame.init()
DISPLAY = pygame.display.set_mode((600,400))
paddle = pygame.Rect(0, 0, 10, 50)
paddle.midleft = DISPLAY.get_rect().midleft
speed = 10
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit(0)
keystate = pygame.key.get_pressed()
dy = keystate[pygame.K_s] - keystate[pygame.K_w]
paddle.move_ip(0, dy * speed)
DISPLAY.fill(pygame.Color("black"))
pygame.draw.rect(DISPLAY, pygame.Color("white"), paddle)
pygame.display.update()
clock.tick(30)
That is ok for Pong but I still wouldn't write my game like this. So read some tutorials and maybe try CodeReview if you really want to improve your code.

Resources