本文整理汇总了Python中pygame.surface.Surface类的典型用法代码示例。如果您正苦于以下问题:Python Surface类的具体用法?Python Surface怎么用?Python Surface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Surface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Panel
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: __init__
def __init__(self, image, movement_speed):
Surface.__init__(self, image.get_size())
self.image = image
self.blit(self.image, (0, 0))
self.movement_speed = movement_speed
示例3: order_reversed_spritesheet
def order_reversed_spritesheet(self, flipped_sheet):
"""Reorganize the frames in a flipped sprite sheet so that
they are in the same order as the original sheet.
Args:
flipped_sheet: A PyGame Surface containing a flipped sprite
sheet.
Returns:
A PyGame Surface containing the sprite sheet with each frame
flipped and in the correct order.
"""
ordered_sheet = Surface((self.spritesheet.get_width(),
self.spritesheet.get_height()),
SRCALPHA)
ordered_sheet.convert_alpha()
for frame_index in xrange(0, self.get_num_of_frames()):
frame_x = self.get_width() * frame_index
old_frame_index = self.get_num_of_frames() - 1 - frame_index
old_region = self.get_frame_region(old_frame_index)
ordered_sheet.blit(flipped_sheet, (frame_x, 0), old_region)
return ordered_sheet
示例4: __init__
def __init__(self, initialFuel):
self._tardisFrame = []
self._tardisFrame.append(pygame.image.load("Resources/tardis0.png").convert_alpha())
self._tardisFrame.append(pygame.image.load("Resources/tardis1.png").convert_alpha())
self._tardisFrame.append(pygame.image.load("Resources/tardis2.png").convert_alpha())
self._tardisFrame.append(pygame.image.load("Resources/tardisLand0.png").convert_alpha())
self._tardisFrame.append(pygame.image.load("Resources/tardisLand1.png").convert_alpha())
self._tardisFrame.append(pygame.image.load("Resources/tardisLand2.png").convert_alpha())
self.tardisHeight = Surface.get_height(self._tardisFrame[0]) * self._imageResample
self.tardisWidth = Surface.get_width(self._tardisFrame[0]) * self._imageResample
self.altitude = screen_height_pixels - self.tardisHeight * 1 # NAM: Adjust start height
self._landingStickLength = max(self.tardisHeight, self.tardisWidth)
self.x = 50
self.angle = 0
self.angleDelta = 0
self.velocityX = 0.1
self.velocityY = 0
self.thrusting = False
self.image = pygame.transform.rotozoom(self._tardisFrame[0], 0, self._imageResample)
self.rotate(45)
self.thrustSound = pygame.mixer.Sound("Resources/tardisthrust.ogg")
self.thrustSound.set_volume(0.5)
self.fuel = initialFuel
pygame.sprite.Sprite.__init__(self)
self.rect = Rect(self.x, 0, self.tardisWidth, self.tardisWidth)
self.maxHeight = math.sqrt(self.tardisHeight * self.tardisHeight + self.tardisWidth * self.tardisWidth)
self.rotationsPerSec = 0.2
self.recalculateRect()
示例5: _createSurfaceImage
def _createSurfaceImage(self, surfaceHeight):
"""
Make the ground surface image.
IDEA: Try changing the color of the ground, or maybe even add a texture to it.
"""
LunarSurface.image = pygame.Surface(
[screen_width_pixels, surfaceHeight + self._groundElevation], pygame.SRCALPHA, 32
).convert_alpha()
initialY = y = (
Surface.get_height(LunarSurface.image) - self._pointsY[0] + self._drop_amount - self._groundElevation
)
polyCoords = [(0, y)]
for i in range(1, len(self._pointsX)):
y -= self._pointsY[i]
polyCoords.append((self._pointsX[i], y))
surfaceCoords = list(polyCoords)
polyCoords.append([screen_width_pixels, Surface.get_height(LunarSurface.image)])
polyCoords.append([0, Surface.get_height(LunarSurface.image)])
polyCoords.append([0, initialY])
pygame.draw.polygon(LunarSurface.image, (64, 64, 64, 128), polyCoords, 0)
for i in range(0, len(surfaceCoords) - 1):
if self._flatCoordinates.count(i) or self._flatEasyCoordinates.count(i):
# NAM change color: color = 0, 255, 255
color = 255, 0, 0
width = 3
else:
color = 128, 128, 128
width = 1
pygame.draw.line(LunarSurface.image, color, surfaceCoords[i], surfaceCoords[i + 1], width)
self._plotlandingScores(surfaceCoords)
示例6: prepare_paragraph
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
示例7: __init__
def __init__(self, size, game):
Surface.__init__(self, size)
self.width, self.height = size
self.game = game
self._load_images()
self.font = pygame.font.SysFont("Comic Sans MS", int(0.09 * self.width))
self.hsfont = pygame.font.SysFont("Comic Sans MS", int(0.09 * self.width))
示例8: __init__
class GameUI:
def __init__(self, mainchar):
self.bg = Surface((600, 1600))
self.statusbar = Surface((200, 600))
self.mainchar = mainchar
self.widget = []
self.create_bg()
def create_bg(self):
rbg = self.bg.get_rect()
bgimg = media.load_image('bg.png').convert()
rbgimg = bgimg.get_rect()
columns = int(rbg.width / rbgimg.width) + 1
rows = int(rbg.height / rbgimg.height) + 1
for y in xrange(rows):
for x in xrange(columns):
if x == 0 and y > 0:
rbgimg = rbgimg.move([-(columns -1 ) * rbgimg.width, rbgimg.height])
if x > 0:
rbgimg = rbgimg.move([rbgimg.width, 0])
self.bg.blit(bgimg, rbgimg)
def update(self):
r = pygame.rect.Rect(0, 0, 200, 100)
for w in self.widget:
w.update()
self.statusbar.blit(w.image, r)
r = r.move((0, 100))
示例9: prepare_table
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
示例10: update
def update(self, duration):
""" update all the contained linewidgets
TODO: check that it works with transparent bg
"""
if self.dirty == 0:
return
# make the box
size = self.rect.size
bgcol = self.bgcolor
if bgcol: # only display a bg img if bgcolor specified
img = Surface(size)
img.fill(bgcol)
img = img.convert()
else: # more or less transparent
img = Surface(size, SRCALPHA) # handles transparency
transparency = 50 # 0 = transparent, 255 = opaque
img.fill((0, 0, 0, transparency))
img = img.convert_alpha()
# blit each line
numelemts = min(len(self.texts), self.maxnumlines)
for i in range(numelemts):
wid = self.linewidgets[i]
wid.set_text(self.texts[-i - 1])
wid.update(duration)
img.blit(wid.image, wid.rect)
self.image = img
示例11: __init__
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
示例12: __init__
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
示例13: menu
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
示例14: _prepare_buttons
def _prepare_buttons(self):
""" Prepares the buttons.
This method pre-renders the plus and minus buttons.
"""
# draw plus button
self._plus_button = Surface((self._font_height, self._font_height), pygame.SRCALPHA, 32)
self._plus_button.convert_alpha()
pygame.draw.rect(self._plus_button, SettingsScreen.COLOR, Rect(0, self._font_height/3, self._font_height, self._font_height/3))
pygame.draw.rect(self._plus_button, SettingsScreen.COLOR, Rect(self._font_height/3, 0, self._font_height/3, self._font_height))
# draw minus button
self._minus_button = Surface((self._font_height, self._font_height), pygame.SRCALPHA, 32)
self._minus_button.convert_alpha()
pygame.draw.rect(self._minus_button, SettingsScreen.COLOR, Rect(0, self._font_height/3, self._font_height, self._font_height/3))
# draw unchecked bool button
self._unchecked_bool = Surface((self._font_height, self._font_height), pygame.SRCALPHA, 32)
self._unchecked_bool.convert_alpha()
pygame.draw.rect(self._unchecked_bool, SettingsScreen.COLOR, Rect(0, 0, self._font_height, self._font_height), 3)
# draw checked bool button
self._checked_bool = Surface((self._font_height, self._font_height), pygame.SRCALPHA, 32)
self._checked_bool.convert_alpha()
pygame.draw.rect(self._checked_bool, SettingsScreen.COLOR, Rect(0, 0, self._font_height, self._font_height), 3)
pygame.draw.line(self._checked_bool, SettingsScreen.COLOR, (0, 0), (self._font_height, self._font_height), 3)
pygame.draw.line(self._checked_bool, SettingsScreen.COLOR, (0, self._font_height), (self._font_height, 0), 3)
示例15: render
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