I am making simple game about clicking circles to learn pygame. I initialize pygame and pygame.font but it gives me an error randomly when I play the game. When I close the game before I get that error it gives me a diffrent error (pygame.error: video system not initialized)
import pygame
import random
import time
#I init here
pygame.init()
pygame.font.init()
class Target():
def __init__(self, p, color):
self.visible = True
self.pos = p
self.color = color
self.startT = time.time()
def isAlive(self):
if time.time()-self.startT >= aliveTime:
global gameOver
gameOver = True
return 0
else:
if self.visible:
return 1
return 0
def colisionCheck(self, color):
if color == pygame.Color(self.color):
self.visible = False
global alive
global score
alive -= 1
score += 1
def update(self):
if self.isAlive():
global targetSize
pygame.draw.circle(screen, self.color, self.pos, targetSize)
class Circle(pygame.sprite.Sprite):
def __init__(self, pos, color, *grps):
super().__init__(*grps)
self.image = pygame.Surface((32, 32))
self.image.set_colorkey((1, 2, 3))
self.image.fill((1, 2, 3))
pygame.draw.circle(self.image, pygame.Color(color), (15, 15), 15)
self.rect = self.image.get_rect(center=pos)
running = True
gameOver = False
score = 0
frames = 0
targetSize = 20
aliveTime = 10
startAliveNum = 5
screenX = 1000
screenY = screenX*0.6
alive = startAliveNum
targets = []
screen = pygame.display.set_mode((screenX, screenY))
font = pygame.font.Font('freesansbold.ttf', 12)
player = Circle(pygame.mouse.get_pos(), 'dodgerblue', targets)
pygame.display.set_caption('My Pygame game')
pygame.display.flip()
def newTargets(ammount):
for i in range(ammount):
pos = random.randint(50, screenX-50), random.randint(50, screenY-50)
targets.append(Target(pos, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))))
newTargets(5)
startT = time.time() + 0.000001
targetStartT = startT
while running:
if gameOver:
running = False
pygame.quit()
frames += 1
text = font.render("{:.2f}fps".format(frames/(time.time()-startT)), True, (0, 0, 0), (255, 255, 255))
##############error here (pygame.error Library not initialized)##############
text2 = font.render("{:.2f} seconds left".format(targetStartT+10-time.time()), True, (0, 0, 0), (255, 255, 255))
text3 = font.render("score: " + str(score), True, (0, 0, 0), (255, 255, 255))
rect = text.get_rect()
rect2 = text2.get_rect()
rect2.center = (rect2.width/2, rect.height*1.5)
rect3 = text3.get_rect()
rect3.center = (rect3.width/2, rect.height*1.5*1.5)
screen.fill((30, 30, 30))
screen.blit(text, rect)
screen.blit(text2, rect2)
screen.blit(text3, rect3)
player.rect.center = pygame.mouse.get_pos()
player.update()
if alive == 0:
startAliveNum += 1
newTargets(startAliveNum)
alive += startAliveNum
aliveTime += 0.5
targetStartT = time.time() + 0.000001
for i in range(len(targets)):
targets[i].update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
color = screen.get_at(pygame.mouse.get_pos())
for i in range(len(targets)):
targets[i].colisionCheck(color)
pygame.display.flip()
########error here when I close the game before It gives me the first error (pygame.error: video system not initialized)########
Could someone please help me fix the errors. (Sory if I spelled someting incorectly)
The problem is calling pygame.quit()
at the beginning of the application loop. When pygame.quit()
is called, all pygame modules will not be initialized and all subsequent calls to pygame functions will fail. Because the application loop executes the statement once to the end of the loop, you get an error. Call pygame.quit()
after the application loop:
while running:
if gameOver:
running = False
#pygame.quit() <--- DELETE
# your code
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
# pygame.quit() <--- DELETE
running = False # <--- INSERT
# your code
# [...]
pygame.quit() # <--- INSERT
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.