PyGame多线程display.update不流畅

时间:2018-04-17 00:51:21

标签: python multithreading python-3.x pygame

我正在用python和pygame制作游戏,因为我的游戏涉及很多精灵,所以我决定为每个敌人\角色使用一个单独的线程,它运行一个方法_mainloop。我刚刚开始这个,我遇到了一个问题。我正在制作一个绘制火柴人的基础课程,并且具有到目前为止能够在X坐标上编写的能力。它几乎完美地运行,但问题是我认为,因为火柴人是在类_mainloop方法中绘制的,并且显示器正在主事件处理循环中更新,这使得火柴人看起来有时候会更厚,这有点奇怪。我不想更新_mainloop中的显示,因为屏幕上会有大约FPS *敌人+玩家,这可能会达到每秒约500 pygame.display.updates,这有点过分。如果有人知道有更好的方法可以做到这一点,你能告诉我吗?好吗?
iv阅读这些:
Pygame Multithreading
Multithreading with Pygame
这些与我的问题无关。我真的找不到任何东西 这是我的代码:

from random import randint
import threading

from pygame.locals import *
import pygame as pg

FPS = 60
CLOCK = pg.time.Clock()
COLOURS = {
    'beige': (232, 202, 145),
    'green': (0, 128, 0),
    'blue': (0, 0, 128),
    'red': (128, 0, 0),
    'dark red': (255, 0, 0),
    'dark blue': (0, 0, 255),
    'dark green': (0, 255, 0),
    'black': (0, 0, 0),
    'aqua': (0, 255, 255),
    'white': (255, 255, 255),
    'teal': (0, 128, 128),
    'purple': (128, 128, 0),
    'dark purple': (255, 255, 0),
    'yellow': (255, 255, 0),
    'silver': (192, 192, 192),
    'gold': (192, 192, 96),
    'gray': (211, 211, 211),
}

class CharacterImage:
    """
    this is a sprite that at this point, should really
    just be able to move around.
    """

    # lots of methods and properties and stuff
    ...

    def build_image(self, surface):
        """
        constructs and draws the stickman to the 
        screen.
        """
        # really big function
        ...

    def move_to(self, pos: 'x', surface, pixels=1):
        """
        moves the character image by pixels
        towards the destination.
        INCOMPLETE: only X coordinates are supported
        """

        current = self.topleft[0]
        direction = 'b' if pos < current else 'f'
        current_pos = current - pixels if direction == 'b' else current + pixels
        self.update_coords((current_pos, self.topleft[1]))
        self.build_image(surface)
        return current_pos

    def start_thread(self, pos, surface, pixels=1):
        a = threading.Thread(target=lambda: self._mainloop(pos, surface, pixels), daemon=True)
        self.mainthread = a
        a.start()

    def _mainloop(self, pos, surface, pixels, *args, **kwargs):
        new_pos = pos
        while self.topleft != new_pos:
            new_pos = self.move_to(pos, surface, pixels, *args, **kwargs)
            CLOCK.tick(FPS)


class WeaponDummy:
    ... # random code


def main():
    a = pg.display.set_mode((800, 400))
    testsurf = pg.surface.Surface((2, 2))
    testsurf.fill(COLOURS['green'])
    s = CharacterImage('test', WeaponDummy(testsurf), (10, 300))
    d = CharacterImage('test', WeaponDummy(testsurf), (10, 320))
    s.build_image(a)
    d.build_image(a)
    d.start_thread(880, a)
    s.start_thread(880, a)

    while True:

        for i in pg.event.get():
            if i.type == QUIT:
                pg.quit()
                raise Exception

        pg.display.update()
        a.fill(COLOURS['black'])

        print('updated')
        CLOCK.tick(FPS)

if __name__ == '__main__':
    main()

如果你想要可以运行的完整代码,你可以得到它here

任何帮助将不胜感激。非常感谢!

0 个答案:

没有答案