Pygame Error: video system not initialized (more info in description) [duplicate] - python

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 get same error as this person: pygame.error: video system not initialized, but I have called pg.init().
Two unexpected things happened,
link didnt appear (the background did, and they're in the same directory
after i close the pygame window i get the error mentioned in title
# -*- coding: utf-8 -*-
import pygame as pg
pg.init()
overflate = pg.display.set_mode((500,350))
pg.display.set_caption("Spill")
bg = pg.image.load("castle.jpg")
link = pg.image.load("link.png")
linkx = 0
linky = 0
overflate.blit(link,(linkx, linky))
pg.display.update()
overflate.blit(bg,(0, 0))
pg.display.update()
while True:
for e in pg.event.get():
if e.type == pg.K_w:
spillerx+=10
if e.type == pg.QUIT:
pg.quit()
pg.display.update()

You have to blit the image after drawing the background:
run = True
while run:
for e in pg.event.get():
if e.type == pg.K_w:
spillerx+=10
if e.type == pg.QUIT:
run = False
overflate.blit(bg,(0, 0))
overflate.blit(link,(linkx, linky))
pg.display.update()
pg.quit()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

Related

When R is pressed. Call a function that resets all variables to default [duplicate]

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()

Previous screen is not clearing in pygame [duplicate]

This question already has an answer here:
How can I move the ball instead of leaving a trail all over the screen in pygame?
(1 answer)
Closed 1 year ago.
Here I'm trying move which is blotted on the screen but the image moving without clearing the previous one
balloon=pygame.image.load("BALLON 1".png)
Bal_x=0
while true:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
Bal_x+=1
screen.blit(BALLON,(Bal_x.120))
pygame.display.update()
clock.tick(120)
It moves like in the image
You have to clear the display with pygame.Surface.fill in every frame:
balloon=pygame.image.load("BALLON 1".png)
Bal_x=0
while true:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
Bal_x+=1
screen.fill((0, 0, 0)) # <--- this is missing
screen.blit(BALLON,(Bal_x.120))
pygame.display.update()
clock.tick(120)
The typical PyGame application loop has to:
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
limit the frames per second to limit CPU usage with pygame.time.Clock.tick

Pygame window isn't appearing [duplicate]

This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
How to Run Python Code on SublimeREPL
(4 answers)
Closed 1 year ago.
I believe this is the necessary code to initialize Pygame:
running = True
pygame.init()
while running:
screen.fill((0, 0, 0))
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
However, it isn't working. I have already tried to update my "pip install pygame", and it did, but to no avail. I use Sublime Text 3.
You have to create a display Surface with pygame.display.set_mode:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
pygame.display.update()

Pygame screen crashes misterioslly, I don't idea Why [duplicate]

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.

Why does it say exit status 1? [duplicate]

This question already has answers here:
Pygame window freezes when it opens
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
I want the code to work. So that I can get a good grade of it. I have tried a lot of things so that the code can work event.
import pygame.sys
from pygame.locals import *
pygame.init()
DISPLAY=pygame.display.set_mode((500,400),0,32)
pygame.disply .set_caption(AMBERMIR)
WHITE=(255,255,255)
BLUE=(0,0,255)
DISPLAY.fill(WHITE)
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
if event.type == QUIT:
pygame.quit()
sys.exit()
You've to implement a main loop which is continuously running. Inside the main loop, an event loop can be handle the events.
Use pygame.event.get() to get and remove all pending events from the loop. The return value of pygame.event.get() is a list of pygame.event.Event objects.
After drawing the scene, the window has to be updated by either pygame.display.update() or pygame.display.flip():
import sys
import pygame
from pygame.locals import *
pygame.init()
AMBERMIR = "my window"
DISPLAY=pygame.display.set_mode((500,400),0,32)
pygame.display.set_caption(AMBERMIR)
WHITE=(255,255,255)
BLUE=(0,0,255)
# main loop
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == QUIT:
run = False # terminate main loop on QUIT
# clear display
DISPLAY.fill(WHITE)
# draw rectangle
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
# update display
pygame.display.update()
pygame.quit()

Categories