for some reason, python did not respond when I quit it
screenshot of python didn't respond
import pygame
pygame.init()
size=(320, 320)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("space invader")
icons=pygame.image.load('info.png')
pygame.display.set_icon(icons)
running=True
def player():
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
pygame.display.quit()
pygame.quit()
do you have any idea what the freak is happening here?
It is not clear what exactly the question is trying to achieve other than to run to its end. The code was missing necessary indentation, this works:
import pygame
pygame.init()
size=(320, 320)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("space invader")
icons=pygame.image.load('info.png')
pygame.display.set_icon(icons)
running=True
def player():
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
pygame.display.quit()
pygame.quit()
I would still point out that the function player() needs to be called somewhere presumably...
Related
I'm new to pygame. I have written the following code, but the generated window doesn't allow me to close it.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Looping and the game running are two different things. If you want to close the game after the loop ends, you should do so with pygame.quit(). Importing sys and adding sys.exit() as afterwards lets you exit the Python script altogether. Depending on which IDE you use, this may not happen automatically.
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
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.
After running this code, I got an error:
pygame.error: video system not initialized
My code:
import sys
import pygame
def run_game():
# Initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Make the most recently drawn screen visible.
pygame.display.flip()
run_game()
Can anyone help me and explain what this error means and how to rectify it?
The error is raised because pygame.event.get is called without an initialized display (pygame.display.set_mode). The problem is that your while loop is not indented correctly, so it is executed before the run_game function is called. The loop should be inside of the run_game function.
import sys
import pygame
def run_game():
# Initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Make the most recently drawn screen visible.
pygame.display.flip()
run_game()
put your code:
run_game()
before while statment`
I ran the code on my Macbook,it didn't work, while I ran the code on my Ubuntu it worked.
My macOS is 10.12.6
(ps:when I used mouse to control the image in pygame(just input the position of my mouse) ,if i didn't clicked the mouse , i can't dragged the image, but i didn't do anything in my program about click event)
This is my code
import pygame
pygame.init()
size = (800,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('My Demo')
done = False
BLACK = (0,0,0)
WHITE = (255,255,255)
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
print('work')
screen.fill(WHITE)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Pygame sometimes mixes up the keys. I already have encountered this problem on Windows, it seems to be an issues on macOS too.
This approach might solve your problem: Pygame keyboard layouts mixed up.
I am unable to see the image when I start the program. I have a folder named "game" and "mygame.py" file with a "background.png" all in it. I have tried using the the PATH "/game/background.png" instead of "background.png" but it doesn't seem to work. Any ideas?
my code:
import pygame , sys
pygame.init()
#screen start
def screen():
screen = [1024,768]
screen = pygame.display.set_mode(screen,0,32)
pygame.display.set_caption("Testing Caption")
background = pygame.image.load("background.png")
screen.blit(background, (0,0))
while True:
screen.blit(background, (0,0))
#keyboard commands
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen()
Thank you.
You are missing the flip/update call:
clock = pygame.time.Clock()
while True:
#keyboard commands
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(background, (0,0))
pygame.display.flip()
clock.tick(40) # keep program running at given FPS
Every blit occurs in a internal buffer, you need to call flip or update once per frame to update the real screen.
To my knowledge you can make this work one of two ways.
You can use an absolute path to the file, such as:
"C:\path_to_game_folder\game\background.png"
or you can use a relative path. To do this add the following code to your program:
import os
dir = os.path.dirname(__file__)
backgroundFile = os.path.join(dir, "background.png")
and change:
pygame.image.load("background.png")
to
pygame.image.load(backgroundFile)
I suggest using relative paths whenever possible it keeps the code portable as well as makes it easier to maintain and distribute.
I got it! Using pygame its important to use "pygame.display.update()" inside while True: screen.flip doesn't work with pygame to refresh or update the screen. Thanks to the users who responded earlier though.
Full Code:
import pygame , sys
pygame.init()
#screen start
def screen():
width , height = 1280,768
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Testing Caption")
background = pygame.image.load("background.jpg")
screen.blit(background, (0,0))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(background, (0,0))
pygame.display.update()
screen()