Home > other >  Pygame how to use double buffer stable refresh interface
Pygame how to use double buffer stable refresh interface

Time:04-07

Company asked to do a visual stimulus interface,
Require stable 1 second frame refresh rate of 60,

And I the Java temporarily to do python also very headache, documents for several days still can't meet the requirements of company the stable refresh,

Now do GUI graphical interface using pygame,

Specific code is as follows:

 

The import pygame, sys

Def pygame_test () :
# initialization
Pygame. The init ()
Create a window #
Size=width, height=1300, 700,
Screen=pygame. Display. Set_mode (size, pygame DOUBLEBUF | pygame. HWSURFACE) # window mode
# set the window title
Pygame. Display. Set_caption (' Demo ')

FPS=60
FpsClock=pygame. Time. Clock ()

# superpose in advance rendering
Img1="../img/frame_10. Tiff "
Img2="../img/frame_11. Tiff "
Img3="../img/frame_12. Tiff "
Img4="../img/frame_13. Tiff "

N=1

RenderingImg1=pygame. Image. The load (img1). The convert ()
RenderingImg2=pygame. Image. The load (img2). The convert ()
RenderingImg3=pygame. Image. The load (img3). The convert ()
RenderingImg4=pygame. Image. The load (img4). The convert ()
# backgroud_img=pygame. Image. The load (image). Convert_alpha loading ways of the () # 3 (alpha channel is suitable for images, and can improve the rendering speed)

# through continuous loop to listen for events
While True:

# get all events
For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
# determine whether the current event to click exit key
Pygame. Quit ()
Sys. The exit () # need to import in advance sys
Elif event. Type==pygame. VIDEORESIZE:
Size=width, height=event. The size [0], the event. The size of [1] # for new size
Screen=pygame. Display. Set_mode (size, pygame DOUBLEBUF | pygame. HWSURFACE) to render after #

If (n==1) :
Screen. Blit (pygame. Transform. Scale (renderingImg1, size), (0, 0))
Elif (n==2) :
Screen. Blit (pygame. Transform. Scale (renderingImg2, size), (0, 0))
Elif (n==3) :
Screen. Blit (pygame. Transform. Scale (renderingImg3, size), (0, 0))
Elif (n==4) :
Screen. Blit (pygame. Transform. Scale (renderingImg4, size), (0, 0))

N=n + 1

If (n & gt; 4) :
N=1

Pygame. Display. Flip ()
FpsClock. Tick (FPS)
# pygame, time delay (16)


Pygame_test ()



Demands is now has four images, 1 s60hz stable these pictures on the page refresh stimulating effect,
Is essentially want to implement the MLB Screen function or PTB Screen function, and study a lot of double buffer but I still can't be stable refresh,
, every brother please give directions,
  • Related