Pygame Window opens then immediately cloeses [duplicate] - python

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed last year.
I am coming back to pygame after around one and a half months of taking a break.
I tried to open and create a window I tried to run it and the window immediately closed.
This is all of the code:
import pygame
pygame.init()
screen_x = 230
screen_y = 230
display = pygame.display.set_mode ((screen_x, screen_y))
I am using VSCode if that does anything, this is the first time I have used VSCode so if I do not do something right I may not know.

You need an event loop, or else the application will close immediately.
Here is some starter code to do this:
import pygame
# screen object(width,height)
screen_x = 230
screen_y = 230
screen = pygame.display.set_mode((screen_x, screen_y))
# Set the caption of the screen
pygame.display.set_caption('Title')
# Variable to keep our game loop running
running = True
# game loop
while running:
# for loop through the event queue
for event in pygame.event.get():
# Check for QUIT event
if event.type == pygame.QUIT:
running = False

Related

Playing sound on right or left Speaker on MouseOver [duplicate]

This question already has answers here:
How would I be able to pan a sound in pygame?
(3 answers)
Closed 1 year ago.
I am trying to make small program in python, using PyQt5.
The program will as have two buttons, and a label in the middle. When the mouse goes over the label, I want to call a def, in order to change the button's color and play a sound from specific speaker (left or right)
I tried pygame, following some posts, but nothing. The sound is playing in both channels.
import time
import pygame
pygame.mixer.init(44100, -16,2,2048)
channel1 = pygame.mixer.Channel(0) # argument must be int
channel2 = pygame.mixer.Channel(1)
print('OkkK')
soundObj = pygame.mixer.Sound('Aloe Blacc - Wake Me Up.wav')
channel2.play(soundObj)
soundObj.set_volume(0.2)
time.sleep(6) # wait and let the sound play for 6 second
soundObj.stop()
Is there a way to fix this and choose the left-right speaker?
Also, is there a way to call a def, On Mouse Over a label?
When you use pygame in general, prefer calling pygame.init() to initialise all of the pygame modules instead of typing separately pygame. module .init(). It will save you time and lines of code.
Then, to play a sound file in pygame, I generaly use pygame.mixer.Sound to get the file, then I call the play() function of the sound object.
So the following imports a sound file, then plays and pans it according to the mouse X position
import pygame
from pygame.locals import *
pygame.init() # init all the modules
sound = pygame.sound.Sound('Aloe Blacc - Wake Me Up.wav')) # import the sound file
sound_played = False
# sound has not been played, so calling set_volume() will return an error
screen = pygame.display.set_mode((640, 480)) # make a screen
running = True
while running: # main loop
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN: # play the sound file
channel = sound.play()
sound_played = True
# start setting the volume now, from this moment where channel is defined
# calculate the pan
pan = pygame.mouse.get_pos()[0] / pygame.display.get_surface().get_size()[0]
left = pan
right = 1 - pan
# pan the sound if the sound has been started to play
if sound_played:
channel.set_volume(left, right)
pygame.display.flip()

Pygame background not loading [duplicate]

This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
MacOSX 10.15.2, Python 3.8 and Pygame 2.0.0.
Hello! I am currently trying to make a dark purple (87,61,122) background in Pygame, but it only appears as a black background, seemingly frozen and loading forever.
Here is my code:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
screen.fill(bgcolor)
pygame.display.flip
Is there anything wrong with the code or is Pygame just refusing to co-operate?
pygame.display.flip is not a function call. You missed the parentheses.
However, you have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. This will update the contents of the entire display.
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (draw all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# handle events
for event in pygame.event.get():
if event.type == QUIT:
run = False
# clear display
screen.fill(bgcolor)
# draw scene
# [...]
# update display
pygame.display.flip()
pygame.quit()
exit()
change pygame.display.flip by pygame.display.flip()
or pygame.display.update()
not that without IDLE, this script will display a window and directly destroy it.
To add like loop:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False #Quiting
screen.fill(bgcolor)
pygame.display.update()

Pygame : The game stops temporarily when I am moving the window

I'm a new in PyGame and I was making a game. Until I discovered that when you were moving the window to another place of your desktop for example, the game temporarily stops. But ticks of the game still running when I used pygame.time.get_ticks().
So I made a completely new program with only the necessary code and it does the same thing.
Can someone explain me why it does it and if we can resolve this problem?
import pygame
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('The Test Program')
running = True
update_counter = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
print(str(update_counter) + " updates")
update_counter += 1
pygame.quit()
quit()
# So try to move the window across your screen and you will see that, prints will stop and they will resume when you will release the click
So someone tell me that was normal, so I think it's only on Windows but there are no solutions. I put a script that show a pause symbol on screen when the cursor leave the window to make it normal.

Pygame.display.update() disappearing [duplicate]

This question already has answers here:
Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.
(2 answers)
Closed 7 years ago.
We have recently moved our school lab to Mac, and when I run some code to display an image from a file, the display appears then disappears immediately. I am using the 3.4 interpreter in Pycharm and Pygame version 1.9.2. Can someone please help?
Here is my code:
# displays a hard-coded filename in a window
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0, 0))
pygame.display.update()
If the code you posted is the complete code, then that is the reason why its exiting immediately, because immediately after calling pygame.display.update() , the program exits. I do not think this should have been working before.
One thing you can do for this , add a loop to run till the user presses close, after your code -
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Put a
time.sleep(10)
after pygame.display.update() see if it stays up longer.

Python pygame window keeps crashing

Whenever I run my code the Python Window that shows up does not respond.
Is there something wrong with my code or do I have to re-install pygame and python?
I get a black pygame window and then it turns white and says not responding?
Also I am new to this so please make this as simple as possible. I tried looking everywhere for the answer but could not get it in a way that I could understand.
Please help me out. Thanks :)
1 - Import library
import pygame
from pygame.locals import *
2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
3 - Load Images
player = pygame.images.load("resources/images/dude.png")
4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(0)
# 6 - draw the screen elements
screen.blit(player, (100,100))
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
Don't import pygame.locals. It is actually unnecessary, since you are already importing pygame.
Also, as #furas said, it should be:
player = pygame.image.load("resources/images/dude.png")
Not:
player = pygame.images.load("resources/images/dude.png")
This will clear up some of the problems in your code.
From my personal experience,if you run pygame code from IDLE it often does not respond at all.Try saving your project as a .py file and then run it with python.exe.It always works for me.
And as furas said use
player = pygame.image.load("resources/images/dude.png")
instead of
player = pygame.images.load("resources/images/dude.png")
Replace that for loop with this
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

Categories

Resources