Currently, when I play any audio using pygame's mixer I get an audio stream called Python (v3.8): Audio Stream with the python logo on the side. I'd like to change the name and logo to that of my application, how can I do this?
Here is a screenshot of the audio stream:
https://imgur.com/mJQ5FY9.png
Here is an example of the code:
from pygame import mixer
mixer.init()
mixer.music.load("forest.ogg")
mixer.music.play()
while not mixer.get_busy():
pass
What you see is a system generated window, but not a Pygame window. Some systems may not even create a window in this case. You cannot change the symbol and the label of this window with Pygame.
As an option you can create your own window and set the caption with pygame.display.set_caption() and the icon with pygame.display.set_icon() (On some systems, the icon must be set before the window is created)
Minimal example:
import pygame
song_name = "zelda_chest.mp3"
pygame.init()
font = pygame.font.SysFont(None, 40)
title_surf = font.render(song_name, True, (255, 255, 0))
pygame.display.set_icon(pygame.image.load("MusicNote.png"))
window = pygame.display.set_mode((max(400, title_surf.get_width()+20), 100))
pygame.display.set_caption(song_name)
pygame.mixer.init()
pygame.mixer.music.load(song_name)
pygame.mixer.music.play()
window_center = window.get_rect().center
clock = pygame.time.Clock()
run = True
while run and pygame.mixer.music.get_busy():
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
window.blit(title_surf, title_surf.get_rect(center = window_center))
pygame.display.flip()
pygame.quit()
exit()
You need:
pygame.display.set_icon(your_icon)
your_icon needs to be a pygame.Surface (e.g. a loaded image (pygame.image.load(path))
Related
Like the title says I am confused why I get the error: pygame.error: video system not initialized
As far as i understand this error is raised if you forget to initialize your code with pygame.init() but I did, here's my code and thanks in advance:
import pygame
from pygame.locals import *
pygame.init()
screen_height = 700
screen_width = 1000
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("platformer")
# load images
sun_img = pygame.image.load("img/sun.png")
backround_img = pygame.image.load("img/sky.png")
run = True
while run:
screen.blit (backround_img,(0,0))
screen.blit(sun_img, (100, 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.update()
the error does not crash the window or anything and it works as intended
its just somewhat annoying.
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.update()
When the event loop happens, if you close the window, you call pygame.quit(), which quits pygame. Then it tries to do:
pygame.display.update()
But pygame has quit, which is why you are getting the message.
separate the logic out. Events in one function or method, updating in another, and drawing and updating the display in another to avoid this.
So im just following a simple Pygame tutorial on youtube and it should work but for some reason every time i try to close the app it crashes and doesn't close normally, here's the code:
import pygame
# Initialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((800, 600))
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# Run game until x is pressed
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
im using the latest version of pycharm and python.
Add a pygame.display.quit() at the last line, you need to tell it to close the window.
Following the Python Crash Course, I copied this input and ran it with an error
import os
import pygame
def play_game():
#Creates Screen
resolution = pygame.display.set_mode((1920, 1080))
pygame.init()
pygame.display.set_caption('Game')
#Colors the background
bg_color = (230,230,230)
#Starts the game
while True:
#Sets background color
screen.fill(bg_color)
#Looks for user inputs
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#updates the screen
pygame.display.flip()
play_game()
The error reads:
NameError: name 'screen' is not defined
How do I fix this? I believe I installed pygame and pip correctly.
You didn't define the name 'screen'. Use the same variable to refer that in screen.fill(), change it to resolution.fill()
Just change line five to :
screen = pygame.display.set_mode((1920, 1080))
I am unable to see the image when I start the program. I have a folder named "game" and "mygame.py" file with a "background.png" all in it. I have tried using the the PATH "/game/background.png" instead of "background.png" but it doesn't seem to work. Any ideas?
my code:
import pygame , sys
pygame.init()
#screen start
def screen():
screen = [1024,768]
screen = pygame.display.set_mode(screen,0,32)
pygame.display.set_caption("Testing Caption")
background = pygame.image.load("background.png")
screen.blit(background, (0,0))
while True:
screen.blit(background, (0,0))
#keyboard commands
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen()
Thank you.
You are missing the flip/update call:
clock = pygame.time.Clock()
while True:
#keyboard commands
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(background, (0,0))
pygame.display.flip()
clock.tick(40) # keep program running at given FPS
Every blit occurs in a internal buffer, you need to call flip or update once per frame to update the real screen.
To my knowledge you can make this work one of two ways.
You can use an absolute path to the file, such as:
"C:\path_to_game_folder\game\background.png"
or you can use a relative path. To do this add the following code to your program:
import os
dir = os.path.dirname(__file__)
backgroundFile = os.path.join(dir, "background.png")
and change:
pygame.image.load("background.png")
to
pygame.image.load(backgroundFile)
I suggest using relative paths whenever possible it keeps the code portable as well as makes it easier to maintain and distribute.
I got it! Using pygame its important to use "pygame.display.update()" inside while True: screen.flip doesn't work with pygame to refresh or update the screen. Thanks to the users who responded earlier though.
Full Code:
import pygame , sys
pygame.init()
#screen start
def screen():
width , height = 1280,768
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Testing Caption")
background = pygame.image.load("background.jpg")
screen.blit(background, (0,0))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(background, (0,0))
pygame.display.update()
screen()
The code loads up a pygame screen window, but when I click the X to close it, it becomes unresponsive. I'm running on a 64-bit system, using a 32-bit python and 32-bit pygame.
from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
games.screen.mainloop()
Mach1723's answer is correct, but I would like to suggest another variant of a main loop:
while 1:
for event in pygame.event.get():
if event.type == QUIT: ## defined in pygame.locals
pygame.quit()
sys.exit()
if event.type == ## Handle other event types here...
## Do other important game loop stuff here.
I'd recommend the following code. First, it includes Clock so your program doesn't eat the CPU doing nothing but polling for events. Second, it calls pygame.quit() which prevents the program from freezing when running under IDLE on windows.
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
pygame.init()
# Set the height and width of the screen
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
#Loop until the user clicks the close button.
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
# -------- Main Program Loop -----------
while done==False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# Set the screen background
screen.fill(black)
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit ()
This is a pretty simple issue, you need to handle the "QUIT" event, see the event documentation at: http://www.pygame.org/docs/ref/event.html
EDIT:
It occurs to me now that you might be handling the "QUIT" event and its not working
but without more details to your code I dunno.
A quick example of a simple way to handle the "QUIT" event:
import sys
import pygame
# Initialize pygame
pygame.init()
pygame.display.set_mode(resolution=(640, 480))
# Simple(ugly) main loop
curEvent = pygame.event.poll()
while curEvent.type != pygame.QUIT:
# do something
curEvent = pygame.event.poll()
In using pygame, you have to handle all events including QUIT so if you don't handle the quit event, your program will not quit. Here's a code.
import sys
import pygame
from pygame.locals import *
def main():
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT: #QUIT is defined at pygame.locals
runnning = False
#other game stuff to be done
if __name__=='__main__':
pygame.init()
pygame.display.set_mode((640,480))
main()