I already checked other topics and I had tested these solutions and without any pos results.. I need to refresh topic with question. Where I have an error?
import pygame
pygame.init()
WIDTH, HEIGHT = 1000, 800
window = pygame.display.set_mode((WIDTH, HEIGHT))
def run(window, width, height):
run = True
clock = pygame.time.Clock()
fps = 60
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
clock.tick(fps)
pygame.quit()
if __name__ == "_main_":
run(window, WIDTH, HEIGHT)
Change from single underscore to double underscores around main here:
if __name__ == "_main_":
It should be
if __name__ == "__main__":
The quit invocation should not be on the while block but after it. Otherwise it quits immediately.
Therefore:
def run(window, width, height):
run = True
clock = pygame.time.Clock()
fps = 60
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
clock.tick(fps)
pygame.quit()
It is a matter of Indentation. The window is closing because you are calling pygame.quit() in the application loop. You need to call pygame.quit() after the application loop.
The name of the top level code environment is "__main__", but not "_main_":
def run(window, width, height):
run = True
clock = pygame.time.Clock()
fps = 60
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
clock.tick(fps)
# INDETATION
#<--|
pygame.quit()
if __name__ == "__main__": # <--
run(window, WIDTH, HEIGHT)
Related
I was trying to draw an image onto the screen but I keep getting errors. The error keeps saying "video system not initialized". I am new to python, can anyone help?
import pygame
import os
#game window
WIN = pygame.display.set_mode((1000, 800))
NAME = pygame.display.set_caption("Space War!")
#FPS limit
FPS = (60)
#image i am trying to draw onto screen
SPACE_BACKGROUND = pygame.image.load(os.path.join('space_background.png'))
pygame.init()
#allows pygame to quit
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
pygame.display.update()
#calling def main(): function
if __name__ == "__main__":
main()
do:
def main():
clock = pygame.time.Clock()
run = True
FPS = 60
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
WIN.blit(SPACE_BACKGROUND, (0, 0)) #you FORGOT this part
pygame.display.update()
BTW the main function cannot access the global 'FPS' variable so declare that within the 'main' function(make it local).
so i've changed the window color to a white color, unfortunately its not updating, it also returns an error
code:
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display = pygame.display.set_caption('PyGame Test')
WHITE = (255,255,255)
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
WIN.fill(WHITE)
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()
what am i doing wrong?
In order to set the caption of the window, you need to type pygame.display.set_caption("Caption"). In your code, I see that you typed pygame.display = pygame.display.set_caption('PyGame Test'), which results in an AttributeError because pygame.display.set_caption() returns None, meaning you set pygame.display to None. So if you replace that line with pygame.display.set_caption('PyGame Test'), it will work.
Full code:
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('PyGame Test')
WHITE = (255, 255, 255)
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
WIN.fill(WHITE)
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()
For more info, check this page
I believe you need to initialize the display module. Try pygame.init()
http://www.pygame.org/docs/ref/display.html#pygame.display.init
import pygame
import os
WIDTH , HEIGHT = 1050,600
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("ok")
WHITE = (255,255,255)
FPS = 60
CHARACTER_SIZE = (100,100)
CHARACTER_IMAGE = pygame.image.load(os.path.join('Models','character.png'))
CHARACTER = pygame.transform.scale('character.png',CHARACTER_SIZE)
def draw_window():
WIN.fill(WHITE)
WIN.blit(CHARACTER,(0,300))
pygame.display.update()
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.quit
if __name__ == "__main__":
main()
After running this I get this error:
argument 1 must be pygame.Surface, not str
reffering to this line of code:
CHARACTER = pygame.transform.scale('character.png',CHARACTER_SIZE)
What s the issue?
You can't transform a string. The first argument has to be the CHARACTER_IMAGE variable.
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()
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()