[英]Very simple PyGame very slow
I want to make a (a bit bigger) game in PyGame, but even with this simple code I just get arround 10 fps instead of 60? 我想在PyGame中制作一个(更大)的游戏,但是即使使用这个简单的代码,我也只能获得10 fps而不是60 fps? Here's the code: 这是代码:
import pygame
res = 1280,720
display = pygame.display.set_mode(res, pygame.RESIZABLE)
pygame.display.set_caption("Test")
background = pygame.transform.smoothscale(pygame.image.load("Background.png"), res)
a = 0
while True:
pygame.time.Clock().tick(60)
display.fill((0,0,0))
a += 10
display.blit(background, (0,0)) #Without this line: arround 20 fps
pygame.draw.rect(display,(255,0,0), (a,8,339,205))
pygame.display.update()
pygame.quit()
What am I doing wrong? 我究竟做错了什么?
Thank you! 谢谢!
Try the following optimizations: 尝试以下优化:
Use the convert()
method on your image pygame.image.load("Background.png").convert()
). 在图片pygame.image.load("Background.png").convert()
上使用convert()
方法。 This makes the animation about 5 times faster. 这使动画快了大约5倍。
Instead of re-blitting entire background every frame, only update the changed parts of the screen. 不必更新每帧的整个背景,而只更新屏幕的更改部分。
You don't need to clear the screen before drawing the background. 绘制背景之前,无需清除屏幕。
Use the same Clock instance every frame. 每帧使用相同的Clock实例。
Here's the code: 这是代码:
import pygame
pygame.init()
res = (1280, 720)
display = pygame.display.set_mode(res, pygame.RESIZABLE)
pygame.display.set_caption("Test")
background = pygame.transform.smoothscale(pygame.image.load(r"E:\Slike\Bing\63.jpg").convert(), res)
a = 0
clock = pygame.time.Clock()
display.blit(background, (0, 0))
pygame.display.update()
while True:
clock.tick(60)
rect = (a,8,339,205)
display.blit(background, rect, rect) # draw the needed part of the background
pygame.display.update(rect) # update the changed area
a += 10
rect = (a,8,339,205)
pygame.draw.rect(display, (255,0,0), rect)
pygame.display.update(rect) # update the changed area
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.