I have a simple Pygame display:
pygame.init()
screen = pygame.display.set_mode((1024, 576))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get(): # to handle clicks on the screen (prevent crash)
if event.type == pygame.QUIT:
pygame.display.quit()
if event.type == pygame.DROPFILE:
path = event.file
print(path)
pygame.display.update()
I'm currently testing the "drop file" event to use it in a project I'm working on. Unfortunately, when I drag a file onto the screen, the cursor turns into a "not allowed" sign and nothing happens when I drop the file. Why does it happen?
Without changing your code much (adding 'import pygame'), it doesn't work for me either. I droped a file and then the same happend for me what happend for you. Thats what I thought.
I first tried Python 3.8.6 with Pygame 1.9.6. Then I remembered, that I have an other installation of Python with 3.9.1 and Pygame version 2.0.0.
This second combination worked for me. I don't know which part made the difference in the end, but I think they did much work for pygame 2.0.0, so give it a try.
This works for me on Windows 10.
Related
I just spent a fair amount of time finding a 64-bit installation of pygame to use with python 3.3, (here) and now am trying to make a window. However, although the window opens up fine it does not close when it hit the x button. In fact, I have to close IDLE to close the window. I am running a 64 bit version of Win 7. Here is my code:
import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
When I append
time.sleep(5)
pygame.quit()
It still doesn't close. My only guess would be that pygame.quit might go inside one of the loops, but even if that were resolved I would greatly prefer being able to close the window when I want to.
Most pygame tutorials seem to suggest exiting by calling pygame.quit() and then sys.exit(). I have personally run into problems (was on a unix system though) where this still did not close the window properly. The solution was to add pygame.display.quit() specifically before pygame.quit(). That should not be necessary as far as I can tell, and I'm afraid I don't know why that solved the issue but it did.
if you want to make pygame close when window button x is pressed, put the code like this:
from sys import exit
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
We put exit() after pygame.quit(), because pygame.quit() makes the system exit and exit() closes that window.
Not sure but try this Because you code runs fine on my system after I add pygame.quit() at the end
import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
try:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
except SystemExit:
pygame.quit()
Its perhaps because as Idle is made on Tkinter and thus Tkinter and Pygame main loop do not have a mutual understanding.
Your code will run very well on command prompt though.
This was the final code that worked for me on OSX whilst keeping the kernel alive on Jupyter. EDIT - it does still crash the kernel sometimes :-(
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
pygame.quit()
exit()
Also needed to downgrade ipython to get rid of some magic alias warning messages using:
conda install ipython=7.2.0
apparently that issue is due to be fixed in ipython 7.6.0
Suffered the same issues on Python 3.7.4 while running it from in IDE (Spyder 3.3.6). In my case the pygame.quit() would not completely close the program.
Nonetheless, adding quit() or exit() did the trick for me!
Add this at the top:
import sys
Add this where you need to quit:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
try using the following command:
sys.exit(0)
notice: You will need to import the sys library in order to use it.
The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.
To answer the original question: You must call pygame.quit() after breaking the main loop. One elegant solution goes as follows:
def run():
pygame.init()
while True:
# ...
for event in pygame.event.get():
# Handle other events
if event.type == pygame.QUIT:
return pygame.quit()
I'm doing a tutorial for using pygame to make games, and I'm having a problem getting a screen to show up. Here is what I have so far:
import pygame
# Initialize the pygame
pygame.init()
# Create the screen, height = 800, width = 600
screen = pygame.display.set_mode((800, 600))
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
I have pygame installed, and have no errors or warnings in PyCharm. When I run this (both in PyCharm and my terminal), I get no screen showing, and all that happens is the spaceship of the python runner is bouncing in my dock. When I run this exact code on my pc, it shows a black screen, which is the desired outcome.
Can anyone help, or am I doing something wrong?
Thank you for your assistance.
I have no problem running your code on a Mac. However, the window showing up is a grey one, not black.
In order to force the display of the window, you can add this after the screen creation:
pygame.display.update()
And to try to figure out the problem,you may want to add a text in the window bar, that may generate a message pointing to your problem.
pygame.display.set_caption("Hello world")
im currently trying to have a setting to switch from windowed mode and fullscreen. But after getting into fullscreen and trying to go back, the game bugs really weird and sticks to the topleft corner
Btw:
display_width = 1280
display_height = 720
elif (Settings_Menu == True):
screen.fill((0,0,0))
screen.blit(settingsscreen, (0,0))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if checkbox1.collidepoint(event.pos):
if(Fullscreen == False):
Fullscreen = True
screen = pygame.display.set_mode((display_width, display_height), pygame.FULLSCREEN)
else:
Fullscreen = False
screen = pygame.display.set_mode((display_width, display_height), pygame.RESIZABLE)
This is a known bug in pygame 2.0 and 2.0.1. Reported on github here
The author there also found a workaround where you quit pygame between switching fullscreen.
Unfortunately, the toggle_fullscreen documentation is wrong. It does not work on windows. Reported on github
Now you might think- pygame has way too many problems with fullscreen, which is reasonable. But they are being fixed. I submitted a patch for your issue which was merged a week ago and will be included in pygame 2.0.2.
Use pygame.display.toggle_fullscreen() to switch between fullscreen and windowed displays.
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if checkbox1.collidepoint(event.pos):
pygame.display.toggle_fullscreen()
The documentation documentation mentions that display driver support "is not great" when using Pygame 1, but it should work for the following display drivers in Pygame 2:
windows (Windows)
x11 (Linux/Unix)
wayland (Linux/Unix)
cocoa (OSX/Mac)
However, at the time of writing this answer, there is a bug in toggle_fullscreen for Windows:
display.toggle_fullscreen does not work when toggling a maximized window to fullscreen #2380
i had this problem and fixed it with:
pygame.display.set_mode(size,pygame.SCALED)
and then i wrote pygame.display.toggle_fullscreen() and worked.
I'm slowly trying to get to know pygame and write my first game in it and honestly, I didn't expect problems so early. So far I've only set a display, that is supposed to be there indefinitely (I just wanted to try it out):
import pygame
pygame.init()
(width, height) = (1000, 700)
screen = pygame.display.set_mode((width, height))
while True:
pygame.display.flip()
But when the window appears it says it's "not responding". I tried deleting the loop so that display would just blink once and vanish, because programm would die immiedately after it's created, but I get the same "not responding" window. I'm using pygame 1.9.2 and python 3.5. I wonder if the trouble may be because of anaconda - the window is opened as subcart for anaconda by default.
Edit: So far I discovered that when I open it not from spyder, but just click on a file it works just fine. Is there any way to make it work by simple run and compile while in spyder or it's just how it's supposed to work?
Add this to your loop. For me the only time it isnt responding is when I click the X and this could be to do with the fact that pygame doesn't know what to do when that happens.
import sys
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
pygame.quit()
sys.exit()
#Try This
import pygame
(width, height) = (1000, 700)
screen=pygame.display.set_mode((width, height))
pygame.display.update()
while True:
for event in pygame.event.get():``
if event.type == pygame.QUIT:
pygame.quit()
quit()
I have done research about this problem, on internet, but there was no solution for my case... The window freezes when i try to close it with (X) button.
And as I said I haven't came across any solution on other posts, so I came here to ask for help. Thank you.
#!/usr/bin/python
import sys
import pygame
# initialize
pygame.init()
# colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
# window size
w_size = [700,500]
main_screen = pygame.display.set_mode(w_size)
# Window info
pygame.display.set_caption("Cancer Cell")
# manage screen update time
clock=pygame.time.Clock()
#background image
bg_img = pygame.image.load("/img/bg_img.png").convert()
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# GAME LOGIC - BEGINNING
# -------- DRAWINGS - BEGINNING --------
main_screen.blit(bg_img, [0,0])
# -------- DRAWINGS - END ----------
# update screen
pygame.display.flip()
clock.tick(20) # 20 FPS limit for loop.
pygame.quit()
Your code work perfectly well on my platform.
My platform:
Scientific Linux 7 (RHEL 7)
Python: 3.4.3
Pygame Version: 1.9.2b8
Window Manager: Gnome3
There is also probably bug in your code.
The "/img/bg_img.png" suggest absolute path, it possible, but very uncommon :).
Works pretty nicely for me. See the demo below:
Not only does your code work as expected on linux Mate 17, python 2.7.6 but it also works with sys.exit() instead of pygame.quit() or nothing at all i.e. no sys.exit() or pygame.quit()
So it turns out that the IDLE is what was causing the problem...
I tried to run the code from commandline and it worked perfectly!
The IDLE that I (was) using is called Wing101.