当前位置: 首页>>代码示例>>Python>>正文


Python Surface.set_colorkey方法代码示例

本文整理汇总了Python中pygame.surface.Surface.set_colorkey方法的典型用法代码示例。如果您正苦于以下问题:Python Surface.set_colorkey方法的具体用法?Python Surface.set_colorkey怎么用?Python Surface.set_colorkey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pygame.surface.Surface的用法示例。


在下文中一共展示了Surface.set_colorkey方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
 def __init__(self, fruit, interp_step):
     """ Prepare the fruit's spr: a square diamond 
     with a number in the center.
     interp_step determines where to position the sprite, 
     based on the view's current sprite step. 
     """
     DirtySprite.__init__(self)
     self.fruit = fruit
     
     # make the square
     sq_surf = Surface((cell_size / 1.414, cell_size / 1.414))
     sq_surf.set_colorkey((255, 0, 255))  # magenta = color key
     sq_surf.fill(FRUIT_COLORS[fruit.fruit_type])
     # rotate for a diamond
     dm_surf = rotate(sq_surf, 45)
     blit_rect = Rect(0, 0, cell_size * 1.414, cell_size * 1.414)
     # blit the diamond as the fruit's image
     img = Surface((cell_size, cell_size))
     img.set_colorkey((255, 0, 255))  # magenta = color key
     img.fill((255, 0, 255))
     img.blit(dm_surf, blit_rect)
     
     # add text at the center
     self.font = Font(None, font_size) 
     txtsurf = self.font.render(str(fruit.fruit_num), True, (0, 0, 0))
     textpos = txtsurf.get_rect(center=(cell_size / 2, cell_size / 2))
     img.blit(txtsurf, textpos)
     
     # prepare rect to blit on screen
     self.resync(interp_step)
     
     self.image = img
开发者ID:gentimouton,项目名称:smofac,代码行数:34,代码来源:fruitspr.py

示例2: render

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
    def render(self, text, antialias, color, background=None):
        """Font.render(text, antialias, color, background=None): return Surface
           draw text on a new Surface"""
        color = Color(color)
        fg = ffi.new("SDL_Color [1]")
        bg = ffi.new("SDL_Color [1]")
        fg[0].r = color.r
        fg[0].g = color.g
        fg[0].b = color.b
        if background:
            try:
                background = Color(background)
                bg[0].r = background.r
                bg[0].g = background.g
                bg[0].b = background.b
            except (TypeError, ValueError):
                # Same error behaviour as pygame
                bg[0].r = 0
                bg[0].g = 0
                bg[0].b = 0
        else:
            bg[0].r = 0
            bg[0].g = 0
            bg[0].b = 0

        if text is None or text == "":
            # Just return a surface of width 1 x font height
            height = sdl.TTF_FontHeight(self._sdl_font)
            surf = Surface((1, height))
            if background and isinstance(background, Color):
                surf.fill(background)
            else:
                # clear the colorkey
                surf.set_colorkey(flags=sdl.SDL_SRCCOLORKEY)
            return surf

        if not isinstance(text, basestring):
            raise TypeError("text must be a string or unicode")
        if "\x00" in text:
            raise ValueError("A null character was found in the text")
        if isinstance(text, unicode):
            text = text.encode("utf-8", "replace")
            if utf_8_needs_UCS_4(text):
                raise UnicodeError("A Unicode character above '\\uFFFF' was found;" " not supported")
        if antialias:
            if background is None:
                sdl_surf = sdl.TTF_RenderUTF8_Blended(self._sdl_font, text, fg[0])
            else:
                sdl_surf = sdl.TTF_RenderUTF8_Shaded(self._sdl_font, text, fg[0], bg[0])
        else:
            sdl_surf = sdl.TTF_RenderUTF8_Solid(self._sdl_font, text, fg[0])
        if not sdl_surf:
            raise SDLError(ffi.string(sdl.TTF_GetError()))
        surf = Surface._from_sdl_surface(sdl_surf)
        if not antialias and background is not None:
            surf.set_colorkey()
            surf.set_palette([(bg[0].r, bg[0].g, bg[0].b)])
        return surf
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:60,代码来源:font.py

