【问题标题】:Why is pygame slowing down? [duplicate]为什么pygame变慢了? [复制]
【发布时间】:2021-11-18 20:12:46
【问题描述】:

我正在尝试使用 Pygame 制作一个 2D 平台游戏,它以 25 fps 的速度开始运行良好,我用clock.tick(25) 将其设置为上限,然后在 10 秒左右后它开始减速。 我试图添加一个不起作用的 deltatime 函数,我尝试用pygame.display.update() 切换出pygame.display.flip(),但没有任何帮助。 我不确定问题是什么,因为没有错误消息,所以我将显示所有 3 个 python 文件。

这是我的主文件 main.py

import pygame
import player
import environment



def run_game():
    pygame.init()

    winW, winH = 900, 650
    screen = pygame.display.set_mode((winW, winH))
    pygame.display.set_caption(" ")
    icon = pygame.Surface((32, 32))
    icon.fill((255, 255, 255))
    pygame.display.set_icon(icon)

    character = player.Player()

    clock = pygame.time.Clock()
    FPS = 25

    running = True
    while running:
        clock.tick(FPS)
        FPS = 25
        print(clock.get_fps())
        screen.fill((50, 100, 100))
        screen.blit(character.surf, character.rect)

        character.jump()

        # Make The Environment
        environment.make_block(1000, 32, 50, 500, (255, 255, 255))
        environment.make_sprite(64, 64, 200, 100, "Grass Block 16x.png")

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
        # Get Pressed Keys
        keys = pygame.key.get_pressed()
        # Check if the player should be moving
        if keys[pygame.K_LEFT]:
            character.rect.move_ip(-6, 0)
        if keys[pygame.K_RIGHT]:
            character.rect.move_ip(6, 0)
        if keys[pygame.K_UP] and character.on_ground:
            character.jumping = True
        else:
            character.jumping = False

        for i in range(len(environment.block_surf_list)):
            screen.blit(environment.block_surf_list[i], environment.block_rect_list[i])
        for i in range(len(environment.sprite_list)):
            screen.blit(environment.sprite_list[i], environment.sprite_rect_list[i])

        pygame.display.flip()


if __name__ == "__main__":
    run_game()

这是我的播放器文件 player.py

import pygame
import environment
from main import *

class Player(pygame.sprite.Sprite):
    def __init__(self, w=32, h=32):
        super(Player, self).__init__()
        self.surf = pygame.Surface((w, h))
        self.rect = self.surf.get_rect()
        self.surf.fill((255, 0, 0))
        self.jumping = False
        self.on_ground = False
        self.fall_speed = 3
        self.air_time = 0
        self.jump_height = 30

    def jump(self):
        if self.jumping and self.air_time <= 5:
            self.rect.y -= self.jump_height
            self.fall_speed = 0
            self.air_time += 1
        else:
            for i in range(len(environment.block_rect_list)):
                if self.rect.colliderect(environment.block_rect_list[i]):
                    self.on_ground = True
                    self.air_time = 0
                else:
                    self.on_ground = False

            if not self.on_ground:
                self.rect.y += self.fall_speed
                self.fall_speed += 2

这是我的环境文件 environment.py

import pygame

block_surf_list = []
block_rect_list = []


def make_block(width, height, x_pos, y_pos, block_color):
    block_surf = pygame.Surface((width, height))
    block_surf.fill(block_color)
    block_rect = block_surf.get_rect()
    block_rect.move_ip(x_pos, y_pos)

    block_surf_list.append(block_surf)
    block_rect_list.append(block_rect)


sprite_list = []
sprite_rect_list = []

test_size_1 = 32


def make_sprite(width, height, x_pos, y_pos, sprite):
    block_image = pygame.image.load(sprite)
    block_image = pygame.transform.scale(block_image, (width, height))
    block_image_rect = block_image.get_rect()
    block_image_rect.move_ip(x_pos, y_pos)

    sprite_list.append(block_image)
    sprite_rect_list.append(block_image_rect)

【问题讨论】:

  • 您正在为每次迭代制作一个新块和一个新精灵。 10 秒后,您将拥有 250 个副本。不要那样做。做一个,在循环之外,改变他们的位置。
  • 你在每一帧都调用pygame.image.load(sprite)。 (make_sprite)
  • 感谢您的帮助,我刚刚将 make_block() 函数移到了循环之外,并且它起作用了。

标签: python pygame


【解决方案1】:

您似乎每帧都在制作一个新块。您可以制作一个块和一个单独的函数来绘制它,而不是制作一个新块并每帧绘制它。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
相关资源
最近更新 更多