I am trying to understand how to display fonts in pygame. But i get an error saying pygame.error: Library not initialized
This error happens after i press the cross button or quit my pygame window.
Can anyone tell my why this error is happening and how can i fix it please?
import pygame
from pygame.locals import *
import sys
from win32api import GetSystemMetrics
pygame.init()
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)-64
WIDTH_HEIGHT = (WIDTH, HEIGHT)
WINDOW = pygame.display.set_mode(WIDTH_HEIGHT)
pygame.init()
font = pygame.font.Font('freesansbold.ttf', 32)
text = ""
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
running = False
text = font.render('Hi', True, (255,255,255))
WINDOW.blit(text, (0, 0))
pygame.display.update()
This is one way to exit (just finishes the program):
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
This is another way (a bit more forceful):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
Related
I'm working on custom basic functions to simplify coding for me, like wait(seconds) or msg(...), and now I'm working on the window setup and updating, it works, but when I put it in a thread, it just won't do anything. I don't get any errors, so I'm confused and frustrated. I dont need you to debug it or anything, I just need help to know where the problem is and why it's a problem.
Here's my script so far (the script is at the bottom):
# Imports
if True:
import pygame, math, random, time, sys, threading
from pygame.locals import *
pygame.init()
# Setup
if True:
win_n = "New Project"
win_w = 800
win_h = 600
win_c = (0, 0, 0)
# Code
if True:
def wait(seconds):
time.sleep(seconds)
def wait_until(bool):
while not bool:
wait(0.001)
# Execute
if True:
def e_ws():
mainClock = pygame.time.Clock()
pygame.display.set_caption(win_n)
monitor_size = [pygame.display.Info().current_w, pygame.display.Info().current_h]
screen = pygame.display.set_mode((win_w, win_h), pygame.RESIZABLE)
fullscreen = False
while True:
screen.fill(win_c)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == VIDEORESIZE:
if not fullscreen:
screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
if event.type == KEYDOWN:
if fullscreen:
screen = pygame.display.set_mode(monitor_size, pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((screen.get_width(), screen.get_height()),
pygame.RESIZABLE)
pygame.display.update()
mainClock.tick(60)
t_ws = threading.Thread(target=e_ws)
t_ws.start()
print("done")
Run a script with python yourscriptname.py from the command line.
I'm new to PyGame (and python in general) and I am just trying to get a window to pop up. That's all I'm after for now. Here's my code:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
I am using Python 3.7.0 in Pycharm and PyGame 1.9.4.
Following Python's PyGame tutorial, your next step is to start a while loop to handle the game frames:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
clock = pygame.time.Clock() # Determine FPS (frames-per-second)
crashed = False
# Game loop
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
Because you need to first put it inside a loop (so it refreshes whatever is inside it), that way it stays on until something alters the condition of that loop.
# Event loop (HERE YOU PUT IT).
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main()
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()
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
I'm trying to run code from a tutorial I found online.
But when I run this code, my background image does not appear, even if it loaded correctly.
Why?
bif = "castle.jpg"
import pygame, sys, pygame.mixer
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1280,960),0, 32)
background = pygame.image.load(bif).convert()
pygame.display.set_caption("castlevania ultimate")
hit_sound = pygame.mixer.Sound("02.wav")
hit = False
if hit is True:
hit_sound.play()
sound = pygame.mixer.Sound("castlevania_1.wav")
sound.play()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
You never call pygame.display.update() in your while loop.
Python is indentation sensitive, so the loop should look like this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
Note that pygame.display.update() is now inside the while loop.