Pygame Freezes on Exit/Quit - python

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.

Related

Why is a Drag and Drop event on Pygame not allowed?

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.

Screen not showing on Mac using pygame

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")

Pygame is not running correctly

I am not able to run anything using pygame as whenever I run anything, even a very simple program displaying a circle, the program yields a black screen which does nothing.
The black screen I am talking about is this black screen
What is this exactly? and is there a way to fix it?
Edit:
I forgot to mention that the program appears to be running well and I get no errors.
Edit #2: This is my very simple program:
import pygame
pygame.init()
screen = pygame.display.set_mode([500, 500])
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255, 255, 255))
SCREEN_TITLE = 'Chess Game'
pygame.display.set_caption(SCREEN_TITLE)
pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
pygame.display.flip()
pygame.quit()
Edit #3: a picture of what is showing on the python console
after I press the exit button
before I press the exit button
You may experience two separated issues:
Issue 1: Pygame installation on MacOS
There are some documented problems when running PyGame with MacOS. Please check that you have correctly installed and setup pygame in your machine. This post might be useful.
Issue 2: Code incorrections
Apart from that, your code has several issues. Your running loop does not display anything since it is stuck processing events and nothing more. Therefore, you see a black screen. Notice that you are printing the screen and the circle when the execution is over.
When using pygame I suggest to differentiate between:
Initialisation: Setup pygame and screen. Render any static content.
Running loop: Process events and render any dynamic content.
End: Display any end animation/object and finalise pygame.
I suggest the following modifications:
Render first the screen
In the running loop just process the events and render the circle
I have added debug messages. You can enable and disable them by running python mygame.py and python -O mygame.py. Be aware that the print statements inside the running loop will print a lot of messages.
Here is the code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# For better print formatting
from __future__ import print_function
# Imports
import pygame
#
# MAIN FUNCTION
#
def main():
# Setup display and static content
if __debug__:
print("Initialising pygame")
pygame.init()
SCREEN_TITLE = 'Chess Game'
pygame.display.set_caption(SCREEN_TITLE)
screen = pygame.display.set_mode([500, 500])
screen.fill((255, 255, 255))
pygame.display.flip()
# Running loop
running = True
while running:
if __debug__:
print("New iteration")
# Process events
if __debug__:
print("- Processing events...")
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Display any dynamic content
if __debug__:
print("- Rendering dynamic content...")
pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
# Update display
if __debug__:
print("- Updating display...")
pygame.display.flip()
# End
if __debug__:
print("End")
pygame.quit()
#
# ENTRY POINT
#
if __name__ == "__main__":
main()
Debug output:
$ python mygame.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Initialising pygame
New iteration
- Processing events...
- Rendering dynamic content...
- Updating display...
.
.
.
New iteration
- Processing events...
- Rendering dynamic content...
- Updating display...
End
Display:

Window resize example from Pygame wiki not working

I took this tutorial from pygame.org which should show how to resize the window properly (image has to be supplied to it, you can use for instance my gravatar). The image should resize to the window, but this doesn't happen with me. Only one VideoResize event is created as soon as I resize the window even so slightly:
<Event(16-VideoResize {'h': 500, 'w': 501, 'size': (501, 500)})>
No other VideoResize events are created (other things like mouse movement or keypresses work). So is the tutorial wrong? Is my computer wrong? What is the proper way of doing it?
I'm running: Python 2.7.5, Pygame 1.9.1, Fedora 20, MATE 1.8.1, Toshiba Satellite.
Here's the code (slightly modified to print the event, but neither the original nor this one work):
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,500), RESIZABLE)
pic = pygame.image.load("example.png")
screen.blit(pygame.transform.scale(pic, (500,500)), (0,0))
pygame.display.flip()
done = False
while not done:
pygame.event.pump()
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == VIDEORESIZE:
print event # show all VIDEORESIZE events
screen = pygame.display.set_mode(event.dict['size'], RESIZABLE) # A
screen.blit(pygame.transform.scale(pic, event.dict['size']), (0,0))
pygame.display.flip()
pygame.display.quit()
If I comment the line # A, then I get plenty of events, but this is the line which resizes the window.
Well I ran the tutorial example with only one change which is I used a pic of a cat called cat.png and it worked fine. The resize is working you just grab a corner and it allows me to adjust it freely with dragging. The picture fills the window whatever size I make it. Have you done other scripts with pygame successfully?

Screen displays only in top left corner of window

I'm starting to explore Python and Pygame, however I am running into a problem. Everything I draw to the screen is only displayed in the top left quarter of the window. I thought it was my code but any demo programs I tried also display the same way.
Demo code from thenewboston on youtube
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,360),0,32)
background = pygame.image.load("Background.jpg").convert()
mouse_c = pygame.image.load("ball.png").convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -= mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
In the video his displays correctly and mine looks like this
Using:
Python 2.7.4 32 bit
pygame 1.9.1 32 bit
mac 10.8.3 64 bit on macbook pro retina
I think either it has to do with the retina display or I installed something wrong. Any help would be appreciated.
The problem is, in fact, with the resolution of the retina screen. In order to get pygame to work correctly, download setresx and use it to set your resolution to any setting without HiDPI.

Categories