This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
pygame window closes immediatly after opening up
(1 answer)
Closed 1 year ago.
I have used Pygame with python 2.7 before but recently I 'upgraded' to python 3.2. I downloaded and installed the newest version of Pygame which is said to work with this version of python. I have, however, had this rather frustrating error on what should be a simple block of code. The code is:
import pygame, random
title = "Hello!"
width = 640
height = 400
pygame.init()
screen = pygame.display.set_mode((width, height))
running = True
clock = pygame.time.Clock()
pygame.display.set_caption(title)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit():
running = False
else:
print(event.type)
clock.tick(240)
pygame.quit()
And every single time I run it I get:
17
1
4
Traceback (most recent call last):
File "C:/Users/David/Desktop/hjdfhksdf.py", line 15, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
Why am I getting this error?
if event.type == pygame.quit():
In the line above, you're calling pygame.quit() which is a function, while what you really want is the constant pygame.QUIT. By calling pygame.quit(), pygame is no longer initialized, which is why you get that error.
Thus, changing the line to:
if event.type == pygame.QUIT: # Note the capitalization
Will solve your problem.
It's important to note that pygame.quit() will not exit the program.
Related
This question already has answers here:
Pygame level/menu states
(2 answers)
Reset and restart pygame program doesn't work
(1 answer)
Closed 2 days ago.
I can't make the restart button
please help me, please, I want to make a reset button in my project, but I do not know how, I want, provided that when I press r, all these games are reset, that is, if I have a record of 1500 and press r so that the record becomes 0 again, the project tetris works with pygame, if I need the code, I can throw it off when R is pressed. Call a function that resets all variables to their default values.
You can detect when 'r' is pressed using keyboard events. Here is a small example:
import pygame
gameDisplay = pygame.display.set_mode((800, 600))
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
print('r pressed!')
# Reset variables here or call a function to reset them
gameDisplay.fill((255, 255, 255))
pygame.display.update()
main()
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed 1 year ago.
on my mac I ran pygame and never got it working and I've done my research and found no conclusion.
The code I used to bring up some sort of screen...
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game objects
# [...]
# clear display
win.fill((0, 0, 0))
# draw game objects
# [...]
# update display
pygame.display.flip()
# limit frames per second
clock.tick(60)
pygame.quit()
The python launcher logo jumps up and down for a second
and comes up with python quit unexepctedly
Process: Python [4082]
Path: /Library/Frameworks
/Python.framework/Versions/3.9/Resources/Python.app
/Contents/MacOS/Python
Identifier: org.python.python
Version: 3.9.5 (3.9.5)
Code Type: X86-64 (Native)
Parent Process: Python [4040]
Responsible: Python [4082]
User ID: 505
Any help on how I can get pygame to work?
Or how to get any screen at all?
btw i installed pygame with pip3
and pip
You aren't telling pygame to run any events, and so the window just closes and the program stops.
This question already has an answer here:
Pygame unresponsive display
(1 answer)
Closed 2 years ago.
I Was try to build a game and I was build the interfaces normally, but sundely pygame screen crashed and the pygame screen window becomes unresponsive always I run the a simple program.
import pygame
pygame.init()
pygame.display.init()
pygame.display.set_caption("Yathezz")
tela = pygame.display.set_mode((600,600))
preto = (0,0,0)
def set_telaPartida(tela,nome__jogador,pontos_totais, situacao):
tela.fill(preto)
#if(situacao == 1):
# set_telaLancaDados(tela,nome__jogador,pontos_totais)
# elif(situacao ==2):
# a = 1
# seta tela perguntando o relançamento
#elif(situacao == 3):
# b= 2
#set_telaRelancaDados
while True:
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
I have done some tests and I suspect that the problem are in this line
pygame.display.update()
But I don't have Idea what's happening. Could someone Help me???
You have to handle the events, by either pygame.event.pump() or pygame.event.get(), to keep the window responding. For instance:
run = Ture
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
See the documentation of 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. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.
pygame.event.get() calls pygame.event.pump(). The pygame.QUIT occurs when the close button is pressed.
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
i have no problem of syntax or any erorr its just a problems when i run the program
import pygame
pygame.init()
# generate window
pygame.display.set_caption("shooter Game")
pygame.display.set_mode((1080, 720))
running = True
while running:
# if the player close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
print("game closed")
sceenshot of the code and the 🚀 of python that not stoping to jump
You don't update the display, which you have to do with
pygame.display.update()
You can add it right after pygame.display.set_mode((1080, 720))
Try like this and tell me if it works :)
Recently I was trying to make a pygame program, and first I made a blank screen that closes when you hit the red X button (hey, I gotta start somewhere). I ran my program, and a trackback error popped up saying my video system was not initialized. Here is my code below:
import pygame
#initialize pygame and make the screen
pygame.init()
screen = pygame.display.set_mode((1000,600))
#main program loop
running = True
while running:
#check for exit
for event in pygame.event.get():
if event.type == pygame.quit():
running = False
Notice how in line 4, I specifically typed "pygame.init()." I ran the code, and this error popped up:
Traceback (most recent call last):
File "C:\Users\13027\Desktop\Hackathon Files\main.py", line 22, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
It says that my video system was not initialized, even though I did pygame.init() in line 4. Is there anything else I should've done, or am I using the wrong version of Python (I'm using 3.8)? Thank you in advance.
Ok so I just realized my error and I'm going to fix it. On line 13, where it says
if event.type == pygame.quit():
I got the syntax wrong and the code should have been
if event.type == pygame.QUIT:
Fix the code like that, and there should be no error.