So I decided to start making a game and I was testing it a bit and then I got this error:
Traceback (most recent call last):
File "TheAviGame.py", line 15, in <module>
font = pygame.font.Font(None,25)
pygame.error: font not initialized
I have no idea what I have done wrong so far...
Code:
#!/usr/bin/python
import pygame
blue = (25,25,112)
black = (0,0,0)
red = (255,0,0)
white = (255,255,255)
groundcolor = (139,69,19)
gameDisplay = pygame.display.set_mode((1336,768))
pygame.display.set_caption("TheAviGame")
direction = 'none'
clock = pygame.time.Clock()
img = pygame.image.load('player.bmp')
imgx = 1000
imgy = 100
font = pygame.font.Font(None,25)
def mts(text, textcolor, x, y):
text = font.render(text, True, textcolor)
gamedisplay.blit(text, [x,y])
def gameloop():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.pygame == pygame.KEYDOWN:
if event.key == pygame.RIGHT:
imgx += 10
gameDisplay.fill(blue)
pygame.display.update()
clock.tick(15)
gameloop()
You never initialized pygame and pygame.font after importing:
# imports
pygame.init() # now use display and fonts
In order to use some pygame modules, either pygame or that specific module has to be initialised before you start using them - this is what the error message is telling you.
To initialize pygame:
import pygame
...
pygame.init()
To initialize a specific library (eg font):
import pygame
...
pygame.font.init()
In most cases, you will want to initialise all of pygame, so use the first version. In general, I would place this at the top of my code, just after importing pygame
You put None in the font:
font = pygame.font.Font(None, 25)
You can’t do that.
You have to put a type of font there.
And you should call pygame.init().
Related
My pygame screen is not updating correctly and i am not sure why.
pygame code:
import pygame
import os
import random
import time
pygame.init()
width = 750
height = 750
win = pygame.display.set_mode((width, height))
pygame.display.set_caption('Space attack')
bg_size = 750, 750
bg = pygame.image.load("/home/pi/Pygames/Space_attack/background-black.png")
image = pygame.transform.scale(bg, bg_size)
#ships
r_ship= pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_red_small.png')
g_ship= pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_green_small.png')
b_ship= pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_blue_small.png')
#player
player = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_yellow.png')
#laser
r_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_red.png')
b_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_blue.png')
g_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_green.png')
p_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_yellow.png')
#game
def main():
run = True
fps = 30
clock = pygame.time.Clock()
def redraw_window():
win.blit(bg, (0,0))
pygame.display.update
while run:
clock.tick(fps)
redraw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
main()
I have look on the internet and found nothing that works, I am new and this is my first pygame.
Any help would be appreciated.
Simply change pygame.display.update to pygame.display.update()
pygame.display.update does not call the function.
If you want to call the function, use function_name(args).
I am currently making a small game but it is showing a strange error message. Namely:
'Pygame Error': Font Not Initialized
What does this mean?
This is my code:
import pygame, random, sys, os, time
import sys
from pygame.locals import *
WINDOWWIDTH = 800
WINDOWHEIGHT = 600
levens = 3
dead = False
Mousevisible = False
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: #escape quits
terminate()
return
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
#fonts
font = pygame.font.SysFont(None, 30)
#def gameover():
#if dead == True:
#pygame.quit
# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('hit the blocks')
pygame.mouse.set_visible(Mousevisible)
# "Start" screen
drawText('Press any key to start the game.', font, windowSurface, (WINDOWWIDTH / 3) - 30, (WINDOWHEIGHT / 3))
drawText('And Enjoy', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3)+30)
pygame.display.update()
waitForPlayerToPressKey()
zero=0
And this is the error:
Traceback (most recent call last):
File "C:\Users\flori\AppData\Local\Programs\Python\Python37-32\schietspel.py", line 30, in <module>
font = pygame.font.SysFont(None, 30)
File "C:\Users\flori\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 320, in SysFont
return constructor(fontname, size, set_bold, set_italic)
File "C:\Users\flori\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 243, in font_constructor
font = pygame.font.Font(fontpath, size)
pygame.error: font not initialized
The problem is that you're setting the font before initializing the game. To fix this, move font = pygame.font.SysFont(None, 30) after pygame.init().
Also, for your code to work, you'll need to define TEXTCOLOR as well. It should be a tuple with RGB values eg. TEXTCOLOR = (255, 255, 255) for white (you can put it at the top with the WINDOWHEIGHT).
I think you have to initialize pygame.font.
Try pygame.font.init() after from pygame.locals import *
For me, adding pygame.init() before pygame.font.Font() fixed the problem.
I imported a custom font called "Celtic-Bit Thin" from a website to use in a project. The file had the correct extension (.ttf) that pygame can import but when used, only the bottom half of the string of text that is displayed is shown (You can only see the bottom half of each letter in the string of text displayed in the font I chose). I'm not sure if it is a problem with the font or the way I implemented it. I tried:
gameFont = pygame.font.Sysfont("Celtic-Bit Thin", 24)
font.render("Hello world!", False, (0,0,0))
I also tried doing it with pygame.font.Font()but it didn't work either. Is the font simply incompatible with pygame or is there some other requirement for importing fonts?
EDIT: Here is a minimal, runnable example:
import pygame
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((800,600))
gameFont = pygame.font.SysFont("Celtic-Bit Thin", 36)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
runnung = False
screen.fill((255,255,255))
screen.blit(gameFont.render("Hello World!",False,(0,0,0)), (300, 300))
pygame.display.flip()
pygame.quit()
AND here is where I downloaded the font
I don't know why the font is cut off, but I've tried to render it with the pygame.freetype module instead of pygame.font and that works fine.
import pygame
import pygame.freetype # Import the freetype module.
pygame.init()
screen = pygame.display.set_mode((800,600))
gameFont = pygame.freetype.SysFont("Celtic-Bit Thin", 24)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255,255,255))
# You can use `render` ...
text_surface, rect = gameFont.render("Hello World!", (0,0,0))
screen.blit(text_surface, (40, 250))
# or `render_to`.
gameFont.render_to(screen, (40, 350), "Hello World!", (0,0,0))
pygame.display.flip()
pygame.quit()
I am new to pygame, and I was expecting the display be cyan and the rectangle to be drawn. The window appears but not in cyan and without the rectangle?
I believe it has something to do with the order or spacing.
Everything was working before I added the rect.
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('''example''')
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
gameDisplay.fill(cyan)
gameExit = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
You should be very careful with your formatting for Python code. Testing your code and fixing up the formatting for the while loop reveals the problem:
C:\src\python\pygame1>python buggy.py
Traceback (most recent call last):
File "buggy.py", line 16, in <module>
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
TypeError: function takes at most 4 arguments (5 given)
If you just replace the pygame.draw.rect call with the correct number of parameters it shows a cyan window. I tested the following replacement line:
pygame.draw.rect(gameDisplay,soft_pink,(389,200,300,70))
When initializing a pygame screen, a surface is returned which you can fill and should fill continuously in your while loop. I suggest you use a surface object for the rectangle as well. Just change your code like so:
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Example') # shouldn't use triple quotes
gameExit = True
surf = pygame.Surface((200, 75)) # takes tuple of width and height
rect = surf.get_rect()
rect.center = (400, 300)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
gameDisplay.fill(cyan) # continuously paint screen cyan
surf.fill(soft_pink) # continuously paint rectangle
gameDisplay.blit(surf, rect) # position rectangle at position
Remember continuous rendering is the underlying secret to game development, and objects are painted in the order you specify. So in this case you'd actually see the rectangle because it's painted after the screen is.
When i make a pygame game and hover over it, the cursor turns into the loading thing, that means I can't resize the window, or if i were to make buttons i wouldn't be able to use them... Help? Here's my code.
import pygame
import random
import time
import os
from pygame.locals import *
red = (255,0,0)
white = (255,255,255)
black = (0,0,0)
green = (0,255,0)
blue = (0,0,255)
pygame.init()
background_colour = (white)
(width, height) = (800, 600)
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
screen.fill(background_colour)
while True:
pygame.draw.circle(screen,
(random.randint(0,255),
random.randint(0,255),random.randint(0,255)),
(random.randint(0,800),random.randint(0,600)), 20)
pygame.display.flip()
So yeah, it always happens with pygame.
Add this in top of your while.
This for runs throught events and if
event.type == pygame.QUIT
This means that you presed cross for closing the window and window will be closed. You can catch all pygame events like this.
while True: #your wile
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#your code