I want to draw two types of images with pygame at 30fps or more.

Asked 2 years ago, Updated 2 years ago, 463 views

I created a program that alternately draws two images using pygame.
Up to 28fps, I was able to do it without any discomfort, but if it reaches 30fps or more, I will not be able to draw because I thought there was a lag.
I don't know if there is something wrong with the program or if I have to change the fps.
Does anyone know?The code is as follows:Thank you for your cooperation.

import time
import sys, random
import pygame
from pygame.locals import*

defmain():
    pygame.init()
    screen=pygame.display.set_mode(960,540))
    pygame.display.set_caption("image")
    logoA=pygame.image.load("checkA.bmp")
    logoA=pygame.transform.scale(logoA, (960,540))
    logoB=pygame.image.load("checkB.bmp")
    logoB = pygame.transform.scale(logoB, (960,540))
    imageFrag = 0
    fps = 28

    screen=pygame.display.set_mode(960,540))
    pygame.display.set_caption("image")
    t1 = time.time()
    while True:
        fpsclock = pygame.time.Clock()
        fpsclock.tick (fps)
        screen.fill(0,0,0))
        if imageFrag == 0:
            screen.blit(logoA,(0,0))
            imageFrag=1
            print ("logoA")
        else:
            screen.blit(logoB,(0,0))
            imageFrag = 0
            print("logoB")
        pygame.display.update()
        for event in pygame.event.get():
            if event.type==KEYDOWN:
                if event.key==K_SPACE:
                    t2 = time.time()
                    print(t2-t1)
                    print(fpsclock.get_fps())
                    pygame.quit()
                    sys.exit()

if__name__=='__main__':
    main()

python python3 pygame

2022-10-27 00:01

1 Answers

I think the clock can be generated out of the loop.

fpsclock=pygame.time.Clock()
while True:

I think this will make it a little faster.However, "lagging" can be balanced with the display refresh rate.

The code for the question alternates between frames.When the switching interval approaches or exceeds the display refresh rate, various symptoms can occur depending on which frame the display picks up.It may not matter if it is still around 30fps, but please check the refresh rate of the display.

Also, I think this program needs to be careful of photosensitivity attacks and select the image to display.


2022-10-27 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.