PyGame-具有简单子画面模拟的低FPS

时间:2019-06-18 04:58:10

标签: python python-3.x pygame

当前正在使用PyGame在Python上进行极其简单的自然选择模拟。我以前用Java制作了这个项目,并在Python上复制了它以用于将来的功能。但是,当我开始在PyGame上工作时,无论精灵数量如何,我都注意到每次模拟的速度都非常低(平均速度为5.67fps)。从1(5.7fps)的精灵到500(5.fps)的FPS,我仍然得到极低的帧率。

这是我目前为该项目准备的三个文件,总共约100行代码,其中大部分是注释和空白。

evolution.py(主要)

MDC

utils.py

## Import Libraries
import sys, pygame, organism, utils

## Initialize
pygame.init()

## Screen Details
size = width, height = 1280, 720
background = (100, 100, 100)
screen = pygame.display.set_mode(size)

## User Inputs
orgCount = input("Starting count of organisms? ")
# (Not added)
#foodCount = input("Starting count of food? ")

## Dictionaries and Sprite Groups
# Organisms
orgDict = {}
orgGroup = pygame.sprite.Group()
# Food (Not added)
#foodDict = {}
#foodGroup = pygame.sprite.Group()

## Add starting values
utils.orgAdd(orgDict, orgGroup, int(orgCount))

print(orgDict)

## Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()

""" Simulation """
while not done:
    ## Exit
    for event in pygame.event.get():
        if event.type == pygame.QUIT: done = True

    ## Display FPS
    pygame.display.set_caption("FPS:" + str(clock.get_fps()))

    ## Set background color
    screen.fill(background)

    orgGroup.update()

    ## Organism render
    orgGroup.draw(screen)

    clock.tick(120)

    ## Update the screen
    pygame.display.flip()

## Idle friendly code
pygame.quit()

organism.py

import organism

# Add new organism(s) with UUID to a dictionary and sprite group
def orgAdd(dictionary, group, quantity=1):
    if quantity > 0:
        org = organism.Organism()
        dictionary[org.uuid] = org
        group.add(org)
        orgAdd(dictionary, group, quantity - 1)

编辑

一些其他信息是否有帮助: Python版本3.6.5(超出此版本的任何版本将无法在OSX Mojave上运行PyGame,这是许多用户的报告问题) PyGame版本1.9.6 OSX Mojave在2018 Macbook Pro上

编辑2

使用10个生物(子图形)运行模拟的示例打印输出

import pygame, uuid, random

size = width, height = 1280, 720
WHITE = (255, 255, 255)

class Organism(pygame.sprite.Sprite):
    def __init__(self, size=25, color=(125, 0, 125), position=(0,0)):
        """ Sprite Initialization """
        super().__init__()

        """ Organism Statistics """
        # Unique User ID
        self.uuid = uuid.uuid1()
        # Color
        self.color = color
        # Size
        self.size = size
        # Position
        if position == (0,0):
            self.position = (random.randrange(0,width), random.randrange(0,height))
        else:
            self.position = position

        # Create the surface of the sprite
        self.image = pygame.Surface((size, size))
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        # Draws the circle on the surface of the sprite
        pygame.draw.circle(self.image, self.color, [self.size//2, self.size//2] , size//2)

        # Sets the initial position of the sprite
        self.rect = pygame.Rect(self.position, (self.size, self.size))
        print(self.position)

    def update(self):
        self.rect.move_ip(random.randrange(-10,10), random.randrange(-10,10))

0 个答案:

没有答案