當前位置: 首頁>>代碼示例>>Python>>正文


Python Background.addSprite方法代碼示例

本文整理匯總了Python中background.Background.addSprite方法的典型用法代碼示例。如果您正苦於以下問題:Python Background.addSprite方法的具體用法?Python Background.addSprite怎麽用?Python Background.addSprite使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在background.Background的用法示例。


在下文中一共展示了Background.addSprite方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Game

# 需要導入模塊: from background import Background [as 別名]
# 或者: from background.Background import addSprite [as 別名]
class Game():
    def __init__(self):
        """
        Initialize the game
        """
        pygame.init()
        self.screen = pygame.display.set_mode(consts.RESOLUTION)
        pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN, pygame.KEYUP])
        # Blit background
        self.background = Background((0, 0))
        # TODO avoid acting on sprite and do actions on group?
        self.player = player.Player({
            'tilesGroup': 'new',
            'x': 400,
            'y': 300
        })
        self.background.setMainSprite(self.player)
        self.init_joystick()

    def init_joystick(self):
        """
        Initialize a joystick if available
        """
        pygame.joystick.init()
        if pygame.joystick.get_count() == 0:
            return
        for joystick_id in range(0, pygame.joystick.get_count()):
            joystick = pygame.joystick.Joystick(joystick_id)
            # Stop on first matching joystick
            if joystick.get_name() in consts.JOYSTICK:
                print "Initializing Joystick id:%d" % joystick.get_id()
                joystick.init()
                self.joystick_mapping = consts.JOYSTICK[joystick.get_name()]
                self.joystick = joystick
                joystick_info = (joystick.get_name(), joystick.get_numaxes())
                print "%s (%d axis)" % joystick_info
                break

    def run(self):
        """
        Main loop
        """
        running = True
        # run until an event tells us to stop
        while running:
            pygame.time.Clock().tick(consts.FPS)
            running = self.handleEvents()
            self.background.update(self.screen.get_size())

            camera = - self.background.xCamera, - self.background.yCamera
            self.screen.blit(self.background.fond, (0, 0))
            # update screen
            rect = pygame.Rect(
                0,
                0,
                consts.RESOLUTION[0],
                consts.RESOLUTION[1]
            )
            pygame.display.update(rect)

    def handleEvents(self):
        """
        Poll for pygame events
        """
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False

            # handle user input
            elif event.type == pygame.KEYDOWN:
                # if the user presses escape or 'q', quit the event loop.
                if event.key in (pygame.K_ESCAPE, pygame.K_q):
                    return False
                # handle speed
                if event.key in (pygame.K_LSHIFT, pygame.K_RSHIFT):
                    self.player.speed = 6
                # movement control
                if event.key == pygame.K_UP:
                    self.player.moveVertical(-1)
                if event.key == pygame.K_DOWN:
                    self.player.moveVertical(1)
                if event.key == pygame.K_LEFT:
                    self.player.moveHorizontal(-1)
                if event.key == pygame.K_RIGHT:
                    self.player.moveHorizontal(1)
                if event.key == pygame.K_c:
                    #~ create new sprite
                    s = player.Player({
                        'tilesGroup': 'scholar',
                        'x': 300,
                        'y': 300,
                        'movePattern': {
                            'type': 'rect',
                            'attributes': {
                                'width': 200,
                                'height': 200
                            }
                        }
                    })
                    self.background.addSprite(s, Background.LAYER_CHARACTERS)
#.........這裏部分代碼省略.........
開發者ID:bcrudolph,項目名稱:python-rpg-engine,代碼行數:103,代碼來源:game.py


注:本文中的background.Background.addSprite方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。