Why my background image is not appearing in the window in pygame? - python

Why my background image is not appearing in the window in pygame?
import pygame
pygame.init()
screen = pygame.display.set_mode ((1000,437))
background = pygame.image.load("background.jpg")
running = True
while running:
screen.fill((255,255,255))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

You just need to update the display with this command: pygame.display.update()
Place the command at the end of your while running: loop.
pygame.init()
screen = pygame.display.set_mode ((1000,437))
background = pygame.image.load("background.jpg")
running = True
while running:
screen.fill((255,255,255))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()

Related

pyagame.error: Video not intiialized. Code Posted

Ask for any more information if needed!
I am using vscode's ide and python 3. When I run script.py the display will pop-up, but a half of a second later the display will disappear and will give this error pyagame.error: Video not intiialized.
import pygame
pygame.init()
run = True
while run:
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
pygame.quit()
x = 250
y = 250
width = 40
height = 60
vol = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT():
run = False
run = False
pygame.QUIT()
You do pygame.quit() immediately after pygame.display.set_mode(). pygame.quit() terminates all pygame modules. Remove it:
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
# pygame.quit() <--- DELETE
pygame.QUIT is not a function, it is an enumerator constant. You can't invoke pygame.QUIT:
if event.type == pygame.QUIT():
if event.type == pygame.QUIT:
You need just one application loop, not 2 of them. Furthermore you have to update the window by either pygame.display.flip() or pygame.display.update()
import pygame
pygame.init()
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
x, y = 250, 250
width, height = 40, 60
vol = 5
run = True
while run:
pygame.time.delay(100)
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear dispaly
screen.fill((0, 0, 0))
# draw the scene
pygame.draw.rect(screen, (255, 0, 0), (x, y, width, height))
# update display
pygame.display.flip()
pygame.quit()

Pygame window freezes on quit

I'm relatively new to python and very new to pygame. I'm trying to use pygame. All programs seem to work fine, except when I try to quit. The window freezes ("application not responding") and I have to force quit it. I'm using OSX, python 3.6, and running it through sublime text if that matters. Code is below:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
done = True
pygame.display.quit()
pygame.quit()
Thanks for your help!
Try this one, it works for me:
import sys
import pygame
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
you can just do this:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
this is what i do
or you can use this:
import pygame
pygame.init()
running = True
width, heigth = 800, 600
size = (width, heigth)
screen = pygame.display.set_mode(size)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
don't use pygame.display.quit()

Background image not appearing in pygame

My code currently looks like this
import pygame
from pygame.locals import *
pygame.init()
width,height = 1080,810
keys = [False,False,False,False]
screen = pygame.display.set_mode((width,height))
game_running = True
background = pygame.image.load("resources/images/background.png")
while game_running:
screen.fill((0,0,0))
screen.blit(background,(0,0))
When the code is run, a black window pops up, but the background isn't there.
I checked the directories to make sure the loading of the image has no issue too.
pygame draws in buffer in RAM memory (to make animation less flicking and tearing).
You have to use pygame.display.update() or pygame.display.update() to send from buffer to video card which will display it on monitor.
import pygame
width = 1080
height = 810
keys = [False, False, False, False]
pygame.init()
screen = pygame.display.set_mode((width,height))
background = pygame.image.load("resources/images/background.png").convert()
game_running = True
while game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game_running = False
screen.fill((0,0,0))
screen.blit(background, (0,0))
pygame.display.flip()
pygame.quit()
Wikipedia: Double buffering in computer graphics

How do i quit and print the events in one loop here

import pygame
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
pygame.display.update()
excape = False
while not excape:
for dork in pygame.event.get():
print(dork)
if pygame.event == pygame.QUIT:
pygame.quit()
quit()
Here the print(dork) is working but when i click the exit button of the window it doesn't quit at all..
So how do i both print events and quit the application in 1 while loop?
First of all, you should update the screen in the while not excape loop.
Secondly, set the excape to True if pygame.event is equal to pygame.QUIT.
So your code will look like this:
import pygame, sys
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
excape = False
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
excape = True
pygame.quit()
sys.exit()
pygame.display.update()
You need to cycle through EVERY pygame event and check if the event is a quit.
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
excape = True

How to exit out of window in pygame 64 bit?

I have created a window in pygame, the only problem is that I can't exit the window without it crashing. Is there a problem with my code that causes this?
import pygame
pygame.init()
gamedisplay = pygame.display.set_mode ((800,600))
pygame.display.set_caption ('snake')
pygame.display.update()
while True:
pass
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.quit()
quit()
Get rid of this:
while True:
pass

Categories