This exact question was asked on this forum last April. However, the only answer was: "Your script works on my system, check and make sure that you have the pygame module intended for your version of python." So I checked this first and it checked out. (I have Python 2.7 and Pygame Version 1.9.1 release for Python 2.7.)
So it's not that, but the following code produces the same error the other person reported:
This application has requested the runtime to terminate it in an unusual way. Please contact the application's support team for more info.
I wish the original poster had said what they did to fix it, because I'm stumped. Note that I do NOT have administrator rights on the machine I'm trying to run this on. Could that be the problem?
The script is below. (This was taken directly from the book Beginning Game Development with Python and Pygame.) It runs right up to the instant you try to resize the window.
background_image_filename = 'sushiplate.jpg'
import pygame
from pygame.locals import *
from sys import exit
SCREEN_SIZE = (640, 480)
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32)
background = pygame.image.load(background_image_filename).convert()
while True:
event = pygame.event.wait()
if event.type == QUIT:
exit()
if event.type == VIDEORESIZE:
SCREEN_SIZE = event.size
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32)
pygame.display.set_caption("Window resized to "+str(event.size))
screen_width, screen_height = SCREEN_SIZE
for y in range(0, screen_height, background.get_height()):
for x in range(0, screen_width, background.get_width()):
screen.blit(background, (x, y))
pygame.display.update()
Warning, you are drawing for every event fired. You instead want:
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == VIDEORESIZE:
# ...
elif event.type == KEYDOWN:
# ...
# draw.
pygame.display.update()
This next code doesn't make sense: What are you trying to do?
screen_width, screen_height = SCREEN_SIZE
for y in range(0, screen_height, background.get_height()):
for x in range(0, screen_width, background.get_width()):
screen.blit(background, (x, y))
pygame.display.update()
Try removing the color depth on the resize mode setting:
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE)
I get the same error as you with the color depth left in, and it goes away when I remove it. I don't know why it would fail, but there's no official documentation at all on the VIDEORESIZE event, other than the Event object has size, w and h fields set to something. (Checking this is difficult right now because pygame.org is down right now, reportedly due to a server RAID error, but you can Google for the information, and view Cached pages.)
Related
I am getting this error whenever I attempt to execute my pygame code:
pygame.error: video system not initialized
from sys import exit
import pygame
from pygame.locals import *
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = screen_width, screen_height = 600, 400
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
def game_loop():
fps_cap = 120
running = True
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): # error is here
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.display.flip()
pygame.quit()
exit()
game_loop()
#!/usr/bin/env python
You haven't called pygame.init() anywhere.
See the basic Intro tutorial, or the specific Import and Initialize tutorial, which explains:
Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.
pygame.init()
This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.
In your particular case, it's probably pygame.display that's complaining that you called either its set_caption or its flip without calling its init first. But really, as the tutorial says, it's better to just init everything at the top than to try to figure out exactly what needs to be initialized when.
Changing code to this, avoids that error.
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
pygame.quit()
if running:
screen.fill(white)
pygame.display.flip()
#this will fool the system to think it has video access
import os
import sys
os.environ["SDL_VIDEODRIVER"] = "dummy"
You just need to add
exit()
To stop running code
example :
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
exit() # Solution
You get an error because you try to set the window title (with set_caption()) but you haven't created a pygame window, so your screen variable is just a tuple containing the size of your future window.
To create a pygame window, you have to call pygame.display.set_mode(windowSize).
Good luck :)
If you doing pygame.init() then solve the problem video system initialized.
but you get the next error like:
(AttributeError: tuple object has no attribute 'fill') this.
this problem is solving when you doing this
screen = pygame.display.set_mode((600, 400))
but not doing like
screen = screen_width, screen_height = 600, 400
Then the full problem is solved.
I made some modifications to your code:
import os
import sys
import math
import pygame
import pygame.mixer
from pygame.locals import *
pygame.init()
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = pygame.display.set_mode((600, 400))
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
while True:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(green)
pygame.display.flip()
For me, it was a problem because I didn't set pygame.quit() out of the loop at the end.
You have to add:
pygame.init()
Before you quit the display you should stop the while loop.
If you using class for your pygame window don't use pygame.init() in your class. Use pygame.init() at below libraries.
You need to initialized pygame using this command pygame.init
If the problem is not solved then following this step
this problem happens when you using a beta version.
so my suggestion is please use the new, old version(If now lunch 3.8 python, you
need to install python 3.7)
Now goto python terminal and install pygame (pip install pygame)
now the problem is solved...
I'm a Macbook user and I have just recently started learning programming games using python.
my issue is that every time I try to close the screen it doesn't close.
import pygame
import sys
pygame.init()
width = 800
hight = 600
screen = pygame.display.set_mode((width , hight))
game_over= False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT :
sys.exit()
When I point the cursor to the screen it turns to a circle... Cursur, so usually I just press force to quit "on the snake icone" in the dock
So how can I fix this and make the screen close just by pressing the close button ?
I'm using python 3.
Thank you.
Full edited and tested code :
import pygame
import sys
pygame.init()
width = 800
hight = 600
running = True
screen = pygame.display.set_mode((width , hight))
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT :
pygame.quit()
sys.exit()
Test on Linux mint with visual studio code. Tested more than 5 times. Expected results.
Like the title says I am confused why I get the error: pygame.error: video system not initialized
As far as i understand this error is raised if you forget to initialize your code with pygame.init() but I did, here's my code and thanks in advance:
import pygame
from pygame.locals import *
pygame.init()
screen_height = 700
screen_width = 1000
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("platformer")
# load images
sun_img = pygame.image.load("img/sun.png")
backround_img = pygame.image.load("img/sky.png")
run = True
while run:
screen.blit (backround_img,(0,0))
screen.blit(sun_img, (100, 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.update()
the error does not crash the window or anything and it works as intended
its just somewhat annoying.
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.update()
When the event loop happens, if you close the window, you call pygame.quit(), which quits pygame. Then it tries to do:
pygame.display.update()
But pygame has quit, which is why you are getting the message.
separate the logic out. Events in one function or method, updating in another, and drawing and updating the display in another to avoid this.
I am getting this error whenever I attempt to execute my pygame code:
pygame.error: video system not initialized
from sys import exit
import pygame
from pygame.locals import *
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = screen_width, screen_height = 600, 400
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
def game_loop():
fps_cap = 120
running = True
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): # error is here
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.display.flip()
pygame.quit()
exit()
game_loop()
#!/usr/bin/env python
You haven't called pygame.init() anywhere.
See the basic Intro tutorial, or the specific Import and Initialize tutorial, which explains:
Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.
pygame.init()
This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.
In your particular case, it's probably pygame.display that's complaining that you called either its set_caption or its flip without calling its init first. But really, as the tutorial says, it's better to just init everything at the top than to try to figure out exactly what needs to be initialized when.
Changing code to this, avoids that error.
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
pygame.quit()
if running:
screen.fill(white)
pygame.display.flip()
#this will fool the system to think it has video access
import os
import sys
os.environ["SDL_VIDEODRIVER"] = "dummy"
You just need to add
exit()
To stop running code
example :
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
exit() # Solution
You get an error because you try to set the window title (with set_caption()) but you haven't created a pygame window, so your screen variable is just a tuple containing the size of your future window.
To create a pygame window, you have to call pygame.display.set_mode(windowSize).
Good luck :)
If you doing pygame.init() then solve the problem video system initialized.
but you get the next error like:
(AttributeError: tuple object has no attribute 'fill') this.
this problem is solving when you doing this
screen = pygame.display.set_mode((600, 400))
but not doing like
screen = screen_width, screen_height = 600, 400
Then the full problem is solved.
I made some modifications to your code:
import os
import sys
import math
import pygame
import pygame.mixer
from pygame.locals import *
pygame.init()
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = pygame.display.set_mode((600, 400))
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
while True:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(green)
pygame.display.flip()
For me, it was a problem because I didn't set pygame.quit() out of the loop at the end.
You have to add:
pygame.init()
Before you quit the display you should stop the while loop.
If you using class for your pygame window don't use pygame.init() in your class. Use pygame.init() at below libraries.
You need to initialized pygame using this command pygame.init
If the problem is not solved then following this step
this problem happens when you using a beta version.
so my suggestion is please use the new, old version(If now lunch 3.8 python, you
need to install python 3.7)
Now goto python terminal and install pygame (pip install pygame)
now the problem is solved...
The code loads up a pygame screen window, but when I click the X to close it, it becomes unresponsive. I'm running on a 64-bit system, using a 32-bit python and 32-bit pygame.
from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
games.screen.mainloop()
Mach1723's answer is correct, but I would like to suggest another variant of a main loop:
while 1:
for event in pygame.event.get():
if event.type == QUIT: ## defined in pygame.locals
pygame.quit()
sys.exit()
if event.type == ## Handle other event types here...
## Do other important game loop stuff here.
I'd recommend the following code. First, it includes Clock so your program doesn't eat the CPU doing nothing but polling for events. Second, it calls pygame.quit() which prevents the program from freezing when running under IDLE on windows.
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
pygame.init()
# Set the height and width of the screen
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
#Loop until the user clicks the close button.
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
# -------- Main Program Loop -----------
while done==False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# Set the screen background
screen.fill(black)
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit ()
This is a pretty simple issue, you need to handle the "QUIT" event, see the event documentation at: http://www.pygame.org/docs/ref/event.html
EDIT:
It occurs to me now that you might be handling the "QUIT" event and its not working
but without more details to your code I dunno.
A quick example of a simple way to handle the "QUIT" event:
import sys
import pygame
# Initialize pygame
pygame.init()
pygame.display.set_mode(resolution=(640, 480))
# Simple(ugly) main loop
curEvent = pygame.event.poll()
while curEvent.type != pygame.QUIT:
# do something
curEvent = pygame.event.poll()
In using pygame, you have to handle all events including QUIT so if you don't handle the quit event, your program will not quit. Here's a code.
import sys
import pygame
from pygame.locals import *
def main():
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT: #QUIT is defined at pygame.locals
runnning = False
#other game stuff to be done
if __name__=='__main__':
pygame.init()
pygame.display.set_mode((640,480))
main()