I'm trying to make a simple game where, given some condition, the vision field of the character (always at the center of the screen) gets radially reduced until it totally closes and the game ends. The idea is to cover the view with a semi transparent mask around the character's vision field. The relevant part of the code with the solution I apply would be:
pg.init()
pg.display.init()
screensize = (width,height)=(600,600)
c=(int(width/2),int(height/2))
screen = pg.display.set_mode(screensize)
surface = pg.Surface((600,600), pg.SRCALPHA)
visionradius=20
run=True
while run:
pg.time.delay(20)
for event in pg.event.get():
if event.type == pg.QUIT:
run=False
if CONDITION()==True:
visionradius+=1
else:
pass
pg.draw.circle(surface,(10,255,10,230),(c[0],c[1]),600,visionradius)
screen.blit(surface, (0,0))
if visionradius>599:
run = False
pg.display.update()
screen.fill((255,255,255))
pg.quit()
The problem with this solution is that it makes my game run slow as radius increases. I would appreciate any ideas for an alternative way for a semi transparent closing view, or for preventing the game running slow. I wonder if pygame draws tons of unitary tickness circles as visionradius increases, which would explain the problem I'm having... If that is so, I could reduce the circle's radius as tickness increases, and it would at least parcially reduce the problem!
The problem with this solution is that it makes my game run slow.
Use pygame.time.Clock()
/ tick()
rather than pygame.time.delay()
.
tick()
the function will delay to keep the game running slower than the given ticks per second. The function ensures a constant frame rate:
clock = pg.time.Clock()
FPS = 50
run = True
while run:
clock.tick(FPS)
# [...]
Furthermore, the reduction seems to accelerate as the area of the circle decreases with the square of the radius. That is an optical illusion.