How do I change the volume of the sound or music in PyGame?

Changing the volume depends on whether you are playing a pygame.mixer.Sound object or playing the music via the pygame.mixer.music module. The volume of a Sound can be changed by set_volume(). The volume argument is a value in range [0.0, 1.0]: pygame.mixer.init() my_sound = pygame.mixer.Sound(‘my_sound.wav’) my_sound.play() my_sound.set_volume(0.5) The volume of the music can be changed by … Read more

Play WAV file in Python

You can use PyAudio. An example here on my Linux it works: #!usr/bin/env python #coding=utf-8 import pyaudio import wave #define stream chunk chunk = 1024 #open a wav format music f = wave.open(r”/usr/share/sounds/alsa/Rear_Center.wav”,”rb”) #instantiate PyAudio p = pyaudio.PyAudio() #open stream stream = p.open(format = p.get_format_from_width(f.getsampwidth()), channels = f.getnchannels(), rate = f.getframerate(), output = True) #read … Read more

How can I drag more than 2 images in PyGame?

Create a list of 4 images: images = [img1, img2, img3, img4] Create a list of image rectangles: img_rects = [images[i].get_rect(topleft = (20+40*i, 20)) for i in range(len(images))] Use pygame.Rect.collidelist to find the image which is clicked: if e.type == pygame.MOUSEBUTTONDOWN: mouse_rect = pygame.Rect(e.pos, (1, 1)) current_image = mouse_rect.collidelist(img_rects) Draw the current_image: if e.type == … Read more

Pygame not returning events while embedded in PyQt

A functional code implementation: import time import contextlib from PySide6.QtCore import QObject, Signal, Qt, QThread from PySide6.QtGui import QImage, QPixmap from PySide6.QtWidgets import QMainWindow, QLabel, QWidget, QVBoxLayout, QApplication with contextlib.redirect_stdout(None): import pygame DISPLAY_WIDTH = 800 DISPLAY_HEIGHT = 600 class PygameWorker(QObject): send_image = Signal(QImage) def run(self): # Initialise screen pygame.init() screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT), pygame.OPENGL) # … Read more

Pygame: Collision by Sides of Sprite

There is no function to get sides collision in PyGame. But you could try to use pygame.Rect.collidepoint to test if A.rect.midleft, A.rect.midright, A.rect.midtop, A.rect.midbottom, A.rect.topleft, A.rect.bottomleft , A.rect.topright, A.rect.bottomright are inside B.rect (pygame.Rect). EDIT: Example code. Use arrows to move player and touch enemy. (probably it is not optimal solution) import pygame WHITE = (255,255,255) … Read more

how to make sprite move upwards and downwards with joystick in pygame

First ensure that you have a joystick, by getting the number of joysticks by pygame.joystick.get_count(). Initialize the joystick by pygame.joystick.Joystick.init: joystick = None if pygame.joystick.get_count() > 0: joystick = pygame.joystick.Joystick(0) joystick.init() Once a joystick is initialized, ist axis value can be get by pygame.joystick.Joystick.get_axis. The value returned by this function is in range [-1, 1]. … Read more

Is there an effiecient way of making a function to drag and drop multiple png’s?

Let’s walk through this step by step. Step 1: Let’s start with a basic skeleton of every pygame game: import pygame def main(): screen = pygame.display.set_mode((640, 480)) clock = pygame.time.Clock() while True: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: return screen.fill(pygame.Color(‘grey’)) pygame.display.flip() clock.tick(60) if __name__ == ‘__main__’: main() We create a … Read more

pygame error, No video mode has been set

I believe that you need to call: screen = pygame.display.set_mode((800, 600)) # change to the real resolution this call will actually return the surface that you want to blit on. Below is the documentation from the linked resource. pygame.display.set_mode() Initialize a window or screen for display set_mode(resolution=(0,0), flags=0, depth=0) -> Surface This function will create … Read more