本文整理汇总了Python中pygame.surface.Surface.fill方法的典型用法代码示例。如果您正苦于以下问题:Python Surface.fill方法的具体用法?Python Surface.fill怎么用?Python Surface.fill使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame.surface.Surface
的用法示例。
在下文中一共展示了Surface.fill方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Panel
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
class Panel(Clickable):
def __init__(self, x, y, w, h, color=0x000000):
self.items = []
self.clickables = []
self.surface = Surface((w, h))
self.visible = True
self.x = x
self.y = y
self.color = color
def draw(self, screen):
if self.visible:
self.surface.fill(self.color)
for item in self.items:
item.draw(self.surface)
screen.blit(self.surface, (self.x, self.y))
def add(self, item):
self.items.append(item)
if isinstance(item, Clickable):
self.clickables.append(item)
def is_pressed(self, wx, wy, button):
if not self.visible:
return False
x = wx - self.x
y = wy - self.y
res = False
for item in self.clickables:
res = res or item.is_pressed(x, y, button)
return res
示例2: update
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
def update(self, duration):
if self.dirty == 0:
return
# TODO: is bliting on existing surf faster than creating a new surface?
size = self.rect.size
txtcolor = self.txtcolor
bgcolor = self.bgcolor
if bgcolor: # opaque bg
txtimg = self.font.render(self.text, True, txtcolor, bgcolor)
txtimg = txtimg.convert()
img = Surface(size)
img.fill(bgcolor)
else: # transparent bg
txtimg = self.font.render(self.text, True, txtcolor)
txtimg = txtimg.convert_alpha()
img = Surface(size, SRCALPHA) # handle transparency
img.fill((0, 0, 0, 0)) # 0 = transparent, 255 = opaque
# center the txt inside its box
ctr_y = size[1] / 2
textpos = txtimg.get_rect(left=2, centery=ctr_y)
img.blit(txtimg, textpos)
self.image = img
示例3: prepare_paragraph
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
def prepare_paragraph(text, width, size="normal", colour=(0,0,0)):
font = FONTS[size]
lines = []
words = text.split()
if words:
lastline = None
line = words[0]
for i in range(1,len(words)):
lastline = line
line = line+" "+words[i]
w,h = font.size(line)
if w > width:
lines.append(lastline)
line = words[i]
else:
line = ""
lines.append(line)
parawidth = max(font.size(each)[0] for each in lines)
lineheight = font.get_height()
paraheight = lineheight*len(lines)
paragraph = Surface((parawidth,paraheight),pygame.SRCALPHA)
paragraph.fill((255,255,255,0))
for y,line in enumerate(lines):
text = prepare(line,size,colour)
paragraph.blit(text,(0,y*lineheight))
return paragraph
示例4: __init__
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [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
示例5: __init__
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
def __init__(self, evManager, numlines=3, rect=(0, 0, 100, 20), txtcolor=(255, 0, 0), bgcolor=None):
Widget.__init__(self, evManager)
# When receiving an event containing text,
# add or remove that text to the widget's set of text lines to display.
# addevents_attrs maps event classes to the text attr of events
# that add text to display.
self.addevents_attrs = {MdAddPlayerEvt: "pname", MNameChangedEvt: "newname", MMyNameChangedEvent: "newname"}
for evtClass in self.addevents_attrs:
self._em.reg_cb(evtClass, self.on_addtextevent)
# rmevents_attrs maps event classes to the text attributes of events
# that remove text to display.
self.rmevents_attrs = {MPlayerLeftEvt: "pname", MNameChangedEvt: "oldname", MMyNameChangedEvent: "oldname"}
for evtClass in self.rmevents_attrs:
self._em.reg_cb(evtClass, self.on_rmtextevent)
self.texts = [] # Each text is a player name.
# gfx
self.font = Font(None, config_get_fontsize())
self.rect = rect
self.txtcolor = txtcolor
self.bgcolor = bgcolor
img = Surface(rect.size)
if bgcolor:
img.fill(bgcolor)
else:
img.set_alpha(0, RLEACCEL) # fully transparent
self.image = img
self.maxnumlines = numlines
self.linewidgets = deque(maxlen=numlines) # deque of TextLabelWidgets
示例6: prepare_table
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
def prepare_table(rows, alignment="lr", size="normal", colour=(0,0,0), padding=0):
f = FONTS[size]
numcolumns = len(rows[0])
numrows = len(rows)
def u(n):
return n if isinstance(n, unicode) else unicode(n)
shapes = [[f.size(u(col)) for col in row] for row in rows]
maxheight = max(max(shape[1] for shape in shaperow) for shaperow in shapes)
widths = [max(shaperow[i][0] for shaperow in shapes) for i in range(numcolumns)]
table = Surface((sum(widths) + padding * (numcolumns - 1),
maxheight * numrows + padding * (numrows - 1)),
pygame.SRCALPHA)
table.fill((255,255,255,0))
y = 0
for r, row in enumerate(rows):
x = 0
for c, col in enumerate(row):
w, h = shapes[r][c]
text = prepare(u(col), size=size, colour=colour)
align = alignment[c]
if align == "r":
adjustx = widths[c] - w
elif align == "c":
adjustx = (widths[c] - w) // 2
else:
adjustx = 0
table.blit(text, (x + adjustx, y))
x += widths[c] + padding
y += maxheight + padding
return table
示例7: __init__
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
class Grid:
surface = None
def __init__(self, width, height, spacing=20):
self.width = width
self.height = height
self.spacing = spacing
self.initializeSurface()
def initializeSurface(self):
self.surface = Surface((self.width, self.height), flags=HWSURFACE | SRCALPHA)
self.surface.fill(Color(0, 0, 0, 0))
for i in range(0, self.width, self.spacing):
pygame.draw.line(self.surface, Color(0, 0, 0, 255), (i, 0), (i, self.height))
for i in range(0, self.height, self.spacing):
pygame.draw.line(self.surface, Color(0, 0, 0, 255), (0, i), (self.width, i))
pygame.draw.line(self.surface,
Color(0, 0, 0, 255),
(self.width - 1, 0),
(self.width - 1, self.height))
pygame.draw.line(self.surface, Color(0, 0, 0, 255),
(0, self.height - 1), (self.width, self.height - 1))
def render(self, screen):
screen.blit(self.surface, (0, 0))
pass
示例8: activate
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
def activate(self):
xsize = 300
ysize = 70
bkg = Surface((xsize, ysize))
bkg.lock()
bkg.fill((128,128,128))
for i in range(1, 4):
draw.rect(bkg,(i*32,i*32,i*32),(4-i,4-i,xsize+(i-4)*2,ysize+(i-4)*2),3)
corner = (64,64,64)
bkg.set_at((0,0), corner)
bkg.set_at((xsize,0), corner)
bkg.set_at((xsize,ysize), corner)
bkg.set_at((0,ysize), corner)
bkg.unlock()
bkg.set_alpha(64)
self.bkg = bkg
if self.title != None:
banner = OutlineTextBanner(self.title, (200,200,200), 20)
self.title_image = banner.render()
self.title_image.set_alpha(96)
self.arrow = res.loadImage("wait_arrow.png", colorkey=1)
示例9: menu
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
class menu(object):
def __init__(self, rect, width):
self.surface = Surface((width, rect.height))
self.surface.fill((100,100,250))
self.buttonDict = dict()
self.addButton(10, 10, 40, 40, "House")
self.updateAll()
def addButton(self, x, y, width, height, text):
self.buttonDict[(x, y, width, height)] = (button.button(width, height, text))
def getButton(self, x, y):
for myRect in self.buttonDict.keys():
if Rect(myRect[0], myRect[1], myRect[2], myRect[3]).collidepoint(x, y):
return self.buttonDict[(myRect[0], myRect[1], myRect[2], myRect[3])].text
return None
def updateAll(self):
for myRect, button in self.buttonDict.items():
self.surface.blit(button.surface, (myRect[0], myRect[1]))
def getSurface(self):
return self.surface
示例10: render
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [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
示例11: text
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
def text(self, value):
#set the new text
self._label.text = value
#reconstruction of the frame surface, maybe resized
frame = Surface((self._padding_left * 2 + self._label.width, self._padding_top * 2 + self._label.height))
frame.fill(Color("grey"))
self._sprite.image = frame
#self._sprite = SpriteFactory().fromSurface("widget.button", frame, self._sprite.rect.topleft, layer=self._sprite.layer)
self._sprite.dirty = 1
示例12: FilteredSpList
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
class FilteredSpList(Clickable):
def __init__(self, data, x, y, filters):
self.data = data
self.filter = filters
self.surface = Surface((200, 170))
self.x = x
self.y = y
self.up_button = Button(86, 0, 28, 21, callback=self.decrease_offset, sprite=SpriteManager().sprites['up_button'])
self.down_button = Button(86, 142, 28, 21, callback=self.increase_offset, sprite=SpriteManager().sprites['down_button'])
self.offset = 0
self.items = [self.up_button, self.down_button]
self.clickables = [self.up_button, self.down_button]
self.reset()
def update_specialists(self):
self.shown_sp_panels = self.sp_panels[self.offset:self.offset + 2]
for i in range(len(self.shown_sp_panels)):
self.shown_sp_panels[i].y = i * 60 + 21
def increase_offset(self):
if self.offset < len(self.data.specialists) - 2:
self.offset += 1
self.update_specialists()
def decrease_offset(self):
if self.offset > 0:
self.offset -= 1
self.update_specialists()
def draw(self, screen):
self.surface.fill(0x000000)
for item in self.items:
item.draw(self.surface)
for item in self.shown_sp_panels:
item.draw(self.surface)
screen.blit(self.surface, (self.x, self.y))
def is_pressed(self, wx, wy, button):
x = wx - self.x
y = wy - self.y
res = False
for item in self.clickables:
res = res or item.is_pressed(x, y, button)
for item in self.shown_sp_panels:
if item.is_pressed(x, y, button):
if item.selected:
self.chosen.add(item.specialist)
else:
self.chosen.remove(item.specialist)
res = True
return res
def reset(self):
free_specialists = [sp for sp in self.data.specialists if not sp.occupied and sp.s_type in self.filter]
self.sp_panels = [SpecialistPanel(0, 0, specialist, True) for specialist in free_specialists]
self.update_specialists()
self.chosen = set()
示例13: World
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
class World(object):
def __init__(self, width, height):
self.width = 300
self.height = 300
self.grid_size = 20
self.pixel_size = Rect(0, 0, width, height)
self.full_grid = Rect(0, 0, self.width, self.height)
self.view_grid = Rect(10, 10, 0, 0)
self.prev_view_grid = None
self.zoom(0)
self.ground = [[0 for col in range(self.width)] for row in range(self.height)]
for y in range(self.height):
for x in range(self.width):
self.ground[y][x] = Assets.colors_db.any('dirt')
self.surface = Surface((width, height), HWSURFACE, 32)
def draw(self, screen):
rect = self.view_grid
if self.prev_view_grid != rect:
self.prev_view_grid = Rect(rect)
x1 = rect.left
y1 = rect.top
x2 = min(self.width, rect.left + rect.width)
y2 = min(self.height, rect.top + rect.height)
r = Rect(0, 0, self.grid_size, self.grid_size)
for row in self.ground[y1:y2 - 1]:
for col in row[x1:x2 - 1]:
self.surface.fill(col, r)
r.left += self.grid_size
r.left = 0
r.top += self.grid_size
screen.blit(self.surface, self.pixel_size)
def view_move(self, move):
self.view_grid.left += move.x
self.view_grid.top += move.y
self.view_grid.clamp_ip(self.full_grid)
def zoom_in(self):
self.zoom(+1)
def zoom_out(self):
self.zoom(-1)
def zoom(self, dz):
if 50 >= self.grid_size + dz >= 5:
self.grid_size = self.grid_size + dz
self.view_grid.width = int(math.ceil(self.pixel_size.width / float(self.grid_size))) + 1
self.view_grid.height = int(math.ceil(self.pixel_size.height / float(self.grid_size))) + 1
self.view_grid.clamp_ip(self.full_grid)
self.prev_view_grid = None
示例14: __init__
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [as 别名]
def __init__(self, x, y, padding_left, padding_top, text="button", text_size=16, layer=900):
super(Button, self).__init__()
self._label = Label(0, 0, text_size, text, layer=layer + 1)
self._label.topleft = (x + padding_left, y + padding_top)
self._padding_left = padding_left
self._padding_top = padding_top
frame = Surface((padding_left * 2 + self._label.width, padding_top * 2 + self._label.height))
frame.fill(Color("grey"))
self._sprite = SpriteFactory().fromSurface("widget.button", frame, (x, y), layer=layer)
示例15: render
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import fill [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