闪电扫描移动的背景图像会导致fps的极度下降



我正在PyGame中制作一个Flappy Bird的克隆,并试图添加一个移动的背景图像。我现在使用的是800x600的图像,由于某种原因,当我对图像进行闪电扫描时,性能会受到严重影响。是不是我做错了什么,占用了很多资源?

import pygame, sys
import random
from pygame import *
pygame.init()
red = (255, 0, 0)
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("flap it up")
clock = pygame.time.Clock()
MAX_VEL = 5
GAP = 175
WALLSPEED = -2
WALLEVENT = pygame.USEREVENT+1
SCOREEVENT = pygame.USEREVENT+2
pygame.time.set_timer(WALLEVENT, 5000)
pygame.time.set_timer(SCOREEVENT, 2500)
myfont = pygame.font.SysFont("monospace", 18)
class Player(object):
    def __init__(self):
        self.rect = pygame.Rect(384, 284, 32, 32)
        self.xvel = 0
        self.yvel = MAX_VEL
        self.xcel = 0
        self.ycel = 1
    def move(self):
        self.rect.top += self.yvel
        if self.yvel < MAX_VEL:
            self.yvel += self.ycel
        else:
            self.yvel = MAX_VEL

    def jump(self):
        self.yvel = -15
    def draw(self):
        pygame.draw.rect(screen, red, self.rect)

class Wall(object):
    def __init__(self, y):
        self.rect1 = pygame.Rect(800, y, 32, 600-y)
        self.rect2 = pygame.Rect(800, 0, 32, y-GAP)
        self.xvel = WALLSPEED
    def move(self, walls):
        self.rect1.x += self.xvel
        self.rect2.x += self.xvel
        if self.rect1.x < -31:
            walls.remove(self)
    def draw(self):
        pygame.draw.rect(screen, green, self.rect1)
        pygame.draw.rect(screen, green, self.rect2)
class BG(object):
    def __init__(self, x):
        self.x = x
        self.xvel = WALLSPEED
        self.img = pygame.image.load("bg.jpg")
    def move(self, bg):
        self.x += self.xvel
        if self.x < -799:
            bg.remove(self)
            bg.append(BG(self.x+1600))
        screen.blit(self.img, (self.x, 0))
def lose():
    pygame.quit()
    quit()
def main(player, walls, bg, score, playing):
    while playing:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                player.jump()
            if event.type == WALLEVENT:
                numbo = random.randrange(GAP, 600, 25)
                walls.append(Wall(numbo))
            if event.type == SCOREEVENT:
                score += 1
        for b in bg:
            b.move(bg)
        label = myfont.render("Score: " + str(score), 1, (0, 0, 0))
        screen.blit(label, (30, 20))
        player.move()
        player.draw()
        for w in walls:
            w.move(walls)
            w.draw()
            if player.rect.colliderect(w.rect1) or player.rect.colliderect(w.rect2):
                lose()
                playing = False

        clock.tick(60)
        pygame.display.update()
player = Player()
walls = []
bg = []
score = 0
walls.append(Wall(300))
bg.append(BG(0))
bg.append(BG(800))
playing = True
main(player, walls, bg, score, playing)

通常情况下,这应该不是问题。你应该尝试手动设置FPS,看看是否有差异

# Create a clock
clock = pygame.time.Clock()
# Set FPS (frames per second)
clock.tick(50)

最新更新