示例3: render

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
    def render(self):
        self.check()
        if self.disabled: return

        pos = self.rect.center

        t = self.mod["eyeType"]

        color0 = (255,255,255)
        color1 = (0,0,0)

        radius = (self.mod["eyeSkill"] + 2) * 3

        color = skinColor(self.mod)

        # we have to determine how big the eye will be before drawing
        size = (radius * 2, radius * 2)
        rect = Rect((0,0), size)

        image = Surface(size)
        image.fill(self.colorkey)
        image.set_colorkey(self.colorkey)

        # locking the surface makes multiple drawing operations quicker
        image.lock()

        # draw the border of the eye
        if radius < 10:
            steps = 16
        else:
            steps = 8

        for t in range(0,360,steps):
            t = radians(t)
            new_color = Color(color.r, color.g, color.b)
            h, s, v, a = new_color.hsva
            v = int(sin(t) * 50) + 50
            if v < 0: v = 0 - v
            new_color.hsva = (h, s, v, a)
            x = int(rect.centerx + cos(t) * (radius - 4))
            y = int(rect.centery + sin(t) * (radius - 4))
            draw.circle(image, new_color, (x, y), 3)

        # draw the white and pupil
        draw.circle(image, color0, rect.center, radius - 3)
        draw.circle(image, color1, rect.center, (radius - 3) / 3)

        image.unlock()

        rect.center = pos

        self.rect = rect
        self.image = image
开发者ID:MacLeek,项目名称:mh,代码行数:55,代码来源:mob.py

示例4: generate_sphere_sprite

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
def generate_sphere_sprite(radius, color, scale, layer=None):
    scaled_radius = int(round(radius * scale))
    if scaled_radius <= 0:
        scaled_radius = 1

    sprite = Sprite()
    if layer is not None:
        sprite.layer = layer

    width = height = scaled_radius * 2
    surface = Surface((width, height))
    surface.set_colorkey((0, 0, 0))
    draw.circle(surface, color, (scaled_radius, scaled_radius), scaled_radius, 0)
    sprite.image = surface

    return sprite
开发者ID:robertfw,项目名称:einsteins-war,代码行数:18,代码来源:galaxy.py

示例5: build_ship_sprite

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
def build_ship_sprite(size=None, scale=None, orientation=None, heading=None, main_engine=False, retro_engine=False):
    ship_height = int(round(size * scale))
    if ship_height <= 0:
        ship_height = 9

    width = ship_height / 3

    flame_height = ship_height / 5
    image_width = width
    image_height = ship_height + (flame_height * 2)

    sprite = Sprite()
    sprite.layer = 10
    surface = Surface((image_width, image_height))
    surface.set_colorkey((0, 0, 0))

    ship_top = flame_height
    ship_bottom = image_height - flame_height
    ship_left = 0
    ship_right = image_width
    ship_middle = image_width / 2

    ship_triangle = [(ship_middle, ship_top), (ship_left, ship_bottom), (ship_right, ship_bottom)]
    draw.polygon(surface, (255, 255, 255), ship_triangle)

    if main_engine:
        bottom_flame = [(ship_middle, ship_bottom), (0, image_height), (image_width, image_height)]
        draw.polygon(surface, (255, 255, 0), bottom_flame)

    if retro_engine:
        top_flame = [(ship_middle, ship_top), (0, 0), (image_width, 0)]
        draw.polygon(surface, (255, 255, 0), top_flame)

    rotated_surface = transform.rotate(surface, -orientation)

    sprite.image = rotated_surface

    return sprite
开发者ID:robertfw,项目名称:einsteins-war,代码行数:40,代码来源:ship.py

示例6: convert_to_colorkey_alpha

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
def convert_to_colorkey_alpha(surf, colorkey=color.Color('magenta')):
        """Give the surface a colorkeyed background that will be
        transparent when drawing.
        Colorkey alpha, unlike per-pixel alpha, will allow the
        surface's transparent background to remain while using
        methods such as Surface.set_alpha().

        Keyword arguments:
            surf        The Surface to convert.
            colorkey    The color value for the colorkey. The default
                        is magenta or RGB(255, 0, 255).
                        This should be set to a color that isn't
                        present in the image, otherwise those areas
                        with a matching colour will be drawn
                        transparent as well.
        """
        colorkeyed_surf = Surface(surf.get_size())

        colorkeyed_surf.fill(colorkey)
        colorkeyed_surf.blit(surf, (0, 0))
        colorkeyed_surf.set_colorkey(colorkey)
        colorkeyed_surf.convert()

        return colorkeyed_surf
开发者ID:MarquisLP,项目名称:Sidewalk-Champion,代码行数:26,代码来源:graphics.py

