python-3.x - 如何在屏幕上同时创建数千个图像和像素?使用 Python

标签 python-3.x graphics pygame

对于一个模拟项目,我需要在 python 下的图形环境中创建和管理大量的单个像素和小 gif。 我试过“graphics.py”。但是,它在添加大约 3000 个对象后崩溃了。错误代码不是不言自明的:

“Python 已停止工作”问题签名:问题事件名称:APPCRASH 应用程序名称:python.exe 应用程序版本:3.5.1150.1013 应用程序时间戳:576eff6a 故障模块名称:tcl86t.dll

你知道问题出在哪里吗?甚至,“graphics.py”是否适合处理大量对象? 谢谢

添加: 这是我实例化新对象或移动它时的示例:

...

    self.image__ = Image(Point(self.x_, self.y_), 1, 1)

           #shaping image with some pixels
    self.shape_single_cell()                

           #sending image to the screen
    CreatureSingleCell.world_handle__.draw_image(self.image__)

...

    #moving the image to a new spot in the screen
def action_single_cell_moving(self, dx, dy):
     self.image__.move(dx, dy)

最佳答案

还不是最终答案,但为了让您暂时了解一些东西,我创建了一个模拟,其中有 1000 个随机抽动的方 block 。使用我在评论中提到的涉及 glBegin 的简单但缓慢的技术,我仍然得到大约 60 fps,而在 10000 个对象时,它下降到 7 fps。目前没有纹理或对象出生/死亡,但我认为这可能是一个好的开始。这是您的进化模拟的合理表示吗?另外让我知道您在计算机上获得的性能,以及这对于您的模拟目的是否足够快,或者是否需要切换到更复杂/高级的技术。

from OpenGL.GL import *
from OpenGL.GL import shaders
from OpenGL.GLU import *
import math
import pygame
import random
import sys
import time

class Creature(object):
    def __init__(self):
        self.size = 10
        self.x = 0
        self.y = 0

    def move(self, dx, dy):
        self.x += dx
        self.y += dy

    def set_pos(self, x, y):
        self.x = x
        self.y = y

class Simulation(object):
    def __init__(self, world_size, num_creatures=1000, max_move=1):#not really max by pythagorean theorem
        self.width, self.height = world_size
        self.max_move = max_move
        self.creatures = self.setup(num_creatures)

    def setup(self, num_creatures):
        creatures = []
        for i in range(num_creatures):
            x = int(random.random()*self.width)
            y = int(random.random()*self.height)
            creature = Creature()
            creature.set_pos(x, y)
            creatures.append(creature)
        return creatures

    def update(self):
        for creature in self.creatures:
            dx = int(round(random.uniform(-self.max_move, self.max_move)))
            dy = int(round(random.uniform(-self.max_move, self.max_move)))
            creature.move(dx, dy)

if __name__ == "__main__":
    width = 800
    height = 600
    title = "Random Simulation"
    target_fps = 60
    pygame.init()
    size = (width, height)
    flags = pygame.DOUBLEBUF|pygame.OPENGL
    pygame.display.set_mode(size, flags)
    pygame.display.set_caption(title)

    c = Creature()
    sim = Simulation(size)

    prev_time = time.time()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
                pygame.quit()

        sim.update()   
        glClear(GL_COLOR_BUFFER_BIT)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, width, height, 0, -1, 1)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        for i in sim.creatures:
            glBegin(GL_QUADS)
            glVertex(i.x - i.size/2.0, i.y - i.size/2.0)
            glVertex(i.x + i.size/2.0, i.y - i.size/2.0)
            glVertex(i.x + i.size/2.0, i.y + i.size/2.0)
            glVertex(i.x - i.size/2.0, i.y + i.size/2.0)
            glEnd()
        pygame.display.flip()

        curr_time = time.time()
        diff = curr_time - prev_time
        delay = max(1.0/target_fps - diff, 0)
        time.sleep(delay)
        fps = 1.0/(delay + diff)
        prev_time = curr_time
        pygame.display.set_caption("{0}: {1:.2f}".format(title, fps))

关于python-3.x - 如何在屏幕上同时创建数千个图像和像素?使用 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40078062/

相关文章:

python - 在 Mac 上安装 pygame 时出现 sdl.h 错误?

python - 如何在pygame中控制单个子弹

创建字典时遇到Python脚本,对于字典的每个键,有多个值(300个条目)列表

c - 带有 GL_CURRENT_COLOR 的 glGetFloatv 不起作用

python - 如何在 Python 中将来自 Web 的原始 html 转换为可解析的 xml

R:独立于缩放旋转文本(使用多行文本)?

java - 使用抽象父类(super class)生成子类的随机实例

python - 游戏 : Why is giving error everytime i quit the snake game?

python - tkinter 执行在大约 140 次迭代后终止,没有错误消息(内存泄漏?)

Python3 列出来自特定目录的文件