Python pygame.draw() Examples

The following are 8 code examples of pygame.draw(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygame , or try the search function .
Example #1
Source File: test_sprites.py    From wasabi2d with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_draw_labels(scene):
    """We can draw text labels."""
    scene.background = '#ccccff'
    scene.layers[0].add_label(
        "Left\naligned.",
        pos=(10, 50),
        color='navy'
    )
    scene.layers[0].add_label(
        "or right",
        font="eunomia_regular",
        fontsize=60,
        pos=(790, 200),
        align="right",
        color=(0.5, 0, 0),
    )
    scene.layers[0].add_label(
        "Center über alles",
        fontsize=40,
        pos=(400, 500),
        align="center",
        color='black',
    ) 
Example #2
Source File: test_sprites.py    From wasabi2d with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_draw_sprite(scene):
    """We can draw a sprite to the scene."""
    scene.layers[0].add_sprite('ship', pos=(400, 300)) 
Example #3
Source File: base_widget.py    From python-examples with MIT License 5 votes vote down vote up
def _draw_base(self):
        """
        Widgets should overload to draw default images found in self._images.
        This method will not be called when the user gives a custom image.

        """
        pass 
Example #4
Source File: base_widget.py    From python-examples with MIT License 5 votes vote down vote up
def _draw_final(self):
        """
        Widgets should overload to draw final things that should
        be drawn regardless of whether a custom image was used or not.

        """
        pass 
Example #5
Source File: base_widget.py    From python-examples with MIT License 5 votes vote down vote up
def _draw_base(self):
        """
        Widgets should overload to draw default images found in self._images.
        This method will not be called when the user gives a custom image.

        """
        pass 
Example #6
Source File: base_widget.py    From python-examples with MIT License 5 votes vote down vote up
def _draw_final(self):
        """
        Widgets should overload to draw final things that should
        be drawn regardless of whether a custom image was used or not.

        """
        pass 
Example #7
Source File: mygame.py    From pysnake with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, **kwargs):
        """初始化

        可选参数:
            game_name       游戏名称
            icon            图标文件名
            screen_size     画面大小
            display_mode    显示模式
            loop_speed      主循环速度
            font_name       字体文件名
            font_size       字体大小
        """
        pygame.init()
        pygame.mixer.init()
        self.game_name = kwargs.get("game_name") or GAME_NAME
        pygame.display.set_caption(self.game_name)
        self.screen_size = kwargs.get("screen_size") or SCREEN_SIZE
        self.screen_width, self.screen_height = self.screen_size
        self.display_mode = kwargs.get("display_mode") or DISPLAY_MODE
        self.images = {}
        self.sounds = {}
        self.musics = {}
        self.icon = kwargs.get("icon") or None
        self.icon and pygame.display.set_icon(pygame.image.load(self.icon))
        self.screen = pygame.display.set_mode(self.screen_size,
                                              self.display_mode)
        self.loop_speed = kwargs.get("loop_speed") or LOOP_SPEED
        self.font_name = kwargs.get("font_name") or FONT_NAME
        self.font_size = kwargs.get("font_size") or FONT_SIZE
        self.font = pygame.font.Font(self.font_name, self.font_size)
        self.clock = pygame.time.Clock()
        self.now = 0
        self.background_color = kwargs.get("background") or BLACK
        self.set_background()
        self.key_bindings = {}                      # 按键与函数绑定字典
        self.add_key_binding(KEY_PAUSE, self.pause)

        self.game_actions = {}                      # 游戏数据更新动作

        self.draw_actions = [self.draw_background]  # 画面更新动作列表

        self.running = True
        self.draw = pygame.draw 
Example #8
Source File: test_sprites.py    From wasabi2d with GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_draw_many_sprites(scene):
    """Test that we can draw hundreds of large sprites.

    We want to test that we grow the available texture space when necessary, so
    we generate and pre-cache new sprites.

    """
    scene.layers[1].add_sprite('ship', pos=(400, 300))
    rng = random.Random(0)
    with patch.dict(images.__dict__, _cache={}):
        for i in range(100):
            s = Surface((64, 64), flags=pygame.SRCALPHA, depth=32)
            segments = rng.randint(6, 9)
            theta = np.linspace(0, 2 * np.pi, segments).reshape((-1, 1))
            verts = np.hstack([
                np.cos(theta),
                np.sin(theta),
            ]).astype('f4')
            radii = np.array([rng.uniform(10, 30) for _ in verts])
            verts *= radii[:, np.newaxis]
            verts += (32, 32)
            color = np.array((rng.randint(128, 200),) * 3, dtype=np.uint8)
            pygame.draw.polygon(
                s,
                points=verts,
                color=color
            )
            pygame.draw.polygon(
                s,
                points=verts,
                color=color // 2,
                width=2,
            )
            k = f'rock{i}'
            images._cache[k, (), ()] = s
            scene.layers[0].add_sprite(
                k,
                pos=(
                    rng.uniform(0, 800),
                    rng.uniform(0, 600),
                )
            )
    assert len(scene.layers.atlas.packer.texs) > 1