当前位置: 首页>>代码示例>>Python>>正文


Python Ball.reset方法代码示例

本文整理汇总了Python中Ball.Ball.reset方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.reset方法的具体用法?Python Ball.reset怎么用?Python Ball.reset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Ball.Ball的用法示例。


在下文中一共展示了Ball.reset方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: NetworkControlledPong

# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import reset [as 别名]

#.........这里部分代码省略.........
        self.ball.vely   = 0
        self.ball.velx   = 0
        self.paused      = True

    def resume(self):
        self.ball.vely = self.tmpBallVelY
        self.ball.velx = self.tmpBallVelX
        self.paused    = False

    def run(self):
        """Runs the game. Contains the game loop that computes and renders
        each frame."""

        print 'Waiting for enough controllers'

        while not self.started:
            if self.handleEvents() == False:
                self.uiServer.stop()
                return 0
            else:
                time.sleep(0)

        print 'Enough controllers registered'
        print 'Starting Event Loop'

        self.running = True
        self.paused  = False

        while self.running:
            self.clock.tick(30)

            self.running = self.handleEvents()

            self.manageBall()

            for sprite in self.sprites:
                sprite.update()

            self.sprites.clear(self.window, self.background)    # clears the window where the sprites currently are, using the background
            dirty = self.sprites.draw(self.window)              # calculates the 'dirty' rectangles that need to be redrawn

            pygame.display.update(dirty)                        # updates just the 'dirty' areas

        print 'Quitting. Thanks for playing'
        self.uiServer.stop()
        return 0

    def handleEvents(self):
        """Poll for PyGame events and behave accordingly. Return false to stop
        the event loop and end the game."""

        for event in pygame.event.get():
            if event.type == QUIT:
                return False

            elif event.type == USEREVENT:
                if self.ball.velx == 0 and self.ball.vely == 0 and not self.paused:
                    self.ball.serve()
                if event.key.lower() == "up":
                    self.paddleMap[event.id].up()
                if event.key.lower() == "down":
                    self.paddleMap[event.id].down()
                if event.key.lower() == "stop":
                    return False

        return True


    def manageBall(self):
        self.ball.rect.x += self.ball.velx
        self.ball.rect.y += self.ball.vely

        if self.ball.rect.top < 0:
            self.ball.rect.top = 1
            self.ball.vely *= -1
            self.pongsound.play()

        elif self.ball.rect.bottom > 400:
            self.ball.rect.bottom = 399
            self.ball.vely *= -1
            self.pongsound.play()

        if self.ball.rect.left < 0:
            self.scoreImage.right()
            self.ball.reset()
            return

        elif self.ball.rect.right > 800:
            self.scoreImage.left()
            self.ball.reset()
            return

        collided = pygame.sprite.spritecollide(self.ball, [self.leftpaddle, self.rightpaddle], dokill=False)

        if len(collided) > 0:
            hitpaddle = collided[0]
            self.ball.velx *= -1
            self.ball.rect.x += self.ball.velx
            self.ball.vely += hitpaddle.velocity/3.0
            self.pingsound.play()
开发者ID:kmhsonnenkind,项目名称:NetworkControlledPong,代码行数:104,代码来源:NetworkControlledPong.py


注:本文中的Ball.Ball.reset方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。