当前位置: 首页>>代码示例>>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;未经允许,请勿转载。