Window freeze after pygame program runs - python

I am learning pygame and have been working on examples given in 'Python Crash Course' book by Eric Matthews (https://g.co/kgs/WP5fay). I have installed pygame version 1.9.3 on macOS High Sierra.
When I run the following program, a window opens and when I click 'X' to close it, the window freezes and the curses keeps circling and I get a python not responding error in activity monitor. I have tried a number of option to fix the problem but it is not going away.
import pygame
import sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game() #when I run this function, a window opens and freezes a couple of seconds after

You have to terminate the application loop and uninitialize all pygame modules with pygame.quit():
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.flip()
pygame.quit()
sys.exit()

Related

Pygame window closes instantly

My pygame window closes without any errors right after I run the code
Here's my code:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
If anyone can help, thank you in advance :)
See Why is my PyGame application not running at all? Your application will exit right after the window is created. You have to implement an application loop. You must implement an application loop to prevent the window from closing immediately:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill((0, 0, 0))
# draw scene
# [...]
pygame.display.flip()
pygame.quit()

I want to create pygame window but it kept saying "video system not initialized" in VS Code

I am using visualcodestudio, but if I run using python IDLE, it runs.
import pygame, sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Invasion")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
You need to initialize using pygame.init() first before going into the loop. Also, I replaced sys.exit() with pygame.quit() as sys.exit() causes the program to not respond with attempting to close the program.
import pygame, sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Invasion")
run_game()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
Or, you could put the loop inside the run_game() function as import random suggested.
import pygame, sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Invasion")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
run_game() # Run the game

Pygame Window refuses to start after importing * from pygame.local

So I started programming a game for a school project and currently face a problem with event.type KEYDOWN.
This is my Code. When running it a window opens but it stays completely black without showing either the background color or the sprite. I tried pinpointing the issue with the print() commands to see in what line exactly the code stops working. Every, except the last print command is executed, which leaves me to believe the While Loop is the cause of my problem.
import pygame
from pygame.locals import *
print("Import complete")
WIDTH = 640
HEIGHT = 480
TITLE = "Tales of Tesbold"
print("Window complete")
Tesbold = Actor("tesbold.png") #Sprites vordefinition
Tesbold.x = 200
Tesbold.y = 100
print("Tesbold complete")
wechsel = True
def draw(): #Hintergrund und alle Sprites
screen.clear()
screen.fill((200,200,200))
Tesbold.draw()
print("Draw Screen complete")
while True:
for event in pygame.event.get():
if event.type == quit:
sys.exit()
if event.type == KEYDOWN:
if(event.key == K_RIGHT):
Tesbold.x += 10
print("While Loop complete")
This is the console output:
pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Import complete
Window complete
Tesbold complete
Draw Screen complete
---------- FINISHED ----------
exit code: 2 status: 0
Can someone tell me what I did wrong for it to not function anymore?
Python is case sensitive. The event type enumerators are all written in capital letters. See pygame.event module:
if event.type == quit:
if event.type == QUIT:
pygame.quit() is a function and uninitialize all pygame modules.

Time Module not working in Pycharm (I am using Python 3.8.5, Pycharm & Pygame)

I am trying to write my first game using Python3 & Pygame in the Pycharm Community IDE. I am trying to make a simple Pong game :-D lol I am a beginner so please help me if you can.
When I run my code, I am getting the following error:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/home/TechSlugz/PycharmProjects/Pong-Project/Pong1.py", line 7, in <module>
clock = pygame.time.clock()
AttributeError: module 'pygame.time' has no attribute 'clock'
Process finished with exit code 1
The code is just the basic set up for a game window, check it out
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.setmode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)
Can you notice anything that I am doing wrong to get this issue? None of my IDEs seem to recognise time as a pygame module but I am pretty damn sure it is...
The clock is a class, so you should use C instead of c.
clock = pygame.time.Clock()
and you have another problem, setmode is not an available function. You meant to write set_mode
The code that works fine for me:
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.Clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)

I keep getting this error "for event in pygame.event.get(): pygame.error: video system not initialized" how do i resolve it?

I am a noob to pygame and I keep getting this error "for event in pygame.event.get():
pygame.error: video system not initialized" here is my code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
run=True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.quit():
run = False
How can i resolve this. i have tried changing pygame.display.set_mode((500, 500)) to pygame.display.get.surface and that ran into the same error. thank you in advance for any help.
-Kenneth Ayers
This line:
if event.type == pygame.quit():
Should be:
if event.type == pygame.QUIT:
You are quiting pygame instead of checking for QUIT.

Categories