本文整理汇总了Python中pygame.surface.Surface.get_width方法的典型用法代码示例。如果您正苦于以下问题:Python Surface.get_width方法的具体用法?Python Surface.get_width怎么用?Python Surface.get_width使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame.surface.Surface
的用法示例。
在下文中一共展示了Surface.get_width方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import get_width [as 别名]
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()
示例2: __init__
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import get_width [as 别名]
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
示例3: ButtonWidget
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import get_width [as 别名]
class ButtonWidget(Widget):
""" clickable widget with text in it """
def __init__(
self, evManager, text, rect=None, onDownClickEvent=None, onUpClickEvent=None, onMouseMoveOutEvent=None
):
Widget.__init__(self, evManager)
# Widget init sets dirty to true, hence the actual text rendering
# will be done in ButtonWidget.update(), called by the view renderer.
self._em.reg_cb(DownClickEvent, self.on_downclick)
self._em.reg_cb(UpClickEvent, self.on_upclick)
self._em.reg_cb(MoveMouseEvent, self.on_mousemove)
# the events to be triggered when the button is clicked or mouse moved
self.onDownClickEvent = onDownClickEvent
self.onUpClickEvent = onUpClickEvent
self.onMouseMoveOutEvent = onMouseMoveOutEvent
self.text = text
self.font = Font(None, config_get_fontsize()) # default font, 40 pixels high
if rect:
self.rect = rect
self.image = Surface(self.rect.size) # container size from specified rect
# if txtimg does not fit in rect, it'll get cut (good thing)
else:
txtimg = self.font.render(self.text, True, (0, 0, 0))
self.rect = txtimg.get_rect()
self.image = Surface(self.rect.size) # container size from rendered text
# if txt changes, the size of the button will stay the same
def update(self, duration):
if self.dirty == 0:
return
if self.focused:
color = config_get_focusedbtn_txtcolor()
bgcolor = config_get_focusedbtn_bgcolor()
else:
color = config_get_unfocusedbtn_txtcolor()
bgcolor = config_get_unfocusedbtn_bgcolor()
# TODO: is bliting on existing faster than creating a new surface?
self.image = Surface(self.rect.size) # rectangle container for the text
self.image.fill(bgcolor)
txtimg = self.font.render(self.text, True, color)
txtimg = txtimg.convert()
textpos = txtimg.get_rect(centerx=self.image.get_width() / 2, centery=self.image.get_height() / 2)
self.image.blit(txtimg, textpos)
# self.dirty is set to 0 by LayeredDirty.update
def on_downclick(self, event):
""" button down focuses and triggers eventual behavior """
if self.rect.collidepoint(event.pos):
self.dirty = 1
self.set_focus(True)
if self.onDownClickEvent:
self._em.post(self.onDownClickEvent)
def on_upclick(self, event):
""" button up loses focus and triggers eventual behavior """
if self.rect.collidepoint(event.pos):
if self.focused:
self.dirty = 1
self.set_focus(False)
if self.onUpClickEvent:
self.log.debug("Clicked on button widget " + self.text)
self._em.post(self.onUpClickEvent)
def on_mousemove(self, event):
""" if focused, lose focus when the mouse moves out """
if self.rect.collidepoint(event.pos): # mouse moved in
pass
else: # mouse moved out
if self.focused:
if not self.onMouseMoveOutEvent: # default behavior
self.set_focus(False)
示例4: Paragraph
# 需要导入模块: from pygame.surface import Surface [as 别名]
# 或者: from pygame.surface.Surface import get_width [as 别名]
class Paragraph(AbstractDrawable):
def __init__(self, parentTextfield):
self.score = 0
self.text = ''
super(Paragraph, self).__init__(parentTextfield)
self.getParent().setFocusedIndex(self.getParent().getChildList().index(self))
self.recalcSize()
self.recalcSurfaceBacursively()
@overrides(AbstractDrawable)
def size(self, value = None):
return self.getParentTextfield().size()[0], len(self.getRowList()) * Constants.CHAR_HEIGHT
@overrides(AbstractDrawable)
def recalcSurface(self):
if self.size() != (self.surface.get_width(), self.surface.get_height()): self.surface = Surface(self.size())
self.surface.fill(self.getBgColor())
i = 0
for row in self.getRowList():
label = Constants.PROJECT_FONT.render(row, 1, self.getParentTextfield().getTextColor(), self.getBgColor())
self.surface.blit(label, [0, i * Constants.CHAR_HEIGHT])
i += 1
# to visually split paragraphs
pygame.draw.line(self.surface, [255,230,230], [0, 0], [self.surface.get_width(), 0])
# drawing pointer
if self.getParent().getFocusedChild() is self:
pointerRow = self.ptr() // self.getCharInRowCount()
pointerCol = self.ptr() % self.getCharInRowCount()
pygame.draw.line(self.surface, [255,0,0],
[pointerCol * Constants.CHAR_WIDTH, pointerRow * Constants.CHAR_HEIGHT],
[pointerCol * Constants.CHAR_WIDTH, (pointerRow + 1) * Constants.CHAR_HEIGHT])
return self.surface
@overrides(AbstractDrawable)
def makeHandler(self): return ParagraphHandler(self)
@overrides(AbstractDrawable)
def getObjectState(self):
return {'text': self.getText(), 'score': self.score}
@overrides(AbstractDrawable)
def setObjectState(self, paragraphData):
self.setText(paragraphData['text'])
self.setScore(paragraphData['score'])
return self
@overrides(AbstractDrawable)
def getChildList(self): return []
# operations with pointer
@overrides(AbstractDrawable)
def setFocusedIndex(self, value):
if value < -1: value = -1
if value >= len(self.getText()): value = len(self.getText()) - 1
self.focusedIndex = value
self.recalcSurfaceBacursively()
self.getParentTextfield().moveScrollToPointer()
return self
def cropToPointer(self):
return self.crop(0, self.ptr())
def cropFromPointer(self):
return self.crop(self.ptr(), -1)
def getTextAfterPointer(self):
return self.text[self.ptr():]
def getTextBeforePointer(self):
return self.text[:self.ptr()]
def cutAfterPointer(self):
cut = self.getTextAfterPointer()
self.cropToPointer()
return cut
def cutReplaceAfterPointer(self, postStr):
cut = self.cutAfterPointer()
self.append(postStr)
return cut
# operations with text
def crop(self, l, r = -1):
r = self.getTextLen() + r if r < 0 else r
self.setText(self.getText()[l:r])
return self
def append(self, strToAppend):
self.setText(self.getText() + strToAppend)
return self
def prepend(self, strToPrepend):
self.setText(strToPrepend + self.getText())
return self
#.........这里部分代码省略.........