我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用pygame.time()。
def warn(self): if self.urgent: type = 'import' else: type = 'use' message = '%s %s: %s' % (type, self.name, self.info) if self.reason: message += "\n(%s)" % self.reason try: import warnings if self.urgent: level = 4 else: level = 3 warnings.warn(message, RuntimeWarning, level) except ImportError: print (message) # we need to import like this, each at a time. the cleanest way to import # our modules is with the import command (not the __import__ function) # first, the "required" modules
def __init__(self, *sprites, **kwargs): """Same as for the pygame.sprite.Group. pygame.sprite.LayeredDirty(*spites, **kwargs): return LayeredDirty You can specify some additional attributes through kwargs: _use_update: True/False default is False _default_layer: the default layer where the sprites without a layer are added. _time_threshold: treshold time for switching between dirty rect mode and fullscreen mode, defaults to 1000./80 == 1000./fps """ LayeredUpdates.__init__(self, *sprites, **kwargs) self._clip = None self._use_update = False self._time_threshold = 1000./80. # 1000./ fps self._bgd = None for key, val in kwargs.items(): if key in ['_use_update', '_time_threshold', '_default_layer']: if hasattr(self, key): setattr(self, key, val)
def __init__(self, *sprites, **kwargs): """initialize group. pygame.sprite.LayeredDirty(*spites, **kwargs): return LayeredDirty You can specify some additional attributes through kwargs: _use_update: True/False (default is False) _default_layer: default layer where the sprites without a layer are added _time_threshold: treshold time for switching between dirty rect mode and fullscreen mode; defaults to updating at 80 frames per second, which is equal to 1000.0 / 80.0 """ LayeredUpdates.__init__(self, *sprites, **kwargs) self._clip = None self._use_update = False self._time_threshold = 1000.0 / 80.0 # 1000.0 / fps self._bgd = None for key, val in kwargs.items(): if key in ['_use_update', '_time_threshold', '_default_layer']: if hasattr(self, key): setattr(self, key, val)
def collide_circle(left, right): """detect collision between two sprites using circles pygame.sprite.collide_circle(left, right): return bool Tests for collision between two sprites by testing whether two circles centered on the sprites overlap. If the sprites have a "radius" attribute, then that radius is used to create the circle; otherwise, a circle is created that is big enough to completely enclose the sprite's rect as given by the "rect" attribute. This function is intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" and an optional "radius" attribute. New in pygame 1.8.0 """ xdistance = left.rect.centerx - right.rect.centerx ydistance = left.rect.centery - right.rect.centery distancesquared = xdistance ** 2 + ydistance ** 2 if hasattr(left, 'radius'): leftradius = left.radius else: leftrect = left.rect # approximating the radius of a square by using half of the diagonal, # might give false positives (especially if its a long small rect) leftradius = 0.5 * ((leftrect.width ** 2 + leftrect.height ** 2) ** 0.5) # store the radius on the sprite for next time setattr(left, 'radius', leftradius) if hasattr(right, 'radius'): rightradius = right.radius else: rightrect = right.rect # approximating the radius of a square by using half of the diagonal # might give false positives (especially if its a long small rect) rightradius = 0.5 * ((rightrect.width ** 2 + rightrect.height ** 2) ** 0.5) # store the radius on the sprite for next time setattr(right, 'radius', rightradius) return distancesquared <= (leftradius + rightradius) ** 2
def __call__(self, left, right): """detect collision between two sprites using scaled circles pygame.sprite.collide_circle_radio(ratio)(left, right): return bool Tests for collision between two sprites by testing whether two circles centered on the sprites overlap after scaling the circle's radius by the stored ratio. If the sprites have a "radius" attribute, that is used to create the circle; otherwise, a circle is created that is big enough to completely enclose the sprite's rect as given by the "rect" attribute. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" and an optional "radius" attribute. """ ratio = self.ratio xdistance = left.rect.centerx - right.rect.centerx ydistance = left.rect.centery - right.rect.centery distancesquared = xdistance ** 2 + ydistance ** 2 if hasattr(left, "radius"): leftradius = left.radius * ratio else: leftrect = left.rect leftradius = ratio * 0.5 * ((leftrect.width ** 2 + leftrect.height ** 2) ** 0.5) # store the radius on the sprite for next time setattr(left, 'radius', leftradius) if hasattr(right, "radius"): rightradius = right.radius * ratio else: rightrect = right.rect rightradius = ratio * 0.5 * ((rightrect.width ** 2 + rightrect.height ** 2) ** 0.5) # store the radius on the sprite for next time setattr(right, 'radius', rightradius) return distancesquared <= (leftradius + rightradius) ** 2
def control(self): """ 1.Main game loop - a big while to check for quit 2.For loop to receive events 3.Check for quit all the time, the 4.For loop looking for specific quit event is in __checkForQuit 5.Mouse up and game is over - black screen case 6. Get a new DS, redraw the board(erase all and redraw), new game starts 7.Mouse up but post a movement of a tile 8. If game is over, repeat game over routine Display game over message 9.If move - get the tile clicked, if button down due to game over replay the background music 10.Update the screen """ running = True clickedTile = -1 movedToTile = -1 gameOver = False while running: self.__checkForQuit() for event in pygame.event.get(): if(event.type == pygame.MOUSEBUTTONUP and gameOver==True): x,y = event.pos if(MSGTEXTRECT.collidepoint(x,y)): self.board = self.__getStartingBoardDS() pygame.draw.rect(SCREEN, GAMEBOARDCOLOR, GAMEBOARDCOORD, 0) reDraw = True self.__drawBoard(self.board, "", reDraw) gameOver = False elif(event.type == pygame.MOUSEBUTTONUP and pygame.MOUSEMOTION and pygame.MOUSEBUTTONDOWN): #unpack the tuple x,y = event.pos movedToTile = self.__getSpotClicked(self.board, x, y) gameOver = self.__checkIfEmptyAndMove(clickedTile, movedToTile) if(gameOver): pygame.mixer.music.stop() pygame.mixer.music.load('gameover.mp3') pygame.mixer.music.play(0) reDraw = False self.__drawBoard(self.board, "Game Over. Continue?", reDraw) elif(event.type == pygame.MOUSEBUTTONDOWN): if(gameOver): pygame.mixer.music.stop() pygame.mixer.music.load('background.mp3') pygame.mixer.music.play(-1) #set a boolean flag as to it was pressed and get the cell in which it happened x,y = event.pos clickedTile = self.__getSpotClicked(self.board, x, y) #Update the screen pygame.display.update()