示例7: PopUpMenu

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
class PopUpMenu(Clickable):
    def __init__(self):
        self.visible = False
        self.items = []
        self.x, self.y, self.w, self.h = (0, 0, 0, 0)
        self.surface = False
        self.dirty = True

    def is_pressed(self, x, y, button):
        if not self.visible:
            return False
        if x < self.x:
            return False
        elif x > self.x + self.w:
            return False
        elif y < self.y:
            return False
        elif y > self.y + self.h:
            return False

        res = False
        for item in self.items:
            res = res or item.is_pressed(x - self.x, y - self.y, button)
        return res

    def show(self, x, y):
        right = x + self.w
        if right > 600:
            x -= right - 600
        bottom = y + self.h
        if bottom > 600:
            y -= bottom - 600
        self.x = x
        self.y = y
        self.visible = True

    def hide(self):
        self.visible = False

    def add_item(self, item):
        self.items.append(item)
        item.x = 0
        item.y = self.h
        self.h += item.h
        if self.w < item.w:
            self.w = item.w
        self.dirty = True

    def clean(self):
        self.w, self.h = 0, 0
        self.items = []

    def draw(self, screen):
        if not self.visible:
            return
        if self.dirty:
            self.surface = Surface((self.w, self.h))
            self.surface.fill(0x111111)
            self.surface.set_colorkey(0x111111)
            for item in self.items:
                item.draw(self.surface)
            self.dirty = False
        screen.blit(self.surface, (self.x, self.y))
开发者ID:trid,项目名称:anticivilization,代码行数:65,代码来源:pop_up_menu.py

示例8: __init__

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
class Animation:
    """Animation class"""
    colorkey = (0xff, 0xff, 0)

    def __init__(self, surface: Surface, rows, cols,
                 frame_time=.1):
        self.surface = surface

        self.rows = rows
        self.cols = cols

        self.frame_width = surface.get_width() / rows
        self.frame_height = surface.get_height() / cols

        self.row = 0
        self._col = 0

        self.frame_time = frame_time
        self.time = 0

        self.image = Surface((self.frame_width, self.frame_height))
        self.image.set_colorkey(Animation.colorkey)

        self.valid = False

    def update_image(self):
        """Updates image"""
        area = Rect(self.row * self.frame_width,
                    self._col * self.frame_height,
                    0, 0)
        area.x = self.row * self.frame_width
        area.y = self._col * self.frame_height
        area.width = area.x + self.frame_width
        area.height = area.y + self.frame_height

        self.image.fill(Animation.colorkey)
        self.image.blit(self.surface, (0, 0), area)

        self.valid = True

    def update(self, delta_time):
        """Updates"""
        self.time += delta_time

        old_row = self.row
        self.row = int(self.time / self.frame_time) % self.rows

        if old_row != self.row or not self.valid:
            self.update_image()

    def reset(self):
        """Resets"""
        self.row = 0
        self._col = 0
        self.time = 0
        self.valid = False

    @property
    def col(self):
        """Returns col"""
        return self._col

    @col.setter
    def col(self, value):
        """Sets column"""
        self._col = value
        self.valid = False
开发者ID:crimson-king,项目名称:Bomberman,代码行数:69,代码来源:animation.py

示例9: Font

# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import set_colorkey [as 别名]
    screen.blit(bg, (50, 50))

    font = Font(None, 25)
    txt = 'qwertyuiop'
    txtimg = font.render(txt, 1, (255, 255, 255)) # antialiasing w/o bg => alpha 
    
    b = Surface((100, 100), SRCALPHA)
    b.fill((111, 111, 111, 128))
    b.blit(txtimg, (10, 10))
    b = b.convert_alpha()
    screen.blit(b, (25, 25))
    
    # what's below has better perf, but bad output when antialias + transparency 
    c = Surface((100, 100))
    colkey = (255, 0, 255)
    c.set_colorkey(colkey, RLEACCEL)
    c.fill(colkey) # make the surface bg invisible
    
    c2 = Surface((100, 100))
    c2.fill((111, 111, 111))
    c2.set_alpha(128, RLEACCEL) # semi-transparent gray bg
    #c.blit(c2, (0, 0))
    c2 = c2.convert()
    txtimg2 = txtimg.convert(c) # sucks if txtimg is antialiased
    c.blit(txtimg2, (10, 10))
    c = c.convert() 
    screen.blit(c2, (125, 25))
    screen.blit(c, (125, 25))
    pygame.display.update()
    
    while pygame.event.poll().type != KEYDOWN:
开发者ID:gentimouton,项目名称:crowd-control,代码行数:33,代码来源:view.py


注:本文中的pygame.surface.Surface.set_colorkey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。