Abhas Bhattacharya
2014-06-12 07:31:39 UTC
The following code draws a circle and moves it by 4px to left every step.
Problem is that at about 60 fps or more, the circle flickers and sometimes
looks cut off to me (approx. illustration -
Loading Image...
). The flickering is not like a screen
flicker, more like state switching between full circle and cut-off circle.
This problems doesnt occur at all upto 45 fps, couldnt be reproduced by
screen capture or pausing the game, as if the problem is only visible to
the naked eye.
Now, there could be two possibilities:
* Screen is not updated over the required region.
None of the update code depends on the fps, so the flicker should have been
fps-independent. But, the problem disappears at 30/45 fps. But,
surprisingly, it is also fixed if display.flip is used instead of update.
* V-sync/monitor refresh problem
Some other pygame flicker questions mentioned this problem. I thought
software rendering doesnt have this problem, but not really sure. Also, why
would display.flip fix this?
Ver- 1.9.2 a0
import pygame
w,h=800,200
fps=60
pygame.init()
screen = pygame.display.set_mode([w, h])
color=pygame.Color("white")
clock=pygame.time.Clock()
radius=20
x,y=800,100
def get_bbox(x,y):
left = x - radius
top = y - radius
width = radius * 2
height = radius * 2
return pygame.Rect((left, top), (width, height))
while True:
old_x=x
x-=4
screen.fill(pygame.Color("black"),get_bbox(old_x,y))
pygame.draw.circle(screen, color, (x, y), radius, 1)
pygame.display.update([get_bbox(x,y),get_bbox(old_x,y)])
clock.tick(fps)
Problem is that at about 60 fps or more, the circle flickers and sometimes
looks cut off to me (approx. illustration -
Loading Image...
flicker, more like state switching between full circle and cut-off circle.
This problems doesnt occur at all upto 45 fps, couldnt be reproduced by
screen capture or pausing the game, as if the problem is only visible to
the naked eye.
Now, there could be two possibilities:
* Screen is not updated over the required region.
None of the update code depends on the fps, so the flicker should have been
fps-independent. But, the problem disappears at 30/45 fps. But,
surprisingly, it is also fixed if display.flip is used instead of update.
* V-sync/monitor refresh problem
Some other pygame flicker questions mentioned this problem. I thought
software rendering doesnt have this problem, but not really sure. Also, why
would display.flip fix this?
Ver- 1.9.2 a0
import pygame
w,h=800,200
fps=60
pygame.init()
screen = pygame.display.set_mode([w, h])
color=pygame.Color("white")
clock=pygame.time.Clock()
radius=20
x,y=800,100
def get_bbox(x,y):
left = x - radius
top = y - radius
width = radius * 2
height = radius * 2
return pygame.Rect((left, top), (width, height))
while True:
old_x=x
x-=4
screen.fill(pygame.Color("black"),get_bbox(old_x,y))
pygame.draw.circle(screen, color, (x, y), radius, 1)
pygame.display.update([get_bbox(x,y),get_bbox(old_x,y)])
clock.tick(fps)